Skip to content

Commit

Permalink
Fix XmpExporterTest (#6289)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor authored Apr 14, 2020
1 parent d0a86e9 commit e023d13
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/main/java/org/jabref/logic/exporter/XmpExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class XmpExporter extends Exporter {

private static final String XMP_SPLIT_PATTERN = "split";
public static final String XMP_SPLIT_DIRECTORY_INDICATOR = "split";

private final XmpPreferences xmpPreferences;

Expand All @@ -33,6 +33,12 @@ public XmpExporter(XmpPreferences xmpPreferences) {
this.xmpPreferences = xmpPreferences;
}

/**
* @param databaseContext the database to export from
* @param file the file to write to. If it contains "split", then the output is split into different files
* @param encoding the encoding to use
* @param entries a list containing all entries that should be exported
*/
@Override
public void export(BibDatabaseContext databaseContext, Path file, Charset encoding, List<BibEntry> entries) throws Exception {
Objects.requireNonNull(databaseContext);
Expand All @@ -45,18 +51,16 @@ public void export(BibDatabaseContext databaseContext, Path file, Charset encodi

// This is a distinction between writing all entries from the supplied list to a single .xmp file,
// or write every entry to a separate file.
if (file.getFileName().toString().trim().equals(XMP_SPLIT_PATTERN)) {

if (file.getFileName().toString().trim().equals(XMP_SPLIT_DIRECTORY_INDICATOR)) {
for (BibEntry entry : entries) {
// Avoid situations, where two cite keys are null
Path entryFile;
String suffix = entry.getId() + "_" + entry.getField(InternalField.KEY_FIELD).orElse("") + ".xmp";
String suffix = entry.getId() + "_" + entry.getField(InternalField.KEY_FIELD).orElse("null") + ".xmp";
if (file.getParent() == null) {
entryFile = Paths.get(suffix);
} else {
entryFile = Paths.get(file.getParent().toString() + "/" + suffix);
}

this.writeBibToXmp(entryFile, Collections.singletonList(entry), encoding);
}
} else {
Expand All @@ -66,7 +70,6 @@ public void export(BibDatabaseContext databaseContext, Path file, Charset encodi

private void writeBibToXmp(Path file, List<BibEntry> entries, Charset encoding) throws IOException {
String xmpContent = XmpUtilWriter.generateXmpStringWithoutXmpDeclaration(entries, this.xmpPreferences);

try (BufferedWriter writer = Files.newBufferedWriter(file, encoding)) {
writer.write(xmpContent);
writer.flush();
Expand Down
19 changes: 10 additions & 9 deletions src/test/java/org/jabref/logic/exporter/XmpExporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,23 @@ public void writeMultipleEntriesInASingleFile(@TempDir Path testFolder) throws E

@Test
public void writeMultipleEntriesInDifferentFiles(@TempDir Path testFolder) throws Exception {
Path file = testFolder.resolve("split");
// set path to the one where the exporter produces several files
Path file = testFolder.resolve(XmpExporter.XMP_SPLIT_DIRECTORY_INDICATOR);
Files.createFile(file);

BibEntry entryTuring = new BibEntry();
entryTuring.setField(StandardField.AUTHOR, "Alan Turing");
BibEntry entryTuring = new BibEntry()
.withField(StandardField.AUTHOR, "Alan Turing");

BibEntry entryArmbrust = new BibEntry();
entryArmbrust.setField(StandardField.AUTHOR, "Michael Armbrust");
entryArmbrust.setCiteKey("Armbrust2010");
BibEntry entryArmbrust = new BibEntry()
.withField(StandardField.AUTHOR, "Michael Armbrust")
.withCiteKey("Armbrust2010");

exporter.export(databaseContext, file, encoding, Arrays.asList(entryTuring, entryArmbrust));
exporter.export(databaseContext, file, encoding, List.of(entryTuring, entryArmbrust));

List<String> lines = Files.readAllLines(file);
assertEquals(Collections.emptyList(), lines);

Path fileTuring = Paths.get(file.getParent().toString() + "/" + entryTuring.getId() + "_null.xmp");
Path fileTuring = Paths.get(file.getParent().toString(), entryTuring.getId() + "_null.xmp");
String actualTuring = String.join("\n", Files.readAllLines(fileTuring)); // we are using \n to join, so we need it in the expected string as well, \r\n would fail

String expectedTuring = " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
Expand All @@ -155,7 +156,7 @@ public void writeMultipleEntriesInDifferentFiles(@TempDir Path testFolder) throw

assertEquals(expectedTuring, actualTuring);

Path fileArmbrust = Paths.get(file.getParent().toString() + "/" + entryArmbrust.getId() + "_Armbrust2010.xmp");
Path fileArmbrust = Paths.get(file.getParent().toString(), entryArmbrust.getId() + "_Armbrust2010.xmp");
String actualArmbrust = String.join("\n", Files.readAllLines(fileArmbrust)); // we are using \n to join, so we need it in the expected string as well, \r\n would fail

String expectedArmbrust = " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
Expand Down

0 comments on commit e023d13

Please sign in to comment.