Skip to content

Commit afc2735

Browse files
committed
Add TAPI demo
1 parent e536eab commit afc2735

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LibraryTest(TestReporter testReporter) {
2222
```
2323

2424
is displayed at the class level in the metadata tab in the test report here:
25-
`build/reports/tests/test/org.example.LibraryTest/index.html`.
25+
`build/reports/tests/test/org.example.ExampleTest/index.html`.
2626

2727
<img width="652" height="395" alt="image" src="https://github.com/user-attachments/assets/ca234200-8e70-425d-b55b-e9bfea734066" />
2828

@@ -53,6 +53,36 @@ void someLibraryMethodReturnsTrue(TestReporter testReporter) {
5353
}
5454
```
5555

56-
is displayed at the method level in the metadata tab in the test report here: `build/reports/tests/test/org.example.LibraryTest/someLibraryMethodReturnsTrue(TestReporter)/index.html`.
56+
is displayed at the method level in the metadata tab in the test report here: `build/reports/tests/test/org.example.ExampleTest/someLibraryMethodReturnsTrue(TestReporter)/index.html`.
5757

5858
<img width="704" height="468" alt="image" src="https://github.com/user-attachments/assets/234533cf-41fa-4dd7-aa09-efcd44250452" />
59+
60+
## Milestone 1 Tooling API Demo
61+
62+
This project demonstrates capturing `ReportEntry` data published by JUnit Platform during test execution in Gradle when running via the Tooling API.
63+
64+
This project contains a demonstration project in `/demo-m1-tapi` which is a JVM application project that runs the `/demo-m1` build using the Tooling API, and listens for `TestMetadataEvent`s while doing so.
65+
66+
Run the demo project using `./gradlew :demon-m1-tapi:run` and the build should succeed.
67+
You will see the `ReportEntry` data published by the test printed to the console using metadata events:
68+
69+
```text
70+
> Task :demo-m1-tapi:run
71+
Parallel Configuration Cache is an incubating feature.
72+
Reusing configuration cache.
73+
> Task :demo-m1:processTestResources NO-SOURCE
74+
> Task :demo-m1:processResources NO-SOURCE
75+
> Task :demo-m1:compileJava FROM-CACHE
76+
> Task :demo-m1:classes UP-TO-DATE
77+
> Task :demo-m1:compileTestJava FROM-CACHE
78+
> Task :demo-m1:testClasses UP-TO-DATE
79+
Received test metadata event: metadata
80+
constructor = value1
81+
Received test metadata event: metadata
82+
beforeEach = value2
83+
Received test metadata event: metadata
84+
test = value3
85+
Received test metadata event: metadata
86+
afterEach = value4
87+
BUILD SUCCESSFUL in 1s
88+
```

demo-m1-tapi/build.gradle.kts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
plugins {
4+
application
5+
}
6+
7+
dependencies {
8+
implementation(gradleApi())
9+
}
10+
11+
application {
12+
mainClass = "org.example.Main"
13+
}
14+
15+
testing {
16+
suites {
17+
named<JvmTestSuite>("test") {
18+
useJUnitJupiter()
19+
}
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.example;
2+
3+
import java.io.File;
4+
import java.util.Set;
5+
6+
import org.gradle.tooling.BuildLauncher;
7+
import org.gradle.tooling.GradleConnector;
8+
import org.gradle.tooling.ProjectConnection;
9+
import org.gradle.tooling.events.OperationType;
10+
import org.gradle.tooling.events.test.*;
11+
12+
@SuppressWarnings("UnstableApiUsage")
13+
public class Main {
14+
public static void main(String[] args) {
15+
GradleConnector connector = GradleConnector.newConnector();
16+
17+
connector.forProjectDirectory(new File("../demo-m1"));
18+
19+
try (ProjectConnection connection = connector.connect()) {
20+
// Configure the launcher to listen and print test metadata events
21+
BuildLauncher launcher = connection.newBuild();
22+
launcher.addProgressListener(progressEvent -> {
23+
if (progressEvent instanceof TestMetadataEvent testMetadataEvent) {
24+
System.out.println("Received test metadata event: " + testMetadataEvent.getDisplayName());
25+
testMetadataEvent.getValues().forEach((key, value) -> System.out.println(" " + key + " = " + value));
26+
}
27+
}, Set.of(OperationType.TEST, OperationType.TEST_METADATA));
28+
29+
// Run the demo-m1 build, rerunning tests to ensure they aren't found to be up-to-date
30+
launcher.forTasks("test", "--rerun");
31+
launcher.setStandardOutput(System.out);
32+
launcher.setStandardError(System.err);
33+
launcher.run();
34+
}
35+
}
36+
}

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ plugins {
1212
id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
1313
}
1414

15-
include("demo-m1")
15+
include("demo-m1", "demo-m1-tapi")
1616

1717
rootProject.name = "sample-rbt-engine"

0 commit comments

Comments
 (0)