From 326ef9f1b8f377ddeae1684efd6972bca62dab41 Mon Sep 17 00:00:00 2001 From: Simon Harrer Date: Fri, 2 Dec 2016 10:00:25 +0100 Subject: [PATCH] Add bibtex key deviation check (#2328) * Add bibtex key deviation check * Remove shared state in BibtexKeyPatternUtil which could cause concurrency issues. * Make usage of GlobalBibtexKeyPattern easier. * Simpler makeLabel methods. --- CHANGELOG.md | 7 ++ .../jabref/shared/DBMSSynchronizerTest.java | 3 +- .../shared/SynchronizationTestSimulator.java | 3 +- .../net/sf/jabref/cli/ArgumentProcessor.java | 2 +- .../java/net/sf/jabref/gui/BasePanel.java | 4 +- .../gui/actions/IntegrityCheckAction.java | 3 +- .../BibtexKeyPatternPanel.java | 5 +- .../SearchFixDuplicateLabels.java | 2 +- .../jabref/gui/entryeditor/EntryEditor.java | 2 +- .../gui/importer/ImportInspectionDialog.java | 4 +- .../gui/openoffice/OpenOfficePanel.java | 2 +- .../BibtexKeyPatternUtil.java | 38 +++++---- .../importer/fileformat/FreeCiteImporter.java | 2 +- .../integrity/BibtexkeyDeviationChecker.java | 45 ++++++++++ .../logic/integrity/IntegrityCheck.java | 9 +- .../logic/util/io/RegExpFileSearch.java | 2 +- .../migrations/PreferencesMigrations.java | 6 +- .../GlobalBibtexKeyPattern.java | 4 + .../net/sf/jabref/pdfimport/PdfImporter.java | 2 +- .../jabref/preferences/JabRefPreferences.java | 3 +- src/main/resources/l10n/JabRef_da.properties | 1 + src/main/resources/l10n/JabRef_de.properties | 1 + src/main/resources/l10n/JabRef_en.properties | 1 + src/main/resources/l10n/JabRef_es.properties | 1 + src/main/resources/l10n/JabRef_fa.properties | 1 + src/main/resources/l10n/JabRef_fr.properties | 1 + src/main/resources/l10n/JabRef_in.properties | 1 + src/main/resources/l10n/JabRef_it.properties | 1 + src/main/resources/l10n/JabRef_ja.properties | 1 + src/main/resources/l10n/JabRef_nl.properties | 1 + src/main/resources/l10n/JabRef_no.properties | 1 + .../resources/l10n/JabRef_pt_BR.properties | 1 + src/main/resources/l10n/JabRef_ru.properties | 1 + src/main/resources/l10n/JabRef_sv.properties | 1 + src/main/resources/l10n/JabRef_tr.properties | 1 + src/main/resources/l10n/JabRef_vi.properties | 1 + src/main/resources/l10n/JabRef_zh.properties | 1 + src/test/java/net/sf/jabref/MetaDataTest.java | 3 +- .../BibtexKeyPatternUtilTest.java | 66 +++++++------- .../MakeLabelWithDatabaseTest.java | 85 +++++++++---------- .../logic/integrity/IntegrityCheckTest.java | 30 ++++++- 41 files changed, 225 insertions(+), 124 deletions(-) create mode 100644 src/main/java/net/sf/jabref/logic/integrity/BibtexkeyDeviationChecker.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 98743e89c0f..ce3a60555d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,8 +14,15 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ### Changed - URLs can now be passed as arguments to the `-import` and `-importToOpen` command line options. The referenced file is downloaded and then imported as usual. - The default emacs executable name on linux changed from `gnuclient` to `emacsclient`. [feature-request 433](https://sourceforge.net/p/jabref/feature-requests/433/) +- Adds integrity check to detect all bibtex keys which deviate from their generation pattern [#2206](https://github.com/JabRef/jabref/issues/2206) - Adds an integrity check that detects invalid DOIs [#1445](https://github.com/JabRef/jabref/issues/1445) + + + + + + ### Fixed - We fixed an issue which caused a metadata loss on reconnection to shared database. [#2219](https://github.com/JabRef/jabref/issues/2219) - We fixed an issue which caused an internal error when leaving the file path field empty and connecting to a shared database. diff --git a/src/databaseTest/java/net/sf/jabref/shared/DBMSSynchronizerTest.java b/src/databaseTest/java/net/sf/jabref/shared/DBMSSynchronizerTest.java index 448a81901d4..d47f9547604 100644 --- a/src/databaseTest/java/net/sf/jabref/shared/DBMSSynchronizerTest.java +++ b/src/databaseTest/java/net/sf/jabref/shared/DBMSSynchronizerTest.java @@ -10,7 +10,6 @@ import net.sf.jabref.logic.exporter.MetaDataSerializer; import net.sf.jabref.logic.formatter.casechanger.LowerCaseFormatter; -import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.cleanup.FieldFormatterCleanup; import net.sf.jabref.model.cleanup.FieldFormatterCleanups; @@ -53,7 +52,7 @@ public void setUp() throws SQLException, DatabaseNotSupportedException, InvalidD bibDatabase = new BibDatabase(); BibDatabaseContext context = new BibDatabaseContext(bibDatabase); - pattern = new GlobalBibtexKeyPattern(AbstractBibtexKeyPattern.split("[auth][year]")); + pattern = GlobalBibtexKeyPattern.fromPattern("[auth][year]"); dbmsSynchronizer = new DBMSSynchronizer(context, ',', pattern); dbmsProcessor = DBMSProcessor.getProcessorInstance(dbmsConnection); diff --git a/src/databaseTest/java/net/sf/jabref/shared/SynchronizationTestSimulator.java b/src/databaseTest/java/net/sf/jabref/shared/SynchronizationTestSimulator.java index 39a3de244da..c70cdb46f55 100644 --- a/src/databaseTest/java/net/sf/jabref/shared/SynchronizationTestSimulator.java +++ b/src/databaseTest/java/net/sf/jabref/shared/SynchronizationTestSimulator.java @@ -4,7 +4,6 @@ import java.util.Collection; import net.sf.jabref.model.Defaults; -import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.database.BibDatabaseContext; import net.sf.jabref.model.database.BibDatabaseMode; @@ -40,7 +39,7 @@ public class SynchronizationTestSimulator { public void setUp() throws SQLException, DatabaseNotSupportedException, InvalidDBMSConnectionPropertiesException { this.dbmsConnection = TestConnector.getTestDBMSConnection(dbmsType); - GlobalBibtexKeyPattern pattern = new GlobalBibtexKeyPattern(AbstractBibtexKeyPattern.split("[auth][year]")); + GlobalBibtexKeyPattern pattern = GlobalBibtexKeyPattern.fromPattern("[auth][year]"); clientContextA = new BibDatabaseContext(new Defaults(BibDatabaseMode.BIBTEX), DatabaseLocation.SHARED, ',', pattern); clientContextA.getDBMSSynchronizer().openSharedDatabase(dbmsConnection); diff --git a/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java b/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java index 76ab791dd07..3739a674bc2 100644 --- a/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java @@ -453,7 +453,7 @@ private void regenerateBibtexKeys(List loaded) { LOGGER.info(Localization.lang("Regenerating BibTeX keys according to metadata")); for (BibEntry entry : database.getEntries()) { // try to make a new label - BibtexKeyPatternUtil.makeLabel( + BibtexKeyPatternUtil.makeAndSetLabel( metaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), database, entry, Globals.prefs.getBibtexKeyPatternPreferences()); } diff --git a/src/main/java/net/sf/jabref/gui/BasePanel.java b/src/main/java/net/sf/jabref/gui/BasePanel.java index 25be6e3ff13..0523c1939c6 100644 --- a/src/main/java/net/sf/jabref/gui/BasePanel.java +++ b/src/main/java/net/sf/jabref/gui/BasePanel.java @@ -433,7 +433,7 @@ public void run() { .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()); for (BibEntry entry : entries) { String oldCiteKey = entry.getCiteKeyOptional().orElse(""); - BibtexKeyPatternUtil.makeLabel(citeKeyPattern, bibDatabaseContext.getDatabase(), + BibtexKeyPatternUtil.makeAndSetLabel(citeKeyPattern, bibDatabaseContext.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences()); String newCiteKey = entry.getCiteKeyOptional().orElse(""); if (!oldCiteKey.equals(newCiteKey)) { @@ -1893,7 +1893,7 @@ public void autoGenerateKeysBeforeSaving() { for (BibEntry bes : bibDatabaseContext.getDatabase().getEntries()) { Optional oldKey = bes.getCiteKeyOptional(); if (!(oldKey.isPresent()) || oldKey.get().isEmpty()) { - BibtexKeyPatternUtil.makeLabel(bibDatabaseContext.getMetaData() + BibtexKeyPatternUtil.makeAndSetLabel(bibDatabaseContext.getMetaData() .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), bibDatabaseContext.getDatabase(), bes, Globals.prefs.getBibtexKeyPatternPreferences()); diff --git a/src/main/java/net/sf/jabref/gui/actions/IntegrityCheckAction.java b/src/main/java/net/sf/jabref/gui/actions/IntegrityCheckAction.java index 4bc92aee69c..5b4dd1cb7dd 100644 --- a/src/main/java/net/sf/jabref/gui/actions/IntegrityCheckAction.java +++ b/src/main/java/net/sf/jabref/gui/actions/IntegrityCheckAction.java @@ -47,7 +47,8 @@ public IntegrityCheckAction(JabRefFrame frame) { @Override public void actionPerformed(ActionEvent e) { IntegrityCheck check = new IntegrityCheck(frame.getCurrentBasePanel().getBibDatabaseContext(), - Globals.prefs.getFileDirectoryPreferences()); + Globals.prefs.getFileDirectoryPreferences(), + Globals.prefs.getBibtexKeyPatternPreferences()); List messages = check.checkBibtexDatabase(); if (messages.isEmpty()) { diff --git a/src/main/java/net/sf/jabref/gui/bibtexkeypattern/BibtexKeyPatternPanel.java b/src/main/java/net/sf/jabref/gui/bibtexkeypattern/BibtexKeyPatternPanel.java index 283fbad1b17..572b6a39396 100644 --- a/src/main/java/net/sf/jabref/gui/bibtexkeypattern/BibtexKeyPatternPanel.java +++ b/src/main/java/net/sf/jabref/gui/bibtexkeypattern/BibtexKeyPatternPanel.java @@ -224,8 +224,9 @@ private void fillPatternUsingPanelData(AbstractBibtexKeyPattern keypatterns) { } protected GlobalBibtexKeyPattern getKeyPatternAsGlobalBibtexKeyPattern() { - GlobalBibtexKeyPattern res = new GlobalBibtexKeyPattern( - AbstractBibtexKeyPattern.split(JabRefPreferences.getInstance().get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN))); + GlobalBibtexKeyPattern res = GlobalBibtexKeyPattern.fromPattern( + JabRefPreferences.getInstance().get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN) + ); fillPatternUsingPanelData(res); return res; } diff --git a/src/main/java/net/sf/jabref/gui/bibtexkeypattern/SearchFixDuplicateLabels.java b/src/main/java/net/sf/jabref/gui/bibtexkeypattern/SearchFixDuplicateLabels.java index dc5fc5f17fe..a786daddc33 100644 --- a/src/main/java/net/sf/jabref/gui/bibtexkeypattern/SearchFixDuplicateLabels.java +++ b/src/main/java/net/sf/jabref/gui/bibtexkeypattern/SearchFixDuplicateLabels.java @@ -93,7 +93,7 @@ public void update() { NamedCompound ce = new NamedCompound(Localization.lang("Resolve duplicate BibTeX keys")); for (BibEntry entry : toGenerateFor) { String oldKey = entry.getCiteKeyOptional().orElse(null); - BibtexKeyPatternUtil.makeLabel(panel.getBibDatabaseContext().getMetaData() + BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData() .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences()); diff --git a/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java index cb8e13c82a0..8bff5615f1f 100644 --- a/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java @@ -1256,7 +1256,7 @@ public void actionPerformed(ActionEvent e) { } } - BibtexKeyPatternUtil.makeLabel(panel.getBibDatabaseContext().getMetaData() + BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData() .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences()); diff --git a/src/main/java/net/sf/jabref/gui/importer/ImportInspectionDialog.java b/src/main/java/net/sf/jabref/gui/importer/ImportInspectionDialog.java index b8b16c81d04..3a66d938fa9 100644 --- a/src/main/java/net/sf/jabref/gui/importer/ImportInspectionDialog.java +++ b/src/main/java/net/sf/jabref/gui/importer/ImportInspectionDialog.java @@ -462,7 +462,7 @@ private void generateKeyForEntry(BibEntry entry) { database.insertEntry(entry); // Generate a unique key: - BibtexKeyPatternUtil.makeLabel( + BibtexKeyPatternUtil.makeAndSetLabel( localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), database, entry, Globals.prefs.getBibtexKeyPatternPreferences()); @@ -506,7 +506,7 @@ private void generateKeys() { entry.setId(IdGenerator.next()); database.insertEntry(entry); - BibtexKeyPatternUtil.makeLabel( + BibtexKeyPatternUtil.makeAndSetLabel( localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), database, entry, Globals.prefs.getBibtexKeyPatternPreferences()); diff --git a/src/main/java/net/sf/jabref/gui/openoffice/OpenOfficePanel.java b/src/main/java/net/sf/jabref/gui/openoffice/OpenOfficePanel.java index 45ab2217451..39d97f685f3 100644 --- a/src/main/java/net/sf/jabref/gui/openoffice/OpenOfficePanel.java +++ b/src/main/java/net/sf/jabref/gui/openoffice/OpenOfficePanel.java @@ -685,7 +685,7 @@ private boolean checkThatEntriesHaveKeys(List entries) { if (!entry.getCiteKeyOptional().isPresent()) { // Generate key BibtexKeyPatternUtil - .makeLabel( + .makeAndSetLabel( panel.getBibDatabaseContext().getMetaData().getCiteKeyPattern(prefs.getKeyPattern()), panel.getDatabase(), entry, prefs); diff --git a/src/main/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java b/src/main/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java index 7e86d49abcf..7178b034402 100644 --- a/src/main/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java +++ b/src/main/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java @@ -16,6 +16,7 @@ import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.cleanup.Formatter; import net.sf.jabref.model.database.BibDatabase; +import net.sf.jabref.model.database.BibDatabaseContext; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.FieldName; @@ -42,17 +43,6 @@ public class BibtexKeyPatternUtil { private static final int CHARS_OF_FIRST = 5; - private static BibDatabase database; - - /** - * Required for LabelPatternUtilTest - * - * @param db the DB to use as global database - */ - public static void setDataBase(BibDatabase db) { - database = db; - } - private static String normalize(String content) { List tokens = new ArrayList<>(); int b = 0; @@ -374,13 +364,17 @@ private static String generateInstitutionKey(String content) { * The given database is used to avoid duplicate keys. * * @param citeKeyPattern - * @param dBase a BibDatabase + * @param database a BibDatabase * @param entry a BibEntry * @return modified BibEntry */ - public static void makeLabel(AbstractBibtexKeyPattern citeKeyPattern, BibDatabase dBase, BibEntry entry, + public static void makeAndSetLabel(AbstractBibtexKeyPattern citeKeyPattern, BibDatabase database, BibEntry entry, BibtexKeyPatternPreferences bibtexKeyPatternPreferences) { - database = dBase; + String newKey = makeLabel(citeKeyPattern, database, entry, bibtexKeyPatternPreferences); + entry.setCiteKey(newKey); + } + + private static String makeLabel(AbstractBibtexKeyPattern citeKeyPattern, BibDatabase database, BibEntry entry, BibtexKeyPatternPreferences bibtexKeyPatternPreferences) { String key; StringBuilder stringBuilder = new StringBuilder(); try { @@ -401,7 +395,7 @@ public static void makeLabel(AbstractBibtexKeyPattern citeKeyPattern, BibDatabas // check whether there is a modifier on the end such as // ":lower" List parts = parseFieldMarker(typeListEntry); - String label = makeLabel(entry, parts.get(0), bibtexKeyPatternPreferences.getKeywordDelimiter()); + String label = makeLabel(entry, parts.get(0), bibtexKeyPatternPreferences.getKeywordDelimiter(), database); // apply modifier if present if (parts.size() > 1) { @@ -438,8 +432,9 @@ public static void makeLabel(AbstractBibtexKeyPattern citeKeyPattern, BibDatabas boolean alwaysAddLetter = bibtexKeyPatternPreferences.isAlwaysAddLetter(); boolean firstLetterA = bibtexKeyPatternPreferences.isFirstLetterA(); + String newKey; if (!alwaysAddLetter && (occurrences == 0)) { - entry.setCiteKey(key); + newKey = key; } else { // The key is already in use, so we must modify it. int number = !alwaysAddLetter && !firstLetterA ? 1 : 0; @@ -456,8 +451,9 @@ public static void makeLabel(AbstractBibtexKeyPattern citeKeyPattern, BibDatabas } } while (occurrences > 0); - entry.setCiteKey(moddedKey); + newKey = moddedKey; } + return newKey; } /** @@ -508,7 +504,7 @@ public static String applyModifiers(final String label, final List parts return resultingLabel; } - public static String makeLabel(BibEntry entry, String value, Character keywordDelimiter) { + public static String makeLabel(BibEntry entry, String value, Character keywordDelimiter, BibDatabase database) { String val = value; try { if (val.startsWith("auth") || val.startsWith("pureauth")) { @@ -1304,4 +1300,10 @@ public static String checkLegalKey(String key, boolean enforceLegalKey) { return StringUtil.replaceSpecialCharacters(newKey.toString()); } + public static String makeLabel(BibDatabaseContext bibDatabaseContext, + BibEntry entry, + BibtexKeyPatternPreferences bibtexKeyPatternPreferences) { + AbstractBibtexKeyPattern citeKeyPattern = bibDatabaseContext.getMetaData().getCiteKeyPattern(bibtexKeyPatternPreferences.getKeyPattern()); + return makeLabel(citeKeyPattern, bibDatabaseContext.getDatabase(), entry, bibtexKeyPatternPreferences); + } } diff --git a/src/main/java/net/sf/jabref/logic/importer/fileformat/FreeCiteImporter.java b/src/main/java/net/sf/jabref/logic/importer/fileformat/FreeCiteImporter.java index fd76ca91cff..e709fffeccc 100644 --- a/src/main/java/net/sf/jabref/logic/importer/fileformat/FreeCiteImporter.java +++ b/src/main/java/net/sf/jabref/logic/importer/fileformat/FreeCiteImporter.java @@ -203,7 +203,7 @@ public ParserResult importEntries(String text) { e.setType(type); // autogenerate label (BibTeX key) - BibtexKeyPatternUtil.makeLabel( + BibtexKeyPatternUtil.makeAndSetLabel( JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext().getMetaData() .getCiteKeyPattern(importFormatPreferences.getBibtexKeyPatternPreferences().getKeyPattern()), JabRefGUI.getMainFrame().getCurrentBasePanel().getDatabase(), e, diff --git a/src/main/java/net/sf/jabref/logic/integrity/BibtexkeyDeviationChecker.java b/src/main/java/net/sf/jabref/logic/integrity/BibtexkeyDeviationChecker.java new file mode 100644 index 00000000000..7ac55a1dae8 --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/integrity/BibtexkeyDeviationChecker.java @@ -0,0 +1,45 @@ +package net.sf.jabref.logic.integrity; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import net.sf.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; +import net.sf.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil; +import net.sf.jabref.logic.integrity.IntegrityCheck.Checker; +import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.model.database.BibDatabaseContext; +import net.sf.jabref.model.entry.BibEntry; + +public class BibtexkeyDeviationChecker implements Checker { + + private final BibDatabaseContext bibDatabaseContext; + private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences; + + public BibtexkeyDeviationChecker(BibDatabaseContext bibDatabaseContext, BibtexKeyPatternPreferences bibtexKeyPatternPreferences) { + this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); + this.bibtexKeyPatternPreferences = Objects.requireNonNull(bibtexKeyPatternPreferences); + } + + @Override + public List check(BibEntry entry) { + Optional valuekey = entry.getCiteKeyOptional(); + if (!valuekey.isPresent()) { + return Collections.emptyList(); + } + + String key = valuekey.get(); + + // generate new key + String generatedKey = BibtexKeyPatternUtil.makeLabel(bibDatabaseContext, entry, bibtexKeyPatternPreferences); + + if (!Objects.equals(key, generatedKey)) { + return Collections.singletonList(new IntegrityMessage( + Localization.lang("BibTeX key %0 deviates from generated key %1", key, generatedKey), entry, BibEntry.KEY_FIELD)); + } + + return Collections.emptyList(); + } + +} diff --git a/src/main/java/net/sf/jabref/logic/integrity/IntegrityCheck.java b/src/main/java/net/sf/jabref/logic/integrity/IntegrityCheck.java index 6a4a8d99159..618d5b6f943 100644 --- a/src/main/java/net/sf/jabref/logic/integrity/IntegrityCheck.java +++ b/src/main/java/net/sf/jabref/logic/integrity/IntegrityCheck.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Objects; +import net.sf.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import net.sf.jabref.model.database.BibDatabaseContext; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.FieldName; @@ -14,10 +15,15 @@ public class IntegrityCheck { private final BibDatabaseContext bibDatabaseContext; private final FileDirectoryPreferences fileDirectoryPreferences; + private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences; - public IntegrityCheck(BibDatabaseContext bibDatabaseContext, FileDirectoryPreferences fileDirectoryPreferences) { + public IntegrityCheck(BibDatabaseContext bibDatabaseContext, + FileDirectoryPreferences fileDirectoryPreferences, + BibtexKeyPatternPreferences bibtexKeyPatternPreferences + ) { this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); this.fileDirectoryPreferences = Objects.requireNonNull(fileDirectoryPreferences); + this.bibtexKeyPatternPreferences = Objects.requireNonNull(bibtexKeyPatternPreferences); } public List checkBibtexDatabase() { @@ -72,6 +78,7 @@ private List checkBibtexEntry(BibEntry entry) { result.addAll(new ISBNChecker().check(entry)); result.addAll(new DOIValidityChecker().check(entry)); result.addAll(new EntryLinkChecker(bibDatabaseContext.getDatabase()).check(entry)); + result.addAll(new BibtexkeyDeviationChecker(bibDatabaseContext, bibtexKeyPatternPreferences).check(entry)); return result; } diff --git a/src/main/java/net/sf/jabref/logic/util/io/RegExpFileSearch.java b/src/main/java/net/sf/jabref/logic/util/io/RegExpFileSearch.java index 94ae4dda150..1ad837e01d8 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/RegExpFileSearch.java +++ b/src/main/java/net/sf/jabref/logic/util/io/RegExpFileSearch.java @@ -324,7 +324,7 @@ public static String getFieldAndFormat(String fieldAndFormat, BibEntry entry, Bi // If no field value was found, try to interpret it as a key generator field marker: String fieldValue = entry.getResolvedFieldOrAlias(beforeColon, database) - .orElse(BibtexKeyPatternUtil.makeLabel(entry, beforeColon, keywordDelimiter)); + .orElse(BibtexKeyPatternUtil.makeLabel(entry, beforeColon, keywordDelimiter, database)); if (fieldValue == null) { return ""; diff --git a/src/main/java/net/sf/jabref/migrations/PreferencesMigrations.java b/src/main/java/net/sf/jabref/migrations/PreferencesMigrations.java index 4e421ef09de..49d10f2b496 100644 --- a/src/main/java/net/sf/jabref/migrations/PreferencesMigrations.java +++ b/src/main/java/net/sf/jabref/migrations/PreferencesMigrations.java @@ -7,7 +7,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefMain; -import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.entry.FieldName; import net.sf.jabref.preferences.JabRefPreferences; @@ -131,8 +130,9 @@ private static void migrateTypedKeyPrefs(JabRefPreferences prefs, Preferences ol throws BackingStoreException { LOGGER.info("Found old Bibtex Key patterns which will be migrated to new version."); - GlobalBibtexKeyPattern keyPattern = new GlobalBibtexKeyPattern(AbstractBibtexKeyPattern - .split(prefs.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN))); + GlobalBibtexKeyPattern keyPattern = GlobalBibtexKeyPattern.fromPattern( + prefs.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN) + ); for (String key : oldPatternPrefs.keys()) { keyPattern.addBibtexKeyPattern(key, oldPatternPrefs.get(key, null)); } diff --git a/src/main/java/net/sf/jabref/model/bibtexkeypattern/GlobalBibtexKeyPattern.java b/src/main/java/net/sf/jabref/model/bibtexkeypattern/GlobalBibtexKeyPattern.java index 7eb0da3b0f2..5c1d4d10942 100644 --- a/src/main/java/net/sf/jabref/model/bibtexkeypattern/GlobalBibtexKeyPattern.java +++ b/src/main/java/net/sf/jabref/model/bibtexkeypattern/GlobalBibtexKeyPattern.java @@ -10,6 +10,10 @@ public GlobalBibtexKeyPattern(List bibtexKeyPattern) { defaultBibtexKeyPattern = bibtexKeyPattern; } + public static GlobalBibtexKeyPattern fromPattern(String pattern) { + return new GlobalBibtexKeyPattern(split(pattern)); + } + @Override public List getLastLevelBibtexKeyPattern(String key) { return defaultBibtexKeyPattern; diff --git a/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java b/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java index c72c9affb1a..fa207d4027c 100644 --- a/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java +++ b/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java @@ -236,7 +236,7 @@ private void doContentImport(String fileName, List res) { // insert entry to database and link file panel.getDatabase().insertEntry(entry); panel.markBaseChanged(); - BibtexKeyPatternUtil.makeLabel(panel.getBibDatabaseContext().getMetaData() + BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData() .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences()); DroppedFileHandler dfh = new DroppedFileHandler(frame, panel); diff --git a/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java b/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java index d1749ea6a83..edb3a077c9a 100644 --- a/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java @@ -67,7 +67,6 @@ import net.sf.jabref.logic.util.Version; import net.sf.jabref.logic.util.io.FileHistory; import net.sf.jabref.logic.xmp.XMPPreferences; -import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.database.BibDatabaseMode; import net.sf.jabref.model.entry.BibEntry; @@ -1078,7 +1077,7 @@ public void flush() { * @return LabelPattern containing all keys. Returned LabelPattern has no parent */ public GlobalBibtexKeyPattern getKeyPattern() { - keyPattern = new GlobalBibtexKeyPattern(AbstractBibtexKeyPattern.split(get(DEFAULT_BIBTEX_KEY_PATTERN))); + keyPattern = GlobalBibtexKeyPattern.fromPattern(get(DEFAULT_BIBTEX_KEY_PATTERN)); Preferences pre = Preferences.userNodeForPackage(JabRefMain.class).node(BIBTEX_KEY_PATTERNS_NODE); try { String[] keys = pre.keys(); diff --git a/src/main/resources/l10n/JabRef_da.properties b/src/main/resources/l10n/JabRef_da.properties index fe7d3aeacbd..26dd504bf5c 100644 --- a/src/main/resources/l10n/JabRef_da.properties +++ b/src/main/resources/l10n/JabRef_da.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_de.properties b/src/main/resources/l10n/JabRef_de.properties index 2b6315f2d65..a542fea74d8 100644 --- a/src/main/resources/l10n/JabRef_de.properties +++ b/src/main/resources/l10n/JabRef_de.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0=Empfohlen_für_%0 This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).=Dies_kann_durch_eine_Downloadbeschränkung_von_Google_Scholar_ausgelöst_werden_(mehr_Details_in_der_'Hilfe'). Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 4251a789bb8..744f9ea6122 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0=Recommended_for_%0 This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).=This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details). Problem_downloading_from_%1=Problem_downloading_from_%1 +BibTeX_key_%0_deviates_from_generated_key_%1=BibTeX_key_%0_deviates_from_generated_key_%1 DOI_%0_is_invalid=DOI_%0_is_invalid diff --git a/src/main/resources/l10n/JabRef_es.properties b/src/main/resources/l10n/JabRef_es.properties index 2acf31c55c6..ff31cce7538 100644 --- a/src/main/resources/l10n/JabRef_es.properties +++ b/src/main/resources/l10n/JabRef_es.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0=Recomendado_para_%0 This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).=Esto_puede_ser_debido_a_alcanzar_el_límite_de_tráfico_de_GoogleScholar_(vea_la_'Ayuda'_para_más_detalles) Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_fa.properties b/src/main/resources/l10n/JabRef_fa.properties index bf04c51529f..c508df27487 100644 --- a/src/main/resources/l10n/JabRef_fa.properties +++ b/src/main/resources/l10n/JabRef_fa.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_fr.properties b/src/main/resources/l10n/JabRef_fr.properties index 7a3762d433b..b0cb3a7e71b 100644 --- a/src/main/resources/l10n/JabRef_fr.properties +++ b/src/main/resources/l10n/JabRef_fr.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0=Recommandé_pour_%0 This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).=La_limitation_de_trafic_de_Google_Scholar_pourrait_avoir_été_atteinte_(voir_l'aide_pour_plus_de_détails). Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_in.properties b/src/main/resources/l10n/JabRef_in.properties index 9a5dae281c8..5eddfd2b400 100644 --- a/src/main/resources/l10n/JabRef_in.properties +++ b/src/main/resources/l10n/JabRef_in.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_it.properties b/src/main/resources/l10n/JabRef_it.properties index ed532443c59..a3e697a767b 100644 --- a/src/main/resources/l10n/JabRef_it.properties +++ b/src/main/resources/l10n/JabRef_it.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0=Raccomandato_per_%0 This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).=Questo_può_essere_dovuto_al_raggiungimento_del_limite_di_traffico_di_Google_Scholar_(vedi_'Aiuto'_per_i_dettagli). Problem_downloading_from_%1=Problema_scaricando_da_%1 +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_ja.properties b/src/main/resources/l10n/JabRef_ja.properties index 5862d9475ff..681741814d8 100644 --- a/src/main/resources/l10n/JabRef_ja.properties +++ b/src/main/resources/l10n/JabRef_ja.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_nl.properties b/src/main/resources/l10n/JabRef_nl.properties index ab0b8827554..ffb67d7ed88 100644 --- a/src/main/resources/l10n/JabRef_nl.properties +++ b/src/main/resources/l10n/JabRef_nl.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_no.properties b/src/main/resources/l10n/JabRef_no.properties index ddbbcceb226..ed649bb4a24 100644 --- a/src/main/resources/l10n/JabRef_no.properties +++ b/src/main/resources/l10n/JabRef_no.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_pt_BR.properties b/src/main/resources/l10n/JabRef_pt_BR.properties index 2dc1294738e..f9a761c17c8 100644 --- a/src/main/resources/l10n/JabRef_pt_BR.properties +++ b/src/main/resources/l10n/JabRef_pt_BR.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_ru.properties b/src/main/resources/l10n/JabRef_ru.properties index e7f5014088f..2130d5ba0af 100644 --- a/src/main/resources/l10n/JabRef_ru.properties +++ b/src/main/resources/l10n/JabRef_ru.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_sv.properties b/src/main/resources/l10n/JabRef_sv.properties index 1eca977e4ba..5d489ebd502 100644 --- a/src/main/resources/l10n/JabRef_sv.properties +++ b/src/main/resources/l10n/JabRef_sv.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_tr.properties b/src/main/resources/l10n/JabRef_tr.properties index acfa180d960..50849525a71 100644 --- a/src/main/resources/l10n/JabRef_tr.properties +++ b/src/main/resources/l10n/JabRef_tr.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0=%0_için_önerilir This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).=Bu,_Google_Scholar'ın_trafik_limitine_erişmekten_dolayı_olabilir_(ayrıntılar_için_'Yardım'a_bakınız). Problem_downloading_from_%1=%1'den_indirmede_hata +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_vi.properties b/src/main/resources/l10n/JabRef_vi.properties index deacfb57dbe..5a4c2c5f76f 100644 --- a/src/main/resources/l10n/JabRef_vi.properties +++ b/src/main/resources/l10n/JabRef_vi.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/main/resources/l10n/JabRef_zh.properties b/src/main/resources/l10n/JabRef_zh.properties index 49045b4e992..feb1185a8f0 100644 --- a/src/main/resources/l10n/JabRef_zh.properties +++ b/src/main/resources/l10n/JabRef_zh.properties @@ -2304,4 +2304,5 @@ Recommended_for_%0= This_might_be_caused_by_reaching_the_traffic_limitation_of_Google_Scholar_(see_'Help'_for_details).= Problem_downloading_from_%1= +BibTeX_key_%0_deviates_from_generated_key_%1= DOI_%0_is_invalid= diff --git a/src/test/java/net/sf/jabref/MetaDataTest.java b/src/test/java/net/sf/jabref/MetaDataTest.java index 94072110348..088af60e4c9 100644 --- a/src/test/java/net/sf/jabref/MetaDataTest.java +++ b/src/test/java/net/sf/jabref/MetaDataTest.java @@ -8,7 +8,6 @@ import net.sf.jabref.logic.exporter.MetaDataSerializer; import net.sf.jabref.logic.formatter.casechanger.LowerCaseFormatter; import net.sf.jabref.logic.util.OS; -import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.cleanup.FieldFormatterCleanup; import net.sf.jabref.model.cleanup.FieldFormatterCleanups; @@ -27,7 +26,7 @@ public class MetaDataTest { @Before public void setUp() { metaData = new MetaData(); - pattern = new GlobalBibtexKeyPattern(AbstractBibtexKeyPattern.split("[auth][year]")); + pattern = GlobalBibtexKeyPattern.fromPattern("[auth][year]"); } @Test diff --git a/src/test/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtilTest.java b/src/test/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtilTest.java index a608979a112..6047f23e134 100644 --- a/src/test/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtilTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtilTest.java @@ -40,11 +40,9 @@ public class BibtexKeyPatternUtilTest { private static ImportFormatPreferences importFormatPreferences; - @Before public void setUp() { importFormatPreferences = JabRefPreferences.getInstance().getImportFormatPreferences(); - BibtexKeyPatternUtil.setDataBase(new BibDatabase()); } @Test @@ -53,7 +51,7 @@ public void testAndInAuthorName() { importFormatPreferences); assertEquals("Holland", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth", - ','), true)); + ',', new BibDatabase()), true)); } @Test @@ -62,7 +60,7 @@ public void testAndAuthorNames() { Optional entry = BibtexParser.singleFromString(bibtexString, importFormatPreferences); assertEquals("HerlandHaugeHelgeland", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry.get(), "authors3", - ','), true)); + ',', new BibDatabase()), true)); } @Test @@ -71,7 +69,7 @@ public void testSpecialLatexCharacterInAuthorName() { "@ARTICLE{kohn, author={Simon Popovi\\v{c}ov\\'{a}}}", importFormatPreferences); assertEquals("Popovicova", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry.get(), "auth", - ','), true)); + ',', new BibDatabase()), true)); } /** @@ -85,73 +83,73 @@ public void testMakeLabelAndCheckLegalKeys() { "@ARTICLE{kohn, author={Andreas Köning}, year={2000}}", importFormatPreferences); assertEquals("Koen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Áöning}, year={2000}}", importFormatPreferences); assertEquals("Aoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Éöning}, year={2000}}", importFormatPreferences); assertEquals("Eoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Íöning}, year={2000}}", importFormatPreferences); assertEquals("Ioen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ĺöning}, year={2000}}", importFormatPreferences); assertEquals("Loen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ńöning}, year={2000}}", importFormatPreferences); assertEquals("Noen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Óöning}, year={2000}}", importFormatPreferences); assertEquals("Ooen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ŕöning}, year={2000}}", importFormatPreferences); assertEquals("Roen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Śöning}, year={2000}}", importFormatPreferences); assertEquals("Soen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Úöning}, year={2000}}", importFormatPreferences); assertEquals("Uoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ýöning}, year={2000}}", importFormatPreferences); assertEquals("Yoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Źöning}, year={2000}}", importFormatPreferences); assertEquals("Zoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); } /** @@ -163,31 +161,31 @@ public void testMakeLabelAndCheckLegalKeysAccentGrave() { "@ARTICLE{kohn, author={Andreas Àöning}, year={2000}}", importFormatPreferences); assertEquals("Aoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Èöning}, year={2000}}", importFormatPreferences); assertEquals("Eoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ìöning}, year={2000}}", importFormatPreferences); assertEquals("Ioen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Òöning}, year={2000}}", importFormatPreferences); assertEquals("Ooen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ùöning}, year={2000}}", importFormatPreferences); assertEquals("Uoen", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry0.get(), "auth3", - ','), true)); + ',', new BibDatabase()), true)); } /** @@ -276,7 +274,7 @@ public void testUniversity() { "@ARTICLE{kohn, author={{Link{\\\"{o}}ping University}}}", importFormatPreferences); assertEquals("UniLinkoeping", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry.get(), "auth", - ','), true)); + ',', new BibDatabase()), true)); } @Test @@ -286,7 +284,7 @@ public void testDepartment() { importFormatPreferences); assertEquals("UniLinkoepingEE", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry.get(), "auth", - ','), true)); + ',', new BibDatabase()), true)); } @Test @@ -296,7 +294,7 @@ public void testSchool() { importFormatPreferences); assertEquals("UniLinkoepingCE", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry.get(), "auth", - ','), true)); + ',', new BibDatabase()), true)); } @Test @@ -305,7 +303,7 @@ public void testInstituteOfTechnology() { "@ARTICLE{kohn, author={{Massachusetts Institute of Technology}}}", importFormatPreferences); assertEquals("MIT", BibtexKeyPatternUtil.checkLegalKey(BibtexKeyPatternUtil.makeLabel(entry.get(), "auth", - ','), true)); + ',', new BibDatabase()), true)); } @Test @@ -665,15 +663,15 @@ public void keywordNKeywordsSeparatedBySpace() { BibEntry entry = new BibEntry(); entry.setField("keywords", "w1, w2a w2b, w3"); - String result = BibtexKeyPatternUtil.makeLabel(entry, "keyword1", ','); + String result = BibtexKeyPatternUtil.makeLabel(entry, "keyword1", ',', new BibDatabase()); assertEquals("w1", result); // check keywords with space - result = BibtexKeyPatternUtil.makeLabel(entry, "keyword2", ','); + result = BibtexKeyPatternUtil.makeLabel(entry, "keyword2", ',', new BibDatabase()); assertEquals("w2a w2b", result); // check out of range - result = BibtexKeyPatternUtil.makeLabel(entry, "keyword4", ','); + result = BibtexKeyPatternUtil.makeLabel(entry, "keyword4", ',', new BibDatabase()); assertEquals("", result); } @@ -683,15 +681,15 @@ public void keywordsNKeywordsSeparatedBySpace() { entry.setField("keywords", "w1, w2a w2b, w3"); // all keywords - String result = BibtexKeyPatternUtil.makeLabel(entry, "keywords", ','); + String result = BibtexKeyPatternUtil.makeLabel(entry, "keywords", ',', new BibDatabase()); assertEquals("w1w2aw2bw3", result); // check keywords with space - result = BibtexKeyPatternUtil.makeLabel(entry, "keywords2", ','); + result = BibtexKeyPatternUtil.makeLabel(entry, "keywords2", ',', new BibDatabase()); assertEquals("w1w2aw2b", result); // check out of range - result = BibtexKeyPatternUtil.makeLabel(entry, "keywords55", ','); + result = BibtexKeyPatternUtil.makeLabel(entry, "keywords55", ',', new BibDatabase()); assertEquals("w1w2aw2bw3", result); } @@ -719,9 +717,9 @@ public void testCheckLegalNullInNullOut() { public void testApplyModifiers() { BibEntry entry = new BibEntry(); entry.setField("title", "Green Scheduling of Whatever"); - assertEquals("GSW", BibtexKeyPatternUtil.makeLabel(entry, "shorttitleINI", ',')); + assertEquals("GSW", BibtexKeyPatternUtil.makeLabel(entry, "shorttitleINI", ',', new BibDatabase())); assertEquals("GreenSchedulingWhatever", BibtexKeyPatternUtil.makeLabel(entry, "shorttitle", - ',')); + ',', new BibDatabase())); } } diff --git a/src/test/java/net/sf/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java b/src/test/java/net/sf/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java index 0b7c309ddee..5e82c71e09d 100644 --- a/src/test/java/net/sf/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java @@ -2,7 +2,6 @@ import java.util.Optional; -import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.DatabaseBibtexKeyPattern; import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.database.BibDatabase; @@ -30,31 +29,31 @@ public void setUp() { entry.setField("year", "2016"); entry.setField("title", "An awesome paper on JabRef"); database.insertEntry(entry); - pattern = new GlobalBibtexKeyPattern(AbstractBibtexKeyPattern.split("[auth][year]")); + pattern = GlobalBibtexKeyPattern.fromPattern("[auth][year]"); bibtexKeyPattern = new DatabaseBibtexKeyPattern(pattern); preferences = new BibtexKeyPatternPreferences("", "", false, true, true, pattern, ','); } @Test public void generateDefaultKey() { - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Doe2016"), entry.getCiteKeyOptional()); } @Test public void generateDefaultKeyAlreadyExistsDuplicatesStartAtA() { - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); BibEntry entry2 = new BibEntry(); entry2.setField("author", "John Doe"); entry2.setField("year", "2016"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry2, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry2, preferences); assertEquals(Optional.of("Doe2016a"), entry2.getCiteKeyOptional()); } @Test public void generateDefaultKeyAlwaysLetter() { preferences = new BibtexKeyPatternPreferences("", "", true, true, true, pattern, ','); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Doe2016a"), entry.getCiteKeyOptional()); } @@ -62,18 +61,18 @@ public void generateDefaultKeyAlwaysLetter() { public void generateDefaultKeyAlwaysLetterAlreadyExistsDuplicatesStartAtB() { preferences = new BibtexKeyPatternPreferences("", "", true, true, true, pattern, ','); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); BibEntry entry2 = new BibEntry(); entry2.setField("author", "John Doe"); entry2.setField("year", "2016"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry2, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry2, preferences); assertEquals(Optional.of("Doe2016b"), entry2.getCiteKeyOptional()); } @Test public void generateDefaultKeyStartDuplicatesAtB() { preferences = new BibtexKeyPatternPreferences("", "", false, false, true, pattern, ','); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Doe2016"), entry.getCiteKeyOptional()); } @@ -81,17 +80,17 @@ public void generateDefaultKeyStartDuplicatesAtB() { public void generateDefaultKeyAlreadyExistsDuplicatesStartAtB() { preferences = new BibtexKeyPatternPreferences("", "", false, false, true, pattern, ','); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); BibEntry entry2 = new BibEntry(); entry2.setField("author", "John Doe"); entry2.setField("year", "2016"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry2, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry2, preferences); assertEquals(Optional.of("Doe2016b"), entry2.getCiteKeyOptional()); } @Test public void generateDefaultKeyAlreadyExistsManyDuplicates() { - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); BibEntry entry2 = new BibEntry(); entry2.setField("author", "John Doe"); entry2.setField("year", "2016"); @@ -102,73 +101,73 @@ public void generateDefaultKeyAlreadyExistsManyDuplicates() { entry3.setField("year", "2016"); entry3.setCiteKey(entry.getCiteKeyOptional().get()); database.insertEntry(entry3); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry3, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry3, preferences); assertEquals(Optional.of("Doe2016a"), entry3.getCiteKeyOptional()); } @Test public void generateDefaultKeyFirstTwoAlreadyExists() { - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); BibEntry entry2 = new BibEntry(); entry2.setField("author", "John Doe"); entry2.setField("year", "2016"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry2, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry2, preferences); database.insertEntry(entry2); BibEntry entry3 = new BibEntry(); entry3.setField("author", "John Doe"); entry3.setField("year", "2016"); entry3.setCiteKey(entry.getCiteKeyOptional().get()); database.insertEntry(entry3); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry3, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry3, preferences); assertEquals(Optional.of("Doe2016b"), entry3.getCiteKeyOptional()); } @Test public void generateDefaultKeyLowerModified() { bibtexKeyPattern.setDefaultValue("[auth:lower][year]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("doe2016"), entry.getCiteKeyOptional()); } @Test public void generateDefaultKeyUpperModified() { bibtexKeyPattern.setDefaultValue("[auth:upper][year]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("DOE2016"), entry.getCiteKeyOptional()); } @Test public void generateDefaultKeyFixedValue() { bibtexKeyPattern.setDefaultValue("[auth]Test[year]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("DoeTest2016"), entry.getCiteKeyOptional()); } @Test public void generateKeyShortYear() { bibtexKeyPattern.setDefaultValue("[shortyear]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("16"), entry.getCiteKeyOptional()); } @Test public void generateKeyAuthN() { bibtexKeyPattern.setDefaultValue("[auth2]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Do"), entry.getCiteKeyOptional()); } @Test public void generateKeyAuthNShortName() { bibtexKeyPattern.setDefaultValue("[auth10]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Doe"), entry.getCiteKeyOptional()); } @Test public void generateKeyEmptyField() { entry = new BibEntry(); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.empty(), entry.getCiteKeyOptional()); } @@ -176,7 +175,7 @@ public void generateKeyEmptyField() { public void generateKeyEmptyFieldDefaultText() { bibtexKeyPattern.setDefaultValue("[author:(No Author Provided)]"); entry.clearField("author"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("NoAuthorProvided"), entry.getCiteKeyOptional()); } @@ -184,42 +183,42 @@ public void generateKeyEmptyFieldDefaultText() { public void generateKeyEmptyFieldColonInDefaultText() { bibtexKeyPattern.setDefaultValue("[author:(Problem:No Author Provided)]"); entry.clearField("author"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Problem:NoAuthorProvided"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitle() { bibtexKeyPattern.setDefaultValue("[title]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("AnawesomepaperonJabRef"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleAbbr() { bibtexKeyPattern.setDefaultValue("[title:abbr]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("AapoJ"), entry.getCiteKeyOptional()); } @Test public void generateKeyShorttitle() { bibtexKeyPattern.setDefaultValue("[shorttitle]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("awesomepaperJabRef"), entry.getCiteKeyOptional()); } @Test public void generateKeyVeryshorttitle() { bibtexKeyPattern.setDefaultValue("[veryshorttitle]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("awesome"), entry.getCiteKeyOptional()); } @Test public void generateKeyShorttitleINI() { bibtexKeyPattern.setDefaultValue("[shorttitleINI]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("apJ"), entry.getCiteKeyOptional()); } @@ -227,7 +226,7 @@ public void generateKeyShorttitleINI() { public void generateKeyAuthNM() { bibtexKeyPattern.setDefaultValue("[auth4_3]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Wond"), entry.getCiteKeyOptional()); } @@ -235,7 +234,7 @@ public void generateKeyAuthNM() { public void generateKeyAuthNMLargeN() { bibtexKeyPattern.setDefaultValue("[auth20_3]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Wonder"), entry.getCiteKeyOptional()); } @@ -243,7 +242,7 @@ public void generateKeyAuthNMLargeN() { public void generateKeyAuthNMLargeM() { bibtexKeyPattern.setDefaultValue("[auth2_4]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.empty(), entry.getCiteKeyOptional()); } @@ -251,7 +250,7 @@ public void generateKeyAuthNMLargeM() { public void generateKeyAuthNMLargeMReallyReturnsEmptyString() { bibtexKeyPattern.setDefaultValue("[auth2_4][year]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("2016"), entry.getCiteKeyOptional()); } @@ -260,7 +259,7 @@ public void generateKeyRegExReplace() { preferences = new BibtexKeyPatternPreferences("2", "3", false, true, true, pattern, ','); bibtexKeyPattern.setDefaultValue("[auth][year]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Doe3016"), entry.getCiteKeyOptional()); } @@ -268,7 +267,7 @@ public void generateKeyRegExReplace() { public void generateKeyAuthIni() { bibtexKeyPattern.setDefaultValue("[authIni2]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("DS"), entry.getCiteKeyOptional()); } @@ -276,49 +275,49 @@ public void generateKeyAuthIni() { public void generateKeyAuthIniMany() { bibtexKeyPattern.setDefaultValue("[authIni10]"); entry.setField("author", "John Doe and Donald Smith and Will Wonder"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("DoeSmiWon"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleTitleCase() { bibtexKeyPattern.setDefaultValue("[title:title_case]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("AnAwesomePaperonJabref"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleCapitalize() { bibtexKeyPattern.setDefaultValue("[title:capitalize]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("AnAwesomePaperOnJabref"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleSentenceCase() { bibtexKeyPattern.setDefaultValue("[title:sentence_case]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Anawesomepaperonjabref"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleTitleCaseAbbr() { bibtexKeyPattern.setDefaultValue("[title:title_case:abbr]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("AAPoJ"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleCapitalizeAbbr() { bibtexKeyPattern.setDefaultValue("[title:capitalize:abbr]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("AAPOJ"), entry.getCiteKeyOptional()); } @Test public void generateKeyTitleSentenceCaseAbbr() { bibtexKeyPattern.setDefaultValue("[title:sentence_case:abbr]"); - BibtexKeyPatternUtil.makeLabel(bibtexKeyPattern, database, entry, preferences); + BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences); assertEquals(Optional.of("Aapoj"), entry.getCiteKeyOptional()); } } diff --git a/src/test/java/net/sf/jabref/logic/integrity/IntegrityCheckTest.java b/src/test/java/net/sf/jabref/logic/integrity/IntegrityCheckTest.java index adadb948178..3fd4c3e82f5 100644 --- a/src/test/java/net/sf/jabref/logic/integrity/IntegrityCheckTest.java +++ b/src/test/java/net/sf/jabref/logic/integrity/IntegrityCheckTest.java @@ -7,7 +7,9 @@ import java.util.List; import java.util.Optional; +import net.sf.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import net.sf.jabref.model.Defaults; +import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.database.BibDatabaseContext; import net.sf.jabref.model.database.BibDatabaseMode; @@ -119,7 +121,15 @@ public void testJournaltitleChecks() { @Test public void testBibtexkeyChecks() { - assertCorrect(createContext("bibtexkey", "Knuth2014")); + final BibDatabaseContext correctContext = createContext("bibtexkey", "Knuth2014"); + correctContext.getDatabase().getEntries().get(0).setField("author","Knuth"); + correctContext.getDatabase().getEntries().get(0).setField("year","2014"); + assertCorrect(correctContext); + + final BibDatabaseContext wrongContext = createContext("bibtexkey", "Knuth2014a"); + wrongContext.getDatabase().getEntries().get(0).setField("author","Knuth"); + wrongContext.getDatabase().getEntries().get(0).setField("year","2014"); + assertWrong(wrongContext); } @Test @@ -317,17 +327,31 @@ private BibDatabaseContext createContext(String field, String value) { private void assertWrong(BibDatabaseContext context) { List messages = new IntegrityCheck(context, - JabRefPreferences.getInstance().getFileDirectoryPreferences()) + JabRefPreferences.getInstance().getFileDirectoryPreferences(), + createBibtexKeyPatternPreferences()) .checkBibtexDatabase(); assertFalse(messages.toString(), messages.isEmpty()); } private void assertCorrect(BibDatabaseContext context) { List messages = new IntegrityCheck(context, - JabRefPreferences.getInstance().getFileDirectoryPreferences()).checkBibtexDatabase(); + JabRefPreferences.getInstance().getFileDirectoryPreferences(), + createBibtexKeyPatternPreferences()).checkBibtexDatabase(); assertEquals(Collections.emptyList(), messages); } + private BibtexKeyPatternPreferences createBibtexKeyPatternPreferences() { + final GlobalBibtexKeyPattern keyPattern = GlobalBibtexKeyPattern.fromPattern("[auth][year]"); + return new BibtexKeyPatternPreferences( + "", + "", + false, + false, + false, + keyPattern, + ','); + } + private BibDatabaseContext withMode(BibDatabaseContext context, BibDatabaseMode mode) { context.setMode(mode); return context;