From 0c75e979b1213329d8e980ca34ae8d4b12ec61d8 Mon Sep 17 00:00:00 2001 From: Cosmin Stroe Date: Wed, 24 May 2017 23:23:49 -0500 Subject: [PATCH 1/4] Use same JVM that maven is using. --- .../tools/maven/AbstractScalaTestMojo.java | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java b/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java index b576678..855ecd9 100644 --- a/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java +++ b/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java @@ -2,31 +2,21 @@ import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.cli.CommandLineException; -import org.codehaus.plexus.util.cli.CommandLineTimeOutException; -import org.codehaus.plexus.util.cli.CommandLineUtils; -import org.codehaus.plexus.util.cli.Commandline; -import org.codehaus.plexus.util.cli.StreamConsumer; +import org.codehaus.plexus.util.cli.*; -import static org.scalatest.tools.maven.MojoUtils.*; - -import java.io.*; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.ArrayList; -import java.util.Map; - -import static java.util.Collections.singletonList; - -import java.net.MalformedURLException; -import java.net.URLClassLoader; -import java.net.URL; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.*; + +import static java.util.Collections.singletonList; +import static org.apache.commons.lang3.StringUtils.isEmpty; +import static org.scalatest.tools.maven.MojoUtils.*; /** * Provides the base for all mojos. @@ -270,7 +260,7 @@ private boolean runForkingOnce(String[] args) throws MojoFailureException { final Commandline cli = new Commandline(); cli.setWorkingDirectory(project.getBasedir()); - cli.setExecutable("java"); + cli.setExecutable(getJvm()); // Set up environment if (environmentVariables != null) { @@ -329,6 +319,19 @@ public void consumeLine(final String line) { } } + private String getJvm() + { + // use the same JVM as the one used to run Maven (the "java.home" one) + String jvmToUse = System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java"; + + if (isEmpty(jvmToUse)) { + jvmToUse = "java"; + } + + getLog().debug( "Using JVM: " + jvmToUse ); + return jvmToUse; + } + private String buildClassPathEnvironment() { StringBuffer buf = new StringBuffer(); boolean first = true; From 0c1fb6563f9e9525437d5e7248409c99df91bbca Mon Sep 17 00:00:00 2001 From: Cosmin Stroe Date: Wed, 31 May 2017 13:31:40 -0500 Subject: [PATCH 2/4] add tests around MojoUtils.getJvm --- .../tools/maven/AbstractScalaTestMojo.java | 13 -------- .../org/scalatest/tools/maven/MojoUtils.java | 11 ++++++- .../scalatest/tools/maven/MojoUtilsTest.scala | 31 +++++++++++++++++++ 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala diff --git a/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java b/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java index 855ecd9..2be8a21 100644 --- a/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java +++ b/src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java @@ -319,19 +319,6 @@ public void consumeLine(final String line) { } } - private String getJvm() - { - // use the same JVM as the one used to run Maven (the "java.home" one) - String jvmToUse = System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java"; - - if (isEmpty(jvmToUse)) { - jvmToUse = "java"; - } - - getLog().debug( "Using JVM: " + jvmToUse ); - return jvmToUse; - } - private String buildClassPathEnvironment() { StringBuffer buf = new StringBuffer(); boolean first = true; diff --git a/src/main/java/org/scalatest/tools/maven/MojoUtils.java b/src/main/java/org/scalatest/tools/maven/MojoUtils.java index 0a939a5..f85a62f 100644 --- a/src/main/java/org/scalatest/tools/maven/MojoUtils.java +++ b/src/main/java/org/scalatest/tools/maven/MojoUtils.java @@ -3,7 +3,8 @@ import java.util.List; import java.util.ArrayList; import java.io.File; -import java.io.IOException; + +import static org.apache.commons.lang3.StringUtils.isEmpty; /** * Provides internal utilities for the Mojo's operations. @@ -121,4 +122,12 @@ static String[] concat(List...lists){ } return c.toArray(new String[c.size()]); } + + static String getJvm() { + if (!isEmpty(System.getProperty( "java.home" ))) { + return System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java"; + } else { + return "java"; + } + } } diff --git a/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala b/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala new file mode 100644 index 0000000..4dea7fe --- /dev/null +++ b/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala @@ -0,0 +1,31 @@ +package org.scalatest.tools.maven + +import org.junit.{Before, Test} + +class MojoUtilsTest { + private var savedJavaHome: Option[String] = _ + + @Before + def save() = { + savedJavaHome = Option(System.getProperty("java.home")) + } + + def restore() = { + savedJavaHome match { + case None => System.clearProperty("java.home") + case Some(value) => System.setProperty("java.home", value) + } + } + + @Test + def getJvmHappyPath() = { + System.setProperty("java.home", "/test/jvm") + assert(MojoUtils.getJvm == "/test/jvm/bin/java") + } + + @Test + def getJvmWithoutJavaHome() = { + System.clearProperty("java.home") + assert(MojoUtils.getJvm == "java") + } +} From a126f764538e0c80b50e9a55bb8c8a175664656e Mon Sep 17 00:00:00 2001 From: Cosmin Stroe Date: Wed, 31 May 2017 13:37:19 -0500 Subject: [PATCH 3/4] correctly restore system property as to not affect other tests --- src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala b/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala index 4dea7fe..5767d18 100644 --- a/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala +++ b/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala @@ -1,6 +1,6 @@ package org.scalatest.tools.maven -import org.junit.{Before, Test} +import org.junit.{After, Before, Test} class MojoUtilsTest { private var savedJavaHome: Option[String] = _ @@ -10,6 +10,7 @@ class MojoUtilsTest { savedJavaHome = Option(System.getProperty("java.home")) } + @After def restore() = { savedJavaHome match { case None => System.clearProperty("java.home") From fe598ec7aabd0dd0af06718fff1bfe834ddbf4ba Mon Sep 17 00:00:00 2001 From: Cosmin Stroe Date: Wed, 31 May 2017 13:50:04 -0500 Subject: [PATCH 4/4] don't fight with the Scala type engine, mark methods as void returning --- .../scala/org/scalatest/tools/maven/MojoUtilsTest.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala b/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala index 5767d18..40b337d 100644 --- a/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala +++ b/src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala @@ -6,12 +6,12 @@ class MojoUtilsTest { private var savedJavaHome: Option[String] = _ @Before - def save() = { + def save(): Unit = { savedJavaHome = Option(System.getProperty("java.home")) } @After - def restore() = { + def restore(): Unit = { savedJavaHome match { case None => System.clearProperty("java.home") case Some(value) => System.setProperty("java.home", value) @@ -19,13 +19,13 @@ class MojoUtilsTest { } @Test - def getJvmHappyPath() = { + def getJvmHappyPath(): Unit = { System.setProperty("java.home", "/test/jvm") assert(MojoUtils.getJvm == "/test/jvm/bin/java") } @Test - def getJvmWithoutJavaHome() = { + def getJvmWithoutJavaHome(): Unit = { System.clearProperty("java.home") assert(MojoUtils.getJvm == "java") }