-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix #2739 null pointer exception on get fulltext #2744
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
Closed
sauliusg
wants to merge
15
commits into
JabRef:master
from
sauliusg:fix-2700-null-pointer-exception-on-Get-fulltext
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fa84e4e
Adding a bunch of debug prints and assert's to find a reason for the
sauliusg 9e62873
Removing the 'HERE' debug print.
sauliusg ab8dd42
Removing the rest of my debug prints.
sauliusg cb89c73
Adding a fix for the [WIP] Clicking 'Get fulltext' triggers a 'null
sauliusg 89493e3
Attempting to create a unit test for the new fix, but so far could not
sauliusg 30afedd
Creating a working unit test for the new relative path fix, and adding
sauliusg 05cdd9a
Fixing the order of imports in the newly created test file.
sauliusg 2c6a209
Temporarily commenting out of the new fix, to see if the new tests catch
sauliusg 54b2ada
Restoring my fix in the 'BibDatabaseContext.java' file, fixing the 'null
sauliusg 9ee768c
Removing the test line that is not needed for the tests to work in
sauliusg 2deedc7
Adding explicite scope ('private') to 'FileDirectoryPreferences
sauliusg e56e68c
Fixed the coding style according to the JabRef conventions.
sauliusg bf24bbd
Switcing from 'assertTrue' to 'assertEquals' in the
sauliusg ee4e023
Changing 'assert ...path != null ...' to 'Objects.requireNonNull(...)'.
sauliusg f76ecba
Commenting variables used in the 'BibDatabaseContextTest' unit test.
sauliusg 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 hidden or 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
84 changes: 84 additions & 0 deletions
84
src/test/java/org/jabref/model/database/BibDatabaseContextTest.java
This file contains hidden or 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,84 @@ | ||
| package org.jabref.model.database; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.file.Paths; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.jabref.model.metadata.FileDirectoryPreferences; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ExpectedException; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class BibDatabaseContextTest { | ||
|
|
||
| // The 'currentWorkingDir' variable is used to re-create the part | ||
| // of the JabRef internal state that we get when the 'jabref | ||
| // biblio.bib' command is invoked from a command line (on | ||
| // Unix/Linux, but I guess on Windows clones as well). In the | ||
| // above-mentioned command, the current working directory must be | ||
| // used as a 'biblio.bib' parent directory. Since the current | ||
| // working directory is different on various computers that invoke | ||
| // the test, we can not hard-code it into the test but must | ||
| // determine it at run-time: | ||
| private String currentWorkingDir; | ||
|
|
||
| // Store the minimal preferences for the | ||
| // BibDatabaseContext.getFileDirectories(File, | ||
| // FileDirectoryPreferences) incocation: | ||
| private FileDirectoryPreferences preferences; | ||
|
|
||
| @Rule | ||
| public ExpectedException thrown = ExpectedException.none(); | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| Map<String, String> mapFieldDirs = new HashMap<>(); | ||
| preferences = new FileDirectoryPreferences("saulius", mapFieldDirs, true); | ||
| currentWorkingDir = Paths.get(System.getProperty("user.dir")).toString(); | ||
| } | ||
|
|
||
| @Test | ||
| public void getFileDirectoriesWithEmptyDbParent() { | ||
| BibDatabaseContext dbContext = new BibDatabaseContext(); | ||
| dbContext.setDatabaseFile(new File("biblio.bib")); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here also please use Paths.get().toFile() |
||
| List<String> fileDirectories = dbContext.getFileDirectories( "file", preferences ); | ||
| assertEquals(Collections.singletonList(currentWorkingDir), | ||
| fileDirectories); | ||
| } | ||
|
|
||
| @Test | ||
| public void getFileDirectoriesWithRelativeDbParent() { | ||
| String dbDirectory = "relative/subdir"; | ||
| BibDatabaseContext dbContext = new BibDatabaseContext(); | ||
| dbContext.setDatabaseFile(new File(dbDirectory + "/" + "biblio.bib")); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the methods of the Paths class. You can then just call Paths.get(dbDirectory).resolve("bib") |
||
| List<String> fileDirectories = dbContext.getFileDirectories("file", preferences); | ||
| assertEquals(Collections.singletonList(currentWorkingDir + "/" + dbDirectory), | ||
| fileDirectories); | ||
| } | ||
|
|
||
| @Test | ||
| public void getFileDirectoriesWithRelativeDottedDbParent() { | ||
| String dbDirectory = "./relative/subdir"; | ||
| BibDatabaseContext dbContext = new BibDatabaseContext(); | ||
| dbContext.setDatabaseFile(new File(dbDirectory + "/" + "biblio.bib")); | ||
| List<String> fileDirectories = dbContext.getFileDirectories("file", preferences); | ||
| assertEquals(Collections.singletonList(currentWorkingDir + "/" + dbDirectory), | ||
| fileDirectories); | ||
| } | ||
|
|
||
| @Test | ||
| public void getFileDirectoriesWithAbsoluteDbParent() { | ||
| String dbDirectory = "/absolute/subdir"; | ||
| BibDatabaseContext dbContext = new BibDatabaseContext(); | ||
| dbContext.setDatabaseFile(new File(dbDirectory + "/" + "biblio.bib")); | ||
| List<String> fileDirectories = dbContext.getFileDirectories("file", preferences); | ||
| assertEquals(Collections.singletonList(dbDirectory), fileDirectories); | ||
| } | ||
| } | ||
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.
I think it is preferable to use temporary folder rule of junit here, so that the test files are automatically deleted afterwards.
Uh oh!
There was an error while loading. Please reload this page.
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.
The directory named in the 'currentWorkingDir' value is not intended to hold any created files; actually, these tests should not create any files (I'll double check that).
The 'currentWorkingDir' variable is used to re-create the part of the JabRef internal state that we get when the 'jabref biblio.bib' command is invoked from a command line (on Unix/Linux, but I guess on Windows clones as well). Since the directory is different on various computers that invoke the test, I can not hard-code it into the test but must determine it at run-time.
Should I comment this in the code, or find a better name for the 'currentWorkingDir' variable?
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.
+1 for commenting in the code.
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.
Checked: the tests do not leave the 'biblio.bib' file behind. The File object, as I understand, holds an abstract system-independent path to a file, but does not open or create the file unless exlicitely requested.
Uh oh!
There was an error while loading. Please reload this page.
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.
Commenting the use of the variables in the test.
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.
You can influence the fileDirectoryPreferences by mocking them, otherwise you always depend on the the settings of the user and can't have a reliable value to test.
In the CleanupTests for Renaming/Moving Files I mocked the relevant options like this:
fileDirPrefs = mock(FileDirectoryPreferences.class); when(fileDirPrefs.isBibLocationAsPrimary()).thenReturn(false);Have a look at
MoveFilesCleanupTest