Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple Unit Tests for #6207 #6240

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ASCIICharacterCheckerTest {

private ASCIICharacterChecker checker;
private BibEntry entry;

@BeforeEach
void setUp() {
checker = new ASCIICharacterChecker();
entry = new BibEntry();
}

@Test
void fieldAcceptsAsciiCharacters() {
entry.setField(StandardField.TITLE, "Only ascii characters!'@12");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void fieldDoesNotAcceptUmlauts() {
entry.setField(StandardField.MONTH, "Umlauts are nöt ällowed");
assertEquals(List.of(new IntegrityMessage("Non-ASCII encoded character found", entry, StandardField.MONTH)), checker.check(entry));
}

@Test
void fieldDoesNotAcceptUnicode() {
entry.setField(StandardField.AUTHOR, "Some unicode ⊕");
assertEquals(List.of(new IntegrityMessage("Non-ASCII encoded character found", entry, StandardField.AUTHOR)), checker.check(entry));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.jabref.logic.journals.Abbreviation;
import org.jabref.logic.journals.JournalAbbreviationRepository;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -32,4 +31,15 @@ void checkValueDoesNotComplainAboutJournalNameThatHasSameAbbreviation() {
abbreviationRepository.addEntry(new Abbreviation("Journal", "Journal"));
assertEquals(Optional.empty(), checker.checkValue("Journal"));
}

@Test
void checkValueDoesNotComplainAboutJournalNameThatHasΝοAbbreviation() {
assertEquals(Optional.empty(), checker.checkValue("IEEE Software"));
}

@Test
void checkValueDoesNotComplainAboutJournalNameThatHasΝοInput() {
assertEquals(Optional.empty(), checker.checkValue(""));
}

}
54 changes: 54 additions & 0 deletions src/test/java/org/jabref/logic/integrity/BibStringCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.jabref.logic.integrity;

import java.util.Collections;
import java.util.List;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BibStringCheckerTest {

private BibStringChecker checker;
private BibEntry entry;

@BeforeEach
void setUp() {
checker = new BibStringChecker();
entry = new BibEntry();
}

@Test
void fieldAcceptsNoHashMarks() {
entry.setField(StandardField.TITLE, "Not a single hash mark");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void monthAcceptsEvenNumberOfHashMarks() {
entry.setField(StandardField.MONTH, "#jan#");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void authorAcceptsEvenNumberOfHashMarks() {
entry.setField(StandardField.AUTHOR, "#einstein# and #newton#");
assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void monthDoesNotAcceptOddNumberOfHashMarks() {
entry.setField(StandardField.MONTH, "#jan");
assertEquals(List.of(new IntegrityMessage("odd number of unescaped '#'", entry, StandardField.MONTH)), checker.check(entry));
}

@Test
void authorDoesNotAcceptOddNumberOfHashMarks() {
entry.setField(StandardField.AUTHOR, "#einstein# #amp; #newton#");
assertEquals(List.of(new IntegrityMessage("odd number of unescaped '#'", entry, StandardField.AUTHOR)), checker.check(entry));
}
}
27 changes: 27 additions & 0 deletions src/test/java/org/jabref/logic/integrity/BibtexKeyCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jabref.logic.integrity;

import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.Test;

public class BibtexKeyCheckerTest {

@Test
void bibTexAcceptsKeyFromAuthorAndYear() {
final BibDatabaseContext correctContext = IntegrityCheckTest.createContext(InternalField.KEY_FIELD, "Knuth2014");
correctContext.getDatabase().getEntries().get(0).setField(StandardField.AUTHOR, "Knuth");
correctContext.getDatabase().getEntries().get(0).setField(StandardField.YEAR, "2014");
IntegrityCheckTest.assertCorrect(correctContext);
}

@Test
void bibtexDooesNotAcceptRandomKey() {
final BibDatabaseContext wrongContext = IntegrityCheckTest.createContext(InternalField.KEY_FIELD, "Knuth2014a");
wrongContext.getDatabase().getEntries().get(0).setField(StandardField.AUTHOR, "Knuth");
wrongContext.getDatabase().getEntries().get(0).setField(StandardField.YEAR, "2014");
IntegrityCheckTest.assertWrong(wrongContext);
}

}
30 changes: 30 additions & 0 deletions src/test/java/org/jabref/logic/integrity/BooktitleCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jabref.logic.integrity;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class BooktitleCheckerTest {

private BooktitleChecker checker;

@BeforeEach
public void setUp() {
checker = new BooktitleChecker();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the @BeforeEach here. Just initialize the checker in line 13. --> One SLOC insted of 6 for the same functionality. -- JUnit initializes the class on each run.

Please check the other (modified) classes, too.

}

@Test
void booktitleAcceptsIfItDoesNotEndWithConferenceOn() {
assertEquals(Optional.empty(), checker.checkValue("2014 Fourth International Conference on Digital Information and Communication Technology and it's Applications (DICTAP)"));
}

@Test
void booktitleDoesNotAcceptsIfItEndsWithConferenceOn() {
assertNotEquals(Optional.empty(), checker.checkValue("Digital Information and Communication Technology and it's Applications (DICTAP), 2014 Fourth International Conference on"));
}

}
50 changes: 50 additions & 0 deletions src/test/java/org/jabref/logic/integrity/BracketCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jabref.logic.integrity;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class BracketCheckerTest {

private BracketChecker checker;

@BeforeEach
void setUp() {
checker = new BracketChecker();
}

@Test
void fieldAcceptsNoBrackets() {
assertEquals(Optional.empty(), checker.checkValue("x"));
}

@Test
void fieldAcceptsEvenNumberOfBrackets() {
assertEquals(Optional.empty(), checker.checkValue("{x}"));
}

@Test
void fieldAcceptsExpectedBracket() {
assertEquals(Optional.empty(), checker.checkValue("{x}x{}x{{}}"));
}

@Test
void fieldDoesNotAcceptOddNumberOfBrackets() {
assertNotEquals(Optional.empty(), checker.checkValue("{x}x{}}x{{}}"));
}

@Test
void fieldDoesNotAcceptUnexpectedClosingBracket() {
assertNotEquals(Optional.empty(), checker.checkValue("}"));
}

@Test
void fieldDoesNotAcceptUnexpectedOpeningBracket() {
assertNotEquals(Optional.empty(), checker.checkValue("{"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jabref.logic.integrity;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class DOIValidityCheckerTest {

private DOIValidityChecker checker;

@BeforeEach
void setUp() {
checker = new DOIValidityChecker();
}

@Test
void doiAcceptsValidInput() {
assertEquals(Optional.empty(), checker.checkValue("10.1023/A:1022883727209"));
}

@Test
void doiAcceptsValidInputWithNotOnlyNumbers() {
assertEquals(Optional.empty(), checker.checkValue("10.17487/rfc1436"));
}

@Test
void doiAcceptsValidInputNoMatterTheLengthOfTheDOIName() {
assertEquals(Optional.empty(), checker.checkValue("10.1002/(SICI)1097-4571(199205)43:4<284::AID-ASI3>3.0.CO;2-0"));
}

@Test
void doiDoesNotAcceptInvalidInput() {
assertNotEquals(Optional.empty(), checker.checkValue("asdf"));
}

}
62 changes: 61 additions & 1 deletion src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@
import java.util.Optional;

import org.jabref.model.database.BibDatabaseContext;

import org.jabref.model.database.BibDatabaseMode;
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;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EditionCheckerTest {

private EditionChecker checker;
private EditionChecker checkerb;
private BibEntry entry;
private BibDatabaseContext bibtex;
private BibDatabaseContext biblatex;

@BeforeEach
void setUp() {
koppor marked this conversation as resolved.
Show resolved Hide resolved
bibtex = new BibDatabaseContext();
bibtex.setMode(BibDatabaseMode.BIBTEX);
biblatex = new BibDatabaseContext();
biblatex.setMode(BibDatabaseMode.BIBLATEX);
checker = new EditionChecker(bibtex, true);
checkerb = new EditionChecker(biblatex, true);
entry = new BibEntry();
}

public BibDatabaseContext bibDatabaseContextEdition = new BibDatabaseContext();

@Test
Expand Down Expand Up @@ -42,4 +62,44 @@ void editionCheckerDoesNotComplainIfAllowIntegerEditionIsEnabled() {
assertEquals(Optional.empty(), editionChecker.checkValue("2"));
}

@Test
void bibTexAcceptsOrdinalNumberInWordsWithCapitalFirstLetter() {
assertEquals(Optional.empty(), checker.checkValue("Second"));
}

@Test
void bibTexDoesNotAcceptOrdinalNumberInWordsWithNonCapitalFirstLetter() {
assertNotEquals(Optional.empty(), checker.checkValue("second"));
}

@Test
void bibTexAcceptsIntegerInputInEdition() {
assertEquals(Optional.empty(), checker.checkValue("2"));
}

@Test
void bibTexAcceptsOrdinalNumberInNumbers() {
assertEquals(Optional.empty(), checker.checkValue("2nd"));
}

@Test
void bibLaTexAcceptsEditionWithCapitalFirstLetter() {
assertEquals(Optional.empty(), checkerb.checkValue("Edition 2000"));
}

@Test
void bibLaTexAcceptsIntegerInputInEdition() {
assertEquals(Optional.empty(), checkerb.checkValue("2"));
}

@Test
void bibLaTexAcceptsEditionAsLiteralString() {
assertEquals(Optional.empty(), checkerb.checkValue("Third, revised and expanded edition"));
}

@Test
void bibLaTexDoesNotAcceptOrdinalNumberInNumbers() {
assertNotEquals(Optional.empty(), checkerb.checkValue("2nd"));
}

}
Loading