Skip to content

Commit

Permalink
Add unit test for opening console
Browse files Browse the repository at this point in the history
Added Junit tests cases to verify that commands retrieve the correct database, and that the command defaults to the active database otherwise.

Tests cases added in response to a bug fix, see: JabRef#8466
  • Loading branch information
RyanEubank committed Feb 8, 2022
1 parent 89bf361 commit 40a28d2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/jabref/gui/OpenConsoleAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class OpenConsoleAction extends SimpleCommand {
private final StateManager stateManager;
private final PreferencesService preferencesService;

/**
* Creates a command that opens the console at the path of the supplied database,
* or defaults to the active database. Use
* {@link #OpenConsoleAction(StateManager, PreferencesService)} if not supplying
* another database.
*/
public OpenConsoleAction(Supplier<BibDatabaseContext> databaseContext, StateManager stateManager, PreferencesService preferencesService) {
this.databaseContext = databaseContext;
this.stateManager = stateManager;
Expand All @@ -29,10 +35,10 @@ public OpenConsoleAction(Supplier<BibDatabaseContext> databaseContext, StateMana
}

/**
* Using this constructor will result in executing the command on the active database
* Using this constructor will result in executing the command on the active database.
*/
public OpenConsoleAction(StateManager stateManager, PreferencesService preferencesService) {
this(() -> stateManager.getActiveDatabase().get(), stateManager, preferencesService);
this(() -> null, stateManager, preferencesService);
}

@Override
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/jabref/gui/util/OpenConsoleActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jabref.gui.util;

import java.util.Optional;

import org.jabref.gui.OpenConsoleAction;
import org.jabref.gui.StateManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.PreferencesService;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class OpenConsoleActionTest {

private final StateManager stateManager = mock(StateManager.class);
private final PreferencesService preferences = mock(PreferencesService.class);
private final BibDatabaseContext current = mock(BibDatabaseContext.class);
private final BibDatabaseContext other = mock(BibDatabaseContext.class);

@BeforeEach
public void setup() {
when(stateManager.activeDatabaseProperty()).thenReturn(OptionalObjectProperty.empty());
when(stateManager.getActiveDatabase()).thenReturn(Optional.of(current));
}

@Test
public void newActionGetsCurrentDatabase() {
OpenConsoleAction action = new OpenConsoleAction(stateManager, preferences);
action.execute();
verify(stateManager, times(1)).getActiveDatabase();
verify(current, times(1)).getDatabasePath();
}

@Test
public void newActionGetsSuppliedDatabase() {
OpenConsoleAction action = new OpenConsoleAction(() -> other, stateManager, preferences);
action.execute();
verify(stateManager, never()).getActiveDatabase();
verify(other, times(1)).getDatabasePath();
}

@Test
public void actionDefaultsToCurrentDatabase() {
OpenConsoleAction action = new OpenConsoleAction(() -> null, stateManager, preferences);
action.execute();
verify(stateManager, times(1)).getActiveDatabase();
verify(current, times(1)).getDatabasePath();
}

}

0 comments on commit 40a28d2

Please sign in to comment.