From c654390bec2e76233041311ad1694c2f3ff486f5 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Tue, 7 Mar 2017 11:09:06 +0100 Subject: [PATCH 1/4] Fix a well hidden bug throwing a NPE if Spoon is launched in an empty working directory --- .../compiler/jdt/JDTBasedSpoonCompiler.java | 2 +- src/test/java/spoon/LauncherTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java index fe4125ed85e..38f818e1217 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java @@ -392,7 +392,7 @@ protected CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFo .classpathOptions(new ClasspathOptions().encoding(this.encoding).classpath(classpath)) // .complianceOptions(new ComplianceOptions().compliance(javaCompliance)) // .advancedOptions(new AdvancedOptions().preserveUnusedVars().continueExecution().enableJavadoc()) // - .sources(new SourceOptions().sources()) // no sources, handled by the JDTBatchCompiler + .sources(new SourceOptions().sources(sourcesFolder.getFiles())) // no sources, handled by the JDTBatchCompiler .build(); } else { args = jdtBuilder.build(); diff --git a/src/test/java/spoon/LauncherTest.java b/src/test/java/spoon/LauncherTest.java index 05077104a37..c724e3c26a4 100644 --- a/src/test/java/spoon/LauncherTest.java +++ b/src/test/java/spoon/LauncherTest.java @@ -8,6 +8,8 @@ import spoon.support.JavaOutputProcessor; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -68,4 +70,20 @@ public void testInitEnvironment() throws Exception { } + /** + * This test should be launched after setting an empty working directory + * @throws Exception + */ + @Test + public void testLauncherInEmptyWorkingDir() throws Exception { + Path path = Files.createTempDirectory("spoon-empty"); + + String workingDir = System.getProperty("user.dir"); + System.setProperty("user.dir", path.toFile().getAbsolutePath()); + + final Launcher launcher = new Launcher(); + launcher.addInputResource("/Users/urli/Github/spoon/src/test/java/spoon/LauncherTest.java"); + launcher.buildModel(); + } + } \ No newline at end of file From 9586f004473bef53e22040ee1dce46ed8d89ff26 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Tue, 7 Mar 2017 11:19:01 +0100 Subject: [PATCH 2/4] Fix the bug by giving only java files and not all files in sourceOptions --- .../spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java | 2 +- src/test/java/spoon/LauncherTest.java | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java index 38f818e1217..26e8f182d9e 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java @@ -392,7 +392,7 @@ protected CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFo .classpathOptions(new ClasspathOptions().encoding(this.encoding).classpath(classpath)) // .complianceOptions(new ComplianceOptions().compliance(javaCompliance)) // .advancedOptions(new AdvancedOptions().preserveUnusedVars().continueExecution().enableJavadoc()) // - .sources(new SourceOptions().sources(sourcesFolder.getFiles())) // no sources, handled by the JDTBatchCompiler + .sources(new SourceOptions().sources(sourcesFolder.getAllJavaFiles())) // no sources, handled by the JDTBatchCompiler .build(); } else { args = jdtBuilder.build(); diff --git a/src/test/java/spoon/LauncherTest.java b/src/test/java/spoon/LauncherTest.java index c724e3c26a4..e0b542d238b 100644 --- a/src/test/java/spoon/LauncherTest.java +++ b/src/test/java/spoon/LauncherTest.java @@ -76,11 +76,6 @@ public void testInitEnvironment() throws Exception { */ @Test public void testLauncherInEmptyWorkingDir() throws Exception { - Path path = Files.createTempDirectory("spoon-empty"); - - String workingDir = System.getProperty("user.dir"); - System.setProperty("user.dir", path.toFile().getAbsolutePath()); - final Launcher launcher = new Launcher(); launcher.addInputResource("/Users/urli/Github/spoon/src/test/java/spoon/LauncherTest.java"); launcher.buildModel(); From b414f81a95b02c5f594936132de91a44f4e578bd Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Tue, 7 Mar 2017 11:23:43 +0100 Subject: [PATCH 3/4] Fix absolute path in test --- src/test/java/spoon/LauncherTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/spoon/LauncherTest.java b/src/test/java/spoon/LauncherTest.java index e0b542d238b..cf10d572ece 100644 --- a/src/test/java/spoon/LauncherTest.java +++ b/src/test/java/spoon/LauncherTest.java @@ -1,6 +1,7 @@ package spoon; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import spoon.compiler.Environment; @@ -75,9 +76,10 @@ public void testInitEnvironment() throws Exception { * @throws Exception */ @Test + @Ignore public void testLauncherInEmptyWorkingDir() throws Exception { final Launcher launcher = new Launcher(); - launcher.addInputResource("/Users/urli/Github/spoon/src/test/java/spoon/LauncherTest.java"); + launcher.addInputResource("/set/absolute/path/spoon/src/test/java/spoon/LauncherTest.java"); launcher.buildModel(); } From eaa4c88f9f588bc16cdd2d6d5ff60b420859a9be Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Tue, 7 Mar 2017 15:26:10 +0100 Subject: [PATCH 4/4] Explicit the test --- src/test/java/spoon/LauncherTest.java | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/test/java/spoon/LauncherTest.java b/src/test/java/spoon/LauncherTest.java index cf10d572ece..3c387929b45 100644 --- a/src/test/java/spoon/LauncherTest.java +++ b/src/test/java/spoon/LauncherTest.java @@ -71,16 +71,25 @@ public void testInitEnvironment() throws Exception { } - /** - * This test should be launched after setting an empty working directory - * @throws Exception - */ @Test - @Ignore public void testLauncherInEmptyWorkingDir() throws Exception { + + // Contract: Spoon can be launched in an empty folder as a working directory + // See: https://github.com/INRIA/spoon/pull/1208 + // This test does not fail (it's not enough to change user.dir we should launch process inside that dir) but it explains the problem final Launcher launcher = new Launcher(); - launcher.addInputResource("/set/absolute/path/spoon/src/test/java/spoon/LauncherTest.java"); - launcher.buildModel(); + Path path = Files.createTempDirectory("emptydir"); + + String oldUserDir = System.getProperty("user.dir"); + System.setProperty("user.dir", path.toFile().getAbsolutePath()); + + // path should exist, otherwise it would crash on a filenotfoundexception before showing the bug + launcher.addInputResource(oldUserDir+"/src/test/java/spoon/LauncherTest.java"); + try { + launcher.buildModel(); + } finally { + System.setProperty("user.dir", oldUserDir); + } } } \ No newline at end of file