diff --git a/src/main/java/org/jabref/gui/BasePanel.java b/src/main/java/org/jabref/gui/BasePanel.java index f57f69d6134..6291f893d02 100644 --- a/src/main/java/org/jabref/gui/BasePanel.java +++ b/src/main/java/org/jabref/gui/BasePanel.java @@ -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); diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index a7f2586d7e0..e6ef5d3128f 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -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")); diff --git a/src/main/java/org/jabref/gui/dialogs/AutosaveUIManager.java b/src/main/java/org/jabref/gui/dialogs/AutosaveUIManager.java index 1fb08fbb184..2ce31738e54 100644 --- a/src/main/java/org/jabref/gui/dialogs/AutosaveUIManager.java +++ b/src/main/java/org/jabref/gui/dialogs/AutosaveUIManager.java @@ -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; @@ -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); } diff --git a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java index ecd1d7e6a2f..3a4fc68ffd2 100644 --- a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java +++ b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java @@ -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; @@ -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); @@ -124,7 +125,7 @@ private boolean doSave() { panel.getBibDatabaseContext() .getMetaData() .getEncoding() - .orElse(Globals.prefs.getDefaultEncoding()), + .orElse(prefs.getDefaultEncoding()), SavePreferences.DatabaseSaveType.ALL); if (success) { @@ -182,10 +183,10 @@ private Optional 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 selectedPath = dialogService.showFileSaveDialog(fileDialogConfiguration); - selectedPath.ifPresent(path -> Globals.prefs.setWorkingDir(path.getParent())); + selectedPath.ifPresent(path -> prefs.setWorkingDir(path.getParent())); return selectedPath; } @@ -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(); } @@ -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) { diff --git a/src/main/java/org/jabref/gui/shared/SharedDatabaseLoginDialogViewModel.java b/src/main/java/org/jabref/gui/shared/SharedDatabaseLoginDialogViewModel.java index dddbce8101a..2f1f0803b8d 100644 --- a/src/main/java/org/jabref/gui/shared/SharedDatabaseLoginDialogViewModel.java +++ b/src/main/java/org/jabref/gui/shared/SharedDatabaseLoginDialogViewModel.java @@ -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); } diff --git a/src/test/java/org/jabref/architecture/TestArchitectureTests.java b/src/test/java/org/jabref/architecture/TestArchitectureTests.java index c37c450d8a0..da908308d9d 100644 --- a/src/test/java/org/jabref/architecture/TestArchitectureTests.java +++ b/src/test/java/org/jabref/architecture/TestArchitectureTests.java @@ -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"; @@ -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 data() { diff --git a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java new file mode 100644 index 00000000000..a0bce778175 --- /dev/null +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -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 = 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); + } +}