|
10 | 10 |
|
11 | 11 | package org.junit.platform.launcher.core; |
12 | 12 |
|
13 | | -import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
14 | 16 | import static org.junit.platform.engine.support.store.NamespacedHierarchicalStore.CloseAction.closeAutoCloseables; |
15 | 17 |
|
16 | 18 | import java.util.List; |
| 19 | +import java.util.function.Supplier; |
| 20 | +import java.util.stream.Stream; |
17 | 21 |
|
18 | | -import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.Arguments; |
| 24 | +import org.junit.jupiter.params.provider.MethodSource; |
| 25 | +import org.junit.platform.commons.PreconditionViolationException; |
19 | 26 | import org.junit.platform.engine.support.store.Namespace; |
20 | 27 | import org.junit.platform.engine.support.store.NamespacedHierarchicalStore; |
21 | 28 | import org.junit.platform.fakes.TestEngineStub; |
|
30 | 37 | @SuppressWarnings("NullAway") |
31 | 38 | class LauncherPreconditionTests { |
32 | 39 |
|
33 | | - private final Launcher launcher = LauncherFactoryForTestingPurposesOnly.createLauncher(new TestEngineStub()); |
34 | | - |
35 | | - @Test |
36 | | - @SuppressWarnings("DataFlowIssue") |
37 | | - void sessionPerRequestLauncherRejectsNullDiscoveryRequest() { |
38 | | - assertPreconditionViolationFor(() -> launcher.discover(null)) // |
39 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
| 40 | + @ParameterizedTest(name = "{0}") |
| 41 | + @MethodSource("launcherSuppliers") |
| 42 | + void launcherRejectsNullRequests(String displayName, Supplier<Launcher> launcherSupplier) { |
| 43 | + assertRejectsNullRequests(launcherSupplier.get()); |
40 | 44 | } |
41 | 45 |
|
42 | | - @Test |
43 | | - @SuppressWarnings("DataFlowIssue") |
44 | | - void sessionPerRequestLauncherRejectsNullExecutionRequests() { |
45 | | - assertPreconditionViolationFor(() -> launcher.execute((LauncherDiscoveryRequest) null)) // |
46 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
47 | | - assertPreconditionViolationFor(() -> launcher.execute((TestPlan) null)) // |
48 | | - .withMessageContaining("TestPlan must not be null"); |
49 | | - assertPreconditionViolationFor(() -> launcher.execute((LauncherExecutionRequest) null)) // |
50 | | - .withMessageContaining("LauncherExecutionRequest must not be null"); |
| 46 | + private static Stream<Arguments> launcherSuppliers() { |
| 47 | + return Stream.of( |
| 48 | + Arguments.of("session-per-request launcher", |
| 49 | + (Supplier<Launcher>) () -> LauncherFactoryForTestingPurposesOnly |
| 50 | + .createLauncher(new TestEngineStub())), |
| 51 | + Arguments.of("default launcher", |
| 52 | + (Supplier<Launcher>) () -> new DefaultLauncher(List.of(new TestEngineStub()), List.of(), |
| 53 | + new NamespacedHierarchicalStore<Namespace>(null, closeAutoCloseables()))), |
| 54 | + Arguments.of("delegating launcher", (Supplier<Launcher>) () -> new DelegatingLauncher(new NoOpLauncher())), |
| 55 | + Arguments.of("intercepting launcher", |
| 56 | + (Supplier<Launcher>) () -> new InterceptingLauncher(new NoOpLauncher(), |
| 57 | + new NoOpLauncherInterceptor()))); |
51 | 58 | } |
52 | 59 |
|
53 | | - @Test |
54 | | - @SuppressWarnings("DataFlowIssue") |
55 | | - void defaultLauncherRejectsNullRequests() { |
56 | | - var defaultLauncher = new DefaultLauncher(List.of(new TestEngineStub()), List.of(), |
57 | | - new NamespacedHierarchicalStore<Namespace>(null, closeAutoCloseables())); |
58 | | - |
59 | | - assertPreconditionViolationFor(() -> defaultLauncher.discover(null)) // |
60 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
61 | | - assertPreconditionViolationFor(() -> defaultLauncher.execute((LauncherDiscoveryRequest) null)) // |
62 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
63 | | - assertPreconditionViolationFor(() -> defaultLauncher.execute((TestPlan) null)) // |
64 | | - .withMessageContaining("TestPlan must not be null"); |
65 | | - assertPreconditionViolationFor(() -> defaultLauncher.execute((LauncherExecutionRequest) null)) // |
66 | | - .withMessageContaining("LauncherExecutionRequest must not be null"); |
| 60 | + private static void assertRejectsNullRequests(Launcher launcher) { |
| 61 | + assertPreconditionViolationExactly(() -> launcher.discover(nullValue(LauncherDiscoveryRequest.class)), |
| 62 | + "LauncherDiscoveryRequest must not be null"); |
| 63 | + assertPreconditionViolationExactly(() -> launcher.execute(nullValue(LauncherDiscoveryRequest.class)), |
| 64 | + "LauncherDiscoveryRequest must not be null"); |
| 65 | + assertPreconditionViolationExactly(() -> launcher.execute(nullValue(TestPlan.class)), "TestPlan must not be null"); |
| 66 | + assertPreconditionViolationExactly(() -> launcher.execute(nullValue(LauncherExecutionRequest.class)), |
| 67 | + "LauncherExecutionRequest must not be null"); |
67 | 68 | } |
68 | 69 |
|
69 | | - @Test |
70 | | - @SuppressWarnings("DataFlowIssue") |
71 | | - void delegatingLauncherRejectsNullRequests() { |
72 | | - var delegatingLauncher = new DelegatingLauncher(new NoOpLauncher()); |
73 | | - |
74 | | - assertPreconditionViolationFor(() -> delegatingLauncher.discover(null)) // |
75 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
76 | | - assertPreconditionViolationFor(() -> delegatingLauncher.execute((LauncherDiscoveryRequest) null)) // |
77 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
78 | | - assertPreconditionViolationFor(() -> delegatingLauncher.execute((TestPlan) null)) // |
79 | | - .withMessageContaining("TestPlan must not be null"); |
80 | | - assertPreconditionViolationFor(() -> delegatingLauncher.execute((LauncherExecutionRequest) null)) // |
81 | | - .withMessageContaining("LauncherExecutionRequest must not be null"); |
| 70 | + private static void assertPreconditionViolationExactly(Runnable action, String expectedMessage) { |
| 71 | + var ex = assertThrows(PreconditionViolationException.class, action::run); |
| 72 | + assertEquals(PreconditionViolationException.class, ex.getClass()); |
| 73 | + assertEquals(expectedMessage, ex.getMessage()); |
| 74 | + assertNull(ex.getCause()); |
82 | 75 | } |
83 | 76 |
|
84 | | - @Test |
85 | | - @SuppressWarnings("DataFlowIssue") |
86 | | - void interceptingLauncherRejectsNullRequests() { |
87 | | - var interceptingLauncher = new InterceptingLauncher(new NoOpLauncher(), new NoOpLauncherInterceptor()); |
88 | | - |
89 | | - assertPreconditionViolationFor(() -> interceptingLauncher.discover(null)) // |
90 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
91 | | - assertPreconditionViolationFor(() -> interceptingLauncher.execute((LauncherDiscoveryRequest) null)) // |
92 | | - .withMessageContaining("LauncherDiscoveryRequest must not be null"); |
93 | | - assertPreconditionViolationFor(() -> interceptingLauncher.execute((TestPlan) null)) // |
94 | | - .withMessageContaining("TestPlan must not be null"); |
95 | | - assertPreconditionViolationFor(() -> interceptingLauncher.execute((LauncherExecutionRequest) null)) // |
96 | | - .withMessageContaining("LauncherExecutionRequest must not be null"); |
| 77 | + /** |
| 78 | + * Produces a typed {@code null} to avoid overload ambiguity and centralize nullability suppressions. |
| 79 | + */ |
| 80 | + @SuppressWarnings({ "DataFlowIssue", "NullAway", "unused" }) |
| 81 | + private static <T> T nullValue(Class<T> type) { |
| 82 | + return null; |
97 | 83 | } |
98 | 84 |
|
99 | 85 | private static final class NoOpLauncher implements Launcher { |
|
0 commit comments