Skip to content

Commit

Permalink
Create test (#4505)
Browse files Browse the repository at this point in the history
* create non working test depend on Globals

* pass preferences via constructor

* create testcase

* refactor test

* refactor code

* add test case

* move to exporter package

* fix code style

* fix test name

* fix test code style

* add SaveDatabaseActionTest in TestArchitectureTest exception
  • Loading branch information
Ali96kz authored and tobiasdiez committed Nov 23, 2018
1 parent c19f3a4 commit 5479070
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void output(String s) {
}

private void setupActions() {
SaveDatabaseAction saveAction = new SaveDatabaseAction(this);
SaveDatabaseAction saveAction = new SaveDatabaseAction(this, Globals.prefs);
CleanupAction cleanUpAction = new CleanupAction(this, Globals.prefs);

actions.put(Actions.UNDO, undoAction);
Expand Down
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 @@ -1335,7 +1335,7 @@ private boolean confirmClose(BasePanel panel) {
if (response.isPresent() && response.get().equals(saveChanges)) {
// The user wants to save.
try {
SaveDatabaseAction saveAction = new SaveDatabaseAction(panel);
SaveDatabaseAction saveAction = new SaveDatabaseAction(panel, Globals.prefs);
if (!saveAction.save()) {
// The action was either canceled or unsuccessful.
output(Localization.lang("Unable to save library"));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/dialogs/AutosaveUIManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.dialogs;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.exporter.SaveDatabaseAction;
import org.jabref.model.database.event.AutosaveEvent;
Expand All @@ -25,7 +26,7 @@ public AutosaveUIManager(BasePanel panel) {
@Subscribe
public void listen(@SuppressWarnings("unused") AutosaveEvent event) {
try {
new SaveDatabaseAction(panel).save();
new SaveDatabaseAction(panel, Globals.prefs).save();
} catch (Throwable e) {
LOGGER.error("Problem occured while saving.", e);
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
Expand Down Expand Up @@ -53,16 +52,18 @@ public class SaveDatabaseAction {
private final BasePanel panel;
private final JabRefFrame frame;
private final DialogService dialogService;
private final JabRefPreferences prefs;

public SaveDatabaseAction(BasePanel panel) {
public SaveDatabaseAction(BasePanel panel, JabRefPreferences prefs) {
this.panel = panel;
this.frame = panel.frame();
this.dialogService = frame.getDialogService();
this.prefs = prefs;
}

private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) throws SaveException {
try {
SavePreferences preferences = Globals.prefs.loadForSaveFromPreferences()
SavePreferences preferences = prefs.loadForSaveFromPreferences()
.withEncoding(encoding)
.withSaveType(saveType);

Expand Down Expand Up @@ -124,7 +125,7 @@ private boolean doSave() {
panel.getBibDatabaseContext()
.getMetaData()
.getEncoding()
.orElse(Globals.prefs.getDefaultEncoding()),
.orElse(prefs.getDefaultEncoding()),
SavePreferences.DatabaseSaveType.ALL);

if (success) {
Expand Down Expand Up @@ -182,10 +183,10 @@ private Optional<Path> getSavePath() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.BIBTEX_DB)
.withDefaultExtension(StandardFileType.BIBTEX_DB)
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY))
.withInitialDirectory(prefs.get(JabRefPreferences.WORKING_DIRECTORY))
.build();
Optional<Path> selectedPath = dialogService.showFileSaveDialog(fileDialogConfiguration);
selectedPath.ifPresent(path -> Globals.prefs.setWorkingDir(path.getParent()));
selectedPath.ifPresent(path -> prefs.setWorkingDir(path.getParent()));
return selectedPath;
}

Expand Down Expand Up @@ -228,7 +229,7 @@ public void saveAs(Path file) {
private boolean readyForAutosave(BibDatabaseContext context) {
return ((context.getLocation() == DatabaseLocation.SHARED) ||
((context.getLocation() == DatabaseLocation.LOCAL)
&& Globals.prefs.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)))
&& prefs.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)))
&&
context.getDatabasePath().isPresent();
}
Expand All @@ -240,7 +241,7 @@ private boolean readyForBackup(BibDatabaseContext context) {
public void saveSelectedAsPlain() {
getSavePath().ifPresent(path -> {
try {
saveDatabase(path, true, Globals.prefs.getDefaultEncoding(), SavePreferences.DatabaseSaveType.PLAIN_BIBTEX);
saveDatabase(path, true, prefs.getDefaultEncoding(), SavePreferences.DatabaseSaveType.PLAIN_BIBTEX);
frame.getFileHistory().newFile(path);
frame.output(Localization.lang("Saved selected to '%0'.", path.toString()));
} catch (SaveException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void openSharedDatabase(DBMSConnectionProperties connectionProperties) {

if (!folder.getValue().isEmpty()) {
try {
new SaveDatabaseAction(panel).saveAs(Paths.get(folder.getValue()));
new SaveDatabaseAction(panel, Globals.prefs).saveAs(Paths.get(folder.getValue()));
} catch (Throwable e) {
LOGGER.error("Error while saving the database", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TestArchitectureTests {
private static final String CLASS_ORG_JABREF_PREFERENCES = "org.jabref.preferences.JabRefPreferences";
private static final String CLASS_ORG_JABREF_PREFERENCES_TEST = "JabRefPreferencesTest";
private static final String CLASS_ORG_JABREF_PREFERENCES_MIGRATIONS_TEST = "PreferencesMigrationsTest";
private static final String CLASS_ORG_JABREF_SAVE_DATABASE_ACTION_TEST = "SaveDatabaseActionTest";
private static final String CLASS_ORG_JABREF_UPDATE_TIMESTAMP_LISTENER_TEST = "UpdateTimestampListenerTest";
private static final String CLASS_ORG_JABREF_ENTRY_EDITOR_TEST = "EntryEditorTest";
private static final String CLASS_ORG_JABREF_LINKED_FILE_VIEW_MODEL_TEST = "LinkedFileViewModelTest";
Expand All @@ -36,9 +37,11 @@ public TestArchitectureTests() {
exceptions = new ArrayList<>();
exceptions.add(CLASS_ORG_JABREF_PREFERENCES_TEST);
exceptions.add(CLASS_ORG_JABREF_PREFERENCES_MIGRATIONS_TEST);
exceptions.add(CLASS_ORG_JABREF_SAVE_DATABASE_ACTION_TEST);
exceptions.add(CLASS_ORG_JABREF_UPDATE_TIMESTAMP_LISTENER_TEST);
exceptions.add(CLASS_ORG_JABREF_ENTRY_EDITOR_TEST);
exceptions.add(CLASS_ORG_JABREF_LINKED_FILE_VIEW_MODEL_TEST);

}

public static Stream<String[]> data() {
Expand Down
103 changes: 103 additions & 0 deletions src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.jabref.gui.exporter;

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

import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.shared.DatabaseLocation;
import org.jabref.preferences.JabRefPreferences;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
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;

class SaveDatabaseActionTest {

private static final String TEST_FILE_PATH = "C:\\Users\\John_Doe\\Jabref";
private final File file = new File(TEST_FILE_PATH);
private Optional<Path> path = Optional.of(file.toPath());

private DialogService dialogService = mock(DialogService.class);
private JabRefPreferences preferences = mock(JabRefPreferences.class);
private BasePanel basePanel = mock(BasePanel.class);
private JabRefFrame jabRefFrame = mock(JabRefFrame.class);
private BibDatabaseContext dbContext = spy(BibDatabaseContext.class);
private SaveDatabaseAction saveDatabaseAction;

@Before
public void setUp() {
when(basePanel.frame()).thenReturn(jabRefFrame);
when(basePanel.getBibDatabaseContext()).thenReturn(dbContext);
when(jabRefFrame.getDialogService()).thenReturn(dialogService);

saveDatabaseAction = spy(new SaveDatabaseAction(basePanel, preferences));
}

@Test
public void saveAsShouldSetWorkingDirectory() {
when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH);
when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(path);
doNothing().when(saveDatabaseAction).saveAs(any());

saveDatabaseAction.saveAs();

verify(preferences, times(1)).setWorkingDir(path.get().getParent());
}

@Test
public void saveAsShouldNotSetWorkingDirectoryIfNotSelected() {
when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH);
when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.empty());
doNothing().when(saveDatabaseAction).saveAs(any());

saveDatabaseAction.saveAs();

verify(preferences, times(0)).setWorkingDir(path.get().getParent());
}

@Test
public void saveAsShouldSetNewDatabasePathIntoContext() {
when(dbContext.getDatabasePath()).thenReturn(Optional.empty());
when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL);
when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false);

saveDatabaseAction.saveAs(file.toPath());

verify(dbContext, times(1)).setDatabaseFile(file.toPath());
}

@Test
public void saveShouldShowSaveAsIfDatabaseNotSelected() {
when(dbContext.getDatabasePath()).thenReturn(Optional.empty());
when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL);
when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false);
when(dialogService.showFileSaveDialog(any())).thenReturn(path);
doNothing().when(saveDatabaseAction).saveAs(file.toPath());

saveDatabaseAction.save();

verify(saveDatabaseAction, times(1)).saveAs(file.toPath());
}

@Test
public void saveShouldNotSaveDatabaseIfPathNotSet() {
when(dbContext.getDatabasePath()).thenReturn(Optional.empty());

boolean result = saveDatabaseAction.save();

assertFalse(result);
}
}

0 comments on commit 5479070

Please sign in to comment.