Skip to content

Commit cb7582e

Browse files
committed
Introduce JFR events for discovery issues
1 parent 3e0a0ef commit cb7582e

File tree

5 files changed

+72
-26
lines changed

5 files changed

+72
-26
lines changed

junit-platform-commons/src/module/org.junit.platform.commons/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
org.junit.jupiter.params,
4646
org.junit.platform.console,
4747
org.junit.platform.engine,
48+
org.junit.platform.jfr,
4849
org.junit.platform.launcher,
4950
org.junit.platform.reporting,
5051
org.junit.platform.runner,

junit-platform-jfr/src/main/java/org/junit/platform/jfr/FlightRecordingDiscoveryListener.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import jdk.jfr.StackTrace;
2424

2525
import org.apiguardian.api.API;
26+
import org.junit.platform.commons.util.ExceptionUtils;
2627
import org.junit.platform.engine.DiscoveryFilter;
28+
import org.junit.platform.engine.DiscoveryIssue;
2729
import org.junit.platform.engine.DiscoverySelector;
2830
import org.junit.platform.launcher.EngineDiscoveryResult;
2931
import org.junit.platform.launcher.LauncherDiscoveryListener;
@@ -71,13 +73,23 @@ public void engineDiscoveryFinished(org.junit.platform.engine.UniqueId engineId,
7173
event.commit();
7274
}
7375

76+
@Override
77+
public void issueEncountered(org.junit.platform.engine.UniqueId engineId, DiscoveryIssue issue) {
78+
DiscoveryIssueEvent event = new DiscoveryIssueEvent();
79+
event.engineId = engineId.toString();
80+
event.severity = issue.severity().name();
81+
event.message = issue.message();
82+
event.source = issue.source().map(Object::toString).orElse(null);
83+
event.cause = issue.cause().map(ExceptionUtils::readStackTrace).orElse(null);
84+
event.commit();
85+
}
86+
7487
@Category({ "JUnit", "Discovery" })
7588
@StackTrace(false)
7689
abstract static class DiscoveryEvent extends Event {
7790
}
7891

7992
@Label("Test Discovery")
80-
@Category({ "JUnit", "Discovery" })
8193
@Name("org.junit.LauncherDiscovery")
8294
static class LauncherDiscoveryEvent extends DiscoveryEvent {
8395

@@ -89,7 +101,6 @@ static class LauncherDiscoveryEvent extends DiscoveryEvent {
89101
}
90102

91103
@Label("Engine Discovery")
92-
@Category({ "JUnit", "Discovery" })
93104
@Name("org.junit.EngineDiscovery")
94105
static class EngineDiscoveryEvent extends DiscoveryEvent {
95106

@@ -100,4 +111,24 @@ static class EngineDiscoveryEvent extends DiscoveryEvent {
100111
@Label("Result")
101112
String result;
102113
}
114+
115+
@Label("Discovery Issue")
116+
@Name("org.junit.DiscoveryIssue")
117+
static class DiscoveryIssueEvent extends DiscoveryEvent {
118+
119+
@Label("Engine Id")
120+
String engineId;
121+
122+
@Label("Severity")
123+
String severity;
124+
125+
@Label("Message")
126+
String message;
127+
128+
@Label("Source")
129+
String source;
130+
131+
@Label("Cause")
132+
String cause;
133+
}
103134
}

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/DiscoveryIssueNotifier.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import static java.util.Collections.emptyList;
1414
import static java.util.Comparator.comparing;
1515
import static java.util.stream.Collectors.partitioningBy;
16+
import static org.junit.platform.commons.util.ExceptionUtils.readStackTrace;
1617

17-
import java.io.PrintWriter;
18-
import java.io.StringWriter;
1918
import java.util.ArrayList;
2019
import java.util.List;
2120
import java.util.Map;
@@ -133,21 +132,12 @@ else if (source instanceof ClassSource) {
133132
appendIdeCompatibleLink(message, classSource.getClassName(), "<no-method>");
134133
}
135134
});
136-
issue.cause().ifPresent(t -> message.append("\n Cause: ").append(getStackTrace(t)));
135+
issue.cause().ifPresent(t -> message.append("\n Cause: ").append(readStackTrace(t)));
137136
}
138137
return message.toString();
139138
}
140139

141140
private static void appendIdeCompatibleLink(StringBuilder message, String className, String methodName) {
142141
message.append("\n at ").append(className).append(".").append(methodName).append("(SourceFile:0)");
143142
}
144-
145-
private static String getStackTrace(Throwable cause) {
146-
StringWriter stringWriter = new StringWriter();
147-
try (PrintWriter writer = new PrintWriter(stringWriter, true)) {
148-
cause.printStackTrace(writer);
149-
writer.flush();
150-
}
151-
return stringWriter.toString();
152-
}
153143
}

platform-tests/src/test/java/org/junit/platform/jfr/FlightRecordingDiscoveryListenerIntegrationTests.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@
1010

1111
package org.junit.platform.jfr;
1212

13+
import static org.junit.platform.commons.util.ExceptionUtils.readStackTrace;
1314
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
1415
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;
1516
import static org.moditect.jfrunit.ExpectedEvent.event;
1617
import static org.moditect.jfrunit.JfrEventsAssert.assertThat;
1718

1819
import org.junit.jupiter.api.Test;
1920
import org.junit.jupiter.api.extension.DisabledOnOpenJ9;
20-
import org.junit.jupiter.engine.JupiterTestEngine;
21-
import org.junit.platform.launcher.core.LauncherFactoryForTestingPurposesOnly;
21+
import org.junit.platform.engine.DiscoveryIssue;
22+
import org.junit.platform.engine.DiscoveryIssue.Severity;
23+
import org.junit.platform.engine.EngineDiscoveryRequest;
24+
import org.junit.platform.engine.TestDescriptor;
25+
import org.junit.platform.engine.UniqueId;
26+
import org.junit.platform.engine.support.descriptor.ClassSource;
27+
import org.junit.platform.fakes.TestEngineStub;
28+
import org.junit.platform.testkit.engine.EngineTestKit;
2229
import org.moditect.jfrunit.EnableEvent;
2330
import org.moditect.jfrunit.JfrEventTest;
2431
import org.moditect.jfrunit.JfrEvents;
@@ -32,22 +39,39 @@ public class FlightRecordingDiscoveryListenerIntegrationTests {
3239
@Test
3340
@EnableEvent("org.junit.*")
3441
void reportsEvents() {
35-
var launcher = LauncherFactoryForTestingPurposesOnly.createLauncher(new JupiterTestEngine());
36-
var request = request() //
42+
var source = ClassSource.from(FlightRecordingDiscoveryListenerIntegrationTests.class);
43+
var cause = new RuntimeException("boom");
44+
var issue = DiscoveryIssue.builder(Severity.WARNING, "some message") //
45+
.source(source) //
46+
.cause(cause) //
47+
.build();
48+
49+
var testEngine = new TestEngineStub() {
50+
@Override
51+
public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
52+
discoveryRequest.getDiscoveryListener().issueEncountered(uniqueId, issue);
53+
return super.discover(discoveryRequest, uniqueId);
54+
}
55+
};
56+
57+
EngineTestKit.discover(testEngine, request() //
3758
.selectors(selectClass(FlightRecordingDiscoveryListenerIntegrationTests.class)) //
3859
.listeners(new FlightRecordingDiscoveryListener()) //
39-
.build();
60+
.build());
4061

41-
launcher.discover(request);
4262
jfrEvents.awaitEvents();
4363

4464
assertThat(jfrEvents) //
4565
.contains(event("org.junit.LauncherDiscovery") //
46-
// TODO JfrUnit does not yey support checking int values
47-
// .with("selectors", 1) //
48-
// .with("filters", 0) //
49-
) //
66+
.with("selectors", 1) //
67+
.with("filters", 0)) //
5068
.contains(event("org.junit.EngineDiscovery") //
51-
.with("uniqueId", "[engine:junit-jupiter]"));
69+
.with("uniqueId", "[engine:TestEngineStub]")) //
70+
.contains(event("org.junit.DiscoveryIssue") //
71+
.with("engineId", "[engine:TestEngineStub]") //
72+
.with("severity", "WARNING") //
73+
.with("message", "some message") //
74+
.with("source", source.toString()) //
75+
.with("cause", readStackTrace(cause)));
5276
}
5377
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ requires java.management
1111
requires org.apiguardian.api static transitive
1212
uses org.junit.platform.commons.support.scanning.ClasspathScanner
1313
qualified exports org.junit.platform.commons.logging to org.junit.jupiter.api org.junit.jupiter.engine org.junit.jupiter.migrationsupport org.junit.jupiter.params org.junit.platform.console org.junit.platform.engine org.junit.platform.launcher org.junit.platform.reporting org.junit.platform.runner org.junit.platform.suite.api org.junit.platform.suite.engine org.junit.platform.testkit org.junit.vintage.engine
14-
qualified exports org.junit.platform.commons.util to org.junit.jupiter.api org.junit.jupiter.engine org.junit.jupiter.migrationsupport org.junit.jupiter.params org.junit.platform.console org.junit.platform.engine org.junit.platform.launcher org.junit.platform.reporting org.junit.platform.runner org.junit.platform.suite.api org.junit.platform.suite.commons org.junit.platform.suite.engine org.junit.platform.testkit org.junit.vintage.engine
14+
qualified exports org.junit.platform.commons.util to org.junit.jupiter.api org.junit.jupiter.engine org.junit.jupiter.migrationsupport org.junit.jupiter.params org.junit.platform.console org.junit.platform.engine org.junit.platform.jfr org.junit.platform.launcher org.junit.platform.reporting org.junit.platform.runner org.junit.platform.suite.api org.junit.platform.suite.commons org.junit.platform.suite.engine org.junit.platform.testkit org.junit.vintage.engine

0 commit comments

Comments
 (0)