Skip to content

Commit

Permalink
Add failing integration test for execution on GraalVM native image
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Sep 17, 2022
1 parent 343170f commit 7229385
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ tasks.test {

distribution {
requirements.add("jdk=8")
localOnly {
includeClasses.add("platform.tooling.support.tests.GraalVmStarterTests") // GraalVM is not installed on Test Distribution agents
}
}
jvmArgumentProviders += JavaHomeDir(project, 8)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
java
id("org.graalvm.buildtools.native")
}

val jupiterVersion: String = System.getenv("JUNIT_JUPITER_VERSION")
val platformVersion: String = System.getenv("JUNIT_PLATFORM_VERSION")

repositories {
maven { url = uri(file(System.getProperty("maven.repo"))) }
mavenCentral()
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:$jupiterVersion")
testRuntimeOnly("org.junit.platform:junit-platform-reporting:$platformVersion")
}

tasks.test {
useJUnitPlatform()

val outputDir = reports.junitXml.outputLocation
jvmArgumentProviders += CommandLineArgumentProvider {
listOf(
"-Djunit.platform.reporting.open.xml.enabled=true",
"-Djunit.platform.reporting.output.dir=${outputDir.get().asFile.absolutePath}"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pluginManagement {
plugins {
id("org.graalvm.buildtools.native") version "0.9.13"
}
repositories {
mavenCentral()
gradlePluginPortal()
}
}

rootProject.name = "graalvm-starter"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package com.example.project;

public class Calculator {

public int add(int a, int b) {
return a + b;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package com.example.project;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class CalculatorTests {

@Test
@DisplayName("1 + 1 = 2")
void addsTwoNumbers() {
Calculator calculator = new Calculator();
assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
}

@ParameterizedTest(name = "{0} + {1} = {2}")
@CsvSource({
"0, 1, 1",
"1, 2, 3",
"49, 51, 100",
"1, 100, 101"
})
void add(int first, int second, int expectedResult) {
Calculator calculator = new Calculator();
assertEquals(expectedResult, calculator.add(first, second),
() -> first + " + " + second + " should equal " + expectedResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ val jupiterVersion: String = System.getenv("JUNIT_JUPITER_VERSION")
val vintageVersion: String = System.getenv("JUNIT_VINTAGE_VERSION")
val platformVersion: String = System.getenv("JUNIT_PLATFORM_VERSION")

// emit default file encoding to a file
file("file.encoding.txt").writeText(System.getProperty("file.encoding"))
file("junit.versions.txt").writeText("""
jupiterVersion=$jupiterVersion
vintageVersion=$vintageVersion
platformVersion=$platformVersion
""")

repositories {
maven { url = uri(file(System.getProperty("maven.repo"))) }
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

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.assumeFalse;
import static platform.tooling.support.Helper.TOOL_TIMEOUT;
import static platform.tooling.support.tests.XmlAssertions.verifyContainsExpectedStartedOpenTestReport;

import java.nio.file.Paths;

import de.sormuras.bartholdy.tool.GradleWrapper;

import org.junit.jupiter.api.Test;

import platform.tooling.support.MavenRepo;
import platform.tooling.support.Request;

/**
* @since 1.9.1
*/
class GraalVmStarterTests {

@Test
void runsTestsInNativeImage() {
var request = Request.builder() //
.setTool(new GradleWrapper(Paths.get(".."))) //
.setProject("graalvm-starter") //
.addArguments("-Dmaven.repo=" + MavenRepo.dir()) //
.addArguments("javaToolchains", "nativeTest", "--no-daemon", "--stacktrace") //
.setTimeout(TOOL_TIMEOUT) //
.build();

var result = request.run();

assertFalse(result.isTimedOut(), () -> "tool timed out: " + result);

assumeFalse(
result.getOutputLines("err").stream().anyMatch(line -> line.contains("No compatible toolchains found")),
"Abort test if GraalVM is not installed");

assertEquals(0, result.getExitCode());
assertTrue(result.getOutputLines("out").stream().anyMatch(line -> line.contains("BUILD SUCCESSFUL")));

var testResultsDir = Request.WORKSPACE.resolve(request.getWorkspace()).resolve("build/test-results/test");
verifyContainsExpectedStartedOpenTestReport(testResultsDir);
}
}

0 comments on commit 7229385

Please sign in to comment.