Skip to content

Commit

Permalink
Sauliusg fix 2700 null pointer exception on get fulltext (#2751)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Siedlerchr authored Apr 16, 2017
1 parent 15525f4 commit d489398
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/jabref/model/database/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,17 @@ public List<String> getFileDirectories(String fieldName, FileDirectoryPreference
}

// 3. preferences directory
preferences.getFileDirectory(fieldName).ifPresent(path ->
fileDirs.add(path.toAbsolutePath().toString())
);
preferences.getFileDirectory(fieldName).ifPresent(path -> fileDirs.add(path.toAbsolutePath().toString()));

// 4. BIB file directory
getDatabasePath().ifPresent(dbPath -> {
String parentDir = dbPath.getParent().toAbsolutePath().toString();
Objects.requireNonNull(dbPath, "dbPath is null");
Path parentPath = dbPath.getParent();
if (parentPath == null) {
parentPath = Paths.get(System.getProperty("user.dir"));
}
Objects.requireNonNull(parentPath, "BibTex database parent path is null");
String parentDir = parentPath.toAbsolutePath().toString();
// Check if we should add it as primary file dir (first in the list) or not:
if (preferences.isBibLocationAsPrimary()) {
fileDirs.add(0, parentDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.jabref.model.database;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

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;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BibDatabaseContextTest {

private Path currentWorkingDir;

// Store the minimal preferences for the
// BibDatabaseContext.getFileDirectories(File,
// FileDirectoryPreferences) incocation:
private FileDirectoryPreferences fileDirPrefs;

@Rule public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() {
fileDirPrefs = mock(FileDirectoryPreferences.class);
currentWorkingDir = Paths.get(System.getProperty("user.dir"));
when(fileDirPrefs.isBibLocationAsPrimary()).thenReturn(true);
}

@Test
public void getFileDirectoriesWithEmptyDbParent() {
BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(Paths.get("biblio.bib").toFile());
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
assertEquals(Collections.singletonList(currentWorkingDir.toString()),
fileDirectories);
}

@Test
public void getFileDirectoriesWithRelativeDbParent() {
Path file = Paths.get("relative/subdir").resolve("biblio.bib");

BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(file.toFile());
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()),
fileDirectories);
}

@Test
public void getFileDirectoriesWithRelativeDottedDbParent() {
Path file = Paths.get("./relative/subdir").resolve("biblio.bib");

BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(file.toFile());
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()),
fileDirectories);
}

@Test
public void getFileDirectoriesWithAbsoluteDbParent() {
Path file = Paths.get("/absolute/subdir").resolve("biblio.bib");

BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(file.toFile());
List<String> fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs);
assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()),
fileDirectories);
}
}

0 comments on commit d489398

Please sign in to comment.