37
37
import java .io .IOException ;
38
38
import java .lang .annotation .Retention ;
39
39
import java .lang .annotation .Target ;
40
+ import java .lang .reflect .AnnotatedElement ;
41
+ import java .lang .reflect .Constructor ;
42
+ import java .lang .reflect .Field ;
43
+ import java .lang .reflect .Method ;
44
+ import java .lang .reflect .Parameter ;
40
45
import java .nio .file .FileSystem ;
41
46
import java .nio .file .Path ;
42
47
import java .util .Optional ;
48
+ import java .util .logging .Level ;
49
+ import java .util .logging .LogRecord ;
43
50
44
51
import com .google .common .jimfs .Jimfs ;
45
52
49
56
import org .junit .jupiter .api .DisplayName ;
50
57
import org .junit .jupiter .api .Nested ;
51
58
import org .junit .jupiter .api .Test ;
59
+ import org .junit .jupiter .api .TestInfo ;
52
60
import org .junit .jupiter .api .condition .DisabledOnOs ;
53
61
import org .junit .jupiter .api .extension .AnnotatedElementContext ;
54
62
import org .junit .jupiter .api .extension .ExtensionConfigurationException ;
55
63
import org .junit .jupiter .api .extension .ExtensionContext ;
56
64
import org .junit .jupiter .api .extension .ExtensionContext .Namespace ;
65
+ import org .junit .jupiter .api .fixtures .TrackLogRecords ;
57
66
import org .junit .jupiter .api .io .CleanupMode ;
58
67
import org .junit .jupiter .api .io .TempDir ;
59
68
import org .junit .jupiter .api .io .TempDirFactory ;
62
71
import org .junit .jupiter .params .ParameterizedTest ;
63
72
import org .junit .jupiter .params .provider .ValueSource ;
64
73
import org .junit .platform .commons .PreconditionViolationException ;
74
+ import org .junit .platform .commons .logging .LogRecordListener ;
65
75
import org .junit .platform .engine .support .store .NamespacedHierarchicalStore ;
66
76
67
77
/**
@@ -273,7 +283,7 @@ void cleanupTempDirectory() throws IOException {
273
283
@ DisplayName ("is done for a cleanup mode of ALWAYS" )
274
284
@ ParameterizedTest
275
285
@ ElementTypeSource
276
- void always (Class <?> elementType ) throws IOException {
286
+ void always (Class <?> elementType , @ TrackLogRecords LogRecordListener listener ) throws IOException {
277
287
reset (factory );
278
288
279
289
closeablePath = TempDirectory .createTempDir (factory , ALWAYS , elementType , elementContext , extensionContext );
@@ -283,30 +293,75 @@ void always(Class<?> elementType) throws IOException {
283
293
284
294
verify (factory ).close ();
285
295
assertThat (closeablePath .get ()).doesNotExist ();
296
+ assertThat (listener .stream (Level .INFO )).map (LogRecord ::getMessage )//
297
+ .noneMatch (m -> m .startsWith ("Skipping cleanup of temp dir" ));
286
298
}
287
299
288
300
@ DisplayName ("is not done for a cleanup mode of NEVER" )
289
301
@ ParameterizedTest
290
302
@ ElementTypeSource
291
- void never (Class <?> elementType ) throws IOException {
303
+ void never (Class <?> elementType , @ TrackLogRecords LogRecordListener listener ) throws Exception {
292
304
reset (factory );
293
305
306
+ when (elementContext .getAnnotatedElement ()).thenReturn (TestCase .class .getDeclaredField ("tempDir" ));
307
+
294
308
closeablePath = TempDirectory .createTempDir (factory , NEVER , elementType , elementContext , extensionContext );
295
309
assertThat (closeablePath .get ()).isDirectory ();
296
310
297
311
closeablePath .close ();
298
312
299
313
verify (factory ).close ();
300
314
assertThat (closeablePath .get ()).exists ();
315
+ assertThat (listener .stream (Level .INFO )).map (LogRecord ::getMessage )//
316
+ .anyMatch (m -> m .startsWith ("Skipping cleanup of temp dir " )
317
+ && m .endsWith (" for field TestCase.tempDir due to CleanupMode.NEVER." ));
318
+ }
319
+
320
+ @ DisplayName ("is not done for a cleanup mode of ON_SUCCESS, if there is an exception (for annotated field)" )
321
+ @ ParameterizedTest
322
+ @ ElementTypeSource
323
+ void onSuccessWithExceptionForAnnotatedField (Class <?> elementType , @ TrackLogRecords LogRecordListener listener )
324
+ throws Exception {
325
+
326
+ Field field = TestCase .class .getDeclaredField ("tempDir" );
327
+
328
+ onSuccessWithException (elementType , listener , field ,
329
+ " for field TestCase.tempDir due to CleanupMode.ON_SUCCESS." );
330
+ }
331
+
332
+ @ DisplayName ("is not done for a cleanup mode of ON_SUCCESS, if there is an exception (for annotated method parameter)" )
333
+ @ ParameterizedTest
334
+ @ ElementTypeSource
335
+ void onSuccessWithExceptionForAnnotatedMethodParameter (Class <?> elementType ,
336
+ @ TrackLogRecords LogRecordListener listener ) throws Exception {
337
+
338
+ Method method = TestCase .class .getDeclaredMethod ("test" , TestInfo .class , Path .class );
339
+ Parameter parameter = method .getParameters ()[1 ];
340
+
341
+ onSuccessWithException (elementType , listener , parameter ,
342
+ "for parameter 'tempDir' in method test(TestInfo, Path) due to CleanupMode.ON_SUCCESS." );
301
343
}
302
344
303
- @ DisplayName ("is not done for a cleanup mode of ON_SUCCESS, if there is an exception" )
345
+ @ DisplayName ("is not done for a cleanup mode of ON_SUCCESS, if there is an exception (for annotated constructor parameter) " )
304
346
@ ParameterizedTest
305
347
@ ElementTypeSource
306
- void onSuccessWithException (Class <?> elementType ) throws IOException {
348
+ void onSuccessWithExceptionForAnnotatedConstructorParameter (Class <?> elementType ,
349
+ @ TrackLogRecords LogRecordListener listener ) throws Exception {
350
+
351
+ Constructor <?> constructor = TestCase .class .getDeclaredConstructor (TestInfo .class , Path .class );
352
+ Parameter parameter = constructor .getParameters ()[1 ];
353
+
354
+ onSuccessWithException (elementType , listener , parameter ,
355
+ "for parameter 'tempDir' in constructor TestCase(TestInfo, Path) due to CleanupMode.ON_SUCCESS." );
356
+ }
357
+
358
+ private void onSuccessWithException (Class <?> elementType , @ TrackLogRecords LogRecordListener listener ,
359
+ AnnotatedElement annotatedElement , String expectedMessage ) throws Exception {
360
+
307
361
reset (factory );
308
362
309
363
when (extensionContext .getExecutionException ()).thenReturn (Optional .of (new Exception ()));
364
+ when (elementContext .getAnnotatedElement ()).thenReturn (annotatedElement );
310
365
311
366
closeablePath = TempDirectory .createTempDir (factory , ON_SUCCESS , elementType , elementContext ,
312
367
extensionContext );
@@ -316,12 +371,16 @@ void onSuccessWithException(Class<?> elementType) throws IOException {
316
371
317
372
verify (factory ).close ();
318
373
assertThat (closeablePath .get ()).exists ();
374
+ assertThat (listener .stream (Level .INFO )).map (LogRecord ::getMessage )//
375
+ .anyMatch (m -> m .startsWith ("Skipping cleanup of temp dir " ) && m .endsWith (expectedMessage ));
319
376
}
320
377
321
378
@ DisplayName ("is done for a cleanup mode of ON_SUCCESS, if there is no exception" )
322
379
@ ParameterizedTest
323
380
@ ElementTypeSource
324
- void onSuccessWithNoException (Class <?> elementType ) throws IOException {
381
+ void onSuccessWithNoException (Class <?> elementType , @ TrackLogRecords LogRecordListener listener )
382
+ throws IOException {
383
+
325
384
reset (factory );
326
385
327
386
when (extensionContext .getExecutionException ()).thenReturn (Optional .empty ());
@@ -334,8 +393,21 @@ void onSuccessWithNoException(Class<?> elementType) throws IOException {
334
393
335
394
verify (factory ).close ();
336
395
assertThat (closeablePath .get ()).doesNotExist ();
396
+ assertThat (listener .stream (Level .INFO )).map (LogRecord ::getMessage )//
397
+ .noneMatch (m -> m .startsWith ("Skipping cleanup of temp dir" ));
337
398
}
338
399
339
400
}
340
401
402
+ static class TestCase {
403
+
404
+ Path tempDir ;
405
+
406
+ TestCase (TestInfo testInfo , Path tempDir ) {
407
+ }
408
+
409
+ void test (TestInfo testInfo , Path tempDir ) {
410
+ }
411
+ }
412
+
341
413
}
0 commit comments