-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Create test #4505
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f1f23e1
create non working test depend on Globals
9960ed2
pass preferences via constructor
153e4d5
create testcase
1a3d52c
refactor test
c5b6a33
refactor code
bda6ee8
Merge branch 'master' into create-test
fd21060
add test case
c229fc8
move to exporter package
61cdf7a
fix code style
05e777b
fix test name
7071a30
fix test code style
0f6c435
add SaveDatabaseActionTest in TestArchitectureTest exception
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not Helped
There was a problem hiding this comment.
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 theSaveDatabaseActionTest
class as an exception here:https://github.com/JabRef/jabref/blob/master/src/test/java/org/jabref/architecture/TestArchitectureTests.java#L41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. fixed