Skip to content

Commit 93a997a

Browse files
authored
Respect JAVA_HOME environment variable (#69)
* Respect JAVA_HOME Closes #26 * Prioritize JAVA_HOME before java.home
1 parent 36cf9ad commit 93a997a

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
<version>${junit.version}</version>
107107
<scope>test</scope>
108108
</dependency>
109+
<dependency>
110+
<groupId>com.github.stefanbirkner</groupId>
111+
<artifactId>system-rules</artifactId>
112+
<version>1.19.0</version>
113+
<scope>test</scope>
114+
</dependency>
109115
</dependencies>
110116

111117
<build>

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.scalatest.tools.maven;
22

3-
import java.util.Collections;
4-
import java.util.List;
5-
import java.util.ArrayList;
63
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
76

87
import static org.apache.commons.lang3.StringUtils.isEmpty;
98

@@ -126,11 +125,26 @@ static String[] concat(List<String>...lists){
126125
return c.toArray(new String[c.size()]);
127126
}
128127

128+
private static String getJavaHome() {
129+
final String result;
130+
if (!isEmpty(System.getenv("JAVA_HOME"))) {
131+
result = System.getenv("JAVA_HOME");
132+
} else if (!isEmpty(System.getProperty("java.home"))) {
133+
result = System.getProperty("java.home");
134+
} else {
135+
result = null;
136+
}
137+
return result;
138+
}
139+
129140
static String getJvm() {
130-
if (!isEmpty(System.getProperty( "java.home" ))) {
131-
return System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java";
141+
final String jh = getJavaHome();
142+
final String result;
143+
if (jh == null) {
144+
result = "java";
132145
} else {
133-
return "java";
146+
result = jh + File.separator + "bin" + File.separator + "java";
134147
}
148+
return result;
135149
}
136150
}
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
package org.scalatest.tools.maven
22

3-
import org.junit.{After, Before, Test}
3+
import org.junit.contrib.java.lang.system.{ClearSystemProperties, EnvironmentVariables}
4+
import org.junit.{Rule, Test}
45

56
class MojoUtilsTest {
6-
private var savedJavaHome: Option[String] = _
7+
val _env: EnvironmentVariables = new EnvironmentVariables()
8+
val _sys: ClearSystemProperties = new ClearSystemProperties("java.home")
79

8-
@Before
9-
def save(): Unit = {
10-
savedJavaHome = Option(System.getProperty("java.home"))
11-
}
10+
@Rule
11+
def env: EnvironmentVariables = _env
1212

13-
@After
14-
def restore(): Unit = {
15-
savedJavaHome match {
16-
case None => System.clearProperty("java.home")
17-
case Some(value) => System.setProperty("java.home", value)
18-
}
19-
}
13+
@Rule
14+
def sys: ClearSystemProperties = _sys
2015

2116
@Test
2217
def getJvmHappyPath(): Unit = {
18+
env.clear("JAVA_HOME")
2319
System.setProperty("java.home", "/test/jvm")
2420
assert(MojoUtils.getJvm == "/test/jvm/bin/java")
2521
}
2622

2723
@Test
2824
def getJvmWithoutJavaHome(): Unit = {
29-
System.clearProperty("java.home")
25+
env.clear("JAVA_HOME")
3026
assert(MojoUtils.getJvm == "java")
3127
}
28+
29+
@Test
30+
def getJvmFromEnvironment(): Unit = {
31+
env.clear("JAVA_HOME")
32+
env.set("JAVA_HOME", "/opt/jdk-11")
33+
Console.print(MojoUtils.getJvm)
34+
assert(MojoUtils.getJvm == "/opt/jdk-11/bin/java")
35+
}
36+
37+
@Test
38+
def getJvmJavaHomeIsPriority(): Unit = {
39+
System.setProperty("java.home", "/test/jvm")
40+
env.set("JAVA_HOME", "/opt/jdk-11")
41+
assert(MojoUtils.getJvm == "/opt/jdk-11/bin/java")
42+
}
3243
}

stale_outputs_checked

Whitespace-only changes.

0 commit comments

Comments
 (0)