Skip to content

Commit

Permalink
JavaExecCmdUtil: use Paths.get to join file paths
Browse files Browse the repository at this point in the history
Instead of relying on the values of the retrieved variables correctly
handling trailing slashes, simply use the `Paths` utility methods to
combine directory/path objects.

Signed-off-by: Kenny Ballou <[email protected]>
  • Loading branch information
kennyballou committed Sep 13, 2023
1 parent 1948d76 commit 2767a27
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.evosuite.runtime.util;

import java.io.File;
import java.nio.file.Paths;
import java.util.Optional;

/**
Expand All @@ -45,19 +46,14 @@ private JavaExecCmdUtil() {
* @apiNote under maven java.home property is ${JAVA_HOME}/jre/bin/java
*/
public static String getJavaBinExecutablePath(final boolean isFullOriginalJavaExecRequired) {
final String separator = System.getProperty("file.separator");
final String JAVA_CMD =
System.getProperty("java.home") + separator + "bin" + separator + "java";
final String JAVA_CMD = Paths.get(System.getProperty("java.home"), "bin", "java").toString();

return getJavaHomeEnv()
.map(javaHomeEnvVar ->
new File(
javaHomeEnvVar + File.separatorChar + "bin" + File.separatorChar + "java" +
getOsName()
.filter(osName -> osName.toLowerCase().contains("windows"))
.map(osName -> ".exe")
.orElse("")
)
Paths.get(javaHomeEnvVar, "bin", "java", getOsName()
.filter(osName -> osName.toLowerCase().contains("windows"))
.map(osName -> ".exe")
.orElse("")).toFile()
)
.filter(File::exists)
.map(File::getPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.evosuite.runtime.util;

import java.nio.file.Paths;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.core.IsEqual;
import org.junit.Before;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void unixNewBehavior() {
// set correct java_home and get real path to java
environmentVariables.set("JAVA_HOME", JAVA_HOME_SYSTEM);
assertThat(JavaExecCmdUtil.getJavaBinExecutablePath(),
IsEqual.equalTo(JAVA_HOME_SYSTEM + SEPARATOR + "bin" + SEPARATOR + "java"));
IsEqual.equalTo(Paths.get(JAVA_HOME_SYSTEM, "bin", "java").toString()));
}
);
}
Expand Down

0 comments on commit 2767a27

Please sign in to comment.