Skip to content

Respect JAVA_HOME environment variable #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/org/scalatest/tools/maven/MojoUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.scalatest.tools.maven;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

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

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

private static String getJavaHome() {
final String result;
if (!isEmpty(System.getenv("JAVA_HOME"))) {
result = System.getenv("JAVA_HOME");
} else if (!isEmpty(System.getProperty("java.home"))) {
result = System.getProperty("java.home");
} else {
result = null;
}
return result;
}

static String getJvm() {
if (!isEmpty(System.getProperty( "java.home" ))) {
return System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java";
final String jh = getJavaHome();
final String result;
if (jh == null) {
result = "java";
} else {
return "java";
result = jh + File.separator + "bin" + File.separator + "java";
}
return result;
}
}
39 changes: 25 additions & 14 deletions src/test/scala/org/scalatest/tools/maven/MojoUtilsTest.scala
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
package org.scalatest.tools.maven

import org.junit.{After, Before, Test}
import org.junit.contrib.java.lang.system.{ClearSystemProperties, EnvironmentVariables}
import org.junit.{Rule, Test}

class MojoUtilsTest {
private var savedJavaHome: Option[String] = _
val _env: EnvironmentVariables = new EnvironmentVariables()
val _sys: ClearSystemProperties = new ClearSystemProperties("java.home")

@Before
def save(): Unit = {
savedJavaHome = Option(System.getProperty("java.home"))
}
@Rule
def env: EnvironmentVariables = _env

@After
def restore(): Unit = {
savedJavaHome match {
case None => System.clearProperty("java.home")
case Some(value) => System.setProperty("java.home", value)
}
}
@Rule
def sys: ClearSystemProperties = _sys

@Test
def getJvmHappyPath(): Unit = {
env.clear("JAVA_HOME")
System.setProperty("java.home", "/test/jvm")
assert(MojoUtils.getJvm == "/test/jvm/bin/java")
}

@Test
def getJvmWithoutJavaHome(): Unit = {
System.clearProperty("java.home")
env.clear("JAVA_HOME")
assert(MojoUtils.getJvm == "java")
}

@Test
def getJvmFromEnvironment(): Unit = {
env.clear("JAVA_HOME")
env.set("JAVA_HOME", "/opt/jdk-11")
Console.print(MojoUtils.getJvm)
assert(MojoUtils.getJvm == "/opt/jdk-11/bin/java")
}

@Test
def getJvmJavaHomeIsPriority(): Unit = {
System.setProperty("java.home", "/test/jvm")
env.set("JAVA_HOME", "/opt/jdk-11")
assert(MojoUtils.getJvm == "/opt/jdk-11/bin/java")
}
}
Empty file added stale_outputs_checked
Empty file.