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 cases in FileDialog Configuration #2918

Merged
merged 3 commits into from
Jun 17, 2017
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: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed a bug that did not allow the correct re-export of the MS-Office XML field `msbib-accessed` with a different date format [#2859](https://github.com/JabRef/jabref/issues/2859).
- We fixed some bugs that prevented the display of FileAnnotations that were created using the Foxit Reader. [#2839, comment](https://github.com/JabRef/jabref/issues/2839#issuecomment-302058227).
- We fixed an error that prevented the FileAnnotation tab to load when the entry had no bibtexkey [#2903](https://github.com/JabRef/jabref/issues/2903).
- We fixed a bug that recursive RegExpBased search found a file in a subdirectory multiple times and non-recursive RegExpBased search erroneously found files in subdirectories.
- We fixed a bug which which could result in an exception when opening/saving files from/to a nonexistent directory [#2917](https://github.com/JabRef/jabref/issues/2917).
- We fixed a bug where recursive RegExpBased search found a file in a subdirectory multiple times and non-recursive RegExpBased search erroneously found files in subdirectories.
### Removed


Expand Down
30 changes: 18 additions & 12 deletions src/main/java/org/jabref/gui/util/FileDialogConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public List<FileChooser.ExtensionFilter> getExtensionFilters() {

public static class Builder {

List<FileChooser.ExtensionFilter> extensionFilter = new ArrayList<>();
private final List<FileChooser.ExtensionFilter> extensionFilter = new ArrayList<>();
private Path initialDirectory;
private FileChooser.ExtensionFilter defaultExtension;
private String initialFileName;
Expand All @@ -72,22 +72,28 @@ public FileDialogConfiguration build() {
}

public Builder withInitialDirectory(Path directory) {

//Dir must be a folder, not a file
if (!Files.isDirectory(directory)) {
directory = directory.getParent();
}
//The lines above work also if the dir does not exist at all!
//NULL is accepted by the filechooser as no inital path
if (!Files.exists(directory)) {
directory = null;
if (directory == null) { //It could be that somehow the path is null, for example if it got deleted in the meantime
initialDirectory = null;
} else { //Dir must be a folder, not a file
if (!Files.isDirectory(directory)) {
directory = directory.getParent();
}
//The lines above work also if the dir does not exist at all!
//NULL is accepted by the filechooser as no inital path
if (!Files.exists(directory)) {
directory = null;
}
initialDirectory = directory;
}
initialDirectory = directory;
return this;
}

public Builder withInitialDirectory(String directory) {
withInitialDirectory(Paths.get(directory));
if (directory != null) {
withInitialDirectory(Paths.get(directory));
} else {
initialDirectory = null;
}
return this;
}

Expand Down
93 changes: 93 additions & 0 deletions src/test/java/org/jabref/gui/util/FileDialogConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.jabref.gui.util;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javafx.stage.FileChooser;

import org.jabref.logic.util.FileExtensions;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.assertEquals;

public class FileDialogConfigurationTest {

@Rule public TemporaryFolder folder = new TemporaryFolder();

@Test
public void testWithValidDirectoryString() throws IOException {
String tempFolder = folder.newFolder().toString();

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(tempFolder).build();

assertEquals(Optional.of(Paths.get(tempFolder)), fileDialogConfiguration.getInitialDirectory());
}

@Test
public void testWithValidDirectoryPath() throws IOException {
Path tempFolder = folder.newFolder().toPath();

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(tempFolder).build();

assertEquals(Optional.of(tempFolder), fileDialogConfiguration.getInitialDirectory());
}

@Test
public void testWithNullStringDirectory() {
String tempFolder = null;
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(tempFolder).build();

assertEquals(Optional.ofNullable(tempFolder), fileDialogConfiguration.getInitialDirectory());
}

@Test
public void testWithNullPathDirectory() {
Path tempFolder = null;
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withInitialDirectory(tempFolder).build();

assertEquals(Optional.ofNullable(tempFolder), fileDialogConfiguration.getInitialDirectory());
}

@Test
public void testSingleExtension() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withDefaultExtension(FileExtensions.BIBTEX_DB).build();

FileChooser.ExtensionFilter filter = toFilter(FileExtensions.BIBTEX_DB);

assertEquals(filter.getExtensions(), fileDialogConfiguration.getDefaultExtension().getExtensions());
}

@Test
public void testMultipleExtension() {
EnumSet<FileExtensions> extensions = EnumSet.allOf(FileExtensions.class);

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilters(extensions).build();

List<FileChooser.ExtensionFilter> extensionFilters = extensions.stream().map(this::toFilter)
.collect(Collectors.toList());

//We use size here as we otherwise would compare object references, as extension filters does not override equals
assertEquals(extensionFilters.size(), fileDialogConfiguration.getExtensionFilters().size());

}

private FileChooser.ExtensionFilter toFilter(FileExtensions extension) {
return new FileChooser.ExtensionFilter(extension.getDescription(),
extension.getExtensionsAsList().stream().map(ending -> "*." + ending).collect(Collectors.toList()));
}

}