Skip to content

Commit 8cdf2f5

Browse files
committed
Overload run() API and reuse console printing listener
1 parent e59b227 commit 8cdf2f5

File tree

8 files changed

+41
-115
lines changed

8 files changed

+41
-115
lines changed

junit-onramp/junit-onramp.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
compileOnlyApi(projects.junitJupiterEngine)
1717

1818
implementation(projects.junitPlatformLauncher)
19+
implementation(projects.junitPlatformConsole)
1920
}
2021

2122
japicmp {

junit-onramp/src/main/java/module-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* import module org.junit.onramp;
1717
*
1818
* void main() {
19-
* JUnit.run(this);
19+
* JUnit.run();
2020
* }
2121
*
2222
* @Test
@@ -31,6 +31,7 @@
3131

3232
requires transitive org.junit.jupiter;
3333
requires org.junit.platform.launcher;
34+
requires org.junit.platform.console;
3435

3536
exports org.junit.onramp;
3637
}

junit-onramp/src/main/java/org/junit/onramp/ContainerFeedPrintingListener.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

junit-onramp/src/main/java/org/junit/onramp/JUnit.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.onramp;
1212

13+
import static java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE;
1314
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
1415
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
1516
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectModule;
@@ -20,34 +21,44 @@
2021

2122
import org.apiguardian.api.API;
2223
import org.junit.platform.commons.JUnitException;
24+
import org.junit.platform.console.output.ColorPalette;
25+
import org.junit.platform.console.output.TestFeedPrintingListener;
26+
import org.junit.platform.engine.DiscoverySelector;
2327
import org.junit.platform.launcher.core.LauncherFactory;
2428
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
2529

2630
@API(status = EXPERIMENTAL, since = "6.0")
2731
public final class JUnit {
28-
29-
private JUnit() {
32+
/**
33+
* Run all tests defined in the caller class.
34+
*/
35+
public static void run() {
36+
var walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
37+
run(selectClass(walker.getCallerClass()));
3038
}
3139

32-
public static void run(Object instance) {
33-
var listener = new SummaryGeneratingListener();
34-
var builder = request();
40+
/**
41+
* Run all tests defined in the given test class.
42+
* @param testClass the class to discover and execute tests in
43+
*/
44+
public static void run(Class<?> testClass) {
45+
run(selectClass(testClass));
46+
}
3547

36-
if (instance instanceof Class<?> testClass) {
37-
// handling `JUnit.run(getClass())`
38-
builder.selectors(selectClass(testClass));
39-
}
40-
else if (instance instanceof Module testModule) {
41-
// handling `JUnit.run(getClass().getModule())`
42-
builder.selectors(selectModule(testModule));
43-
}
44-
else {
45-
// handling `JUnit.run(this)`
46-
builder.selectors(selectClass(instance.getClass()));
47-
}
48+
/**
49+
* Run all tests defined in the given module.
50+
* @param testModule the module to discover and execute tests in
51+
*/
52+
public static void run(Module testModule) {
53+
run(selectModule(testModule));
54+
}
4855

49-
var request = builder.forExecution() //
50-
.listeners(listener, new ContainerFeedPrintingListener()) //
56+
private static void run(DiscoverySelector selector) {
57+
var listener = new SummaryGeneratingListener();
58+
var writer = new PrintWriter(System.out, true, Charset.defaultCharset());
59+
var printer = new TestFeedPrintingListener(writer, ColorPalette.DEFAULT);
60+
var request = request().selectors(selector).forExecution() //
61+
.listeners(listener, printer) //
5162
.build();
5263
var launcher = LauncherFactory.create();
5364
launcher.execute(request);
@@ -61,4 +72,7 @@ else if (instance instanceof Module testModule) {
6172
summary.getTotalFailureCount(), //
6273
summary.getTotalFailureCount() == 1 ? "" : "s"));
6374
}
75+
76+
private JUnit() {
77+
}
6478
}

junit-platform-console/junit-platform-console.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
tasks {
2929
compileJava {
3030
options.compilerArgs.addAll(listOf(
31+
"-Xlint:-module", // due to qualified exports
3132
"--add-modules", "info.picocli",
3233
"--add-reads", "${javaModuleName}=info.picocli"
3334
))

junit-platform-console/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
requires org.junit.platform.launcher;
2525
requires org.junit.platform.reporting;
2626

27+
exports org.junit.platform.console.output to org.junit.onramp;
28+
2729
provides java.util.spi.ToolProvider with org.junit.platform.console.ConsoleLauncherToolProvider;
2830
}

platform-tooling-support-tests/projects/jar-describe-module/junit-onramp.expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ requires java.base mandated
44
requires org.apiguardian.api static transitive
55
requires org.jspecify static transitive
66
requires org.junit.jupiter transitive
7+
requires org.junit.platform.console
78
requires org.junit.platform.launcher

platform-tooling-support-tests/projects/jar-describe-module/junit-platform-console.expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ requires org.junit.platform.engine
77
requires org.junit.platform.launcher
88
requires org.junit.platform.reporting
99
provides java.util.spi.ToolProvider with org.junit.platform.console.ConsoleLauncherToolProvider
10+
qualified exports org.junit.platform.console.output to org.junit.onramp
1011
contains org.junit.platform.console
1112
contains org.junit.platform.console.command
1213
contains org.junit.platform.console.options
13-
contains org.junit.platform.console.output
1414
contains org.junit.platform.console.shadow.picocli
1515
main-class org.junit.platform.console.ConsoleLauncher

0 commit comments

Comments
 (0)