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 #7543

Merged
merged 9 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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,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));
}
}