Skip to content

Commit 0c1fb65

Browse files
committed
add tests around MojoUtils.getJvm
1 parent 0c75e97 commit 0c1fb65

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,6 @@ public void consumeLine(final String line) {
319319
}
320320
}
321321

322-
private String getJvm()
323-
{
324-
// use the same JVM as the one used to run Maven (the "java.home" one)
325-
String jvmToUse = System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java";
326-
327-
if (isEmpty(jvmToUse)) {
328-
jvmToUse = "java";
329-
}
330-
331-
getLog().debug( "Using JVM: " + jvmToUse );
332-
return jvmToUse;
333-
}
334-
335322
private String buildClassPathEnvironment() {
336323
StringBuffer buf = new StringBuffer();
337324
boolean first = true;

src/main/java/org/scalatest/tools/maven/MojoUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.List;
44
import java.util.ArrayList;
55
import java.io.File;
6-
import java.io.IOException;
6+
7+
import static org.apache.commons.lang3.StringUtils.isEmpty;
78

89
/**
910
* Provides internal utilities for the Mojo's operations.
@@ -121,4 +122,12 @@ static String[] concat(List<String>...lists){
121122
}
122123
return c.toArray(new String[c.size()]);
123124
}
125+
126+
static String getJvm() {
127+
if (!isEmpty(System.getProperty( "java.home" ))) {
128+
return System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java";
129+
} else {
130+
return "java";
131+
}
132+
}
124133
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.scalatest.tools.maven
2+
3+
import org.junit.{Before, Test}
4+
5+
class MojoUtilsTest {
6+
private var savedJavaHome: Option[String] = _
7+
8+
@Before
9+
def save() = {
10+
savedJavaHome = Option(System.getProperty("java.home"))
11+
}
12+
13+
def restore() = {
14+
savedJavaHome match {
15+
case None => System.clearProperty("java.home")
16+
case Some(value) => System.setProperty("java.home", value)
17+
}
18+
}
19+
20+
@Test
21+
def getJvmHappyPath() = {
22+
System.setProperty("java.home", "/test/jvm")
23+
assert(MojoUtils.getJvm == "/test/jvm/bin/java")
24+
}
25+
26+
@Test
27+
def getJvmWithoutJavaHome() = {
28+
System.clearProperty("java.home")
29+
assert(MojoUtils.getJvm == "java")
30+
}
31+
}

0 commit comments

Comments
 (0)