From b81e4c4ef36db53c2b10d09c3b674972edd75c82 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Mon, 13 Nov 2017 12:57:54 +0100 Subject: [PATCH 1/2] Add a new test and fix it #1716 --- src/main/java/spoon/MavenLauncher.java | 21 +++++++++++-------- src/test/java/spoon/MavenLauncherTest.java | 6 ++++++ .../maven-launcher/very-simple/pom.xml | 10 +++++++++ .../very-simple/src/main/java/Test.java | 3 +++ 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/test/resources/maven-launcher/very-simple/pom.xml create mode 100644 src/test/resources/maven-launcher/very-simple/src/main/java/Test.java diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index bb7dc81101a..2c4d32c334b 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -341,17 +341,20 @@ private String getProperty(String key) { * @return the source version of the project */ public int getSourceVersion() { - for (Plugin plugin : model.getBuild().getPlugins()) { - if (!"maven-compiler-plugin".equals(plugin.getArtifactId())) { - continue; - } - Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration(); - Xpp3Dom source = configuration.getChild("source"); - if (source != null) { - return Integer.parseInt(extractVariable(source.getValue()).substring(2)); + if (model.getBuild() != null) { + for (Plugin plugin : model.getBuild().getPlugins()) { + if (!"maven-compiler-plugin".equals(plugin.getArtifactId())) { + continue; + } + Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration(); + Xpp3Dom source = configuration.getChild("source"); + if (source != null) { + return Integer.parseInt(extractVariable(source.getValue()).substring(2)); + } + break; } - break; } + String javaVersion = getProperty("java.version"); if (javaVersion != null) { return Integer.parseInt(extractVariable(javaVersion).substring(2)); diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 45f9510392c..5c0009120fb 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -41,4 +41,10 @@ public void mavenLauncherOnANotExistingFileTest() { public void mavenLauncherOnDirectoryWithoutPomTest() { new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.APP_SOURCE); } + + @Test + public void mavenLauncherTestWithVerySimpleProject() { + MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/very-simple", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); + assertEquals(1, launcher.getModelBuilder().getInputSources().size()); + } } diff --git a/src/test/resources/maven-launcher/very-simple/pom.xml b/src/test/resources/maven-launcher/very-simple/pom.xml new file mode 100644 index 00000000000..a5375c56d22 --- /dev/null +++ b/src/test/resources/maven-launcher/very-simple/pom.xml @@ -0,0 +1,10 @@ + + + 4.0.0 + + org.foo.bar + foobar + 1.0.0-SNAPSHOT + jar + foobar + diff --git a/src/test/resources/maven-launcher/very-simple/src/main/java/Test.java b/src/test/resources/maven-launcher/very-simple/src/main/java/Test.java new file mode 100644 index 00000000000..f8f032329bc --- /dev/null +++ b/src/test/resources/maven-launcher/very-simple/src/main/java/Test.java @@ -0,0 +1,3 @@ +public class Test { + int field = 42; +} \ No newline at end of file From 66027d4a5e4f1d417a7451505725cb78e35cc350 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Mon, 13 Nov 2017 13:18:09 +0100 Subject: [PATCH 2/2] Fix checkstyle --- src/main/java/spoon/MavenLauncher.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 2c4d32c334b..55b1c299964 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -354,7 +354,6 @@ public int getSourceVersion() { break; } } - String javaVersion = getProperty("java.version"); if (javaVersion != null) { return Integer.parseInt(extractVariable(javaVersion).substring(2));