Skip to content

Commit

Permalink
Adapt JavaVersionsTests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 28, 2024
1 parent e56437d commit 26187f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public ProcessStarter putEnvironment(String key, String value) {
return this;
}

public ProcessStarter putEnvironment(Map<String, String> values) {
environment.putAll(values);
return this;
}

public ProcessResult startAndWait() throws InterruptedException {
return start().waitFor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public static ProcessStarter gradlew() {
return withCommonEnvironmentVariables(starter);
}

public static ProcessStarter maven() {
var starter = new ProcessStarter() //
.executable(Path.of(System.getProperty("mavenDistribution")).resolve("bin/mvn")) //
.putEnvironment("JAVA_HOME", getGradleJavaHome().orElseThrow(TestAbortedException::new));
return withCommonEnvironmentVariables(starter);
}

private static ProcessStarter withCommonEnvironmentVariables(ProcessStarter starter) {
starter.putEnvironment("JUNIT_JUPITER_VERSION", Helper.version("junit-jupiter"));
starter.putEnvironment("JUNIT_VINTAGE_VERSION", Helper.version("junit-vintage"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
package platform.tooling.support.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static platform.tooling.support.Helper.TOOL_TIMEOUT;
import static platform.tooling.support.tests.Projects.copyToWorkspace;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -23,10 +22,11 @@
import de.sormuras.bartholdy.tool.Java;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import platform.tooling.support.Helper;
import platform.tooling.support.MavenRepo;
import platform.tooling.support.Request;
import platform.tooling.support.process.ProcessStarters;

/**
* @since 1.4
Expand All @@ -36,39 +36,37 @@ class JavaVersionsTests {
@GlobalResource
LocalMavenRepo localMavenRepo;

@TempDir
Path workspace;

@Test
void java_8() {
void java_8() throws Exception {
var java8Home = Helper.getJavaHome("8");
assumeTrue(java8Home.isPresent(), "Java 8 installation directory not found!");
var actualLines = execute("8", java8Home.get(), Map.of());
var actualLines = execute(java8Home.get(), Map.of());

assertTrue(actualLines.contains("[WARNING] Tests run: 2, Failures: 0, Errors: 0, Skipped: 1"));
}

@Test
void java_default() {
var actualLines = execute("default", new Java().getHome(), MavenEnvVars.FOR_JDK24_AND_LATER);
void java_default() throws Exception {
var actualLines = execute(new Java().getHome(), MavenEnvVars.FOR_JDK24_AND_LATER);

assertTrue(actualLines.contains("[WARNING] Tests run: 2, Failures: 0, Errors: 0, Skipped: 1"));
}

List<String> execute(String version, Path javaHome, Map<String, String> environmentVars) {
var builder = Request.builder() //
.setTool(Request.maven()) //
.setProject(Projects.JAVA_VERSIONS) //
.setWorkspace("java-versions-" + version) //
List<String> execute(Path javaHome, Map<String, String> environmentVars) throws Exception {
var result = ProcessStarters.maven() //
.workingDir(copyToWorkspace(Projects.JAVA_VERSIONS, workspace)) //
.putEnvironment("JAVA_HOME", javaHome) //
.putEnvironment(environmentVars) //
.addArguments(localMavenRepo.toCliArgument(), "-Dmaven.repo=" + MavenRepo.dir()) //
.addArguments("--update-snapshots", "--batch-mode", "verify") //
.setTimeout(TOOL_TIMEOUT) //
.setJavaHome(javaHome);
environmentVars.forEach(builder::putEnvironment);

var result = builder.build().run();
.startAndWait();

assertFalse(result.isTimedOut(), () -> "tool timed out: " + result);
assertEquals(0, result.getExitCode());
assertEquals("", result.getOutput("err"));
return result.getOutputLines("out");
assertEquals(0, result.exitCode());
assertEquals("", result.stdErr());
return result.stdOutLines();
}

}

0 comments on commit 26187f0

Please sign in to comment.