From e023d13424ab0d96273cea1ebd3ee420b91c3ea3 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Tue, 14 Apr 2020 16:55:56 +0200 Subject: [PATCH] Fix XmpExporterTest (#6289) --- .../jabref/logic/exporter/XmpExporter.java | 15 +++++++++------ .../logic/exporter/XmpExporterTest.java | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/jabref/logic/exporter/XmpExporter.java b/src/main/java/org/jabref/logic/exporter/XmpExporter.java index 9a045f8ebc8..59f7c445b6b 100644 --- a/src/main/java/org/jabref/logic/exporter/XmpExporter.java +++ b/src/main/java/org/jabref/logic/exporter/XmpExporter.java @@ -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; @@ -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 entries) throws Exception { Objects.requireNonNull(databaseContext); @@ -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 { @@ -66,7 +70,6 @@ public void export(BibDatabaseContext databaseContext, Path file, Charset encodi private void writeBibToXmp(Path file, List entries, Charset encoding) throws IOException { String xmpContent = XmpUtilWriter.generateXmpStringWithoutXmpDeclaration(entries, this.xmpPreferences); - try (BufferedWriter writer = Files.newBufferedWriter(file, encoding)) { writer.write(xmpContent); writer.flush(); diff --git a/src/test/java/org/jabref/logic/exporter/XmpExporterTest.java b/src/test/java/org/jabref/logic/exporter/XmpExporterTest.java index d102c01aa39..120d27f8638 100644 --- a/src/test/java/org/jabref/logic/exporter/XmpExporterTest.java +++ b/src/test/java/org/jabref/logic/exporter/XmpExporterTest.java @@ -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 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 = " \n" + @@ -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 = " \n" +