Skip to content

Commit

Permalink
Create test (#4518)
Browse files Browse the repository at this point in the history
* refactor and create test for SaveAllAction

* refactor code

* refactor code SaveAllAction

* change test name

* use Paths.get instead of new File
  • Loading branch information
Ali96kz authored and Siedlerchr committed Nov 29, 2018
1 parent 89d4d74 commit 37be334
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/main/java/org/jabref/gui/exporter/SaveAllAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.exporter;

import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.actions.Actions;
import org.jabref.gui.actions.SimpleCommand;
Expand All @@ -9,23 +10,27 @@
public class SaveAllAction extends SimpleCommand {

private final JabRefFrame frame;
private final DialogService dialogService;

public SaveAllAction(JabRefFrame frame) {
this.frame = frame;
this.dialogService = frame.getDialogService();
}

@Override
public void execute() {
frame.output(Localization.lang("Saving all libraries..."));
dialogService.notify(Localization.lang("Saving all libraries..."));

for (BasePanel panel : frame.getBasePanelList()) {
if (!panel.getBibDatabaseContext().getDatabasePath().isPresent()) {
frame.showBasePanel(panel);

// TODO: Ask for path
//It will ask a path before saving.
panel.runCommand(Actions.SAVE_AS);
} else {
panel.runCommand(Actions.SAVE);
// TODO: can we find out whether the save was actually done or not?
}
panel.runCommand(Actions.SAVE);
// TODO: can we find out whether the save was actually done or not?
}
frame.output(Localization.lang("Save all finished."));

dialogService.notify(Localization.lang("Save all finished."));
}
}
77 changes: 77 additions & 0 deletions src/test/java/org/jabref/gui/exporter/SaveAllActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.jabref.gui.exporter;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;

import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.actions.Actions;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;

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

import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class SaveAllActionTest {

private BasePanel firstPanel = mock(BasePanel.class);
private BasePanel secondPanel = mock(BasePanel.class);
private JabRefFrame jabRefFrame = mock(JabRefFrame.class);
private DialogService dialogService = mock(DialogService.class);
private BibDatabaseContext bibDatabaseContext = mock(BibDatabaseContext.class);
private Optional<Path> databasePath = Optional.of(Paths.get("C:\\Users\\John_Doe\\Jabref"));
private SaveAllAction saveAllAction;

@Before
public void setUp() {
when(firstPanel.getBibDatabaseContext()).thenReturn(bibDatabaseContext);
when(secondPanel.getBibDatabaseContext()).thenReturn(bibDatabaseContext);
when(bibDatabaseContext.getDatabasePath()).thenReturn(databasePath);

when(jabRefFrame.getBasePanelList()).thenReturn(Arrays.asList(firstPanel, secondPanel));
when(jabRefFrame.getDialogService()).thenReturn(dialogService);

saveAllAction = new SaveAllAction(jabRefFrame);
}

@Test
public void executeShouldRunSaveCommandInEveryPanel() {
doNothing().when(dialogService).notify(anyString());

saveAllAction.execute();

verify(firstPanel, times(1)).runCommand(Actions.SAVE);
verify(secondPanel, times(1)).runCommand(Actions.SAVE);
}

@Test
public void executeShouldNotifyAboutSavingProcess() {
when(bibDatabaseContext.getDatabasePath()).thenReturn(databasePath);

saveAllAction.execute();

verify(dialogService, times(1)).notify(Localization.lang("Saving all libraries..."));
verify(dialogService, times(1)).notify(Localization.lang("Save all finished."));
}

@Test
public void executeShouldShowSaveAsWindowIfDatabaseNotSelected() {
when(bibDatabaseContext.getDatabasePath()).thenReturn(Optional.empty());
doNothing().when(dialogService).notify(anyString());

saveAllAction.execute();

verify(firstPanel, times(1)).runCommand(Actions.SAVE_AS);
verify(secondPanel, times(1)).runCommand(Actions.SAVE_AS);
}
}

0 comments on commit 37be334

Please sign in to comment.