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 6 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,24 @@
package org.jabref.logic.integrity;

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

import org.junit.jupiter.api.Test;

public class ASCIICharacterCheckerTest {

@Test
void fieldAcceptsASCIICharacters() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void fieldAcceptsASCIICharacters() {
void fieldAcceptsAsciiCharacters() {

Even not conform with current JabRef classes, please use defined camel case - see https://google.github.io/styleguide/javaguide.html#s5.3-camel-case for details.

IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.TITLE, "Only ascii characters!'@12"));
}

@Test
void fieldDoesNotAcceptUmlauts() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.MONTH, "Umlauts are nöt ällowed"));
}

@Test
void fieldDoesNotAcceptUnicode() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.AUTHOR, "Some unicode ⊕"));
}

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

import java.util.Arrays;
import java.util.Optional;

import org.jabref.logic.journals.Abbreviation;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,4 +35,25 @@ void checkValueDoesNotComplainAboutJournalNameThatHasSameAbbreviation() {
abbreviationRepository.addEntry(new Abbreviation("Journal", "Journal"));
assertEquals(Optional.empty(), checker.checkValue("Journal"));
}

@Test
void journalNameAcceptsFullForm() {
for (Field field : Arrays.asList(StandardField.BOOKTITLE, StandardField.JOURNAL)) {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(field, "IEEE Software"));
Copy link
Member

Choose a reason for hiding this comment

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

Please use checker.checkValue instead of IntegrityCheckTest.... Reason: Keep the tests focused in the test subject.

See the name "AbbreviationCheckerTest"? It tests the class "AbbreviationChecker". This class is instantiated in line 25.

Also applies for the other checks you added. Could you please rework?

Copy link
Contributor Author

@dimitra-karadima dimitra-karadima Apr 23, 2020

Choose a reason for hiding this comment

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

@koppor I changed every class that has a checkValue method. But some of them like ASCIICharacterChecker.java has a different method(public List check(BibEntry entry)). Do you want me to change those tests too? If yes can you suggest me something? Because it is like IntegrityCheck.java and you haven't made any comments on my test class.

Copy link
Member

Choose a reason for hiding this comment

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

  • No errors: assertEquals(Collections.emptyList(), assciCharacterChecker.check(entry));
  • Errors: assertEquals(List.of(new IntegrityCheckMessage("...text", StandardFields.DOI)), assciCharacterChecker.check(entry));). This way, the real error message is checked. And if someone changes something in the checker, it is also noted in the tests. See our long disucssions at Remove dash from illegal characters. #6300 (which are also caused by failing tests - and we were forced to think about the intended behavior more deeply).

}
}

@Test
void journalNameAcceptsEmptyInput() {
for (Field field : Arrays.asList(StandardField.BOOKTITLE, StandardField.JOURNAL)) {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(field, ""));
}
}

@Test
void journalNameDoesNotAcceptNonAbbreviatedForm() {
for (Field field : Arrays.asList(StandardField.BOOKTITLE, StandardField.JOURNAL)) {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(field, "IEEE SW"));
}
}
}
33 changes: 33 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,33 @@
package org.jabref.logic.integrity;

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

import org.junit.jupiter.api.Test;

public class BibStringCheckerTest {

@Test
void fieldAcceptsNoHashMarks() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.TITLE, "Not a single hash mark"));
}

@Test
void monthAcceptsEvenNumberOfHashMarks() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.MONTH, "#jan#"));
}

@Test
void authorAcceptsEvenNumberOfHashMarks() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.AUTHOR, "#einstein# and #newton#"));
}

@Test
void monthDoesNotAcceptOddNumberOfHashMarks() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.MONTH, "#jan"));
}

@Test
void authorDoesNotAcceptOddNumberOfHashMarks() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.AUTHOR, "#einstein# #amp; #newton#"));
}
}
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);
}

}
20 changes: 20 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,20 @@
package org.jabref.logic.integrity;

import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;

import org.junit.jupiter.api.Test;

public class BooktitleCheckerTest {

@Test
void booktitleAcceptsIfItDoesNotEndWithConferenceOn() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.BOOKTITLE, "2014 Fourth International Conference on Digital Information and Communication Technology and it's Applications (DICTAP)", StandardEntryType.Proceedings));
}

@Test
void booktitleDoesNotAcceptsIfItEndsWithConferenceOn() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.BOOKTITLE, "Digital Information and Communication Technology and it's Applications (DICTAP), 2014 Fourth International Conference on", StandardEntryType.Proceedings));
}

}
39 changes: 39 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,39 @@
package org.jabref.logic.integrity;

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

import org.junit.jupiter.api.Test;

public class BracketCheckerTest {

@Test
void fieldAcceptsNoBrackets() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.TITLE, "x"));
}

@Test
void fieldAcceptsEvenNumberOfBrackets() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.TITLE, "{x}"));
}

@Test
void fieldAcceptsExpectedBracket() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.TITLE, "{x}x{}x{{}}"));
}

@Test
void fieldDoesNotAcceptOddNumberOfBrackets() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.TITLE, "{x}x{}}x{{}}"));
}

@Test
void fieldDoesNotAcceptUnexpectedClosingBracket() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.TITLE, "}"));
}

@Test
void fieldDoesNotAcceptUnexpectedOpeningBracket() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.TITLE, "{"));
}

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

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

import org.junit.jupiter.api.Test;

public class DOIValidityCheckerTest {

@Test
void doiAcceptsValidInput() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.DOI, "10.1023/A:1022883727209"));
}

@Test
void doiAcceptsValidInputWithNotOnlyNumbers() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.DOI, "10.17487/rfc1436"));
}

@Test
void doiAcceptsValidInputNoMatterTheLengthOfTheDOIName() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.DOI, "10.1002/(SICI)1097-4571(199205)43:4<284::AID-ASI3>3.0.CO;2-0"));
}

@Test
void doiDoesNotAcceptInvalidInput() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.DOI, "asdf"));
}

}
42 changes: 42 additions & 0 deletions src/test/java/org/jabref/logic/integrity/EditionCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Optional;

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

import org.junit.jupiter.api.Test;

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

@Test
void bibTexAcceptsOrdinalNumberInWordsWithCapitalFirstLetter() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "Second"), BibDatabaseMode.BIBTEX));
}

@Test
void bibTexDoesNotAcceptOrdinalNumberInWordsWithNonCapitalFirstLetter() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "second"), BibDatabaseMode.BIBTEX));
}

@Test
void bibTexAcceptsIntegerInputInEdition() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "2"), BibDatabaseMode.BIBTEX));
}

@Test
void bibTexAcceptsOrdinalNumberInNumbers() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "2nd"), BibDatabaseMode.BIBTEX));
}

@Test
void bibLaTexAcceptsEditionWithCapitalFirstLetter() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "Edition 2000"), BibDatabaseMode.BIBLATEX));
}

@Test
void bibLaTexAcceptsIntegerInputInEdition() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "2"), BibDatabaseMode.BIBLATEX));
}

@Test
void bibLaTexAcceptsEditionAsLiteralString() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "Third, revised and expanded edition"), BibDatabaseMode.BIBLATEX));
}

@Test
void bibLaTexDoesNotAcceptOrdinalNumberInNumbers() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.withMode(IntegrityCheckTest.createContext(StandardField.EDITION, "2nd"), BibDatabaseMode.BIBLATEX));
}

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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.MetaData;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;

public class FileCheckerTest {

@Test
void testFileChecks() {
MetaData metaData = mock(MetaData.class);
Mockito.when(metaData.getDefaultFileDirectory()).thenReturn(Optional.of("."));
Mockito.when(metaData.getUserFileDirectory(any(String.class))).thenReturn(Optional.empty());
// FIXME: must be set as checkBibtexDatabase only activates title checker based on database mode
Mockito.when(metaData.getMode()).thenReturn(Optional.of(BibDatabaseMode.BIBTEX));

IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.FILE, ":build.gradle:gradle", metaData));
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.FILE, "description:build.gradle:gradle", metaData));
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.FILE, ":asflakjfwofja:PDF", metaData));
}

@Test
void fileCheckFindsFilesRelativeToBibFile(@TempDir Path testFolder) throws IOException {
Path bibFile = testFolder.resolve("lit.bib");
Files.createFile(bibFile);
Path pdfFile = testFolder.resolve("file.pdf");
Files.createFile(pdfFile);

BibDatabaseContext databaseContext = IntegrityCheckTest.createContext(StandardField.FILE, ":file.pdf:PDF");
databaseContext.setDatabasePath(bibFile);

IntegrityCheckTest.assertCorrect(databaseContext);
}

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

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

import org.junit.jupiter.api.Test;

public class HTMLCharacterCheckerTest {

@Test
void titleAcceptsNonHTMLEncodedCharacters() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.TITLE, "Not a single {HTML} character"));
}

@Test
void monthAcceptsNonHTMLEncodedCharacters() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.MONTH, "#jan#"));
}

@Test
void authorAcceptsNonHTMLEncodedCharacters() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.AUTHOR, "A. Einstein and I. Newton"));
}

@Test
void urlAcceptsNonHTMLEncodedCharacters() {
IntegrityCheckTest.assertCorrect(IntegrityCheckTest.createContext(StandardField.URL, "http://www.thinkmind.org/index.php?view=article&amp;articleid=cloud_computing_2013_1_20_20130"));
}

@Test
void authorDoesNotAcceptHTMLEncodedCharacters() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.AUTHOR, "Lenhard, J&#227;rg"));
}

@Test
void journalDoesNotAcceptHTMLEncodedCharacters() {
IntegrityCheckTest.assertWrong(IntegrityCheckTest.createContext(StandardField.JOURNAL, "&Auml;rling Str&ouml;m for &#8211; &#x2031;"));
}


}
Loading