Skip to content

Commit 14aa558

Browse files
committed
Document EventConditions
Resolves #1621.
1 parent d5a31f1 commit 14aa558

File tree

1 file changed

+203
-2
lines changed

1 file changed

+203
-2
lines changed

junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/EventConditions.java

Lines changed: 203 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,57 +52,144 @@ private EventConditions() {
5252
/* no-op */
5353
}
5454

55+
/**
56+
* Create a new {@link Condition} that matches if and only if an
57+
* {@link Event} matches all of the supplied conditions.
58+
*/
5559
@SafeVarargs
5660
@SuppressWarnings("varargs")
5761
public static Condition<Event> event(Condition<? super Event>... conditions) {
5862
return Assertions.allOf(conditions);
5963
}
6064

65+
/**
66+
* Create a new {@link Condition} that matches if and only if an
67+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
68+
* an instance of {@link EngineDescriptor}.
69+
*/
6170
public static Condition<Event> engine() {
6271
return new Condition<>(byTestDescriptor(EngineDescriptor.class::isInstance), "is an engine");
6372
}
6473

74+
/**
75+
* Create a new {@link Condition} that matches if and only if an
76+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
77+
* a {@linkplain TestDescriptor#isTest() test} and its
78+
* {@linkplain TestDescriptor#getUniqueId() unique id} contains the supplied
79+
* {@link String}.
80+
*
81+
* @see #test()
82+
* @see #uniqueIdSubstring(String)
83+
*/
6584
public static Condition<Event> test(String uniqueIdSubstring) {
6685
return Assertions.allOf(test(), uniqueIdSubstring(uniqueIdSubstring));
6786
}
6887

88+
/**
89+
* Create a new {@link Condition} that matches if and only if an
90+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
91+
* a {@linkplain TestDescriptor#isTest() test}, its
92+
* {@linkplain TestDescriptor#getUniqueId() unique id} contains the supplied
93+
* {@link String}, and its {@linkplain TestDescriptor#getDisplayName()
94+
* display name} equals the supplied {@link String}.
95+
*
96+
* @see #test()
97+
* @see #uniqueIdSubstring(String)
98+
* @see #displayName(String)
99+
*/
69100
public static Condition<Event> test(String uniqueIdSubstring, String displayName) {
70101
return Assertions.allOf(test(), uniqueIdSubstring(uniqueIdSubstring), displayName(displayName));
71102
}
72103

104+
/**
105+
* Create a new {@link Condition} that matches if and only if an
106+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
107+
* a {@linkplain TestDescriptor#isTest() test}.
108+
*/
73109
public static Condition<Event> test() {
74110
return new Condition<>(byTestDescriptor(TestDescriptor::isTest), "is a test");
75111
}
76112

113+
/**
114+
* Create a new {@link Condition} that matches if and only if an
115+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
116+
* a {@linkplain TestDescriptor#isContainer() container} and its
117+
* {@linkplain TestDescriptor#getUniqueId() unique id} contains the
118+
* fully-qualified name of the supplied {@link Class}.
119+
*/
77120
public static Condition<Event> container(Class<?> clazz) {
78121
return container(clazz.getName());
79122
}
80123

124+
/**
125+
* Create a new {@link Condition} that matches if and only if an
126+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
127+
* a {@linkplain TestDescriptor#isContainer() container} and its
128+
* {@linkplain TestDescriptor#getUniqueId() unique id} contains the supplied
129+
* {@link String}.
130+
*/
81131
public static Condition<Event> container(String uniqueIdSubstring) {
82132
return container(uniqueIdSubstring(uniqueIdSubstring));
83133
}
84134

135+
/**
136+
* Create a new {@link Condition} that matches if and only if an
137+
* {@link Event} matches the supplied {@code Condition} and its
138+
* {@linkplain Event#getTestDescriptor() test descriptor} is a
139+
* {@linkplain TestDescriptor#isContainer() container}.
140+
*/
85141
public static Condition<Event> container(Condition<Event> condition) {
86142
return Assertions.allOf(container(), condition);
87143
}
88144

145+
/**
146+
* Create a new {@link Condition} that matches if and only if an
147+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
148+
* a {@linkplain TestDescriptor#isContainer() container}.
149+
*/
89150
public static Condition<Event> container() {
90151
return new Condition<>(byTestDescriptor(TestDescriptor::isContainer), "is a container");
91152
}
92153

154+
/**
155+
* Create a new {@link Condition} that matches if and only if an
156+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor} is
157+
* a {@linkplain TestDescriptor#isContainer() container} and its
158+
* {@linkplain TestDescriptor#getUniqueId() unique id} contains the
159+
* simple name of the supplied {@link Class} as well as the fully-qualified
160+
* name of its {@linkplain Class#getEnclosingClass() enclosing class}.
161+
*/
93162
public static Condition<Event> nestedContainer(Class<?> clazz) {
94-
return Assertions.allOf(container(uniqueIdSubstring(clazz.getEnclosingClass().getName())),
95-
container(uniqueIdSubstring(clazz.getSimpleName())));
163+
return Assertions.allOf(container(clazz.getEnclosingClass()), uniqueIdSubstring(clazz.getSimpleName()));
96164
}
97165

166+
/**
167+
* Create a new {@link Condition} that matches if and only if an
168+
* {@link Event}'s {@linkplain Event#getType() type} is
169+
* {@link EventType#DYNAMIC_TEST_REGISTERED} and its
170+
* {@linkplain TestDescriptor#getUniqueId() unique id} contains the
171+
* supplied {@link String}.
172+
*/
98173
public static Condition<Event> dynamicTestRegistered(String uniqueIdSubstring) {
99174
return dynamicTestRegistered(uniqueIdSubstring(uniqueIdSubstring));
100175
}
101176

177+
/**
178+
* Create a new {@link Condition} that matches if and only if an
179+
* {@link Event}'s {@linkplain Event#getType() type} is
180+
* {@link EventType#DYNAMIC_TEST_REGISTERED} and it matches the supplied
181+
* {@code Condition}.
182+
*/
102183
public static Condition<Event> dynamicTestRegistered(Condition<Event> condition) {
103184
return Assertions.allOf(type(DYNAMIC_TEST_REGISTERED), condition);
104185
}
105186

187+
/**
188+
* Create a new {@link Condition} that matches if and only if the
189+
* {@linkplain TestDescriptor#getUniqueId() unique id} of an
190+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor}
191+
* contains the supplied {@link String}.
192+
*/
106193
public static Condition<Event> uniqueIdSubstring(String uniqueIdSubstring) {
107194
Predicate<UniqueId.Segment> predicate = segment -> {
108195
String text = segment.getType() + ":" + segment.getValue();
@@ -115,36 +202,107 @@ public static Condition<Event> uniqueIdSubstring(String uniqueIdSubstring) {
115202
"descriptor with uniqueId substring '%s'", uniqueIdSubstring);
116203
}
117204

205+
/**
206+
* Create a new {@link Condition} that matches if and only if the
207+
* {@linkplain TestDescriptor#getDisplayName()} display name} of an
208+
* {@link Event}'s {@linkplain Event#getTestDescriptor() test descriptor}
209+
* is equal to the supplied {@link String}.
210+
*/
118211
public static Condition<Event> displayName(String displayName) {
119212
return new Condition<>(byTestDescriptor(where(TestDescriptor::getDisplayName, isEqual(displayName))),
120213
"descriptor with display name '%s'", displayName);
121214
}
122215

216+
/**
217+
* Create a new {@link Condition} that matches if and only if an
218+
* {@link Event}'s {@linkplain Event#getType() type} is
219+
* {@link EventType#SKIPPED} and the
220+
* {@linkplain Event#getPayload() reason} is equal to the supplied
221+
* {@link String}.
222+
*
223+
* @see #reason(String)
224+
*/
123225
public static Condition<Event> skippedWithReason(String expectedReason) {
124226
return Assertions.allOf(type(SKIPPED), reason(expectedReason));
125227
}
126228

229+
/**
230+
* Create a new {@link Condition} that matches if and only if an
231+
* {@link Event}'s {@linkplain Event#getType() type} is
232+
* {@link EventType#SKIPPED} and the
233+
* {@linkplain Event#getPayload() reason} matches the supplied
234+
* {@link Predicate}.
235+
*
236+
* @see #reason(Predicate)
237+
*/
127238
public static Condition<Event> skippedWithReason(Predicate<String> predicate) {
128239
return Assertions.allOf(type(SKIPPED), reason(predicate));
129240
}
130241

242+
/**
243+
* Create a new {@link Condition} that matches if and only if an
244+
* {@link Event}'s {@linkplain Event#getType() type} is
245+
* {@link EventType#STARTED}.
246+
*
247+
* @see #reason(Predicate)
248+
*/
131249
public static Condition<Event> started() {
132250
return type(STARTED);
133251
}
134252

253+
/**
254+
* Create a new {@link Condition} that matches if and only if an
255+
* {@link Event}'s {@linkplain Event#getType() type} is
256+
* {@link EventType#FINISHED} and its
257+
* {@linkplain Event#getPayload() result} has a
258+
* {@linkplain TestExecutionResult#getStatus() status} of
259+
* {@link TestExecutionResult.Status#ABORTED ABORTED} as well as a
260+
* {@linkplain TestExecutionResult#getThrowable() cause} that matches the
261+
* supplied {@code Condition}.
262+
*/
135263
public static Condition<Event> abortedWithReason(Condition<? super Throwable> causeCondition) {
136264
return finishedWithCause(ABORTED, causeCondition);
137265
}
138266

267+
/**
268+
* Create a new {@link Condition} that matches if and only if an
269+
* {@link Event}'s {@linkplain Event#getType() type} is
270+
* {@link EventType#FINISHED} and its
271+
* {@linkplain Event#getPayload() result} has a
272+
* {@linkplain TestExecutionResult#getStatus() status} of
273+
* {@link TestExecutionResult.Status#ABORTED ABORTED} as well as a
274+
* {@linkplain TestExecutionResult#getThrowable() cause} that matches all of
275+
* the supplied {@code Conditions}.
276+
*/
139277
@SafeVarargs
140278
public static Condition<Event> abortedWithReason(Condition<Throwable>... conditions) {
141279
return finishedWithCause(ABORTED, conditions);
142280
}
143281

282+
/**
283+
* Create a new {@link Condition} that matches if and only if an
284+
* {@link Event}'s {@linkplain Event#getType() type} is
285+
* {@link EventType#FINISHED} and its
286+
* {@linkplain Event#getPayload() result} has a
287+
* {@linkplain TestExecutionResult#getStatus() status} of
288+
* {@link TestExecutionResult.Status#FAILED FAILED} as well as a
289+
* {@linkplain TestExecutionResult#getThrowable() cause} that matches the
290+
* supplied {@code Condition}.
291+
*/
144292
public static Condition<Event> finishedWithFailure(Condition<? super Throwable> causeCondition) {
145293
return finishedWithCause(FAILED, causeCondition);
146294
}
147295

296+
/**
297+
* Create a new {@link Condition} that matches if and only if an
298+
* {@link Event}'s {@linkplain Event#getType() type} is
299+
* {@link EventType#FINISHED} and its
300+
* {@linkplain Event#getPayload() result} has a
301+
* {@linkplain TestExecutionResult#getStatus() status} of
302+
* {@link TestExecutionResult.Status#FAILED FAILED} as well as a
303+
* {@linkplain TestExecutionResult#getThrowable() cause} that matches all of
304+
* the supplied {@code Conditions}.
305+
*/
148306
@SafeVarargs
149307
public static Condition<Event> finishedWithFailure(Condition<Throwable>... conditions) {
150308
return finishedWithCause(FAILED, conditions);
@@ -169,32 +327,75 @@ private static Condition<Event> finishedWithCause(Status expectedStatus,
169327
TestExecutionResultConditions.throwable(causeCondition)));
170328
}
171329

330+
/**
331+
* Create a new {@link Condition} that matches if and only if an
332+
* {@link Event}'s {@linkplain Event#getType() type} is
333+
* {@link EventType#FINISHED} and its
334+
* {@linkplain Event#getPayload() result} has a
335+
* {@linkplain TestExecutionResult#getStatus() status} of
336+
* {@link TestExecutionResult.Status#FAILED FAILED}.
337+
*/
172338
public static Condition<Event> finishedWithFailure() {
173339
return finished(TestExecutionResultConditions.status(FAILED));
174340
}
175341

342+
/**
343+
* Create a new {@link Condition} that matches if and only if an
344+
* {@link Event}'s {@linkplain Event#getType() type} is
345+
* {@link EventType#FINISHED} and its
346+
* {@linkplain Event#getPayload() result} has a
347+
* {@linkplain TestExecutionResult#getStatus() status} of
348+
* {@link TestExecutionResult.Status#SUCCESSFUL SUCCESSFUL}.
349+
*/
176350
public static Condition<Event> finishedSuccessfully() {
177351
return finished(TestExecutionResultConditions.status(SUCCESSFUL));
178352
}
179353

354+
/**
355+
* Create a new {@link Condition} that matches if and only if an
356+
* {@link Event}'s {@linkplain Event#getType() type} is
357+
* {@link EventType#FINISHED} and its
358+
* {@linkplain Event#getPayload() payload} is an instance of
359+
* {@link TestExecutionResult} that matches the supplied {@code Condition}.
360+
*/
180361
public static Condition<Event> finished(Condition<TestExecutionResult> resultCondition) {
181362
return Assertions.allOf(type(FINISHED), result(resultCondition));
182363
}
183364

365+
/**
366+
* Create a new {@link Condition} that matches if and only if an
367+
* {@link Event}'s {@linkplain Event#getType() type} is equal to the
368+
* supplied {@link EventType}.
369+
*/
184370
public static Condition<Event> type(EventType expectedType) {
185371
return new Condition<>(byType(expectedType), "type is %s", expectedType);
186372
}
187373

374+
/**
375+
* Create a new {@link Condition} that matches if and only if an
376+
* {@link Event}'s {@linkplain Event#getPayload() payload} is an instance of
377+
* {@link TestExecutionResult} that matches the supplied {@code Condition}.
378+
*/
188379
public static Condition<Event> result(Condition<TestExecutionResult> condition) {
189380
return new Condition<>(byPayload(TestExecutionResult.class, condition::matches), "event with result where %s",
190381
condition);
191382
}
192383

384+
/**
385+
* Create a new {@link Condition} that matches if and only if an
386+
* {@link Event}'s {@linkplain Event#getPayload() payload} is an instance of
387+
* {@link String} that is equal to the supplied value.
388+
*/
193389
public static Condition<Event> reason(String expectedReason) {
194390
return new Condition<>(byPayload(String.class, isEqual(expectedReason)), "event with reason '%s'",
195391
expectedReason);
196392
}
197393

394+
/**
395+
* Create a new {@link Condition} that matches if and only if an
396+
* {@link Event}'s {@linkplain Event#getPayload() payload} is an instance of
397+
* {@link String} that matches the supplied {@link Predicate}.
398+
*/
198399
public static Condition<Event> reason(Predicate<String> predicate) {
199400
return new Condition<>(byPayload(String.class, predicate), "event with custom reason predicate");
200401
}

0 commit comments

Comments
 (0)