diff --git a/CHANGELOG.md b/CHANGELOG.md index 98f9d7a466b..a62276061b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We increased the relative size of the "abstract" field in the entry editor. [Feature request in the forum](http://discourse.jabref.org/t/entry-preview-in-version-4/827) - Crossreferenced entries are now used when a BibTex key is generated for an entry with empty fields. [#2811](https://github.com/JabRef/jabref/issues/2811) - We now set the `WM_CLASS` of the UI to org-jabref-JabRefMain to allow certain Un*x window managers to properly identify its windows +- We added an option to convert entries in the biblatex format to BibTeX so that you can again collaborate with these nostalgic diehards. [Feature request in the forum](http://discourse.jabref.org/t/convert-back-from-biblatex-to-bib/) - We changed the default paths for the OpenOffice/LibreOffice binaries to the default path for LibreOffice - File annotation tab now removes newlines and hyphens before newlines from content and displays an empty String instead of N/A if no contents are found. [#3280](https://github.com/JabRef/jabref/issues/3280) - We moved the groups field from the "Other fields" tab to "General" (you may have to reset your editor preferences under Options > Set up general fields) diff --git a/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java b/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java index 56f346c115b..9bfe710e7f3 100644 --- a/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java +++ b/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java @@ -6,6 +6,7 @@ import java.util.Optional; import java.util.Set; +import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JPanel; @@ -33,6 +34,7 @@ public class CleanupPresetPanel { private JCheckBox cleanUpRenamePDFonlyRelativePaths; private JCheckBox cleanUpUpgradeExternalLinks; private JCheckBox cleanUpBiblatex; + private JCheckBox cleanUpBibtex; private FieldFormatterCleanupsPanel cleanUpFormatters; private JPanel panel; @@ -71,6 +73,11 @@ private void init() { Localization.lang("Upgrade external PDF/PS links to use the '%0' field.", FieldName.FILE)); cleanUpBiblatex = new JCheckBox(Localization.lang( "Convert to biblatex format (for example, move the value of the 'journal' field to 'journaltitle')")); + cleanUpBibtex = new JCheckBox(Localization.lang( + "Convert to BibTeX format (for example, move the value of the 'journaltitle' field to 'journal')")); + ButtonGroup biblatexConversion = new ButtonGroup(); // Only make "to Biblatex" or "to BibTeX" selectable + biblatexConversion.add(cleanUpBiblatex); + biblatexConversion.add(cleanUpBibtex); cleanUpFormatters = new FieldFormatterCleanupsPanel(Localization.lang("Run field formatter:"), Cleanups.DEFAULT_SAVE_ACTIONS); @@ -78,7 +85,7 @@ private void init() { updateDisplay(cleanupPreset); FormLayout layout = new FormLayout("left:15dlu, fill:pref:grow", - "pref, pref, pref, pref, pref, fill:pref:grow, pref,pref, pref,190dlu, fill:pref:grow,"); + "pref, pref, pref, pref, pref, fill:pref:grow, pref,pref, pref, pref,190dlu, fill:pref:grow,"); FormBuilder builder = FormBuilder.create().layout(layout); builder.add(cleanUpDOI).xyw(1, 1, 2); @@ -90,9 +97,10 @@ private void init() { currentPattern = currentPattern.concat(Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN)); builder.add(new JLabel(currentPattern)).xy(2, 6); builder.add(cleanUpRenamePDFonlyRelativePaths).xy(2, 7); - builder.add(cleanUpBiblatex).xyw(1, 8, 2); - builder.add(cleanUpISSN).xyw(1, 9, 2); - builder.add(cleanUpFormatters).xyw(1, 10, 2); + builder.add(cleanUpBibtex).xyw(1, 8, 2); + builder.add(cleanUpBiblatex).xyw(1, 9, 2); + builder.add(cleanUpISSN).xyw(1, 10, 2); + builder.add(cleanUpFormatters).xyw(1, 11, 2); panel = builder.build(); scrollPane = new JScrollPane(panel); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); @@ -111,7 +119,8 @@ private void updateDisplay(CleanupPreset preset) { cleanUpRenamePDFonlyRelativePaths.setEnabled(cleanUpRenamePDF.isSelected()); cleanUpUpgradeExternalLinks.setSelected(preset.isCleanUpUpgradeExternalLinks()); cleanUpBiblatex.setSelected(preset.isConvertToBiblatex()); - cleanUpBiblatex.setSelected(preset.isCleanUpISSN()); + cleanUpBibtex.setSelected(preset.isConvertToBibtex()); + cleanUpISSN.setSelected(preset.isCleanUpISSN()); cleanUpFormatters.setValues(preset.getFormatterCleanups()); } @@ -149,6 +158,9 @@ public CleanupPreset getCleanupPreset() { if (cleanUpBiblatex.isSelected()) { activeJobs.add(CleanupPreset.CleanupStep.CONVERT_TO_BIBLATEX); } + if (cleanUpBibtex.isSelected()) { + activeJobs.add(CleanupPreset.CleanupStep.CONVERT_TO_BIBTEX); + } activeJobs.add(CleanupPreset.CleanupStep.FIX_FILE_LINKS); diff --git a/src/main/java/org/jabref/logic/cleanup/CleanupPreset.java b/src/main/java/org/jabref/logic/cleanup/CleanupPreset.java index ab661810426..ca109ba5782 100644 --- a/src/main/java/org/jabref/logic/cleanup/CleanupPreset.java +++ b/src/main/java/org/jabref/logic/cleanup/CleanupPreset.java @@ -60,6 +60,9 @@ public static CleanupPreset loadFromPreferences(JabRefPreferences preferences) { if (preferences.getBoolean(JabRefPreferences.CLEANUP_CONVERT_TO_BIBLATEX)) { activeJobs.add(CleanupStep.CONVERT_TO_BIBLATEX); } + if (preferences.getBoolean(JabRefPreferences.CLEANUP_CONVERT_TO_BIBTEX)) { + activeJobs.add(CleanupStep.CONVERT_TO_BIBTEX); + } if (preferences.getBoolean(JabRefPreferences.CLEANUP_FIX_FILE_LINKS)) { activeJobs.add(CleanupStep.FIX_FILE_LINKS); } @@ -102,6 +105,10 @@ public boolean isConvertToBiblatex() { return isActive(CleanupStep.CONVERT_TO_BIBLATEX); } + public boolean isConvertToBibtex() { + return isActive(CleanupStep.CONVERT_TO_BIBTEX); + } + public boolean isRenamePdfOnlyRelativePaths() { return isActive(CleanupStep.RENAME_PDF_ONLY_RELATIVE_PATHS); } @@ -117,6 +124,7 @@ public void storeInPreferences(JabRefPreferences preferences) { preferences.putBoolean(JabRefPreferences.CLEANUP_UPGRADE_EXTERNAL_LINKS, isActive(CleanupStep.CLEAN_UP_UPGRADE_EXTERNAL_LINKS)); preferences.putBoolean(JabRefPreferences.CLEANUP_CONVERT_TO_BIBLATEX, isActive(CleanupStep.CONVERT_TO_BIBLATEX)); + preferences.putBoolean(JabRefPreferences.CLEANUP_CONVERT_TO_BIBTEX, isActive(CleanupStep.CONVERT_TO_BIBTEX)); preferences.putBoolean(JabRefPreferences.CLEANUP_FIX_FILE_LINKS, isActive(CleanupStep.FIX_FILE_LINKS)); preferences.putStringList(JabRefPreferences.CLEANUP_FORMATTERS, formatterCleanups.getAsStringList(OS.NEWLINE)); @@ -146,6 +154,10 @@ public enum CleanupStep { * Converts to biblatex format */ CONVERT_TO_BIBLATEX, + /** + * Converts to bibtex format + */ + CONVERT_TO_BIBTEX, MOVE_PDF, FIX_FILE_LINKS, CLEAN_UP_ISSN diff --git a/src/main/java/org/jabref/logic/cleanup/CleanupWorker.java b/src/main/java/org/jabref/logic/cleanup/CleanupWorker.java index 739fa740e1e..9ed2c3dd5a3 100644 --- a/src/main/java/org/jabref/logic/cleanup/CleanupWorker.java +++ b/src/main/java/org/jabref/logic/cleanup/CleanupWorker.java @@ -51,7 +51,10 @@ private List determineCleanupActions(CleanupPreset preset) { List jobs = new ArrayList<>(); if (preset.isConvertToBiblatex()) { - jobs.add(new BiblatexCleanup()); + jobs.add(new ConvertToBiblatexCleanup()); + } + if (preset.isConvertToBibtex()) { + jobs.add(new ConvertToBibtexCleanup()); } if (preset.getFormatterCleanups().isEnabled()) { jobs.addAll(preset.getFormatterCleanups().getConfiguredActions()); diff --git a/src/main/java/org/jabref/logic/cleanup/BiblatexCleanup.java b/src/main/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanup.java similarity index 96% rename from src/main/java/org/jabref/logic/cleanup/BiblatexCleanup.java rename to src/main/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanup.java index e6546e2b1e7..7e12b11a631 100644 --- a/src/main/java/org/jabref/logic/cleanup/BiblatexCleanup.java +++ b/src/main/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanup.java @@ -14,7 +14,7 @@ /** * Converts the entry to biblatex format. */ -public class BiblatexCleanup implements CleanupJob { +public class ConvertToBiblatexCleanup implements CleanupJob { @Override public List cleanup(BibEntry entry) { diff --git a/src/main/java/org/jabref/logic/cleanup/ConvertToBibtexCleanup.java b/src/main/java/org/jabref/logic/cleanup/ConvertToBibtexCleanup.java new file mode 100644 index 00000000000..07106271607 --- /dev/null +++ b/src/main/java/org/jabref/logic/cleanup/ConvertToBibtexCleanup.java @@ -0,0 +1,52 @@ +package org.jabref.logic.cleanup; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.jabref.model.FieldChange; +import org.jabref.model.cleanup.CleanupJob; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.EntryConverter; +import org.jabref.model.entry.FieldName; +import org.jabref.model.strings.StringUtil; + +/** + * Converts the entry to biblatex format. + */ +public class ConvertToBibtexCleanup implements CleanupJob { + + @Override + public List cleanup(BibEntry entry) { + List changes = new ArrayList<>(); + + // Dates: get date and fill year and month + // If there already exists a non blank/empty value for the field, then it is not overwritten + entry.getPublicationDate().ifPresent(date -> { + if (StringUtil.isBlank(entry.getField(FieldName.YEAR))) { + date.getYear().flatMap(year -> entry.setField(FieldName.YEAR, year.toString())).ifPresent(changes::add); + } + + if (StringUtil.isBlank(entry.getField(FieldName.MONTH))) { + date.getMonth().flatMap(month -> entry.setField(FieldName.MONTH, month.getJabRefFormat())).ifPresent(changes::add); + } + + if (changes.size() > 0) { + entry.clearField(FieldName.DATE).ifPresent(changes::add); + } + }); + + for (Map.Entry alias : EntryConverter.FIELD_ALIASES_TEX_TO_LTX.entrySet()) { + String oldFieldName = alias.getValue(); + String newFieldName = alias.getKey(); + entry.getField(oldFieldName).ifPresent(oldValue -> { + if (!oldValue.isEmpty() && (!entry.getField(newFieldName).isPresent())) { + // There is content in the old field and no value in the new, so just copy + entry.setField(newFieldName, oldValue).ifPresent(changes::add); + entry.clearField(oldFieldName).ifPresent(changes::add); + } + }); + } + return changes; + } +} diff --git a/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java b/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java index 1b41286d370..7c16ec6d697 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java @@ -124,6 +124,10 @@ public List parseEntries(String bibtexString) throws ParseException { return parseEntries(new StringReader(bibtexString)); } + public Optional parseSingleEntry(String bibtexString) throws ParseException { + return parseEntries(bibtexString).stream().findFirst(); + } + /** * Will parse the BibTex-Data found when reading from reader. Ignores any encoding supplied in the file by * "Encoding: myEncoding". diff --git a/src/main/java/org/jabref/logic/xmp/XMPUtil.java b/src/main/java/org/jabref/logic/xmp/XMPUtil.java index a2473751925..743b472131d 100644 --- a/src/main/java/org/jabref/logic/xmp/XMPUtil.java +++ b/src/main/java/org/jabref/logic/xmp/XMPUtil.java @@ -629,7 +629,7 @@ private static void writeToDCSchema(XMPSchemaDublinCore dcSchema, BibEntry entry * * Bibtex-Fields used: year, month */ - entry.getPublicationDate() + entry.getFieldOrAlias(FieldName.DATE) .ifPresent(publicationDate -> dcSchema.addSequenceValue("dc:date", publicationDate)); continue; } diff --git a/src/main/java/org/jabref/model/entry/BibEntry.java b/src/main/java/org/jabref/model/entry/BibEntry.java index 54e8e0000ab..b488be85ffe 100644 --- a/src/main/java/org/jabref/model/entry/BibEntry.java +++ b/src/main/java/org/jabref/model/entry/BibEntry.java @@ -588,8 +588,8 @@ public Optional getTitle() { * * @return will return the publication date of the entry or null if no year was found. */ - public Optional getPublicationDate() { - return getFieldOrAlias(FieldName.DATE); + public Optional getPublicationDate() { + return getFieldOrAlias(FieldName.DATE).flatMap(Date::parse); } public String getParsedSerialization() { diff --git a/src/main/java/org/jabref/model/entry/Date.java b/src/main/java/org/jabref/model/entry/Date.java index 08bb3d7f552..fb558155984 100644 --- a/src/main/java/org/jabref/model/entry/Date.java +++ b/src/main/java/org/jabref/model/entry/Date.java @@ -16,6 +16,18 @@ public class Date { private final TemporalAccessor date; + public Date(int year, int month, int dayOfMonth) { + this(LocalDate.of(year, month, dayOfMonth)); + } + + public Date(int year, int month) { + this(YearMonth.of(year, month)); + } + + public Date(int year) { + this(Year.of(year)); + } + public Date(TemporalAccessor date) { this.date = date; } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 155f53cd4ee..4ceec591ce4 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -304,6 +304,7 @@ public class JabRefPreferences implements PreferencesService { public static final String CLEANUP_RENAME_PDF_ONLY_RELATIVE_PATHS = "CleanUpRenamePDFonlyRelativePaths"; public static final String CLEANUP_UPGRADE_EXTERNAL_LINKS = "CleanUpUpgradeExternalLinks"; public static final String CLEANUP_CONVERT_TO_BIBLATEX = "CleanUpConvertToBiblatex"; + public static final String CLEANUP_CONVERT_TO_BIBTEX = "CleanUpConvertToBibtex"; public static final String CLEANUP_FIX_FILE_LINKS = "CleanUpFixFileLinks"; public static final String CLEANUP_FORMATTERS = "CleanUpFormatters"; public static final String IMPORT_DEFAULT_PDF_IMPORT_STYLE = "importDefaultPDFimportStyle"; @@ -909,12 +910,13 @@ private static Optional getNextUnit(Reader data) throws IOException { } private static void insertDefaultCleanupPreset(Map storage) { - EnumSet deactivedJobs = EnumSet.of( + EnumSet deactivatedJobs = EnumSet.of( CleanupPreset.CleanupStep.CLEAN_UP_UPGRADE_EXTERNAL_LINKS, CleanupPreset.CleanupStep.RENAME_PDF_ONLY_RELATIVE_PATHS, - CleanupPreset.CleanupStep.CONVERT_TO_BIBLATEX); + CleanupPreset.CleanupStep.CONVERT_TO_BIBLATEX, + CleanupPreset.CleanupStep.CONVERT_TO_BIBTEX); - CleanupPreset preset = new CleanupPreset(EnumSet.complementOf(deactivedJobs), Cleanups.DEFAULT_SAVE_ACTIONS); + CleanupPreset preset = new CleanupPreset(EnumSet.complementOf(deactivatedJobs), Cleanups.DEFAULT_SAVE_ACTIONS); storage.put(CLEANUP_DOI, preset.isCleanUpDOI()); storage.put(CLEANUP_ISSN, preset.isCleanUpISSN()); @@ -924,6 +926,7 @@ private static void insertDefaultCleanupPreset(Map storage) { storage.put(CLEANUP_RENAME_PDF_ONLY_RELATIVE_PATHS, preset.isRenamePdfOnlyRelativePaths()); storage.put(CLEANUP_UPGRADE_EXTERNAL_LINKS, preset.isCleanUpUpgradeExternalLinks()); storage.put(CLEANUP_CONVERT_TO_BIBLATEX, preset.isConvertToBiblatex()); + storage.put(CLEANUP_CONVERT_TO_BIBTEX, preset.isConvertToBibtex()); storage.put(CLEANUP_FIX_FILE_LINKS, preset.isFixFileLinks()); storage.put(CLEANUP_FORMATTERS, convertListToString(preset.getFormatterCleanups().getAsStringList(OS.NEWLINE))); } diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index abd463a6c6a..93644528c73 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -1892,6 +1892,7 @@ value=value Show\ preferences=Show preferences Save\ actions=Save actions Enable\ save\ actions=Enable save actions +Convert\ to\ BibTeX\ format\ (for\ example,\ move\ the\ value\ of\ the\ 'journaltitle'\ field\ to\ 'journal')=Convert to BibTeX format (for example, move the value of the 'journaltitle' field to 'journal') Other\ fields=Other fields Show\ remaining\ fields=Show remaining fields diff --git a/src/test/java/org/jabref/logic/cleanup/BibtexBiblatexRoundtripTest.java b/src/test/java/org/jabref/logic/cleanup/BibtexBiblatexRoundtripTest.java new file mode 100644 index 00000000000..1a2e903673f --- /dev/null +++ b/src/test/java/org/jabref/logic/cleanup/BibtexBiblatexRoundtripTest.java @@ -0,0 +1,62 @@ +package org.jabref.logic.cleanup; + +import org.jabref.model.entry.BibEntry; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class BibtexBiblatexRoundtripTest { + private BibEntry bibtex; + private BibEntry biblatex; + + @BeforeEach + void setUp() { + bibtex = new BibEntry("article"); + bibtex.setField("author", "Frame, J. S. and Robinson, G. de B. and Thrall, R. M."); + bibtex.setField("title", "The hook graphs of the symmetric groups"); + bibtex.setField("journal", "Canadian J. Math."); + bibtex.setField("fjournal", "Canadian Journal of Mathematics. Journal Canadien de Math\\'ematiques"); + bibtex.setField("volume", "6"); + bibtex.setField("year", "1954"); + bibtex.setField("pages", "316--324"); + bibtex.setField("issn", "0008-414X"); + bibtex.setField("mrclass", "20.0X"); + bibtex.setField("mrnumber", "0062127"); + bibtex.setField("mrreviewer", "D. E. Littlewood"); + + biblatex = new BibEntry("article"); + biblatex.setField("author", "Frame, J. S. and Robinson, G. de B. and Thrall, R. M."); + biblatex.setField("title", "The hook graphs of the symmetric groups"); + biblatex.setField("journaltitle", "Canadian J. Math."); + biblatex.setField("fjournal", "Canadian Journal of Mathematics. Journal Canadien de Math\\'ematiques"); + biblatex.setField("volume", "6"); + biblatex.setField("date", "1954"); + biblatex.setField("pages", "316--324"); + biblatex.setField("issn", "0008-414X"); + biblatex.setField("mrclass", "20.0X"); + biblatex.setField("mrnumber", "0062127"); + biblatex.setField("mrreviewer", "D. E. Littlewood"); + } + + @Test + void roundTripBibtexToBiblatexIsIdentity() { + BibEntry clone = (BibEntry) bibtex.clone(); + new ConvertToBiblatexCleanup().cleanup(clone); + assertEquals(biblatex, clone); + new ConvertToBibtexCleanup().cleanup(clone); + + assertEquals(bibtex, clone); + } + + @Test + void roundTripBiblatexToBibtexIsIdentity() { + BibEntry clone = (BibEntry) biblatex.clone(); + new ConvertToBibtexCleanup().cleanup(clone); + assertEquals(bibtex, clone); + new ConvertToBiblatexCleanup().cleanup(clone); + + assertEquals(biblatex, clone); + } +} diff --git a/src/test/java/org/jabref/logic/cleanup/BiblatexCleanupTest.java b/src/test/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanupTest.java similarity index 65% rename from src/test/java/org/jabref/logic/cleanup/BiblatexCleanupTest.java rename to src/test/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanupTest.java index 214603ce8da..f6ac76d65ad 100644 --- a/src/test/java/org/jabref/logic/cleanup/BiblatexCleanupTest.java +++ b/src/test/java/org/jabref/logic/cleanup/ConvertToBiblatexCleanupTest.java @@ -9,37 +9,49 @@ import org.junit.Before; import org.junit.Test; -public class BiblatexCleanupTest { +public class ConvertToBiblatexCleanupTest { - private BiblatexCleanup worker; + private ConvertToBiblatexCleanup worker; @Before public void setUp() { - worker = new BiblatexCleanup(); + worker = new ConvertToBiblatexCleanup(); } @Test - public void convertToBiblatexMovesYearMonthToDate() { + public void cleanupMovesYearMonthToDate() { BibEntry entry = new BibEntry(); entry.setField("year", "2011"); entry.setField("month", "#jan#"); worker.cleanup(entry); + Assert.assertEquals(Optional.empty(), entry.getField(FieldName.YEAR)); Assert.assertEquals(Optional.empty(), entry.getField(FieldName.MONTH)); Assert.assertEquals(Optional.of("2011-01"), entry.getField(FieldName.DATE)); } @Test - public void convertToBiblatexDateAlreadyPresent() { + public void cleanupWithDateAlreadyPresentDoesNothing() { BibEntry entry = new BibEntry(); entry.setField("year", "2011"); entry.setField("month", "#jan#"); entry.setField("date", "2012"); worker.cleanup(entry); + Assert.assertEquals(Optional.of("2011"), entry.getField(FieldName.YEAR)); Assert.assertEquals(Optional.of("#jan#"), entry.getField(FieldName.MONTH)); Assert.assertEquals(Optional.of("2012"), entry.getField(FieldName.DATE)); } + + @Test + public void cleanupMovesJournalToJournaltitle() { + BibEntry entry = new BibEntry().withField("journal", "Best of JabRef"); + + worker.cleanup(entry); + + Assert.assertEquals(Optional.empty(), entry.getField(FieldName.JOURNAL)); + Assert.assertEquals(Optional.of("Best of JabRef"), entry.getField(FieldName.JOURNALTITLE)); + } } diff --git a/src/test/java/org/jabref/logic/cleanup/ConvertToBibtexCleanupTest.java b/src/test/java/org/jabref/logic/cleanup/ConvertToBibtexCleanupTest.java new file mode 100644 index 00000000000..5b5de9d37b4 --- /dev/null +++ b/src/test/java/org/jabref/logic/cleanup/ConvertToBibtexCleanupTest.java @@ -0,0 +1,53 @@ +package org.jabref.logic.cleanup; + +import java.util.Optional; + +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.FieldName; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ConvertToBibtexCleanupTest { + + private ConvertToBibtexCleanup worker; + + @Before + public void setUp() { + worker = new ConvertToBibtexCleanup(); + } + + @Test + public void cleanupMovesDateToYearAndMonth() { + BibEntry entry = new BibEntry().withField("date", "2011-01"); + + worker.cleanup(entry); + + Assert.assertEquals(Optional.empty(), entry.getField(FieldName.DATE)); + Assert.assertEquals(Optional.of("2011"), entry.getField(FieldName.YEAR)); + Assert.assertEquals(Optional.of("#jan#"), entry.getField(FieldName.MONTH)); + } + + @Test + public void cleanupWithYearAlreadyPresentDoesNothing() { + BibEntry entry = new BibEntry(); + entry.setField("year", "2011"); + entry.setField("date", "2012"); + + worker.cleanup(entry); + + Assert.assertEquals(Optional.of("2011"), entry.getField(FieldName.YEAR)); + Assert.assertEquals(Optional.of("2012"), entry.getField(FieldName.DATE)); + } + + @Test + public void cleanupMovesJournaltitleToJournal() { + BibEntry entry = new BibEntry().withField("journaltitle", "Best of JabRef"); + + worker.cleanup(entry); + + Assert.assertEquals(Optional.empty(), entry.getField(FieldName.JOURNALTITLE)); + Assert.assertEquals(Optional.of("Best of JabRef"), entry.getField(FieldName.JOURNAL)); + } +} diff --git a/src/test/java/org/jabref/logic/importer/ParsedBibEntryTests.java b/src/test/java/org/jabref/logic/importer/ParsedBibEntryTests.java deleted file mode 100644 index 79c80fec9ee..00000000000 --- a/src/test/java/org/jabref/logic/importer/ParsedBibEntryTests.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.jabref.logic.importer; - -import java.util.Optional; - -import org.jabref.logic.importer.fileformat.BibtexParser; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Answers; - -import static org.mockito.Mockito.mock; - -public class ParsedBibEntryTests { - - - private ImportFormatPreferences importFormatPreferences; - - - @Before - public void setUp() { - importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); - } - - @Test - public void testGetPublicationDate() throws ParseException { - - Assert.assertEquals(Optional.of("2003-02"), - (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}, month = #FEB# }", - importFormatPreferences)).get().getPublicationDate()); - - Assert.assertEquals(Optional.of("2003-03"), - (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}, month = 3 }", - importFormatPreferences)).get().getPublicationDate()); - - Assert.assertEquals(Optional.of("2003"), - (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}}", importFormatPreferences)) - .get().getPublicationDate()); - - Assert.assertEquals(Optional.empty(), - (BibtexParser.singleFromString("@ARTICLE{HipKro03, month = 3 }", importFormatPreferences)).get() - .getPublicationDate()); - - Assert.assertEquals(Optional.empty(), - (BibtexParser.singleFromString("@ARTICLE{HipKro03, author={bla}}", importFormatPreferences)) - .get().getPublicationDate()); - - Assert.assertEquals(Optional.of("2003-12"), - (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}, month = #DEC# }", - importFormatPreferences)).get().getPublicationDate()); - - } - -} diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java index 86fd11506a9..ac7b8cb9739 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java @@ -27,6 +27,7 @@ import org.jabref.model.database.BibDatabaseMode; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexString; +import org.jabref.model.entry.Date; import org.jabref.model.entry.EntryType; import org.jabref.model.entry.FieldName; import org.jabref.model.groups.AllEntriesGroup; @@ -1167,7 +1168,7 @@ public void parseConvertsMultipleTabsToSpace() throws IOException { public void parsePreservesMultipleSpacesInNonWrappableField() throws IOException { when(importFormatPreferences.getFieldContentParserPreferences().getNonWrappableFields()) .thenReturn(Collections.singletonList("file")); - ParserResult result = parser + ParserResult result = BibtexParser .parse(new StringReader("@article{canh05,file = {ups sala}}"), importFormatPreferences); Collection c = result.getDatabase().getEntries(); @@ -1759,4 +1760,22 @@ public void parseEmptyFileLeadsToPreamble() throws IOException { assertFalse(result.hasWarnings()); assertEquals(Optional.empty(), result.getDatabase().getPreamble()); } + + @Test + public void parseYearWithMonthString() throws Exception { + Optional result = parser.parseSingleEntry("@ARTICLE{HipKro03, year = {2003}, month = #FEB# }"); + assertEquals(new Date(2003, 2), result.get().getPublicationDate().get()); + } + + @Test + public void parseYearWithMonthNumber() throws Exception { + Optional result = parser.parseSingleEntry("@ARTICLE{HipKro03, year = {2003}, month = 2 }"); + assertEquals(new Date(2003, 2), result.get().getPublicationDate().get()); + } + + @Test + public void parseYear() throws Exception { + Optional result = parser.parseSingleEntry("@ARTICLE{HipKro03, year = {2003} }"); + assertEquals(new Date(2003), result.get().getPublicationDate().get()); + } }