Skip to content

Commit

Permalink
Provide the means to customize the folder containing the java executa…
Browse files Browse the repository at this point in the history
…ble for RJR (#687)
  • Loading branch information
raul-arabaolaza committed Nov 29, 2023
1 parent 0d2b4aa commit 25ae7e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public final class RealJenkinsRule implements TestRule {

private File war;

private String altJavaHome;

private boolean includeTestClasspathPlugins = true;

private final String token = UUID.randomUUID().toString();
Expand Down Expand Up @@ -309,6 +311,14 @@ public RealJenkinsRule withWar(File war) {
return this;
}

/**
* Allows to specify an alternate, not the one specified in JAVA_HOME, folder containing the JVM to use to launch the instance
*/
public RealJenkinsRule withAltJavaHome(String altJavaHome) {
this.altJavaHome = altJavaHome;
return this;
}

public RealJenkinsRule withLogger(Class<?> clazz, Level level) {
return withLogger(clazz.getName(), level);
}
Expand Down Expand Up @@ -658,7 +668,7 @@ public void startJenkins() throws Throwable {
Stream.of(cp.split(File.pathSeparator)).collect(Collectors.joining(System.lineSeparator())),
StandardCharsets.UTF_8);
List<String> argv = new ArrayList<>(List.of(
new File(System.getProperty("java.home"), "bin/java").getAbsolutePath(),
new File(altJavaHome != null ? altJavaHome : System.getProperty("java.home"), "bin/java").getAbsolutePath(),
"-ea",
"-Dhudson.Main.development=true",
"-DRealJenkinsRule.location=" + RealJenkinsRule.class.getProtectionDomain().getCodeSource().getLocation(),
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ public void whenUsingFailurePlugin() throws Throwable {
assertThat(jse.getMessage(), containsString("Error</h1><pre>java.io.IOException: oops"));
}

@Test
public void whenUsingWrongAltJavaHome() throws Throwable {
IOException ex = assertThrows(
IOException.class, () -> rrWithFailure.withAltJavaHome("/noexists").startJenkins());
assertThat(ex.getMessage(), containsString(File.separator + "noexists" + File.separator + "bin" + File.separator + "java"));
}

@Test public void smokesAltJavaHome() throws Throwable {
String altJavaHome = System.getProperty("java.home");
rr.extraEnv("SOME_ENV_VAR", "value").extraEnv("NOT_SET", null).withAltJavaHome(altJavaHome).withLogger(Jenkins.class, Level.FINEST).then(RealJenkinsRuleTest::_smokes);
}

// TODO interesting scenarios to test:

Check warning on line 318 in src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: interesting scenarios to test:
// · throw an exception of a type defined in Jenkins code
// · run with optional dependencies disabled
Expand Down

0 comments on commit 25ae7e3

Please sign in to comment.