2323import static org .junit .platform .launcher .listeners .UniqueIdTrackingListener .LISTENER_ENABLED_PROPERTY_NAME ;
2424import static org .junit .platform .launcher .listeners .UniqueIdTrackingListener .OUTPUT_DIR_PROPERTY_NAME ;
2525import static org .junit .platform .launcher .listeners .UniqueIdTrackingListener .OUTPUT_FILE_PREFIX_PROPERTY_NAME ;
26+ import static org .junit .platform .launcher .listeners .UniqueIdTrackingListener .WORKING_DIR_PROPERTY_NAME ;
2627import static org .junit .platform .testkit .engine .Event .byTestDescriptor ;
2728import static org .junit .platform .testkit .engine .EventConditions .abortedWithReason ;
2829import static org .junit .platform .testkit .engine .EventConditions .event ;
3536import java .io .UncheckedIOException ;
3637import java .nio .file .Files ;
3738import java .nio .file .Path ;
38- import java .nio .file .Paths ;
3939import java .util .ArrayList ;
4040import java .util .HashMap ;
4141import java .util .List ;
4444import java .util .stream .Stream ;
4545
4646import org .assertj .core .api .Condition ;
47+ import org .junit .jupiter .api .BeforeEach ;
4748import org .junit .jupiter .api .Disabled ;
4849import org .junit .jupiter .api .DynamicTest ;
4950import org .junit .jupiter .api .Nested ;
5051import org .junit .jupiter .api .Test ;
5152import org .junit .jupiter .api .TestFactory ;
53+ import org .junit .jupiter .api .io .TempDir ;
5254import org .junit .platform .engine .TestDescriptor ;
5355import org .junit .platform .engine .TestExecutionResult ;
5456import org .junit .platform .engine .discovery .ClassSelector ;
55- import org .junit .platform .launcher .LauncherDiscoveryRequest ;
5657import org .junit .platform .launcher .TestExecutionListener ;
5758import org .junit .platform .launcher .TestIdentifier ;
5859import org .junit .platform .launcher .TestPlan ;
@@ -88,6 +89,15 @@ class UniqueIdTrackingListenerIntegrationTests {
8889
8990 private static final String [] expectedConcurrentUniqueIds = { testA , testB , testC , testD , testE , testF };
9091
92+ @ TempDir
93+ Path workingDir ;
94+
95+ @ BeforeEach
96+ void createFakeGradleFiles () throws Exception {
97+ Files .createFile (workingDir .resolve ("build.gradle" ));
98+ Files .createDirectory (workingDir .resolve ("build" ));
99+ }
100+
91101 @ Test
92102 void confirmExpectedUniqueIdsViaEngineTestKit () {
93103 // @formatter:off
@@ -116,28 +126,18 @@ private Condition<Event> uniqueId(String uniqueId) {
116126
117127 @ Test
118128 void listenerIsRegisteredButDisabledByDefault () throws Exception {
119- long numListenersRegistered = ServiceLoader .load (TestExecutionListener .class ).stream ()//
129+ var numListenersRegistered = ServiceLoader .load (TestExecutionListener .class ).stream ()//
120130 .filter (provider -> UniqueIdTrackingListener .class .equals (provider .type ()))//
121131 .count ();
122132 assertThat (numListenersRegistered ).isEqualTo (1 );
123133
124- String outputDir = "build" ;
125- String prefix = DEFAULT_OUTPUT_FILE_PREFIX ;
134+ var actualUniqueIds = executeTests (Map .of ());
126135
127- deleteFiles (outputDir , prefix );
136+ // Sanity check using the results of our local TestExecutionListener
137+ assertThat (actualUniqueIds ).containsExactlyInAnyOrder (expectedUniqueIds );
128138
129- try {
130- List <String > actualUniqueIds = executeTests (Map .of ());
131-
132- // Sanity check using the results of our local TestExecutionListener
133- assertThat (actualUniqueIds ).containsExactlyInAnyOrder (expectedUniqueIds );
134-
135- // Check that files were not generated by the UniqueIdTrackingListener
136- assertThat (findFiles (outputDir , prefix )).isEmpty ();
137- }
138- finally {
139- deleteFiles (outputDir , prefix );
140- }
139+ // Check that files were not generated by the UniqueIdTrackingListener
140+ assertThat (findFiles (workingDir , DEFAULT_OUTPUT_FILE_PREFIX )).isEmpty ();
141141 }
142142
143143 @ Test
@@ -147,20 +147,20 @@ void verifyUniqueIdsAreTrackedWithDefaults() throws Exception {
147147
148148 @ Test
149149 void verifyUniqueIdsAreTrackedWithCustomOutputFile () throws Exception {
150- String customPrefix = "test_ids" ;
150+ var customPrefix = "test_ids" ;
151151 verifyUniqueIdsAreTracked ("build" , customPrefix , Map .of (OUTPUT_FILE_PREFIX_PROPERTY_NAME , customPrefix ));
152152 }
153153
154154 @ Test
155155 void verifyUniqueIdsAreTrackedWithCustomOutputDir () throws Exception {
156- String customDir = "build/UniqueIdTrackingListenerIntegrationTests" ;
156+ var customDir = "build/UniqueIdTrackingListenerIntegrationTests" ;
157157 verifyUniqueIdsAreTracked (customDir , DEFAULT_OUTPUT_FILE_PREFIX , Map .of (OUTPUT_DIR_PROPERTY_NAME , customDir ));
158158 }
159159
160160 @ Test
161161 void verifyUniqueIdsAreTrackedWithCustomOutputFileAndCustomOutputDir () throws Exception {
162- String customPrefix = "test_ids" ;
163- String customDir = "build/UniqueIdTrackingListenerIntegrationTests" ;
162+ var customPrefix = "test_ids" ;
163+ var customDir = "build/UniqueIdTrackingListenerIntegrationTests" ;
164164
165165 verifyUniqueIdsAreTracked (customDir , customPrefix ,
166166 Map .of (OUTPUT_DIR_PROPERTY_NAME , customDir , OUTPUT_FILE_PREFIX_PROPERTY_NAME , customPrefix ));
@@ -172,59 +172,45 @@ private void verifyUniqueIdsAreTracked(String outputDir, String prefix, Map<Stri
172172 configurationParameters = new HashMap <>(configurationParameters );
173173 configurationParameters .put (LISTENER_ENABLED_PROPERTY_NAME , "true" );
174174
175- deleteFiles (outputDir , prefix );
176-
177- try {
178- List <String > actualUniqueIds = executeTests (configurationParameters );
175+ var actualUniqueIds = executeTests (configurationParameters );
179176
180- // Sanity check using the results of our local TestExecutionListener
181- assertThat (actualUniqueIds ).containsExactlyInAnyOrder (expectedUniqueIds );
177+ // Sanity check using the results of our local TestExecutionListener
178+ assertThat (actualUniqueIds ).containsExactlyInAnyOrder (expectedUniqueIds );
182179
183- // Check contents of the file (or files) generated by the UniqueIdTrackingListener
184- assertThat (readAllFiles (outputDir , prefix )).containsExactlyInAnyOrder (expectedUniqueIds );
185- }
186- finally {
187- deleteFiles (outputDir , prefix );
188- }
180+ // Check contents of the file (or files) generated by the UniqueIdTrackingListener
181+ assertThat (readAllFiles (workingDir .resolve (outputDir ), prefix )).containsExactlyInAnyOrder (expectedUniqueIds );
189182 }
190183
191184 @ Test
192185 void verifyUniqueIdsAreTrackedWithConcurrentlyExecutingTestPlans () throws Exception {
193- String customDir = "build/UniqueIdTrackingListenerIntegrationTests" ;
194- String prefix = DEFAULT_OUTPUT_FILE_PREFIX ;
186+ var customDir = workingDir . resolve ( "build/UniqueIdTrackingListenerIntegrationTests" ) ;
187+ var prefix = DEFAULT_OUTPUT_FILE_PREFIX ;
195188
196189 Map <String , String > configurationParameters = new HashMap <>();
197190 configurationParameters .put (LISTENER_ENABLED_PROPERTY_NAME , "true" );
198- configurationParameters .put (OUTPUT_DIR_PROPERTY_NAME , customDir );
199-
200- deleteFiles (customDir , prefix );
191+ configurationParameters .put (OUTPUT_DIR_PROPERTY_NAME , customDir .toAbsolutePath ().toString ());
201192
202- try {
203- Stream .of (TestCase2 .class , TestCase3 .class , TestCase4 .class ).parallel ()//
204- .forEach (clazz -> executeTests (configurationParameters , selectClass (clazz )));
193+ Stream .of (TestCase2 .class , TestCase3 .class , TestCase4 .class ).parallel ()//
194+ .forEach (clazz -> executeTests (configurationParameters , selectClass (clazz )));
205195
206- // 3 output files should have been generated.
207- assertThat (findFiles (customDir , prefix )).hasSize (3 );
196+ // 3 output files should have been generated.
197+ assertThat (findFiles (customDir , prefix )).hasSize (3 );
208198
209- // Check contents of the file (or files) generated by the UniqueIdTrackingListener
210- assertThat (readAllFiles (customDir , prefix )).containsExactlyInAnyOrder (expectedConcurrentUniqueIds );
211- }
212- finally {
213- deleteFiles (customDir , prefix );
214- }
199+ // Check contents of the file (or files) generated by the UniqueIdTrackingListener
200+ assertThat (readAllFiles (customDir , prefix )).containsExactlyInAnyOrder (expectedConcurrentUniqueIds );
215201 }
216202
217- private static List <String > executeTests (Map <String , String > configurationParameters ) {
203+ private List <String > executeTests (Map <String , String > configurationParameters ) {
218204 return executeTests (configurationParameters , selectClasses ());
219205 }
220206
221- private static List <String > executeTests (Map <String , String > configurationParameters ,
222- ClassSelector ... classSelectors ) {
207+ private List <String > executeTests (Map <String , String > configurationParameters , ClassSelector ... classSelectors ) {
223208 List <String > uniqueIds = new ArrayList <>();
224- LauncherDiscoveryRequest request = request ()//
209+ var request = request ()//
225210 .selectors (classSelectors )//
226211 .filters (includeEngines ("junit-jupiter" ))//
227212 .configurationParameters (configurationParameters )//
213+ .configurationParameter (WORKING_DIR_PROPERTY_NAME , workingDir .toAbsolutePath ().toString ())//
228214 .build ();
229215 LauncherFactory .create ().execute (request , new TestExecutionListener () {
230216
@@ -260,8 +246,7 @@ private static ClassSelector[] selectClasses() {
260246 selectClass (DisabledTestCase .class ) };
261247 }
262248
263- private static Stream <Path > findFiles (String dir , String prefix ) throws IOException {
264- Path outputDir = Paths .get (dir );
249+ private static Stream <Path > findFiles (Path outputDir , String prefix ) throws IOException {
265250 if (!Files .exists (outputDir )) {
266251 return Stream .empty ();
267252 }
@@ -270,18 +255,7 @@ private static Stream<Path> findFiles(String dir, String prefix) throws IOExcept
270255 && path .getFileName ().toString ().startsWith (prefix )));
271256 }
272257
273- private void deleteFiles (String outputDir , String prefix ) throws IOException {
274- findFiles (outputDir , prefix ).forEach (file -> {
275- try {
276- Files .deleteIfExists (file );
277- }
278- catch (IOException ex ) {
279- throw new UncheckedIOException (ex );
280- }
281- });
282- }
283-
284- private Stream <String > readAllFiles (String outputDir , String prefix ) throws IOException {
258+ private Stream <String > readAllFiles (Path outputDir , String prefix ) throws IOException {
285259 return findFiles (outputDir , prefix ).map (outputFile -> {
286260 try {
287261 return Files .readAllLines (outputFile );
@@ -306,6 +280,7 @@ void passingTest() {
306280 void skippedTest () {
307281 }
308282
283+ @ SuppressWarnings ("DataFlowIssue" )
309284 @ Test
310285 void abortedTest () {
311286 assumeTrue (false );
0 commit comments