diff --git a/src/test/java/org/jabref/logic/formatter/bibtexfields/RemoveBracesFormatterTest.java b/src/test/java/org/jabref/logic/formatter/bibtexfields/RemoveBracesFormatterTest.java index b2f417cd587..45a418094bc 100644 --- a/src/test/java/org/jabref/logic/formatter/bibtexfields/RemoveBracesFormatterTest.java +++ b/src/test/java/org/jabref/logic/formatter/bibtexfields/RemoveBracesFormatterTest.java @@ -37,6 +37,11 @@ public void formatKeepsShortString() { assertEquals("t", formatter.format("t")); } + @Test + public void formatRemovesBracesOnly() { + assertEquals("", formatter.format("{}")); + } + @Test public void formatKeepsEmptyString() { assertEquals("", formatter.format("")); diff --git a/src/test/java/org/jabref/logic/integrity/BracesCorrectorTest.java b/src/test/java/org/jabref/logic/integrity/BracesCorrectorTest.java index b14d6e5c49f..245829abea3 100644 --- a/src/test/java/org/jabref/logic/integrity/BracesCorrectorTest.java +++ b/src/test/java/org/jabref/logic/integrity/BracesCorrectorTest.java @@ -22,6 +22,11 @@ public void inputWithoutBraces() { assertEquals("banana", BracesCorrector.apply("banana")); } + @Test + public void inputAlreadyCorrect() { + assertEquals("{banana}", BracesCorrector.apply("{banana}")); + } + @Test public void inputMissingClosing() { assertEquals("{banana}", BracesCorrector.apply("{banana")); diff --git a/src/test/java/org/jabref/logic/integrity/CitationKeyDeviationCheckerTest.java b/src/test/java/org/jabref/logic/integrity/CitationKeyDeviationCheckerTest.java new file mode 100644 index 00000000000..731dbfac2ee --- /dev/null +++ b/src/test/java/org/jabref/logic/integrity/CitationKeyDeviationCheckerTest.java @@ -0,0 +1,51 @@ +package org.jabref.logic.integrity; + +import java.util.Collections; +import java.util.List; + +import org.jabref.logic.citationkeypattern.AbstractCitationKeyPattern; +import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; +import org.jabref.logic.citationkeypattern.GlobalCitationKeyPattern; +import org.jabref.logic.l10n.Localization; +import org.jabref.model.database.BibDatabase; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.field.InternalField; +import org.jabref.model.entry.field.StandardField; +import org.jabref.model.metadata.MetaData; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CitationKeyDeviationCheckerTest { + + private final BibDatabaseContext bibDatabaseContext = mock(BibDatabaseContext.class); + private final BibDatabase bibDatabase = mock(BibDatabase.class); + private final MetaData metaData = mock(MetaData.class); + private final AbstractCitationKeyPattern abstractCitationKeyPattern = mock(AbstractCitationKeyPattern.class); + private final GlobalCitationKeyPattern globalCitationKeyPattern = mock(GlobalCitationKeyPattern.class); + private final CitationKeyPatternPreferences citationKeyPatternPreferences = mock(CitationKeyPatternPreferences.class); + private final CitationKeyDeviationChecker checker = new CitationKeyDeviationChecker(bibDatabaseContext, citationKeyPatternPreferences); + + @BeforeEach + void setUp() { + when(bibDatabaseContext.getMetaData()).thenReturn(metaData); + when(citationKeyPatternPreferences.getKeyPattern()).thenReturn(globalCitationKeyPattern); + when(metaData.getCiteKeyPattern(citationKeyPatternPreferences.getKeyPattern())).thenReturn(abstractCitationKeyPattern); + when(bibDatabaseContext.getDatabase()).thenReturn(bibDatabase); + } + + @Test + void citationKeyDeviatesFromGeneratedKey() { + BibEntry entry = new BibEntry().withField(InternalField.KEY_FIELD, "Knuth2014") + .withField(StandardField.AUTHOR, "Knuth") + .withField(StandardField.YEAR, "2014"); + List expected = Collections.singletonList(new IntegrityMessage( + Localization.lang("Citation key deviates from generated key"), entry, InternalField.KEY_FIELD)); + assertEquals(expected, checker.check(entry)); + } +} diff --git a/src/test/java/org/jabref/logic/integrity/CitationKeyDuplicationCheckerTest.java b/src/test/java/org/jabref/logic/integrity/CitationKeyDuplicationCheckerTest.java new file mode 100644 index 00000000000..b506e87194b --- /dev/null +++ b/src/test/java/org/jabref/logic/integrity/CitationKeyDuplicationCheckerTest.java @@ -0,0 +1,45 @@ +package org.jabref.logic.integrity; + +import java.util.Collections; +import java.util.List; + +import org.jabref.logic.l10n.Localization; +import org.jabref.model.database.BibDatabase; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.field.InternalField; +import org.jabref.model.entry.field.StandardField; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CitationKeyDuplicationCheckerTest { + + @Test + void emptyCitationKey() { + BibEntry entry = new BibEntry().withField(InternalField.KEY_FIELD, "") + .withField(StandardField.AUTHOR, "Knuth") + .withField(StandardField.YEAR, "2014"); + BibDatabase bibDatabase = new BibDatabase(List.of(entry)); + CitationKeyDuplicationChecker checker = new CitationKeyDuplicationChecker(bibDatabase); + + List expected = Collections.emptyList(); + assertEquals(expected, checker.check(entry)); + } + + @Test + void hasDuplicateCitationKey() { + BibEntry entry = new BibEntry().withField(InternalField.KEY_FIELD, "Knuth2014") + .withField(StandardField.AUTHOR, "Knuth") + .withField(StandardField.YEAR, "2014"); + BibEntry entry2 = new BibEntry().withField(InternalField.KEY_FIELD, "Knuth2014") + .withField(StandardField.AUTHOR, "Knuth") + .withField(StandardField.YEAR, "2014"); + BibDatabase bibDatabase = new BibDatabase(List.of(entry, entry2)); + CitationKeyDuplicationChecker checker = new CitationKeyDuplicationChecker(bibDatabase); + + List expected = Collections.singletonList( + new IntegrityMessage(Localization.lang("Duplicate citation key"), entry, StandardField.KEY)); + assertEquals(expected, checker.check(entry)); + } +}