1717package rife .bld .extension ;
1818
1919import org .junit .jupiter .api .*;
20+ import org .junit .jupiter .api .extension .ExtendWith ;
21+ import org .junit .jupiter .api .extension .RegisterExtension ;
2022import org .mockito .Mockito ;
2123import rife .bld .BaseProject ;
2224import rife .bld .extension .junitreporter .JUnitXmlParser ;
25+ import rife .bld .extension .testing .LoggingExtension ;
2326import rife .bld .extension .testing .TestLogHandler ;
2427import rife .bld .operations .exceptions .ExitStatusException ;
2528
3740import static org .mockito .Mockito .mock ;
3841import static org .mockito .Mockito .when ;
3942
43+ @ ExtendWith (LoggingExtension .class )
4044@ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
4145@ SuppressWarnings ({"PMD.AvoidDuplicateLiterals" , "PMD.TestClassWithoutTestCases" })
4246class JUnitReporterOperationTest {
4347 @ SuppressWarnings ("LoggerInitializedWithForeignClass" )
44- private final Logger logger = Logger .getLogger (JUnitReporterOperation .class .getName ());
45- private TestLogHandler logHandler ;
48+ private static final Logger LOGGER = Logger .getLogger (JUnitReporterOperation .class .getName ());
49+ private static final TestLogHandler TEST_LOG_HANDLER = new TestLogHandler ();
50+
51+ @ RegisterExtension
52+ @ SuppressWarnings ("unused" )
53+ private static final LoggingExtension LOGGING_EXTENSION = new LoggingExtension (LOGGER , TEST_LOG_HANDLER );
4654
4755 private void assertLogContains (String message ) {
48- assertThat (logHandler .containsMessage (message ))
56+ assertThat (TEST_LOG_HANDLER .containsMessage (message ))
4957 .as ("Expected log to contain message: '%s'. Actual log messages: %s" ,
50- message , logHandler .getLogMessages ())
58+ message , TEST_LOG_HANDLER .getLogMessages ())
5159 .isTrue ();
5260 }
5361
@@ -60,21 +68,6 @@ void exampleReportMustExist() {
6068 assertThat (reportFile .length ()).isGreaterThan (0 );
6169 }
6270
63- @ BeforeEach
64- void setupLogging () {
65- logHandler = new TestLogHandler ();
66- logger .addHandler (logHandler );
67- logger .setLevel (Level .ALL );
68- logHandler .setLevel (Level .ALL );
69- }
70-
71- @ AfterEach
72- void teardownLogging () {
73- if (logHandler != null ) {
74- logger .removeHandler (logHandler );
75- }
76- }
77-
7871 @ Nested
7972 @ DisplayName ("Argument Parsing Tests" )
8073 class ArgumentParsingTests {
@@ -140,7 +133,7 @@ void executeWithSilentMode() {
140133 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
141134
142135 // In silent mode, no log messages should be recorded
143- assertThat (logHandler .getLogMessages ()).isEmpty ();
136+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
144137 }
145138 }
146139
@@ -225,14 +218,14 @@ class LoggingLevelTests {
225218 @ Test
226219 void executeWithLoggingDisabled () {
227220 // Disable SEVERE logging to test the logging condition branches
228- logger .setLevel (Level .OFF );
221+ LOGGER .setLevel (Level .OFF );
229222
230223 var operation = new JUnitReporterOperation ();
231224
232225 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
233226
234227 // No messages should be logged when logging is disabled
235- assertThat (logHandler .getLogMessages ()).isEmpty ();
228+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
236229 }
237230
238231 @ Test
@@ -243,19 +236,19 @@ void executeWithReportFileAndSilentMode() {
243236 .silent (true );
244237 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
245238
246- assertThat (logHandler .getLogMessages ()).isEmpty ();
239+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
247240 }
248241
249242 @ Test
250243 void executeWithReportFileNullAndLoggingDisabled () {
251- logger .setLevel (Level .OFF );
244+ LOGGER .setLevel (Level .OFF );
252245
253246 var operation = new JUnitReporterOperation ()
254247 .fromProject (new BaseProject ())
255248 .reportFile ((Path ) null );
256249 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
257250
258- assertThat (logHandler .getLogMessages ()).isEmpty ();
251+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
259252 }
260253 }
261254
@@ -315,7 +308,7 @@ void executeLargeReport() {
315308
316309 @ Test
317310 void executeThrowsUnexpectedRuntimeException () {
318- logger .setLevel (Level .WARNING );
311+ LOGGER .setLevel (Level .WARNING );
319312 var mockedProject = mock (BaseProject .class );
320313 when (mockedProject .buildDirectory ()).thenReturn (new File ("example/build" ));
321314
@@ -333,16 +326,13 @@ void executeThrowsUnexpectedRuntimeException() {
333326 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
334327
335328 var message = "Unexpected error: Simulated unexpected error" ;
336- assertThat (logHandler .containsMessage (message ))
337- .as ("Expected log to contain message: '%s'. Actual log messages: %s" ,
338- message , logHandler .getLogMessages ())
339- .isTrue ();
329+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).contains (message );
340330 }
341331 }
342332
343333 @ Test
344334 void executeThrowsUnexpectedRuntimeExceptionWithFineLogging () {
345- logger .setLevel (Level .FINE );
335+ LOGGER .setLevel (Level .FINE );
346336 var mockedProject = mock (BaseProject .class );
347337 when (mockedProject .buildDirectory ()).thenReturn (new File ("example/build" ));
348338
@@ -360,16 +350,13 @@ void executeThrowsUnexpectedRuntimeExceptionWithFineLogging() {
360350 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
361351
362352 var message = "Unexpected error" ;
363- assertThat (logHandler .containsMessage (message ))
364- .as ("Expected log to contain message: '%s'. Actual log messages: %s" ,
365- message , logHandler .getLogMessages ())
366- .isTrue ();
353+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).contains (message );
367354 }
368355 }
369356
370357 @ Test
371358 void executeThrowsUnexpectedRuntimeExceptionWithNoLogging () {
372- logger .setLevel (Level .OFF );
359+ LOGGER .setLevel (Level .OFF );
373360 var mockedProject = mock (BaseProject .class );
374361 when (mockedProject .buildDirectory ()).thenReturn (new File ("example/build" ));
375362
@@ -386,7 +373,7 @@ void executeThrowsUnexpectedRuntimeExceptionWithNoLogging() {
386373
387374 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
388375
389- assertThat (logHandler .getLogMessages ()).isEmpty ();
376+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
390377 }
391378 }
392379
@@ -409,7 +396,7 @@ void executeThrowsUnexpectedRuntimeExceptionWithSilentMode() {
409396
410397 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
411398
412- assertThat (logHandler .getLogMessages ()).isEmpty ();
399+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
413400 }
414401 }
415402
@@ -423,12 +410,12 @@ void executeWithInvalidFailureIndex() {
423410
424411 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
425412
426- assertLogContains ("The failure index is out of bounds" );
413+ assertThat ( TEST_LOG_HANDLER . getLogMessages ()). contains ("The failure index is out of bounds" );
427414 }
428415
429416 @ Test
430417 void executeWithInvalidFailureIndexAndLoggingDisabled () {
431- logger .setLevel (Level .OFF );
418+ LOGGER .setLevel (Level .OFF );
432419 var mockedProject = mock (BaseProject .class );
433420 when (mockedProject .arguments ()).thenReturn (new ArrayList <>(List .of ("--i=2.3" )));
434421 when (mockedProject .buildDirectory ()).thenReturn (new File ("example/build" ));
@@ -437,7 +424,7 @@ void executeWithInvalidFailureIndexAndLoggingDisabled() {
437424
438425 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
439426
440- assertThat (logHandler .getLogMessages ()).isEmpty ();
427+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
441428 }
442429
443430 @ Test
@@ -452,7 +439,7 @@ void executeWithInvalidFailureIndexAndSilentMode() {
452439
453440 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
454441
455- assertThat (logHandler .getLogMessages ()).isEmpty ();
442+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
456443 }
457444
458445 @ Test
@@ -465,7 +452,7 @@ void executeWithInvalidGroupIndex() {
465452
466453 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
467454
468- assertLogContains ("The group index is out of bounds" );
455+ assertThat ( TEST_LOG_HANDLER . getLogMessages ()). contains ("The group index is out of bounds" );
469456 }
470457
471458 @ Test
@@ -509,7 +496,7 @@ void executeWithParserException() {
509496
510497 @ Test
511498 void executeWithParserExceptionAndLoggingDisabled () {
512- logger .setLevel (Level .OFF );
499+ LOGGER .setLevel (Level .OFF );
513500 var project = mock (BaseProject .class );
514501 when (project .buildDirectory ()).thenReturn (new File ("build" ));
515502
@@ -520,7 +507,7 @@ void executeWithParserExceptionAndLoggingDisabled() {
520507 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
521508
522509 // Verify parser error is not logged when silent mode is enabled
523- assertThat (logHandler .getLogMessages ()).isEmpty ();
510+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
524511 }
525512
526513 @ Test
@@ -536,15 +523,15 @@ void executeWithParserExceptionAndSilentMode() {
536523 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
537524
538525 // Verify parser error is not logged when silent mode is enabled
539- assertThat (logHandler .getLogMessages ()).isEmpty ();
526+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).isEmpty ();
540527 }
541528
542529 @ Test
543530 void executeWithReportFileNull () {
544531 var operation = new JUnitReporterOperation ().fromProject (new BaseProject ());
545532 operation .reportFile ((Path ) null );
546533 assertThatThrownBy (operation ::execute ).isInstanceOf (ExitStatusException .class );
547- assertThat (logHandler .getLogMessages ()).contains ("A report file is required to run this operation." );
534+ assertThat (TEST_LOG_HANDLER .getLogMessages ()).contains ("A report file is required to run this operation." );
548535 }
549536
550537 @ Test
0 commit comments