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

Create test #4505

Merged
merged 12 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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;

public class SaveDatabaseActionTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis architecture error message might be related to the fact that the TestClass is public.
Remove the public modifier. At least I remember that I once had the same problem and fixed it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not Helped

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try not to use the Preference class in tests. In this there is no easy way to achieve this, so you are allowed to add the SaveDatabaseActionTest class as an exception here:
https://github.com/JabRef/jabref/blob/master/src/test/java/org/jabref/architecture/TestArchitectureTests.java#L41

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. fixed


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);
}
}