-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more unit tests to three gui classes (#7636)
* Added two Junit tests using mocks for the gui * Add JUnit test with mocks for CopyMoreAction * organized imports * modified the tests * Modified the tests * fix checkstyle issue and comment out the use of Globals * Extract Globals.prefs * Update ExportToClipboardActionTest.java * Fix checkstyle issues * Fix checkstyle issue Co-authored-by: Siedlerchr <[email protected]>
- Loading branch information
1 parent
7271f6b
commit 7aff889
Showing
8 changed files
with
371 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package org.jabref.gui.edit; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import javafx.collections.FXCollections; | ||
import javafx.collections.ObservableList; | ||
|
||
import org.jabref.gui.ClipBoardManager; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.JabRefDialogService; | ||
import org.jabref.gui.StateManager; | ||
import org.jabref.gui.actions.StandardActions; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.database.BibDatabase; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
import org.jabref.model.entry.types.StandardEntryType; | ||
import org.jabref.preferences.PreferencesService; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class CopyMoreActionTest { | ||
|
||
private CopyMoreAction copyMoreAction; | ||
private DialogService dialogService = spy(DialogService.class); | ||
private ClipBoardManager clipBoardManager = mock(ClipBoardManager.class); | ||
private PreferencesService preferencesService = mock(PreferencesService.class); | ||
private StateManager stateManager = mock(StateManager.class); | ||
private BibEntry entry; | ||
private List<String> titles = new ArrayList<String>(); | ||
private List<String> keys = new ArrayList<String>(); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
String title = "A tale from the trenches"; | ||
entry = new BibEntry(StandardEntryType.Misc) | ||
.withField(StandardField.AUTHOR, "Souti Chattopadhyay and Nicholas Nelson and Audrey Au and Natalia Morales and Christopher Sanchez and Rahul Pandita and Anita Sarma") | ||
.withField(StandardField.TITLE, title) | ||
.withField(StandardField.YEAR, "2020") | ||
.withField(StandardField.DOI, "10.1145/3377811.3380330") | ||
.withField(StandardField.SUBTITLE, "cognitive biases and software development") | ||
.withCitationKey("abc"); | ||
titles.add(title); | ||
keys.add("abc"); | ||
} | ||
|
||
@Test | ||
public void testExecuteOnFail() { | ||
when(stateManager.getActiveDatabase()).thenReturn(Optional.empty()); | ||
when(stateManager.getSelectedEntries()).thenReturn(FXCollections.emptyObservableList()); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
verify(clipBoardManager, times(0)).setContent(any(String.class)); | ||
verify(dialogService, times(0)).notify(any(String.class)); | ||
} | ||
|
||
@Test | ||
public void testExecuteCopyTitleWithNoTitle() { | ||
BibEntry entryWithNoTitle = (BibEntry) entry.clone(); | ||
entryWithNoTitle.clearField(StandardField.TITLE); | ||
ObservableList<BibEntry> entriesWithNoTitles = FXCollections.observableArrayList(entryWithNoTitle); | ||
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(entriesWithNoTitles)); | ||
|
||
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); | ||
when(stateManager.getSelectedEntries()).thenReturn(entriesWithNoTitles); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
verify(clipBoardManager, times(0)).setContent(any(String.class)); | ||
verify(dialogService, times(1)).notify(Localization.lang("None of the selected entries have titles.")); | ||
} | ||
|
||
@Test | ||
public void testExecuteCopyTitleOnPartialSuccess() { | ||
BibEntry entryWithNoTitle = (BibEntry) entry.clone(); | ||
entryWithNoTitle.clearField(StandardField.TITLE); | ||
ObservableList<BibEntry> mixedEntries = FXCollections.observableArrayList(entryWithNoTitle, entry); | ||
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(mixedEntries)); | ||
|
||
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); | ||
when(stateManager.getSelectedEntries()).thenReturn(mixedEntries); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
String copiedTitles = String.join("\n", titles); | ||
verify(clipBoardManager, times(1)).setContent(copiedTitles); | ||
verify(dialogService, times(1)).notify(Localization.lang("Warning: %0 out of %1 entries have undefined title.", | ||
Integer.toString(mixedEntries.size() - titles.size()), Integer.toString(mixedEntries.size()))); | ||
} | ||
|
||
@Test | ||
public void testExecuteCopyTitleOnSuccess() { | ||
ObservableList<BibEntry> entriesWithTitles = FXCollections.observableArrayList(entry); | ||
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(entriesWithTitles)); | ||
|
||
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); | ||
when(stateManager.getSelectedEntries()).thenReturn(entriesWithTitles); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
String copiedTitles = String.join("\n", titles); | ||
verify(clipBoardManager, times(1)).setContent(copiedTitles); | ||
verify(dialogService, times(1)).notify(Localization.lang("Copied '%0' to clipboard.", | ||
JabRefDialogService.shortenDialogMessage(copiedTitles))); | ||
} | ||
|
||
@Test | ||
public void testExecuteCopyKeyWithNoKey() { | ||
BibEntry entryWithNoKey = (BibEntry) entry.clone(); | ||
entryWithNoKey.clearCiteKey(); | ||
ObservableList<BibEntry> entriesWithNoKeys = FXCollections.observableArrayList(entryWithNoKey); | ||
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(entriesWithNoKeys)); | ||
|
||
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); | ||
when(stateManager.getSelectedEntries()).thenReturn(entriesWithNoKeys); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
verify(clipBoardManager, times(0)).setContent(any(String.class)); | ||
verify(dialogService, times(1)).notify(Localization.lang("None of the selected entries have citation keys.")); | ||
} | ||
|
||
@Test | ||
public void testExecuteCopyKeyOnPartialSuccess() { | ||
BibEntry entryWithNoKey = (BibEntry) entry.clone(); | ||
entryWithNoKey.clearCiteKey(); | ||
ObservableList<BibEntry> mixedEntries = FXCollections.observableArrayList(entryWithNoKey, entry); | ||
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(mixedEntries)); | ||
|
||
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); | ||
when(stateManager.getSelectedEntries()).thenReturn(mixedEntries); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
String copiedKeys = String.join("\n", keys); | ||
verify(clipBoardManager, times(1)).setContent(copiedKeys); | ||
verify(dialogService, times(1)).notify(Localization.lang("Warning: %0 out of %1 entries have undefined citation key.", | ||
Integer.toString(mixedEntries.size() - titles.size()), Integer.toString(mixedEntries.size()))); | ||
} | ||
|
||
@Test | ||
public void testExecuteCopyKeyOnSuccess() { | ||
ObservableList<BibEntry> entriesWithKeys = FXCollections.observableArrayList(entry); | ||
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(entriesWithKeys)); | ||
|
||
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); | ||
when(stateManager.getSelectedEntries()).thenReturn(entriesWithKeys); | ||
copyMoreAction = new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, clipBoardManager, preferencesService); | ||
copyMoreAction.execute(); | ||
|
||
String copiedKeys = String.join("\n", keys); | ||
verify(clipBoardManager, times(1)).setContent(copiedKeys); | ||
verify(dialogService, times(1)).notify(Localization.lang("Copied '%0' to clipboard.", | ||
JabRefDialogService.shortenDialogMessage(copiedKeys))); | ||
} | ||
} |
Oops, something went wrong.