Skip to content

Commit f3e1aa9

Browse files
authored
Improve documentation of LauncherExecutionRequestBuilder (#5224)
Based on feedback received in #4728
1 parent dd09775 commit f3e1aa9

File tree

2 files changed

+111
-8
lines changed

2 files changed

+111
-8
lines changed

documentation/modules/ROOT/pages/advanced-topics/launcher-api.adoc

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ failed can be achieved as follows.
363363

364364
[source,java,indent=0]
365365
----
366-
include::example$java/example/UsingTheLauncherDemo.java[tags=cancellation]
366+
include::example$java/example/UsingTheLauncherDemo.java[tags=cancellation-direct]
367367
----
368368
<1> Create a `{CancellationToken}`
369369
<2> Implement a `{TestExecutionListener}` that calls `cancel()` when a test fails
@@ -387,3 +387,37 @@ At the time of writing, the following test engines support cancellation:
387387
* `{junit-platform-suite-engine}`
388388
* Any `{TestEngine}` extending `{HierarchicalTestEngine}` such as Spock and Cucumber
389389
====
390+
391+
[[launcher-cancellation-execution-request-from-discovery-request]]
392+
=== Building a LauncherExecutionRequest from a LauncherDiscoveryRequest
393+
394+
Rather than building a `{LauncherExecutionRequest}` by starting with a
395+
`{LauncherDiscoveryRequestBuilder}` and calling `forExecution()`, you can create a
396+
`{LauncherExecutionRequestBuilder}` from a previously built `{LauncherDiscoveryRequest}`.
397+
398+
[source,java,indent=0]
399+
----
400+
include::example$java/example/UsingTheLauncherDemo.java[tags=cancellation-discovery-request]
401+
----
402+
<1> Build a `{LauncherDiscoveryRequest}`
403+
<2> Create a `{LauncherExecutionRequestBuilder}` with it
404+
<3> Register the cancellation token
405+
<4> Register the listener
406+
<5> Pass the `{LauncherExecutionRequest}` to `Launcher.execute`
407+
408+
[[launcher-cancellation-execution-request-from-test-plan]]
409+
=== Building a LauncherExecutionRequest from a TestPlan
410+
411+
Alternatively, a `{LauncherExecutionRequestBuilder}` can be created from a previously
412+
discovered `{TestPlan}`.
413+
414+
[source,java,indent=0]
415+
----
416+
include::example$java/example/UsingTheLauncherDemo.java[tags=cancellation-test-plan]
417+
----
418+
<1> Build a `{LauncherDiscoveryRequest}`
419+
<2> Discover a `{TestPlan}` via `Launcher.discover`
420+
<3> Create a `{LauncherExecutionRequestBuilder}` with it
421+
<4> Register the cancellation token
422+
<5> Register the listener
423+
<6> Pass the `{LauncherExecutionRequest}` to `Launcher.execute`

documentation/src/test/java/example/UsingTheLauncherDemo.java

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.platform.launcher.TestPlan;
3737
import org.junit.platform.launcher.core.LauncherConfig;
3838
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
39+
import org.junit.platform.launcher.core.LauncherExecutionRequestBuilder;
3940
import org.junit.platform.launcher.core.LauncherFactory;
4041
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
4142
import org.junit.platform.launcher.listeners.TestExecutionSummary;
@@ -119,8 +120,8 @@ void launcherConfig() {
119120

120121
@Test
121122
@SuppressWarnings("unused")
122-
void cancellation() {
123-
// tag::cancellation[]
123+
void cancellationDirect() {
124+
// tag::cancellation-direct[]
124125
CancellationToken cancellationToken = CancellationToken.create(); // <1>
125126

126127
TestExecutionListener failFastListener = new TestExecutionListener() {
@@ -132,24 +133,92 @@ public void executionFinished(TestIdentifier identifier, TestExecutionResult res
132133
}
133134
};
134135

135-
// end::cancellation[]
136+
// end::cancellation-direct[]
136137
// @formatter:off
137-
// tag::cancellation[]
138+
// tag::cancellation-direct[]
138139
LauncherExecutionRequest executionRequest = LauncherDiscoveryRequestBuilder.request()
139140
.selectors(selectClass(MyTestClass.class))
140141
.forExecution()
142+
// end::cancellation-direct[]
143+
// @formatter:on
144+
// tag::cancellation-direct[]
141145
.cancellationToken(cancellationToken) // <3>
142146
.listeners(failFastListener) // <4>
143147
.build();
144-
// end::cancellation[]
148+
149+
try (LauncherSession session = LauncherFactory.openSession()) {
150+
session.getLauncher().execute(executionRequest); // <5>
151+
}
152+
// end::cancellation-direct[]
153+
}
154+
155+
@Test
156+
@SuppressWarnings("unused")
157+
void cancellationFromDiscoveryRequest() {
158+
CancellationToken cancellationToken = CancellationToken.create();
159+
160+
TestExecutionListener failFastListener = new TestExecutionListener() {
161+
@Override
162+
public void executionFinished(TestIdentifier identifier, TestExecutionResult result) {
163+
if (result.getStatus() == FAILED) {
164+
cancellationToken.cancel();
165+
}
166+
}
167+
};
168+
145169
// @formatter:off
146-
// tag::cancellation[]
170+
// tag::cancellation-discovery-request[]
171+
LauncherDiscoveryRequest discoveryRequest = LauncherDiscoveryRequestBuilder.request()
172+
.selectors(selectClass(MyTestClass.class))
173+
.build(); // <1>
174+
// end::cancellation-discovery-request[]
175+
// @formatter:on
176+
// tag::cancellation-discovery-request[]
177+
178+
LauncherExecutionRequest executionRequest = LauncherExecutionRequestBuilder.request(discoveryRequest) // <2>
179+
.cancellationToken(cancellationToken) // <3>
180+
.listeners(failFastListener) // <4>
181+
.build();
147182

148183
try (LauncherSession session = LauncherFactory.openSession()) {
149184
session.getLauncher().execute(executionRequest); // <5>
150185
}
151-
// end::cancellation[]
186+
// end::cancellation-discovery-request[]
187+
}
188+
189+
@Test
190+
@SuppressWarnings("unused")
191+
void cancellationFromTestPlan() {
192+
CancellationToken cancellationToken = CancellationToken.create();
193+
194+
TestExecutionListener failFastListener = new TestExecutionListener() {
195+
@Override
196+
public void executionFinished(TestIdentifier identifier, TestExecutionResult result) {
197+
if (result.getStatus() == FAILED) {
198+
cancellationToken.cancel();
199+
}
200+
}
201+
};
202+
203+
// @formatter:off
204+
// tag::cancellation-test-plan[]
205+
LauncherDiscoveryRequest discoveryRequest = LauncherDiscoveryRequestBuilder.request()
206+
.selectors(selectClass(MyTestClass.class))
207+
.build(); // <1>
208+
// end::cancellation-test-plan[]
152209
// @formatter:on
210+
// tag::cancellation-test-plan[]
211+
212+
try (LauncherSession session = LauncherFactory.openSession()) {
213+
var launcher = session.getLauncher();
214+
TestPlan testPlan = launcher.discover(discoveryRequest); // <2>
215+
LauncherExecutionRequest executionRequest = LauncherExecutionRequestBuilder.request(testPlan) // <3>
216+
.cancellationToken(cancellationToken) // <4>
217+
.listeners(failFastListener) // <5>
218+
.build();
219+
launcher.execute(executionRequest); // <6>
220+
}
221+
// end::cancellation-test-plan[]
153222
}
154223

155224
}

0 commit comments

Comments
 (0)