Skip to content
Open
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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ FROM eclipse-temurin:27_35-jdk@sha256:473d02d477a8152a05dc42fde10b965754f1863d45
WORKDIR /opt/test-runner
COPY bin/run.sh bin/run.sh
COPY --from=build /app/build/libs/java-test-runner.jar .
COPY bin/create-aot-cache.sh bin/create-aot-cache.sh
COPY aot-training aot-training
RUN sh bin/create-aot-cache.sh

ENTRYPOINT ["sh", "/opt/test-runner/bin/run.sh"]
24 changes: 24 additions & 0 deletions aot-training/src/main/java/Training.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.List;
import java.util.stream.Collectors;

class Training {

String greet(String name) {
System.out.println("Greeting " + name);
return "Hello, " + name + "!";
}

int divide(int a, int b) {
return a / b;
}

List<String> shout(List<String> words) {
return words.stream().map(String::toUpperCase).collect(Collectors.toList());
}

void reject(String input) {
if (input.isEmpty()) {
throw new IllegalArgumentException("Input must not be empty");
}
}
}
92 changes: 92 additions & 0 deletions aot-training/src/test/java/TrainingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

// This exercise is run once while the Docker image is built, to record which classes
// the test runner loads and how it uses them (see bin/create-aot-cache.sh).
// Its tests cover the paths a typical run takes: passing, failing and erroring tests,
// parameterized tests, printed output and task tags. Some tests fail on purpose.
public class TrainingTest {

private Training training;

@BeforeEach
public void setup() {
training = new Training();
}

@Test
@Tag("task:1")
@DisplayName("greets by name")
public void greetsByName() {
assertThat(training.greet("Alice")).isEqualTo("Hello, Alice!");
}

@Test
@Tag("task:1")
public void failsAnAssertJAssertion() {
assertThat(training.greet("Bob")).isEqualTo("Hi, Bob!");
}

@Test
@Tag("task:2")
public void failsAJUnitAssertion() {
assertEquals(3, training.divide(7, 3));
}

@Test
@Tag("task:2")
public void throwsAnUnexpectedException() {
assertThat(training.divide(1, 0)).isZero();
}

@Test
public void checksAnExpectedException() {
assertThatThrownBy(() -> training.reject(""))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Input must not be empty");
assertThrows(IllegalArgumentException.class, () -> training.reject(""));
}

@Test
public void comparesLists() {
assertThat(training.shout(List.of("a", "b"))).containsExactly("A", "B");
assertThat(training.shout(List.of("a", "b"))).containsExactly("B", "A");
}

@ParameterizedTest
@MethodSource("divisions")
public void dividesFromAMethodSource(int a, int b, int expected) {
assertThat(training.divide(a, b)).isEqualTo(expected);
}

private static Stream<Arguments> divisions() {
return Stream.of(Arguments.of(6, 3, 2), Arguments.of(9, 3, 4));
}

@ParameterizedTest
@CsvSource({"Alice, 'Hello, Alice!'", "Bob, 'Hello, Bob!'"})
public void greetsFromACsvSource(String name, String expected) {
assertThat(training.greet(name)).isEqualTo(expected);
}

@ParameterizedTest(name = "{0} is not empty")
@ValueSource(strings = {"a", "b"})
public void acceptsFromAValueSource(String input) {
training.reject(input);
}
}
31 changes: 31 additions & 0 deletions bin/create-aot-cache.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env sh

# Synopsis:
# Create the JVM's ahead-of-time (AOT) cache for the test runner.
# This runs once, while the Docker image is built.

# Output:
# Writes /opt/test-runner/java-test-runner.aot, which bin/run.sh passes to the JVM.

# Without a cache, every run spends more than a second finding, loading and linking
# the classes of javac, JUnit and AssertJ, and running their code in the interpreter.
# This script runs the test runner once on the sample exercise in aot-training/.
# The JVM records every class loaded during that run and saves them, already loaded
# and linked, in the cache, along with profiling data for their methods.
# Every later run starts from that state.

# The JVM only uses the cache when a run uses the same jar and compatible options,
# so this must use the same options as bin/run.sh. If the JVM can't use the cache,
# it will print an error and run as before, without the speed-up.

set -e

training_folder="/tmp/aot-training"

rm -rf "${training_folder}"
cp -R /opt/test-runner/aot-training "${training_folder}"

java -XX:AOTCacheOutput=/opt/test-runner/java-test-runner.aot \
-jar /opt/test-runner/java-test-runner.jar training "${training_folder}" "${training_folder}"

rm -rf "${training_folder}"
6 changes: 5 additions & 1 deletion bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ cp -R $input_folder/* .

find . -mindepth 1 -type f | grep 'Test.java' | xargs -I file sed -i "s/@Ignore(.*)//g;s/@Ignore//g;s/@Disabled(.*)//g;s/@Disabled//g;" file

java -jar /opt/test-runner/java-test-runner.jar $problem_slug . $output_folder
# -XX:AOTCache loads the JVM's ahead-of-time cache, created by bin/create-aot-cache.sh
# when the image is built. The classes of javac, JUnit and AssertJ come from the cache
# already loaded and linked, which halves the time of a typical run.
java -XX:AOTCache=/opt/test-runner/java-test-runner.aot \
-jar /opt/test-runner/java-test-runner.jar $problem_slug . $output_folder