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 Spoon launch with a wrong path for input resource #1057

Merged
merged 16 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from 15 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
8 changes: 5 additions & 3 deletions src/main/java/spoon/support/compiler/FileSystemFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public FileSystemFile(File file) {

public InputStream getContent() {
try {
if (!this.file.exists()) {
throw new FileNotFoundException("The following file does not exist: " + this.file.getCanonicalPath());
}
return new FileInputStream(file);
} catch (FileNotFoundException e) {
Launcher.LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
throw new SpoonException(e);
}
return null;
}

public String getName() {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/api/FileSystemFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;

import org.junit.rules.ExpectedException;
import spoon.Launcher;
import spoon.LauncherTest;
import spoon.SpoonException;
import spoon.compiler.SpoonFolder;
import spoon.support.compiler.FileSystemFolder;

Expand All @@ -19,4 +25,17 @@ public void jarFileIsNotSubfolder() {
List<SpoonFolder> subFolders = folder.getSubFolders();
assertTrue(subFolders.isEmpty());
}

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

@Test
public void testLauncherWithWrongPathAsInput() {
expectedEx.expect(SpoonException.class);
expectedEx.expectMessage("java.io.FileNotFoundException");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to use a try catch and make the assert in catch


Launcher spoon = new Launcher();
spoon.addInputResource("./src/wrong/direction/File.java");
spoon.buildModel();
}
}