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

Fix NPE when importing from CLI #4281

Merged
merged 2 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,12 @@ private List<ParserResult> processArguments() {
if (cli.isFileExport()) {
if (!loaded.isEmpty()) {
exportFile(loaded, cli.getFileExport().split(","));
LOGGER.debug("Finished export");
} else {
System.err.println(Localization.lang("The output option depends on a valid import option."));
}
}

LOGGER.debug("Finished export");

if (cli.isPreferencesExport()) {
try {
Globals.prefs.exportPreferences(cli.getPreferencesExport());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/importer/ParserResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public String getErrorMessage() {
}

public BibDatabaseContext getDatabaseContext() {
return new BibDatabaseContext(database, metaData, file.toFile());
return new BibDatabaseContext(database, metaData, file);
}

public void setDatabaseContext(BibDatabaseContext bibDatabaseContext) {
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/org/jabref/model/database/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BibDatabaseContext {
/**
* The file where this database was last saved to.
*/
private File file;
private Optional<Path> file;
private DatabaseSynchronizer dbmsSynchronizer;
private CoarseChangeFilter dbmsListener;
private DatabaseLocation location;
Expand All @@ -57,28 +57,29 @@ public BibDatabaseContext(BibDatabase database, MetaData metaData, Defaults defa
this.database = Objects.requireNonNull(database);
this.metaData = Objects.requireNonNull(metaData);
this.location = DatabaseLocation.LOCAL;
this.file = Optional.empty();
}

public BibDatabaseContext(BibDatabase database, MetaData metaData) {
this(database, metaData, new Defaults());
}

public BibDatabaseContext(BibDatabase database, MetaData metaData, File file, Defaults defaults,
public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file, Defaults defaults,
DatabaseLocation location) {
this(database, metaData, defaults);
Objects.requireNonNull(location);
this.setDatabaseFile(file);
this.file = Optional.ofNullable(file);

if (location == DatabaseLocation.LOCAL) {
convertToLocalDatabase();
}
}

public BibDatabaseContext(BibDatabase database, MetaData metaData, File file, Defaults defaults) {
public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file, Defaults defaults) {
this(database, metaData, file, defaults, DatabaseLocation.LOCAL);
}

public BibDatabaseContext(BibDatabase database, MetaData metaData, File file) {
public BibDatabaseContext(BibDatabase database, MetaData metaData, Path file) {
this(database, metaData, file, new Defaults());
}

Expand Down Expand Up @@ -109,19 +110,19 @@ public void setMode(BibDatabaseMode bibDatabaseMode) {
*/
@Deprecated
public Optional<File> getDatabaseFile() {
return Optional.ofNullable(file);
return file.map(Path::toFile);
}

public void setDatabaseFile(File file) {
this.file = file;
this.file = Optional.ofNullable(file).map(File::toPath);
}

public Optional<Path> getDatabasePath() {
return Optional.ofNullable(file).map(File::toPath);
return file;
}

public void clearDatabaseFile() {
this.file = null;
this.file = Optional.empty();
}

public BibDatabase getDatabase() {
Expand Down Expand Up @@ -188,16 +189,12 @@ public List<String> getFileDirectories(String fieldName, FileDirectoryPreference
List<String> fileDirs = new ArrayList<>();

// 1. metadata user-specific directory
Optional<String> userFileDirectory = metaData.getUserFileDirectory(preferences.getUser());
if (userFileDirectory.isPresent()) {
fileDirs.add(getFileDirectoryPath(userFileDirectory.get()));
}
metaData.getUserFileDirectory(preferences.getUser())
.ifPresent(userFileDirectory -> fileDirs.add(getFileDirectoryPath(userFileDirectory)));

// 2. metadata general directory
Optional<String> metaDataDirectory = metaData.getDefaultFileDirectory();
if (metaDataDirectory.isPresent()) {
fileDirs.add(getFileDirectoryPath(metaDataDirectory.get()));
}
metaData.getDefaultFileDirectory()
.ifPresent(metaDataDirectory -> fileDirs.add(getFileDirectoryPath(metaDataDirectory)));

// 3. preferences directory
preferences.getFileDirectory(fieldName).ifPresent(path -> fileDirs.add(path.toAbsolutePath().toString()));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/model/database/BibDatabases.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static BibDatabase createDatabase(Collection<BibEntry> bibentries) {
* an unsucessful import (wrong format) that returns a number of empty entries.
*/
public static List<BibEntry> purgeEmptyEntries(Collection<BibEntry> entries) {
return entries.stream().filter(e -> !e.getFieldNames().isEmpty()).collect(Collectors.toList());
return entries.stream()
.filter(entry -> !entry.getFieldNames().isEmpty())
.collect(Collectors.toList());
}
}
19 changes: 14 additions & 5 deletions src/test/java/org/jabref/cli/JabRefCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JabRefCLITest {
class JabRefCLITest {

@Test
public void testCLIParsingLongOptions() {
void parsingLongOptions() {
JabRefCLI cli = new JabRefCLI(new String[] {"--nogui", "--import=some/file", "--output=some/export/file"});

assertEquals(Collections.emptyList(), cli.getLeftOver());
Expand All @@ -20,7 +20,7 @@ public void testCLIParsingLongOptions() {
}

@Test
public void testCLIParsingShortOptions() {
void parsingShortOptions() {
JabRefCLI cli = new JabRefCLI(new String[] {"-n", "-i=some/file", "-o=some/export/file"});

assertEquals(Collections.emptyList(), cli.getLeftOver());
Expand All @@ -30,7 +30,7 @@ public void testCLIParsingShortOptions() {
}

@Test
public void testPreferencesExport() {
void preferencesExport() {
JabRefCLI cli = new JabRefCLI(new String[] {"-n", "-x=some/file"});

assertEquals(Collections.emptyList(), cli.getLeftOver());
Expand All @@ -39,11 +39,20 @@ public void testPreferencesExport() {
}

@Test
public void recognizesImportBibtex() {
void recognizesImportBibtex() {
String bibtex = "@article{test, title=\"test title\"}";
JabRefCLI cli = new JabRefCLI(new String[]{"-ib", bibtex});
assertEquals(Collections.emptyList(), cli.getLeftOver());
assertTrue(cli.isBibtexImport());
assertEquals(bibtex, cli.getBibtexImport());
}

@Test
void recognizesImportBibtexLong() {
String bibtex = "@article{test, title=\"test title\"}";
JabRefCLI cli = new JabRefCLI(new String[]{"-importBibtex", bibtex});
assertEquals(Collections.emptyList(), cli.getLeftOver());
assertTrue(cli.isBibtexImport());
assertEquals(bibtex, cli.getBibtexImport());
}
}