Skip to content

Commit

Permalink
Add more unit tests to three gui classes (#7636)
Browse files Browse the repository at this point in the history
* 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 <siedlerkiller@gmail.com>
  • Loading branch information
ningxie1991 and Siedlerchr committed Apr 26, 2021
1 parent 7271f6b commit 7aff889
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.getClipboardManager(), prefs.getPreviewPreferences())),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService, Globals.exportFactory, Globals.getClipboardManager(), Globals.TASK_EXECUTOR))),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService, Globals.exportFactory, Globals.getClipboardManager(), Globals.TASK_EXECUTOR, prefs))),

factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, this, stateManager)),

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.actions.SimpleCommand;
Expand All @@ -28,6 +27,7 @@
import org.jabref.logic.util.OS;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -46,21 +46,25 @@ public class ExportToClipboardAction extends SimpleCommand {
private final ExporterFactory exporterFactory;
private final ClipBoardManager clipBoardManager;
private final TaskExecutor taskExecutor;
private final PreferencesService preferences;

public ExportToClipboardAction(JabRefFrame frame, DialogService dialogService, ExporterFactory exporterFactory, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor) {
public ExportToClipboardAction(JabRefFrame frame, DialogService dialogService, ExporterFactory exporterFactory, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor, PreferencesService prefs) {
this.frame = frame;
this.dialogService = dialogService;
this.exporterFactory = exporterFactory;
this.clipBoardManager = clipBoardManager;
this.taskExecutor = taskExecutor;
this.preferences = prefs;
}

public ExportToClipboardAction(LibraryTab panel, DialogService dialogService, ExporterFactory exporterFactory, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor) {
public ExportToClipboardAction(LibraryTab panel, DialogService dialogService, ExporterFactory exporterFactory, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor, PreferencesService prefs) {
this.panel = panel;
this.dialogService = dialogService;
this.exporterFactory = exporterFactory;
this.clipBoardManager = clipBoardManager;
this.taskExecutor = taskExecutor;
this.preferences = prefs;

}

@Override
Expand All @@ -81,7 +85,7 @@ public void execute() {

// Find default choice, if any
Exporter defaultChoice = exporters.stream()
.filter(exporter -> exporter.getName().equals(Globals.prefs.getImportExportPreferences().getLastExportExtension()))
.filter(exporter -> exporter.getName().equals(preferences.getImportExportPreferences().getLastExportExtension()))
.findAny()
.orElse(null);

Expand All @@ -102,12 +106,12 @@ private ExportResult exportToClipboard(Exporter exporter) throws Exception {
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = panel.getBibDatabaseContext()
.getFileDirectories(Globals.prefs.getFilePreferences());
preferences.storeFileDirforDatabase(panel.getBibDatabaseContext()
.getFileDirectories(preferences.getFilePreferences()));

// Add chosen export type to last used preference, to become default
Globals.prefs.storeImportExportPreferences(
Globals.prefs.getImportExportPreferences().withLastExportExtension(exporter.getName()));
preferences.storeImportExportPreferences(
preferences.getImportExportPreferences().withLastExportExtension(exporter.getName()));

Path tmp = null;
try {
Expand All @@ -122,7 +126,7 @@ private ExportResult exportToClipboard(Exporter exporter) throws Exception {
panel.getBibDatabaseContext()
.getMetaData()
.getEncoding()
.orElse(Globals.prefs.getDefaultEncoding()),
.orElse(preferences.getDefaultEncoding()),
entries);
// Read the file and put the contents on the clipboard:

Expand Down Expand Up @@ -159,7 +163,7 @@ private String readFileToString(Path tmp) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(tmp, panel.getBibDatabaseContext()
.getMetaData()
.getEncoding()
.orElse(Globals.prefs.getDefaultEncoding()))) {
.orElse(preferences.getDefaultEncoding()))) {
return reader.lines().collect(Collectors.joining(OS.NEWLINE));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static Menu createCopySubMenu(LibraryTab libraryTab,
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, previewPreferences)));
}

copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(libraryTab, dialogService, Globals.exportFactory, clipBoardManager, Globals.TASK_EXECUTOR)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(libraryTab, dialogService, Globals.exportFactory, clipBoardManager, Globals.TASK_EXECUTOR, preferencesService)));
return copySpecialMenu;
}
}
8 changes: 7 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1049,12 +1049,18 @@ public void importPreferences(Path file) throws JabRefException {
}
}

private FileLinkPreferences getFileLinkPreferences() {
@Override
public FileLinkPreferences getFileLinkPreferences() {
return new FileLinkPreferences(
get(MAIN_FILE_DIRECTORY), // REALLY HERE?
fileDirForDatabase);
}

@Override
public void storeFileDirforDatabase(List<Path> dirs) {
this.fileDirForDatabase = dirs;
}

@Override
public LayoutFormatterPreferences getLayoutFormatterPreferences(JournalAbbreviationRepository repository) {
return new LayoutFormatterPreferences(
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Language;
import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.layout.format.FileLinkPreferences;
import org.jabref.logic.layout.format.NameFormatterPreferences;
import org.jabref.logic.net.ProxyPreferences;
import org.jabref.logic.openoffice.OpenOfficePreferences;
Expand Down Expand Up @@ -278,6 +279,10 @@ public interface PreferencesService {

void storeShouldAutosave(boolean shouldAutosave);

FileLinkPreferences getFileLinkPreferences();

void storeFileDirforDatabase(List<Path> dirs);

//*************************************************************************************************************
// Import/Export preferences
//*************************************************************************************************************
Expand Down
168 changes: 168 additions & 0 deletions src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java
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)));
}
}
Loading

0 comments on commit 7aff889

Please sign in to comment.