Skip to content

Commit

Permalink
Add simple unit tests (#7543)
Browse files Browse the repository at this point in the history
* Branch Coverage Tests

FieldChange.java
18% -> 94%
Abbreviation.java
63% -> 88%
SuggestionProviders.java
0% -> 100%

* Boundary Tests

FileHelper.java
-> Boundary testing of an empty file

CitationKeyGenerator.java
-> Boundary testing of testlagepage parser for 0-00 & 1-1

HTMLCharacterChecker.java
-> Null Value Boundary test

* Partition Tests

ParsedEntryLink.java
-> Partition testing of ParsingEntryLink

UpperCaseFormatter.java
-> Partition testing for special characters

CitationStyleCacheTest.java
-> Partition testing of cache storage

* Update SuggestionProvidersTest.java

SPTest typo fix

* CodeStyle Fixes

Checkstyle passed

* Update AbbreviationTest.java

Adjustments for @ellieMayVelasquez feedback
#7543 (review)

* Applying feedback to PR

addressed all comments & suggestions to test files

* checkstyle fix
  • Loading branch information
nasdas-dev authored May 11, 2021
1 parent 97ad19b commit 807f85e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jabref.gui.autocompleter;

import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

class SuggestionProvidersTest {

@Test
void getForFieldTest() {
BibDatabase database = new BibDatabase();
JournalAbbreviationRepository abbreviationRepository = mock(JournalAbbreviationRepository.class);
BibEntry entry = new BibEntry();
Field personEntryField = StandardField.AUTHOR;
Field singleEntryField = StandardField.XREF;
Field multipleEntryField = StandardField.XDATA;
Field journalEntryField = StandardField.JOURNAL;
Field publisherEntryField = StandardField.PUBLISHER;
Field specialEntryField = SpecialField.PRINTED;
AutoCompletePreferences autoCompletePreferences = new AutoCompletePreferences(true, AutoCompleteFirstNameMode.BOTH, AutoCompletePreferences.NameFormat.BOTH, FieldFactory.parseFieldList(personEntryField.getName() + ";" + singleEntryField.getName() + ";" + multipleEntryField.getName() + ";" + journalEntryField.getName() + ";" + publisherEntryField.getName() + ";" + specialEntryField.getName()), null);
SuggestionProviders sp = new SuggestionProviders(database, abbreviationRepository, autoCompletePreferences);
SuggestionProviders empty = new SuggestionProviders();

entry.setField(personEntryField, "Goethe");
entry.setField(singleEntryField, "Single");
entry.setField(multipleEntryField, "Multiple");
entry.setField(journalEntryField, "Journal");
entry.setField(publisherEntryField, "Publisher");
entry.setField(specialEntryField, "2000");

assertEquals(org.jabref.gui.autocompleter.EmptySuggestionProvider.class, empty.getForField(personEntryField).getClass());
assertEquals(org.jabref.gui.autocompleter.PersonNameSuggestionProvider.class, sp.getForField(personEntryField).getClass());
assertEquals(org.jabref.gui.autocompleter.BibEntrySuggestionProvider.class, sp.getForField(singleEntryField).getClass());
assertEquals(org.jabref.gui.autocompleter.BibEntrySuggestionProvider.class, sp.getForField(multipleEntryField).getClass());
assertEquals(org.jabref.gui.autocompleter.JournalsSuggestionProvider.class, sp.getForField(journalEntryField).getClass());
assertEquals(org.jabref.gui.autocompleter.JournalsSuggestionProvider.class, sp.getForField(publisherEntryField).getClass());
assertEquals(org.jabref.gui.autocompleter.WordSuggestionProvider.class, sp.getForField(specialEntryField).getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,15 @@ void testPagePrefixNull() {

@Test
void testLastPage() {

assertEquals("27", CitationKeyGenerator.lastPage("7--27"));
assertEquals("27", CitationKeyGenerator.lastPage("--27"));
assertEquals("", CitationKeyGenerator.lastPage(""));
assertEquals("111", CitationKeyGenerator.lastPage("42--111"));
assertEquals("97", CitationKeyGenerator.lastPage("7,41,73--97"));
assertEquals("97", CitationKeyGenerator.lastPage("7,41,97--73"));
assertEquals("43", CitationKeyGenerator.lastPage("43+"));
assertEquals("0", CitationKeyGenerator.lastPage("00--0"));
assertEquals("1", CitationKeyGenerator.lastPage("1--1"));
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.jabref.logic.formatter.casechanger;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

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

Expand All @@ -10,24 +13,23 @@
*/
public class UpperCaseFormatterTest {

private UpperCaseFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new UpperCaseFormatter();
}
private UpperCaseFormatter formatter = new UpperCaseFormatter();

@Test
public void test() {
assertEquals("LOWER", formatter.format("LOWER"));
assertEquals("UPPER", formatter.format("upper"));
assertEquals("UPPER", formatter.format("UPPER"));
assertEquals("UPPER {lower}", formatter.format("upper {lower}"));
assertEquals("UPPER {l}OWER", formatter.format("upper {l}ower"));
@ParameterizedTest
@MethodSource("upperCaseTests")
public void upperCasetest(String expectedFormat, String inputFormat) {
assertEquals(expectedFormat, formatter.format(inputFormat));
}

@Test
public void formatExample() {
assertEquals("KDE {Amarok}", formatter.format(formatter.getExampleInput()));
private static Stream<Arguments> upperCaseTests() {
return Stream.of(
Arguments.of("LOWER", "LOWER"),
Arguments.of("UPPER", "upper"),
Arguments.of("UPPER", "UPPER"),
Arguments.of("UPPER {lower}", "upper {lower}"),
Arguments.of("UPPER {l}OWER", "upper {l}ower"),
Arguments.of("1", "1"),
Arguments.of("!", "!")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
import org.junit.jupiter.api.Test;

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

public class HTMLCharacterCheckerTest {

private final HTMLCharacterChecker checker = new HTMLCharacterChecker();
private final BibEntry entry = new BibEntry();

@Test
void fieldNullValueCheck() {
Exception exception = assertThrows(
NullPointerException.class,
() -> entry.setField(StandardField.AUTHOR, null),
"field value must not be null"
);
}

@Test
void titleAcceptsNonHTMLEncodedCharacters() {
entry.setField(StandardField.TITLE, "Not a single {HTML} character");
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/jabref/model/entry/EntryLinkListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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.assertTrue;

public class EntryLinkListTest {
Expand All @@ -21,6 +22,7 @@ public class EntryLinkListTest {
private ParsedEntryLink link;
private BibEntry source;
private BibEntry target;
private BibEntry entry;

@BeforeEach
public void before() {
Expand Down Expand Up @@ -59,6 +61,13 @@ public void givenFieldValueAndDatabaseWhenParsingThenExpectLink() {
assertEquals(expected, link);
}

@Test
public void givenBibEntryWhenParsingThenExpectLink() {
entry = create("entry");
ParsedEntryLink expected = new ParsedEntryLink(entry);
assertFalse(expected.getLinkedEntry().isEmpty());
}

@Test
public void givenNullFieldValueAndDatabaseWhenParsingThenExpectLinksIsEmpty() {
links = EntryLinkList.parse(null, database);
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/jabref/model/util/FileHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.model.util;

import java.nio.file.Path;
import java.util.Optional;

import org.junit.jupiter.api.Test;
Expand All @@ -18,4 +19,10 @@ public void fileExtensionFromUrl() {
final String filePath = "https://link.springer.com/content/pdf/10.1007%2Fs40955-018-0121-9.pdf";
assertEquals(Optional.of("pdf"), FileHelper.getFileExtension(filePath));
}

@Test
public void testFileNameEmpty() {
Path path = Path.of("/");
assertEquals(Optional.of(path), FileHelper.find("", path));
}
}

0 comments on commit 807f85e

Please sign in to comment.