1
1
/*
2
- * Copyright 2012-2016 the original author or authors.
2
+ * Copyright 2012-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
41
41
import org .springframework .boot .context .event .ApplicationStartedEvent ;
42
42
import org .springframework .boot .logging .java .JavaLoggingSystem ;
43
43
import org .springframework .boot .testutil .InternalOutputCapture ;
44
+ import org .springframework .context .ApplicationEvent ;
45
+ import org .springframework .context .ApplicationListener ;
44
46
import org .springframework .context .event .ContextClosedEvent ;
47
+ import org .springframework .context .event .SimpleApplicationEventMulticaster ;
45
48
import org .springframework .context .support .GenericApplicationContext ;
46
49
import org .springframework .test .context .support .TestPropertySourceUtils ;
47
50
import org .springframework .test .util .ReflectionTestUtils ;
@@ -83,8 +86,7 @@ public class LoggingApplicationListenerTests {
83
86
public void init () throws SecurityException , IOException {
84
87
LogManager .getLogManager ().readConfiguration (
85
88
JavaLoggingSystem .class .getResourceAsStream ("logging.properties" ));
86
- this .initializer .onApplicationEvent (
87
- new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
89
+ multicastEvent (new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
88
90
new File ("target/foo.log" ).delete ();
89
91
new File (tmpDir () + "/spring.log" ).delete ();
90
92
}
@@ -352,8 +354,8 @@ public void parseArgsDisabled() throws Exception {
352
354
public void parseArgsDoesntReplace () throws Exception {
353
355
this .initializer .setSpringBootLogging (LogLevel .ERROR );
354
356
this .initializer .setParseArgs (false );
355
- this . initializer . onApplicationEvent (new ApplicationStartedEvent (
356
- this . springApplication , new String [] { "--debug" }));
357
+ multicastEvent (new ApplicationStartedEvent (this . springApplication ,
358
+ new String [] { "--debug" }));
357
359
this .initializer .initialize (this .context .getEnvironment (),
358
360
this .context .getClassLoader ());
359
361
this .logger .debug ("testatdebug" );
@@ -363,7 +365,7 @@ public void parseArgsDoesntReplace() throws Exception {
363
365
@ Test
364
366
public void bridgeHandlerLifecycle () throws Exception {
365
367
assertThat (bridgeHandlerInstalled ()).isTrue ();
366
- this . initializer . onApplicationEvent (new ContextClosedEvent (this .context ));
368
+ multicastEvent (new ContextClosedEvent (this .context ));
367
369
assertThat (bridgeHandlerInstalled ()).isFalse ();
368
370
}
369
371
@@ -396,7 +398,7 @@ public void shutdownHookIsNotRegisteredByDefault() throws Exception {
396
398
TestLoggingApplicationListener listener = new TestLoggingApplicationListener ();
397
399
System .setProperty (LoggingSystem .class .getName (),
398
400
TestShutdownHandlerLoggingSystem .class .getName ());
399
- listener . onApplicationEvent (
401
+ multicastEvent ( listener ,
400
402
new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
401
403
listener .initialize (this .context .getEnvironment (), this .context .getClassLoader ());
402
404
assertThat (listener .shutdownHook ).isNull ();
@@ -409,7 +411,7 @@ public void shutdownHookCanBeRegistered() throws Exception {
409
411
TestShutdownHandlerLoggingSystem .class .getName ());
410
412
TestPropertySourceUtils .addInlinedPropertiesToEnvironment (this .context ,
411
413
"logging.register_shutdown_hook=true" );
412
- listener . onApplicationEvent (
414
+ multicastEvent ( listener ,
413
415
new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
414
416
listener .initialize (this .context .getEnvironment (), this .context .getClassLoader ());
415
417
assertThat (listener .shutdownHook ).isNotNull ();
@@ -422,29 +424,29 @@ public void shutdownHookCanBeRegistered() throws Exception {
422
424
public void closingContextCleansUpLoggingSystem () {
423
425
System .setProperty (LoggingSystem .SYSTEM_PROPERTY ,
424
426
TestCleanupLoggingSystem .class .getName ());
425
- this . initializer . onApplicationEvent (
427
+ multicastEvent (
426
428
new ApplicationStartedEvent (this .springApplication , new String [0 ]));
427
429
TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem ) ReflectionTestUtils
428
430
.getField (this .initializer , "loggingSystem" );
429
431
assertThat (loggingSystem .cleanedUp ).isFalse ();
430
- this . initializer . onApplicationEvent (new ContextClosedEvent (this .context ));
432
+ multicastEvent (new ContextClosedEvent (this .context ));
431
433
assertThat (loggingSystem .cleanedUp ).isTrue ();
432
434
}
433
435
434
436
@ Test
435
437
public void closingChildContextDoesNotCleanUpLoggingSystem () {
436
438
System .setProperty (LoggingSystem .SYSTEM_PROPERTY ,
437
439
TestCleanupLoggingSystem .class .getName ());
438
- this . initializer . onApplicationEvent (
440
+ multicastEvent (
439
441
new ApplicationStartedEvent (this .springApplication , new String [0 ]));
440
442
TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem ) ReflectionTestUtils
441
443
.getField (this .initializer , "loggingSystem" );
442
444
assertThat (loggingSystem .cleanedUp ).isFalse ();
443
445
GenericApplicationContext childContext = new GenericApplicationContext ();
444
446
childContext .setParent (this .context );
445
- this . initializer . onApplicationEvent (new ContextClosedEvent (childContext ));
447
+ multicastEvent (new ContextClosedEvent (childContext ));
446
448
assertThat (loggingSystem .cleanedUp ).isFalse ();
447
- this . initializer . onApplicationEvent (new ContextClosedEvent (this .context ));
449
+ multicastEvent (new ContextClosedEvent (this .context ));
448
450
assertThat (loggingSystem .cleanedUp ).isTrue ();
449
451
childContext .close ();
450
452
}
@@ -491,17 +493,26 @@ public void logFilePropertiesCanReferenceSystemProperties() {
491
493
public void applicationFailedEventCleansUpLoggingSystem () {
492
494
System .setProperty (LoggingSystem .SYSTEM_PROPERTY ,
493
495
TestCleanupLoggingSystem .class .getName ());
494
- this . initializer . onApplicationEvent (
496
+ multicastEvent (
495
497
new ApplicationStartedEvent (this .springApplication , new String [0 ]));
496
498
TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem ) ReflectionTestUtils
497
499
.getField (this .initializer , "loggingSystem" );
498
500
assertThat (loggingSystem .cleanedUp ).isFalse ();
499
- this .initializer
500
- .onApplicationEvent (new ApplicationFailedEvent (this .springApplication ,
501
- new String [0 ], new GenericApplicationContext (), new Exception ()));
501
+ multicastEvent (new ApplicationFailedEvent (this .springApplication , new String [0 ],
502
+ new GenericApplicationContext (), new Exception ()));
502
503
assertThat (loggingSystem .cleanedUp ).isTrue ();
503
504
}
504
505
506
+ private void multicastEvent (ApplicationEvent event ) {
507
+ multicastEvent (this .initializer , event );
508
+ }
509
+
510
+ private void multicastEvent (ApplicationListener <?> listener , ApplicationEvent event ) {
511
+ SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster ();
512
+ multicaster .addApplicationListener (listener );
513
+ multicaster .multicastEvent (event );
514
+ }
515
+
505
516
private boolean bridgeHandlerInstalled () {
506
517
Logger rootLogger = LogManager .getLogManager ().getLogger ("" );
507
518
Handler [] handlers = rootLogger .getHandlers ();
0 commit comments