From f1f23e1c4c1360cd9033add9bc8baa4c2c35a967 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Mon, 19 Nov 2018 12:10:52 +0600 Subject: [PATCH 01/11] create non working test depend on Globals --- .../jabref/gui/SaveDatabaseActionTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/test/java/org/jabref/gui/SaveDatabaseActionTest.java diff --git a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java new file mode 100644 index 00000000000..87ed33c90d2 --- /dev/null +++ b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java @@ -0,0 +1,84 @@ +package org.jabref.gui; + +import org.jabref.Globals; +import org.jabref.gui.exporter.SaveDatabaseAction; +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 java.io.File; +import java.nio.file.Path; +import java.util.Optional; + +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.*; + +public class SaveDatabaseActionTest { + + private final File file = new File("C:\\Users\\John_Doe\\Jabref"); + private Optional path = Optional.of(file.toPath()); + + private DialogService dialogService = mock(DialogService.class); + private JabRefPreferences jabRefPreferences = mock(JabRefPreferences.class); + private BasePanel basePanel = mock(BasePanel.class); + private JabRefFrame jabRefFrame = mock(JabRefFrame.class); + private SaveDatabaseAction saveDatabaseAction; + private BibDatabaseContext dbContext = mock(BibDatabaseContext.class); + + @Before + public void setUp(){ + Globals.prefs = jabRefPreferences; + + when(basePanel.frame()).thenReturn(jabRefFrame); + when(basePanel.getBibDatabaseContext()).thenReturn(dbContext); + when(jabRefFrame.getDialogService()).thenReturn(dialogService); + + saveDatabaseAction = spy(new SaveDatabaseAction(basePanel)); + } + + @Test + public void saveAsShouldSetWorkingDirectory(){ + when(jabRefPreferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); + when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(path); + doNothing().when(saveDatabaseAction).saveAs(any()); + + saveDatabaseAction.saveAs(); + + verify(jabRefPreferences, times(1)).setWorkingDir(path.get().getParent()); + } + + @Test + public void saveAsShouldNotSetWorkingDirectoryIfNotSelected(){ + when(jabRefPreferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); + when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.empty()); + doNothing().when(saveDatabaseAction).saveAs(any()); + + saveDatabaseAction.saveAs(); + + verify(jabRefPreferences, times(0)).setWorkingDir(path.get().getParent()); + } + + @Test + public void saveAsShouldSetNewDatabasePathIntoContext(){ + when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); + when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); + when(jabRefPreferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); + + saveDatabaseAction.saveAs(file.toPath()); + + verify(dbContext, times(1)).setDatabaseFile(file.toPath()); + } + + @Test + public void saveShouldNotSaveDatabasePathNotSetted(){ + when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); + + boolean result = saveDatabaseAction.save(); + + assertFalse(result); + } +} + From 9960ed25c7ee17de165a0c3656c368816fbd7c78 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Mon, 19 Nov 2018 16:09:28 +0600 Subject: [PATCH 02/11] pass preferences via constructor --- src/main/java/org/jabref/gui/BasePanel.java | 2 +- src/main/java/org/jabref/gui/JabRefFrame.java | 2 +- .../jabref/gui/dialogs/AutosaveUIManager.java | 3 ++- .../jabref/gui/exporter/SaveDatabaseAction.java | 17 +++++++++-------- .../SharedDatabaseLoginDialogViewModel.java | 2 +- 5 files changed, 14 insertions(+), 12 deletions(-) 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 60d43a613cb..203f28ec559 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) { @@ -175,10 +176,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; } @@ -221,7 +222,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(); } @@ -233,7 +234,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 18d0c881baf..a9a77fbc05a 100644 --- a/src/main/java/org/jabref/gui/shared/SharedDatabaseLoginDialogViewModel.java +++ b/src/main/java/org/jabref/gui/shared/SharedDatabaseLoginDialogViewModel.java @@ -161,7 +161,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); } From 153e4d5686f35f63e47cd6afde4b03e3b69e2703 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Mon, 19 Nov 2018 17:25:52 +0600 Subject: [PATCH 03/11] create testcase --- .../jabref/gui/SaveDatabaseActionTest.java | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java index 87ed33c90d2..0863d643100 100644 --- a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java @@ -1,8 +1,9 @@ package org.jabref.gui; -import org.jabref.Globals; +import org.apache.pdfbox.contentstream.operator.state.Save; import org.jabref.gui.exporter.SaveDatabaseAction; import org.jabref.gui.util.FileDialogConfiguration; +import org.jabref.logic.exporter.SavePreferences; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.shared.DatabaseLocation; import org.jabref.preferences.JabRefPreferences; @@ -10,6 +11,7 @@ import org.junit.Test; import java.io.File; +import java.nio.charset.Charset; import java.nio.file.Path; import java.util.Optional; @@ -22,50 +24,48 @@ public class SaveDatabaseActionTest { private Optional path = Optional.of(file.toPath()); private DialogService dialogService = mock(DialogService.class); - private JabRefPreferences jabRefPreferences = mock(JabRefPreferences.class); + private JabRefPreferences preferences = mock(JabRefPreferences.class); private BasePanel basePanel = mock(BasePanel.class); private JabRefFrame jabRefFrame = mock(JabRefFrame.class); private SaveDatabaseAction saveDatabaseAction; - private BibDatabaseContext dbContext = mock(BibDatabaseContext.class); + private BibDatabaseContext dbContext = spy(BibDatabaseContext.class); @Before public void setUp(){ - Globals.prefs = jabRefPreferences; - when(basePanel.frame()).thenReturn(jabRefFrame); when(basePanel.getBibDatabaseContext()).thenReturn(dbContext); when(jabRefFrame.getDialogService()).thenReturn(dialogService); - saveDatabaseAction = spy(new SaveDatabaseAction(basePanel)); + saveDatabaseAction = spy(new SaveDatabaseAction(basePanel, preferences)); } @Test - public void saveAsShouldSetWorkingDirectory(){ - when(jabRefPreferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); + public void saveAs_setWorkingDirectory(){ + when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(path); doNothing().when(saveDatabaseAction).saveAs(any()); saveDatabaseAction.saveAs(); - verify(jabRefPreferences, times(1)).setWorkingDir(path.get().getParent()); + verify(preferences, times(1)).setWorkingDir(path.get().getParent()); } @Test - public void saveAsShouldNotSetWorkingDirectoryIfNotSelected(){ - when(jabRefPreferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); + public void saveAs_notSetWorkingDirectory_ifNotSelected(){ + when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.empty()); doNothing().when(saveDatabaseAction).saveAs(any()); saveDatabaseAction.saveAs(); - verify(jabRefPreferences, times(0)).setWorkingDir(path.get().getParent()); + verify(preferences, times(0)).setWorkingDir(path.get().getParent()); } @Test - public void saveAsShouldSetNewDatabasePathIntoContext(){ + public void saveAs_setNewDatabasePath_intoContext(){ when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); - when(jabRefPreferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); + when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); saveDatabaseAction.saveAs(file.toPath()); @@ -73,7 +73,21 @@ public void saveAsShouldSetNewDatabasePathIntoContext(){ } @Test - public void saveShouldNotSaveDatabasePathNotSetted(){ + public void saveAs_saveDatabaseByNewPath(){ + SavePreferences savePreferences = mock(SavePreferences.class); + + when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); + when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); + when(preferences.getDefaultEncoding()).thenReturn(Charset.defaultCharset()); + when(preferences.loadForSaveFromPreferences()).thenReturn(savePreferences); + when(savePreferences.withEncoding(any())).thenReturn(savePreferences); + when(savePreferences.withSaveType(any())).thenReturn(savePreferences); + + saveDatabaseAction.saveAs(file.toPath()); + } + + @Test + public void save_notSaveDatabase_pathNotSet(){ when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); boolean result = saveDatabaseAction.save(); From 1a3d52c3f7f08b48c4d53005044ed5fc154f371c Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Tue, 20 Nov 2018 10:28:12 +0600 Subject: [PATCH 04/11] refactor test --- .../jabref/gui/SaveDatabaseActionTest.java | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java index 0863d643100..1725657bc49 100644 --- a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java @@ -20,7 +20,8 @@ public class SaveDatabaseActionTest { - private final File file = new File("C:\\Users\\John_Doe\\Jabref"); + 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); @@ -40,8 +41,8 @@ public void setUp(){ } @Test - public void saveAs_setWorkingDirectory(){ - when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); + 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()); @@ -51,8 +52,8 @@ public void saveAs_setWorkingDirectory(){ } @Test - public void saveAs_notSetWorkingDirectory_ifNotSelected(){ - when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn("C:\\Users\\John_Doe\\Jabref"); + 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()); @@ -62,7 +63,7 @@ public void saveAs_notSetWorkingDirectory_ifNotSelected(){ } @Test - public void saveAs_setNewDatabasePath_intoContext(){ + public void saveAsShouldSetNewDatabasePathIntoContext(){ when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); @@ -73,21 +74,7 @@ public void saveAs_setNewDatabasePath_intoContext(){ } @Test - public void saveAs_saveDatabaseByNewPath(){ - SavePreferences savePreferences = mock(SavePreferences.class); - - when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); - when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); - when(preferences.getDefaultEncoding()).thenReturn(Charset.defaultCharset()); - when(preferences.loadForSaveFromPreferences()).thenReturn(savePreferences); - when(savePreferences.withEncoding(any())).thenReturn(savePreferences); - when(savePreferences.withSaveType(any())).thenReturn(savePreferences); - - saveDatabaseAction.saveAs(file.toPath()); - } - - @Test - public void save_notSaveDatabase_pathNotSet(){ + public void saveShouldNotSaveDatabaseIfPathNotSet(){ when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); boolean result = saveDatabaseAction.save(); From c5b6a3373b35f83526844dfdc755ef3bffeba6e3 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Thu, 22 Nov 2018 09:23:54 +0600 Subject: [PATCH 05/11] refactor code --- .../jabref/gui/SaveDatabaseActionTest.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java index 1725657bc49..38efdff186f 100644 --- a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java @@ -41,7 +41,7 @@ public void setUp(){ } @Test - public void saveAsShouldSetWorkingDirectory(){ + public void saveAs_shouldSetWorkingDirectory(){ when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(path); doNothing().when(saveDatabaseAction).saveAs(any()); @@ -52,7 +52,7 @@ public void saveAsShouldSetWorkingDirectory(){ } @Test - public void saveAsShouldNotSetWorkingDirectoryIfNotSelected(){ + public void saveAs_shouldNotSetWorkingDirectoryIfNotSelected(){ when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.empty()); doNothing().when(saveDatabaseAction).saveAs(any()); @@ -63,7 +63,7 @@ public void saveAsShouldNotSetWorkingDirectoryIfNotSelected(){ } @Test - public void saveAsShouldSetNewDatabasePathIntoContext(){ + public void saveAs_shouldSetNewDatabasePathIntoContext(){ when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); @@ -74,7 +74,18 @@ public void saveAsShouldSetNewDatabasePathIntoContext(){ } @Test - public void saveShouldNotSaveDatabaseIfPathNotSet(){ + public void save_shouldShowSaveAsIfDatabaseNotSelected(){ + when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); + when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); + when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); + + saveDatabaseAction.save(); + + verify(dbContext, times(1)).setDatabaseFile(file.toPath()); + } + + @Test + public void save_shouldNotSaveDatabaseIfPathNotSet(){ when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); boolean result = saveDatabaseAction.save(); From fd21060cce7f477703f586b218488626dfe773f6 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Thu, 22 Nov 2018 09:57:34 +0600 Subject: [PATCH 06/11] add test case --- src/test/java/org/jabref/gui/SaveDatabaseActionTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java index 38efdff186f..f2104577d05 100644 --- a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java @@ -1,9 +1,7 @@ package org.jabref.gui; -import org.apache.pdfbox.contentstream.operator.state.Save; import org.jabref.gui.exporter.SaveDatabaseAction; import org.jabref.gui.util.FileDialogConfiguration; -import org.jabref.logic.exporter.SavePreferences; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.shared.DatabaseLocation; import org.jabref.preferences.JabRefPreferences; @@ -11,7 +9,6 @@ import org.junit.Test; import java.io.File; -import java.nio.charset.Charset; import java.nio.file.Path; import java.util.Optional; @@ -28,8 +25,8 @@ public class SaveDatabaseActionTest { private JabRefPreferences preferences = mock(JabRefPreferences.class); private BasePanel basePanel = mock(BasePanel.class); private JabRefFrame jabRefFrame = mock(JabRefFrame.class); - private SaveDatabaseAction saveDatabaseAction; private BibDatabaseContext dbContext = spy(BibDatabaseContext.class); + private SaveDatabaseAction saveDatabaseAction; @Before public void setUp(){ @@ -78,10 +75,12 @@ public void save_shouldShowSaveAsIfDatabaseNotSelected(){ 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(dbContext, times(1)).setDatabaseFile(file.toPath()); + verify(saveDatabaseAction, times(1)).saveAs(file.toPath()); } @Test From c229fc86bb974c7f2abda4fd602811606c4a37b8 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Thu, 22 Nov 2018 10:00:56 +0600 Subject: [PATCH 07/11] move to exporter package --- .../jabref/gui/{ => exporter}/SaveDatabaseActionTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) rename src/test/java/org/jabref/gui/{ => exporter}/SaveDatabaseActionTest.java (96%) diff --git a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java similarity index 96% rename from src/test/java/org/jabref/gui/SaveDatabaseActionTest.java rename to src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java index f2104577d05..285bc203187 100644 --- a/src/test/java/org/jabref/gui/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -1,5 +1,8 @@ -package org.jabref.gui; +package org.jabref.gui.exporter; +import org.jabref.gui.BasePanel; +import org.jabref.gui.DialogService; +import org.jabref.gui.JabRefFrame; import org.jabref.gui.exporter.SaveDatabaseAction; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.model.database.BibDatabaseContext; From 61cdf7a38a4968a9a54c5296a6752467ef9f3591 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Thu, 22 Nov 2018 10:03:35 +0600 Subject: [PATCH 08/11] fix code style --- .../gui/exporter/SaveDatabaseActionTest.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java index 285bc203187..0df0836e6ed 100644 --- a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -3,7 +3,6 @@ import org.jabref.gui.BasePanel; import org.jabref.gui.DialogService; import org.jabref.gui.JabRefFrame; -import org.jabref.gui.exporter.SaveDatabaseAction; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.shared.DatabaseLocation; @@ -32,7 +31,7 @@ public class SaveDatabaseActionTest { private SaveDatabaseAction saveDatabaseAction; @Before - public void setUp(){ + public void setUp() { when(basePanel.frame()).thenReturn(jabRefFrame); when(basePanel.getBibDatabaseContext()).thenReturn(dbContext); when(jabRefFrame.getDialogService()).thenReturn(dialogService); @@ -41,7 +40,7 @@ public void setUp(){ } @Test - public void saveAs_shouldSetWorkingDirectory(){ + public void saveAs_shouldSetWorkingDirectory() { when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(path); doNothing().when(saveDatabaseAction).saveAs(any()); @@ -52,7 +51,7 @@ public void saveAs_shouldSetWorkingDirectory(){ } @Test - public void saveAs_shouldNotSetWorkingDirectoryIfNotSelected(){ + public void saveAs_shouldNotSetWorkingDirectoryIfNotSelected() { when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.empty()); doNothing().when(saveDatabaseAction).saveAs(any()); @@ -63,7 +62,7 @@ public void saveAs_shouldNotSetWorkingDirectoryIfNotSelected(){ } @Test - public void saveAs_shouldSetNewDatabasePathIntoContext(){ + public void saveAs_shouldSetNewDatabasePathIntoContext() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); @@ -74,7 +73,7 @@ public void saveAs_shouldSetNewDatabasePathIntoContext(){ } @Test - public void save_shouldShowSaveAsIfDatabaseNotSelected(){ + public void save_shouldShowSaveAsIfDatabaseNotSelected() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); @@ -87,7 +86,7 @@ public void save_shouldShowSaveAsIfDatabaseNotSelected(){ } @Test - public void save_shouldNotSaveDatabaseIfPathNotSet(){ + public void save_shouldNotSaveDatabaseIfPathNotSet() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); boolean result = saveDatabaseAction.save(); @@ -95,4 +94,3 @@ public void save_shouldNotSaveDatabaseIfPathNotSet(){ assertFalse(result); } } - From 05e777bd1065a4cdaf848c0a88b9c8ff298a70de Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Thu, 22 Nov 2018 12:52:01 +0600 Subject: [PATCH 09/11] fix test name --- .../jabref/gui/exporter/SaveDatabaseActionTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java index 0df0836e6ed..a068c1f4a1f 100644 --- a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -40,7 +40,7 @@ public void setUp() { } @Test - public void saveAs_shouldSetWorkingDirectory() { + 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()); @@ -51,7 +51,7 @@ public void saveAs_shouldSetWorkingDirectory() { } @Test - public void saveAs_shouldNotSetWorkingDirectoryIfNotSelected() { + 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()); @@ -62,7 +62,7 @@ public void saveAs_shouldNotSetWorkingDirectoryIfNotSelected() { } @Test - public void saveAs_shouldSetNewDatabasePathIntoContext() { + public void saveAsShouldSetNewDatabasePathIntoContext() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); @@ -73,7 +73,7 @@ public void saveAs_shouldSetNewDatabasePathIntoContext() { } @Test - public void save_shouldShowSaveAsIfDatabaseNotSelected() { + public void saveShouldShowSaveAsIfDatabaseNotSelected() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); @@ -86,7 +86,7 @@ public void save_shouldShowSaveAsIfDatabaseNotSelected() { } @Test - public void save_shouldNotSaveDatabaseIfPathNotSet() { + public void saveShouldNotSaveDatabaseIfPathNotSet() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); boolean result = saveDatabaseAction.save(); From 7071a30b9b8a84ef569cb9abc2232cadc5f92f85 Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Thu, 22 Nov 2018 14:43:03 +0600 Subject: [PATCH 10/11] fix test code style --- .../gui/exporter/SaveDatabaseActionTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java index a068c1f4a1f..5a0f284e958 100644 --- a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -1,5 +1,9 @@ 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; @@ -7,15 +11,18 @@ 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 java.io.File; -import java.nio.file.Path; -import java.util.Optional; - import static org.junit.Assert.assertFalse; -import static org.mockito.Mockito.*; +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 { From 0f6c4359fea6032f0d5acab65a759045cf9af5df Mon Sep 17 00:00:00 2001 From: Ali_Zhagparov Date: Fri, 23 Nov 2018 10:04:37 +0600 Subject: [PATCH 11/11] add SaveDatabaseActionTest in TestArchitectureTest exception --- .../java/org/jabref/architecture/TestArchitectureTests.java | 3 +++ .../java/org/jabref/gui/exporter/SaveDatabaseActionTest.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) 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 index 5a0f284e958..a0bce778175 100644 --- a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -24,7 +24,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -public class SaveDatabaseActionTest { +class SaveDatabaseActionTest { private static final String TEST_FILE_PATH = "C:\\Users\\John_Doe\\Jabref"; private final File file = new File(TEST_FILE_PATH);