|
10 | 10 |
|
11 | 11 | package org.junit.platform.testkit.engine; |
12 | 12 |
|
| 13 | +import static java.util.Collections.sort; |
13 | 14 | import static java.util.function.Predicate.isEqual; |
14 | 15 | import static java.util.stream.Collectors.toList; |
15 | 16 | import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
|
20 | 21 | import java.io.OutputStream; |
21 | 22 | import java.io.PrintWriter; |
22 | 23 | import java.io.Writer; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
23 | 26 | import java.util.Collections; |
24 | 27 | import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Optional; |
25 | 30 | import java.util.function.Consumer; |
26 | 31 | import java.util.function.Function; |
27 | 32 | import java.util.function.Predicate; |
|
36 | 41 | import org.junit.platform.commons.util.Preconditions; |
37 | 42 | import org.junit.platform.engine.TestExecutionResult; |
38 | 43 | import org.junit.platform.engine.TestExecutionResult.Status; |
| 44 | +import org.opentest4j.AssertionFailedError; |
39 | 45 |
|
40 | 46 | /** |
41 | 47 | * {@code Events} is a facade that provides a fluent API for working with |
@@ -259,6 +265,67 @@ public final void assertEventsMatchExactly(Condition<? super Event>... condition |
259 | 265 | assertEventsMatchExactly(this.events, conditions); |
260 | 266 | } |
261 | 267 |
|
| 268 | + /** |
| 269 | + * Assert that all provided conditions are matched by an {@linkplain Event event} |
| 270 | + * contained in this {@code Events} object regardless of order. |
| 271 | + * Note that this method does a partial match, i.e. some events may not match any |
| 272 | + * of the provided conditions. |
| 273 | + * |
| 274 | + * <p>Conditions can be imported statically from {@link EventConditions} |
| 275 | + * and {@link TestExecutionResultConditions}. |
| 276 | + * |
| 277 | + * <h4>Example</h4> |
| 278 | + * |
| 279 | + * <pre class="code"> |
| 280 | + * executionResults.testEvents().assertEventsMatchLoosely( |
| 281 | + * event(test("exampleTestMethod"), started()), |
| 282 | + * event(test("exampleTestMethod"), finishedSuccessfully()) |
| 283 | + * ); |
| 284 | + * </pre> |
| 285 | + * |
| 286 | + * @param conditions the conditions to match against; never {@code null} |
| 287 | + * @see EventConditions |
| 288 | + * @see TestExecutionResultConditions |
| 289 | + */ |
| 290 | + @SafeVarargs |
| 291 | + @SuppressWarnings("varargs") |
| 292 | + public final void assertEventsMatchLoosely(Condition<? super Event>... conditions) { |
| 293 | + Preconditions.notNull(conditions, "conditions must not be null"); |
| 294 | + Preconditions.containsNoNullElements(conditions, "conditions must not contain null elements"); |
| 295 | + assertEventsMatchLoosely(this.events, conditions); |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * Assert that all provided conditions are matched by an {@linkplain Event event} |
| 300 | + * contained in this {@code Events} object. |
| 301 | + * Note that this method does a partial match, i.e. some events may not match any |
| 302 | + * of the provided conditions. |
| 303 | + * However, the conditions provided must be in the correct order. |
| 304 | + * |
| 305 | + * <p>Conditions can be imported statically from {@link EventConditions} |
| 306 | + * and {@link TestExecutionResultConditions}. |
| 307 | + * |
| 308 | + * <h4>Example</h4> |
| 309 | + * |
| 310 | + * <pre class="code"> |
| 311 | + * executionResults.testEvents().assertEventsMatchLooselyInOrder( |
| 312 | + * event(test("exampleTestMethod"), started()), |
| 313 | + * event(test("exampleTestMethod"), finishedSuccessfully()) |
| 314 | + * ); |
| 315 | + * </pre> |
| 316 | + * |
| 317 | + * @param conditions the conditions to match against; never {@code null} |
| 318 | + * @see EventConditions |
| 319 | + * @see TestExecutionResultConditions |
| 320 | + */ |
| 321 | + @SafeVarargs |
| 322 | + @SuppressWarnings("varargs") |
| 323 | + public final void assertEventsMatchLooselyInOrder(Condition<? super Event>... conditions) { |
| 324 | + Preconditions.notNull(conditions, "conditions must not be null"); |
| 325 | + Preconditions.containsNoNullElements(conditions, "conditions must not contain null elements"); |
| 326 | + assertEventsMatchLooselyInOrder(this.events, conditions); |
| 327 | + } |
| 328 | + |
262 | 329 | /** |
263 | 330 | * Shortcut for {@code org.assertj.core.api.Assertions.assertThat(events.list())}. |
264 | 331 | * |
@@ -336,4 +403,63 @@ private static void assertEventsMatchExactly(List<Event> events, Condition<? sup |
336 | 403 | softly.assertAll(); |
337 | 404 | } |
338 | 405 |
|
| 406 | + @SafeVarargs |
| 407 | + private static void assertEventsMatchLoosely(List<Event> events, Condition<? super Event>... conditions) { |
| 408 | + SoftAssertions softly = new SoftAssertions(); |
| 409 | + for (Condition<? super Event> condition : conditions) { |
| 410 | + checkCondition(events, softly, condition); |
| 411 | + } |
| 412 | + softly.assertAll(); |
| 413 | + } |
| 414 | + |
| 415 | + @SafeVarargs |
| 416 | + @SuppressWarnings("varargs") |
| 417 | + private static void assertEventsMatchLooselyInOrder(List<Event> events, Condition<? super Event>... conditions) { |
| 418 | + Assertions.assertThat(conditions).hasSizeLessThanOrEqualTo(events.size()); |
| 419 | + SoftAssertions softly = new SoftAssertions(); |
| 420 | + |
| 421 | + // @formatter:off |
| 422 | + List<Integer> indices = Arrays.stream(conditions) |
| 423 | + .map(condition -> findEvent(events, softly, condition)) |
| 424 | + .filter(Objects::nonNull) |
| 425 | + .map(events::indexOf) |
| 426 | + .collect(toList()); |
| 427 | + // @formatter:on |
| 428 | + |
| 429 | + if (isNotInIncreasingOrder(indices)) { |
| 430 | + throw new AssertionFailedError("Conditions are not in the correct order."); |
| 431 | + } |
| 432 | + |
| 433 | + softly.assertAll(); |
| 434 | + } |
| 435 | + |
| 436 | + private static boolean isNotInIncreasingOrder(List<Integer> indices) { |
| 437 | + List<Integer> copy = new ArrayList<>(indices); |
| 438 | + sort(copy); |
| 439 | + |
| 440 | + return !indices.equals(copy); |
| 441 | + } |
| 442 | + |
| 443 | + private static void checkCondition(List<Event> events, SoftAssertions softly, Condition<? super Event> condition) { |
| 444 | + boolean matches = events.stream().anyMatch(condition::matches); |
| 445 | + |
| 446 | + if (!matches) { |
| 447 | + softly.fail("Condition did not match any event: " + condition); |
| 448 | + } |
| 449 | + } |
| 450 | + |
| 451 | + private static Event findEvent(List<Event> events, SoftAssertions softly, Condition<? super Event> condition) { |
| 452 | + // @formatter:off |
| 453 | + Optional<Event> matchedEvent = events.stream() |
| 454 | + .filter(condition::matches) |
| 455 | + .findFirst(); |
| 456 | + // @formatter:on |
| 457 | + |
| 458 | + if (!matchedEvent.isPresent()) { |
| 459 | + softly.fail("Condition did not match any event: " + condition); |
| 460 | + } |
| 461 | + |
| 462 | + return matchedEvent.orElse(null); |
| 463 | + } |
| 464 | + |
339 | 465 | } |
0 commit comments