|
1 | | -/* |
2 | | - * Copyright 2017 Tobias Stadler |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, |
11 | | - * software distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and limitations under the License. |
14 | | - */ |
15 | | -package org.pitest.junit5; |
16 | | - |
17 | | -import java.util.Optional; |
18 | | - |
19 | | -import org.junit.platform.engine.TestExecutionResult; |
20 | | -import org.junit.platform.engine.discovery.DiscoverySelectors; |
21 | | -import org.junit.platform.engine.support.descriptor.MethodSource; |
22 | | -import org.junit.platform.launcher.Launcher; |
23 | | -import org.junit.platform.launcher.LauncherDiscoveryRequest; |
24 | | -import org.junit.platform.launcher.TestExecutionListener; |
25 | | -import org.junit.platform.launcher.TestIdentifier; |
26 | | -import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
27 | | -import org.junit.platform.launcher.core.LauncherFactory; |
28 | | -import org.pitest.testapi.AbstractTestUnit; |
29 | | -import org.pitest.testapi.Description; |
30 | | -import org.pitest.testapi.ResultCollector; |
31 | | - |
32 | | -/** |
33 | | - * |
34 | | - * @author Tobias Stadler |
35 | | - */ |
36 | | -public class JUnit5TestUnit extends AbstractTestUnit { |
37 | | - |
38 | | - private final Class<?> testClass; |
39 | | - |
40 | | - private final TestIdentifier testIdentifier; |
41 | | - |
42 | | - public JUnit5TestUnit(Class<?> testClass, TestIdentifier testIdentifier) { |
43 | | - super(new Description(testIdentifier.getDisplayName(), testClass)); |
44 | | - this.testClass = testClass; |
45 | | - this.testIdentifier = testIdentifier; |
46 | | - } |
47 | | - |
48 | | - @Override |
49 | | - public void execute(ResultCollector resultCollector) { |
50 | | - Launcher launcher = LauncherFactory.create(); |
51 | | - LauncherDiscoveryRequest launcherDiscoveryRequest = LauncherDiscoveryRequestBuilder |
52 | | - .request() |
53 | | - .selectors(DiscoverySelectors.selectUniqueId(testIdentifier.getUniqueId())) |
54 | | - .build(); |
55 | | - |
56 | | - launcher.registerTestExecutionListeners(new TestExecutionListener() { |
57 | | - @Override |
58 | | - public void executionSkipped(TestIdentifier testIdentifier, String reason) { |
59 | | - testIdentifier.getSource().ifPresent(testSource -> { |
60 | | - if (testSource instanceof MethodSource) { |
61 | | - resultCollector.notifySkipped(new Description(testIdentifier.getDisplayName(), testClass)); |
62 | | - } |
63 | | - }); |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public void executionStarted(TestIdentifier testIdentifier) { |
68 | | - testIdentifier.getSource().ifPresent(testSource -> { |
69 | | - if (testSource instanceof MethodSource) { |
70 | | - resultCollector.notifyStart(new Description(testIdentifier.getDisplayName(), testClass)); |
71 | | - } |
72 | | - }); |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { |
77 | | - testIdentifier.getSource().ifPresent(testSource -> { |
78 | | - if (testSource instanceof MethodSource) { |
79 | | - Optional<Throwable> throwable = testExecutionResult.getThrowable(); |
80 | | - |
81 | | - if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) { |
82 | | - // abort treated as success |
83 | | - // see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html |
84 | | - resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); |
85 | | - } else if (throwable.isPresent()) { |
86 | | - resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass), throwable.get()); |
87 | | - } else { |
88 | | - resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); |
89 | | - } |
90 | | - } |
91 | | - }); |
92 | | - } |
93 | | - |
94 | | - }); |
95 | | - launcher.execute(launcherDiscoveryRequest); |
96 | | - } |
97 | | - |
98 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2017 Tobias Stadler |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package org.pitest.junit5; |
| 16 | + |
| 17 | +import org.junit.platform.engine.TestExecutionResult; |
| 18 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 19 | +import org.junit.platform.engine.support.descriptor.MethodSource; |
| 20 | +import org.junit.platform.launcher.Launcher; |
| 21 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 22 | +import org.junit.platform.launcher.TestExecutionListener; |
| 23 | +import org.junit.platform.launcher.TestIdentifier; |
| 24 | +import org.junit.platform.launcher.TestPlan; |
| 25 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 26 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 27 | +import org.pitest.testapi.AbstractTestUnit; |
| 28 | +import org.pitest.testapi.Description; |
| 29 | +import org.pitest.testapi.ResultCollector; |
| 30 | + |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Tobias Stadler |
| 35 | + */ |
| 36 | +public class JUnit5TestUnit extends AbstractTestUnit { |
| 37 | + |
| 38 | + private final Class<?> testClass; |
| 39 | + |
| 40 | + private final TestIdentifier testIdentifier; |
| 41 | + |
| 42 | + public JUnit5TestUnit(Class<?> testClass, TestIdentifier testIdentifier) { |
| 43 | + super(new Description(testIdentifier.getDisplayName(), testClass)); |
| 44 | + this.testClass = testClass; |
| 45 | + this.testIdentifier = testIdentifier; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void execute(ResultCollector resultCollector) { |
| 50 | + Launcher launcher = LauncherFactory.create(); |
| 51 | + |
| 52 | + String executionId = getExecutionId(resultCollector); |
| 53 | + LauncherDiscoveryRequest launcherDiscoveryRequest = LauncherDiscoveryRequestBuilder |
| 54 | + .request() |
| 55 | + .configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true") |
| 56 | + .configurationParameter(ConditionalTestFilter.EXECUTION_ID_KEY, executionId) |
| 57 | + .selectors(DiscoverySelectors.selectUniqueId(testIdentifier.getUniqueId())) |
| 58 | + .build(); |
| 59 | + |
| 60 | + // todo: shall we inject the registry? |
| 61 | + TestUnitExecutionsRegistry executionsRegistry = TestUnitExecutionsRegistry.getInstance(); |
| 62 | + launcher.registerTestExecutionListeners(new TestExecutionListener() { |
| 63 | + @Override |
| 64 | + public void testPlanExecutionStarted(TestPlan testPlan) { |
| 65 | + executionsRegistry.add(executionId); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void testPlanExecutionFinished(TestPlan testPlan) { |
| 70 | + executionsRegistry.remove(executionId); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void executionSkipped(TestIdentifier testIdentifier, String reason) { |
| 75 | + testIdentifier.getSource().ifPresent(testSource -> { |
| 76 | + if (testSource instanceof MethodSource) { |
| 77 | + resultCollector |
| 78 | + .notifySkipped(new Description(testIdentifier.getDisplayName(), testClass)); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void executionStarted(TestIdentifier testIdentifier) { |
| 85 | + testIdentifier.getSource().ifPresent(testSource -> { |
| 86 | + if (testSource instanceof MethodSource) { |
| 87 | + resultCollector |
| 88 | + .notifyStart(new Description(testIdentifier.getDisplayName(), testClass)); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void executionFinished(TestIdentifier testIdentifier, |
| 95 | + TestExecutionResult testExecutionResult) { |
| 96 | + testIdentifier.getSource().ifPresent(testSource -> { |
| 97 | + if (testSource instanceof MethodSource) { |
| 98 | + Optional<Throwable> throwable = testExecutionResult.getThrowable(); |
| 99 | + |
| 100 | + if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) { |
| 101 | + // abort treated as success |
| 102 | + // see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html |
| 103 | + resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); |
| 104 | + } else if (throwable.isPresent()) { |
| 105 | + resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass), |
| 106 | + throwable.get()); |
| 107 | + |
| 108 | + if (resultCollector.shouldExit()) { |
| 109 | + executionsRegistry.abortExecution(executionId); |
| 110 | + } |
| 111 | + } else { |
| 112 | + resultCollector |
| 113 | + .notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + }); |
| 120 | + launcher.execute(launcherDiscoveryRequest); |
| 121 | + } |
| 122 | + |
| 123 | + private String getExecutionId(ResultCollector resultCollector) { |
| 124 | + return testIdentifier.getUniqueId() + ":" |
| 125 | + + System.identityHashCode(resultCollector); |
| 126 | + } |
| 127 | +} |
0 commit comments