Skip to content
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

Sauliusg fix 2700 null pointer exception on get fulltext #2751

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 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 Apr 14, 2017
9e62873
Removing the 'HERE' debug print.
sauliusg Apr 14, 2017
ab8dd42
Removing the rest of my debug prints.
sauliusg Apr 14, 2017
cb89c73
Adding a fix for the [WIP] Clicking 'Get fulltext' triggers a 'null
sauliusg Apr 14, 2017
89493e3
Attempting to create a unit test for the new fix, but so far could not
sauliusg Apr 14, 2017
30afedd
Creating a working unit test for the new relative path fix, and adding
sauliusg Apr 15, 2017
05cdd9a
Fixing the order of imports in the newly created test file.
sauliusg Apr 15, 2017
2c6a209
Temporarily commenting out of the new fix, to see if the new tests catch
sauliusg Apr 15, 2017
54b2ada
Restoring my fix in the 'BibDatabaseContext.java' file, fixing the 'null
sauliusg Apr 15, 2017
9ee768c
Removing the test line that is not needed for the tests to work in
sauliusg Apr 15, 2017
2deedc7
Adding explicite scope ('private') to 'FileDirectoryPreferences
sauliusg Apr 16, 2017
e56e68c
Fixed the coding style according to the JabRef conventions.
sauliusg Apr 16, 2017
bf24bbd
Switcing from 'assertTrue' to 'assertEquals' in the
sauliusg Apr 16, 2017
ee4e023
Changing 'assert ...path != null ...' to 'Objects.requireNonNull(...)'.
sauliusg Apr 16, 2017
f76ecba
Commenting variables used in the 'BibDatabaseContextTest' unit test.
sauliusg Apr 16, 2017
f423df8
Merge branch 'fix-2700-null-pointer-exception-on-Get-fulltext' of htt…
Siedlerchr Apr 16, 2017
95506f2
Fix paths handling in test and mock fileDirPrefs
Siedlerchr Apr 16, 2017
5891114
Fix modernizer
Siedlerchr Apr 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}