Skip to content

Commit 19c8f53

Browse files
sauliusgkoppor
authored andcommitted
Sauliusg fix 2700 null pointer exception on get fulltext (#2751)
* Adding a bunch of debug prints and assert's to find a reason for the 'null pointer exception'. * Removing the 'HERE' debug print. * Removing the rest of my debug prints. * Adding a fix for the [WIP] Clicking 'Get fulltext' triggers a 'null pointer exception' when a databse is opened in a current directory. * Attempting to create a unit test for the new fix, but so far could not re-create the necessary run environment. * Creating a working unit test for the new relative path fix, and adding also three regression tests for the old functionality. * Fixing the order of imports in the newly created test file. * Temporarily commenting out of the new fix, to see if the new tests catch the previous bug. Indeed, the new unit test fails both in Eclipse and in the CLI './gradlew build' run. * Restoring my fix in the 'BibDatabaseContext.java' file, fixing the 'null pointer exception' if a database is passed as a local file without explicite path. The tests in 'BibDatabaseContextTest.java' pass again. * Removing the test line that is not needed for the tests to work in 'BibDatabaseContextTest'. * Adding explicite scope ('private') to 'FileDirectoryPreferences preferences'. * Fixed the coding style according to the JabRef conventions. * Switcing from 'assertTrue' to 'assertEquals' in the BibDatabaseContextTest unit test. * Changing 'assert ...path != null ...' to 'Objects.requireNonNull(...)'. * Commenting variables used in the 'BibDatabaseContextTest' unit test. * Fix paths handling in test and mock fileDirPrefs * Fix modernizer
1 parent 9d60b21 commit 19c8f53

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/main/java/org/jabref/model/database/BibDatabaseContext.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,17 @@ public List<String> getFileDirectories(String fieldName, FileDirectoryPreference
192192
}
193193

194194
// 3. preferences directory
195-
preferences.getFileDirectory(fieldName).ifPresent(path ->
196-
fileDirs.add(path.toAbsolutePath().toString())
197-
);
195+
preferences.getFileDirectory(fieldName).ifPresent(path -> fileDirs.add(path.toAbsolutePath().toString()));
198196

199197
// 4. BIB file directory
200198
getDatabasePath().ifPresent(dbPath -> {
201-
String parentDir = dbPath.getParent().toAbsolutePath().toString();
199+
Objects.requireNonNull(dbPath, "dbPath is null");
200+
Path parentPath = dbPath.getParent();
201+
if (parentPath == null) {
202+
parentPath = Paths.get(System.getProperty("user.dir"));
203+
}
204+
Objects.requireNonNull(parentPath, "BibTex database parent path is null");
205+
String parentDir = parentPath.toAbsolutePath().toString();
202206
// Check if we should add it as primary file dir (first in the list) or not:
203207
if (preferences.isBibLocationAsPrimary()) {
204208
fileDirs.add(0, parentDir);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.jabref.model.database;
2+
3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import org.jabref.model.metadata.FileDirectoryPreferences;
9+
10+
import org.junit.Before;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.rules.ExpectedException;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.when;
18+
19+
public class BibDatabaseContextTest {
20+
21+
private Path currentWorkingDir;
22+
23+
// Store the minimal preferences for the
24+
// BibDatabaseContext.getFileDirectories(File,
25+
// FileDirectoryPreferences) incocation:
26+
private FileDirectoryPreferences fileDirPrefs;
27+
28+
@Rule public ExpectedException thrown = ExpectedException.none();
29+
30+
@Before
31+
public void setUp() {
32+
fileDirPrefs = mock(FileDirectoryPreferences.class);
33+
currentWorkingDir = Paths.get(System.getProperty("user.dir"));
34+
when(fileDirPrefs.isBibLocationAsPrimary()).thenReturn(true);
35+
}
36+
37+
@Test
38+
public void getFileDirectoriesWithEmptyDbParent() {
39+
BibDatabaseContext dbContext = new BibDatabaseContext();
40+
dbContext.setDatabaseFile(Paths.get("biblio.bib").toFile());
41+
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
42+
assertEquals(Collections.singletonList(currentWorkingDir.toString()),
43+
fileDirectories);
44+
}
45+
46+
@Test
47+
public void getFileDirectoriesWithRelativeDbParent() {
48+
Path file = Paths.get("relative/subdir").resolve("biblio.bib");
49+
50+
BibDatabaseContext dbContext = new BibDatabaseContext();
51+
dbContext.setDatabaseFile(file.toFile());
52+
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
53+
assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()),
54+
fileDirectories);
55+
}
56+
57+
@Test
58+
public void getFileDirectoriesWithRelativeDottedDbParent() {
59+
Path file = Paths.get("./relative/subdir").resolve("biblio.bib");
60+
61+
BibDatabaseContext dbContext = new BibDatabaseContext();
62+
dbContext.setDatabaseFile(file.toFile());
63+
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
64+
assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()),
65+
fileDirectories);
66+
}
67+
68+
@Test
69+
public void getFileDirectoriesWithAbsoluteDbParent() {
70+
Path file = Paths.get("/absolute/subdir").resolve("biblio.bib");
71+
72+
BibDatabaseContext dbContext = new BibDatabaseContext();
73+
dbContext.setDatabaseFile(file.toFile());
74+
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
75+
assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()),
76+
fileDirectories);
77+
}
78+
}

0 commit comments

Comments
 (0)