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

Refactoring and addition of unit tests #7597

Merged
merged 32 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3084c57
Branch Coverage Tests
nasdas-dev Mar 15, 2021
0004bc5
Boundary Tests
nasdas-dev Mar 15, 2021
4e4f8fe
Partition Tests
nasdas-dev Mar 15, 2021
8f68850
Update SuggestionProvidersTest.java
nasdas-dev Mar 15, 2021
f8ed4b1
upstream fetch
nasdas-dev Mar 15, 2021
4ea42ed
CodeStyle Fixes
nasdas-dev Mar 17, 2021
8e297b7
Update AbbreviationTest.java
nasdas-dev Mar 26, 2021
969a29d
Refactor LayoutEntryTest
nasdas-dev Mar 29, 2021
79153d3
Refactor OpenDatabaseTest
nasdas-dev Mar 29, 2021
bb6f44e
Refactor GroupTreeViewModel
nasdas-dev Mar 29, 2021
075a82d
Update GroupTreeViewModelTest.java
nasdas-dev Mar 29, 2021
9f9ce03
Refactor MergeReviewIntoCommentActionMigrationTest
nasdas-dev Mar 29, 2021
a0bf585
Update MergeReviewIntoCommentActionMigrationTest.java
nasdas-dev Mar 29, 2021
1d520a1
Refactor SpecialFieldsToSeparateFieldsTest
nasdas-dev Mar 29, 2021
250d859
remove aftereach methods
nasdas-dev Mar 29, 2021
a170e5d
Refactor JournalAbbreviationsViewModelMixedAbbreviationsTabTest
nasdas-dev Mar 29, 2021
e4e6a9c
Refactor UpdateTimestampListenerTest
nasdas-dev Mar 29, 2021
a7a5373
Refactor FileFieldWriterTest
nasdas-dev Mar 29, 2021
6585b8e
Refactor SentenceAnalyzerTest
nasdas-dev Mar 29, 2021
12537a9
checkstyle Fixes
nasdas-dev Mar 30, 2021
a3f5753
Add unit tests to increase code coverage
nasdas-dev Mar 30, 2021
f42a2d5
Merge branch 'a2-ds' of https://github.com/ningxie1991/jabref into ni…
koppor Jun 18, 2021
de0c98b
Fix indent
koppor Jun 18, 2021
29db2be
Make SuggestionProvidersTest parameterized
koppor Jun 20, 2021
ebeaa3c
Adress review comments
koppor Jun 20, 2021
30c9276
Rewrite FieldChangeTest
koppor Jun 20, 2021
3de65c5
Remove obsolete variable
koppor Jun 20, 2021
281ccca
Rewrite SentenceAnalyzerTest to ParamterizedTest
koppor Jun 20, 2021
e66b372
Merge remote-tracking branch 'origin/main' into ningxie1991-a2-ds
koppor Jun 20, 2021
2b5cd88
Remove assert messages
koppor Jun 20, 2021
c1888d3
Fix checkstyle
koppor Jun 20, 2021
7550426
Fix assertion
koppor Jun 21, 2021
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
Expand Up @@ -27,7 +27,6 @@ public SuggestionProviders() {
}

public SuggestionProvider<?> getForField(Field field) {

if (isEmpty || !autoCompletePreferences.getCompleteFields().contains(field)) {
return new EmptySuggestionProvider();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/model/FieldChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class FieldChange {
private final String newValue;

public FieldChange(BibEntry entry, Field field, String oldValue, String newValue) {
this.entry = entry;
this.field = field;
this.entry = Objects.requireNonNull(entry);
this.field = Objects.requireNonNull(field);
this.oldValue = oldValue;
this.newValue = newValue;
}
Expand Down
9 changes: 3 additions & 6 deletions src/test/java/org/jabref/gui/UpdateTimestampListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class UpdateTimestampListenerTest {
private PreferencesService preferencesMock;
private TimestampPreferences timestampPreferencesMock;

private final String baseDate = "2000-1-1";
private final String newDate = "2000-1-2";

@BeforeEach
void setUp() {
database = new BibDatabase();
Expand All @@ -39,9 +42,6 @@ void setUp() {

@Test
void updateTimestampEnabled() {
final String baseDate = "2000-1-1";
final String newDate = "2000-1-2";

final boolean includeTimestamp = true;

when(timestampPreferencesMock.now()).thenReturn(newDate);
Expand All @@ -60,9 +60,6 @@ void updateTimestampEnabled() {

@Test
void updateTimestampDisabled() {
final String baseDate = "2000-1-1";
final String newDate = "2000-1-2";

final boolean includeTimestamp = false;

when(timestampPreferencesMock.now()).thenReturn(newDate);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,70 @@
package org.jabref.gui.autocompleter;

import java.util.Set;
import java.util.stream.Stream;

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.BeforeEach;
import org.junit.jupiter.api.Test;
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;
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());
}
private SuggestionProviders suggestionProviders;

@BeforeEach
public void initializeSuggestionProviders() {
BibDatabase database = new BibDatabase();
JournalAbbreviationRepository abbreviationRepository = mock(JournalAbbreviationRepository.class);
Set<Field> completeFields = Set.of(StandardField.AUTHOR, StandardField.XREF, StandardField.XDATA, StandardField.JOURNAL, StandardField.PUBLISHER, SpecialField.PRINTED);
AutoCompletePreferences autoCompletePreferences = new AutoCompletePreferences(
true,
AutoCompleteFirstNameMode.BOTH,
AutoCompletePreferences.NameFormat.BOTH,
completeFields,
null);
this.suggestionProviders = new SuggestionProviders(database, abbreviationRepository, autoCompletePreferences);
}

private static Stream<Arguments> getTestPairs() {
return Stream.of(
// a person
Arguments.of(org.jabref.gui.autocompleter.PersonNameSuggestionProvider.class, StandardField.AUTHOR),

// a single entry field
Arguments.of(org.jabref.gui.autocompleter.BibEntrySuggestionProvider.class, StandardField.XREF),

// multi entry fieldg
Arguments.of(org.jabref.gui.autocompleter.JournalsSuggestionProvider.class, StandardField.JOURNAL),

// TODO: We should offer pre-configured publishers
Arguments.of(org.jabref.gui.autocompleter.JournalsSuggestionProvider.class, StandardField.PUBLISHER),

// TODO: Auto completion should be aware of possible values of special fields
Arguments.of(org.jabref.gui.autocompleter.WordSuggestionProvider.class, SpecialField.PRINTED)
);
}

@ParameterizedTest
@MethodSource("getTestPairs")
public void testAppropriateCompleterReturned(Class<SuggestionProvider<BibEntry>> expected, Field field) {
assertEquals(expected, suggestionProviders.getForField(field).getClass());
}

@Test
void emptySuggestionProviderReturnedForEmptySuggestionProviderList() {
SuggestionProviders empty = new SuggestionProviders();
assertEquals(EmptySuggestionProvider.class, empty.getForField(StandardField.AUTHOR).getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jabref.gui.documentviewer;

import java.io.IOException;
import java.nio.file.Path;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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

class PdfDocumentViewModelTest {

@Test
void getPagesTest(@TempDir Path tempDir) throws IOException {
try (PDDocument mockPDF = new PDDocument()) {
Path pdfFile = tempDir.resolve("mockPDF.pdf");

mockPDF.addPage(new PDPage());
mockPDF.save(pdfFile.toAbsolutePath().toString());

PdfDocumentViewModel PDFviewModel = new PdfDocumentViewModel(mockPDF);

assertEquals(1, PDFviewModel.getPages().size());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jabref.gui.preferences.keybindings;

import java.util.Optional;

import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import org.jabref.gui.DialogService;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.preferences.PreferencesService;

import org.junit.jupiter.api.Test;

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

class KeyBindingViewModelTest {

@Test
void resetToDefault() {
// Set new key binding
KeyBindingRepository keyBindingRepository = new KeyBindingRepository();
KeyBindingsTabViewModel keyBindingsTabViewModel = new KeyBindingsTabViewModel(keyBindingRepository, mock(DialogService.class), mock(PreferencesService.class));
KeyBinding binding = KeyBinding.ABBREVIATE;

KeyBindingViewModel viewModel = new KeyBindingViewModel(keyBindingRepository, binding, binding.getDefaultKeyBinding());
keyBindingsTabViewModel.selectedKeyBindingProperty().set(Optional.of(viewModel));

KeyEvent shortcutKeyEvent = new KeyEvent(KeyEvent.KEY_PRESSED, "F1", "F1", KeyCode.F1, true, false, false,
false);

assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent));

keyBindingsTabViewModel.setNewBindingForCurrent(shortcutKeyEvent);
keyBindingsTabViewModel.storeSettings();

assertTrue(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent));

// Reset to default
viewModel.resetToDefault();

assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent));
}
}
23 changes: 17 additions & 6 deletions src/test/java/org/jabref/logic/bibtex/FileFieldWriterTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.jabref.logic.bibtex;

import java.nio.file.Path;
import java.util.stream.Stream;

import org.jabref.model.entry.LinkedFile;

import org.junit.jupiter.api.Test;
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;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -31,12 +35,19 @@ public void testQuoteNull() {
assertNull(FileFieldWriter.quote(null));
}

@Test
public void testEncodeStringArray() {
assertEquals("a:b;c:d", FileFieldWriter.encodeStringArray(new String[][] {{"a", "b"}, {"c", "d"}}));
assertEquals("a:;c:d", FileFieldWriter.encodeStringArray(new String[][] {{"a", ""}, {"c", "d"}}));
assertEquals("a:" + null + ";c:d", FileFieldWriter.encodeStringArray(new String[][] {{"a", null}, {"c", "d"}}));
assertEquals("a:\\:b;c\\;:d", FileFieldWriter.encodeStringArray(new String[][] {{"a", ":b"}, {"c;", "d"}}));
private static Stream<Arguments> getEncodingTestData() {
return Stream.of(
Arguments.of("a:b;c:d", new String[][] {{"a", "b"}, {"c", "d"}}),
Arguments.of("a:;c:d", new String[][] {{"a", ""}, {"c", "d"}}),
Arguments.of("a:" + null + ";c:d", new String[][] {{"a", null}, {"c", "d"}}),
Arguments.of("a:\\:b;c\\;:d", new String[][] {{"a", ":b"}, {"c;", "d"}})
);
}

@ParameterizedTest
@MethodSource("getEncodingTestData")
public void testEncodeStringArray(String expected, String[][] values) {
assertEquals(expected, FileFieldWriter.encodeStringArray(values));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jabref.logic.citationstyle;

import java.util.List;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import org.junit.jupiter.api.Test;

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

class CitationStyleCacheTest {

private BibEntry bibEntry;
private List<BibEntry> entries;
private BibDatabase database;
private BibDatabaseContext databaseContext;
private CitationStyleCache csCache;

@Test
void getCitationForTest() {
BibEntry bibEntry = new BibEntry().withCitationKey("test");
List<BibEntry> entries = List.of(bibEntry);
BibDatabase database = new BibDatabase(entries);
BibDatabaseContext databaseContext = new BibDatabaseContext(database);
CitationStyleCache csCache = new CitationStyleCache(databaseContext);

assertNotNull(csCache.getCitationFor(bibEntry));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -17,19 +18,24 @@ public class UpperCaseFormatterTest {

@ParameterizedTest
@MethodSource("upperCaseTests")
public void upperCasetest(String expectedFormat, String inputFormat) {
public void upperCaseTest(String expectedFormat, String inputFormat) {
assertEquals(expectedFormat, formatter.format(inputFormat));
}

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("!", "!")
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("!", "!")
);
}

@Test
public void formatExample() {
assertEquals("KDE {Amarok}", formatter.format(formatter.getExampleInput()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public class HTMLCharacterCheckerTest {
private final BibEntry entry = new BibEntry();

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

Expand Down Expand Up @@ -60,5 +59,4 @@ void journalDoesNotAcceptHTMLEncodedCharacters() {
entry.setField(StandardField.JOURNAL, "&Auml;rling Str&ouml;m for &#8211; &#x2031;");
assertEquals(List.of(new IntegrityMessage("HTML encoded character found", entry, StandardField.JOURNAL)), checker.check(entry));
}

}
10 changes: 10 additions & 0 deletions src/test/java/org/jabref/logic/journals/AbbreviationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;

class AbbreviationTest {

Expand Down Expand Up @@ -101,4 +103,12 @@ void testDefaultAndShortestUniqueAbbreviationsAreSame() {
Abbreviation abbreviation = new Abbreviation("Long Name", "L N");
assertEquals(abbreviation.getAbbreviation(), abbreviation.getShortestUniqueAbbreviation());
}

@Test
void testEquals() {
Abbreviation abbreviation = new Abbreviation("Long Name", "L N", "LN");
Abbreviation otherAbbreviation = new Abbreviation("Long Name", "L N", "LN");
assertTrue(abbreviation.equals(otherAbbreviation));
assertFalse(abbreviation.equals("String"));
}
}
Loading