11/*
2- * Copyright 2012-2016 the original author or authors.
2+ * Copyright 2012-2017 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4141import org .springframework .boot .context .event .ApplicationStartedEvent ;
4242import org .springframework .boot .logging .java .JavaLoggingSystem ;
4343import org .springframework .boot .testutil .InternalOutputCapture ;
44+ import org .springframework .context .ApplicationEvent ;
45+ import org .springframework .context .ApplicationListener ;
4446import org .springframework .context .event .ContextClosedEvent ;
47+ import org .springframework .context .event .SimpleApplicationEventMulticaster ;
4548import org .springframework .context .support .GenericApplicationContext ;
4649import org .springframework .test .context .support .TestPropertySourceUtils ;
4750import org .springframework .test .util .ReflectionTestUtils ;
@@ -83,8 +86,7 @@ public class LoggingApplicationListenerTests {
8386 public void init () throws SecurityException , IOException {
8487 LogManager .getLogManager ().readConfiguration (
8588 JavaLoggingSystem .class .getResourceAsStream ("logging.properties" ));
86- this .initializer .onApplicationEvent (
87- new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
89+ multicastEvent (new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
8890 new File ("target/foo.log" ).delete ();
8991 new File (tmpDir () + "/spring.log" ).delete ();
9092 }
@@ -352,8 +354,8 @@ public void parseArgsDisabled() throws Exception {
352354 public void parseArgsDoesntReplace () throws Exception {
353355 this .initializer .setSpringBootLogging (LogLevel .ERROR );
354356 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" }));
357359 this .initializer .initialize (this .context .getEnvironment (),
358360 this .context .getClassLoader ());
359361 this .logger .debug ("testatdebug" );
@@ -363,7 +365,7 @@ public void parseArgsDoesntReplace() throws Exception {
363365 @ Test
364366 public void bridgeHandlerLifecycle () throws Exception {
365367 assertThat (bridgeHandlerInstalled ()).isTrue ();
366- this . initializer . onApplicationEvent (new ContextClosedEvent (this .context ));
368+ multicastEvent (new ContextClosedEvent (this .context ));
367369 assertThat (bridgeHandlerInstalled ()).isFalse ();
368370 }
369371
@@ -396,7 +398,7 @@ public void shutdownHookIsNotRegisteredByDefault() throws Exception {
396398 TestLoggingApplicationListener listener = new TestLoggingApplicationListener ();
397399 System .setProperty (LoggingSystem .class .getName (),
398400 TestShutdownHandlerLoggingSystem .class .getName ());
399- listener . onApplicationEvent (
401+ multicastEvent ( listener ,
400402 new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
401403 listener .initialize (this .context .getEnvironment (), this .context .getClassLoader ());
402404 assertThat (listener .shutdownHook ).isNull ();
@@ -409,7 +411,7 @@ public void shutdownHookCanBeRegistered() throws Exception {
409411 TestShutdownHandlerLoggingSystem .class .getName ());
410412 TestPropertySourceUtils .addInlinedPropertiesToEnvironment (this .context ,
411413 "logging.register_shutdown_hook=true" );
412- listener . onApplicationEvent (
414+ multicastEvent ( listener ,
413415 new ApplicationStartedEvent (new SpringApplication (), NO_ARGS ));
414416 listener .initialize (this .context .getEnvironment (), this .context .getClassLoader ());
415417 assertThat (listener .shutdownHook ).isNotNull ();
@@ -422,29 +424,29 @@ public void shutdownHookCanBeRegistered() throws Exception {
422424 public void closingContextCleansUpLoggingSystem () {
423425 System .setProperty (LoggingSystem .SYSTEM_PROPERTY ,
424426 TestCleanupLoggingSystem .class .getName ());
425- this . initializer . onApplicationEvent (
427+ multicastEvent (
426428 new ApplicationStartedEvent (this .springApplication , new String [0 ]));
427429 TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem ) ReflectionTestUtils
428430 .getField (this .initializer , "loggingSystem" );
429431 assertThat (loggingSystem .cleanedUp ).isFalse ();
430- this . initializer . onApplicationEvent (new ContextClosedEvent (this .context ));
432+ multicastEvent (new ContextClosedEvent (this .context ));
431433 assertThat (loggingSystem .cleanedUp ).isTrue ();
432434 }
433435
434436 @ Test
435437 public void closingChildContextDoesNotCleanUpLoggingSystem () {
436438 System .setProperty (LoggingSystem .SYSTEM_PROPERTY ,
437439 TestCleanupLoggingSystem .class .getName ());
438- this . initializer . onApplicationEvent (
440+ multicastEvent (
439441 new ApplicationStartedEvent (this .springApplication , new String [0 ]));
440442 TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem ) ReflectionTestUtils
441443 .getField (this .initializer , "loggingSystem" );
442444 assertThat (loggingSystem .cleanedUp ).isFalse ();
443445 GenericApplicationContext childContext = new GenericApplicationContext ();
444446 childContext .setParent (this .context );
445- this . initializer . onApplicationEvent (new ContextClosedEvent (childContext ));
447+ multicastEvent (new ContextClosedEvent (childContext ));
446448 assertThat (loggingSystem .cleanedUp ).isFalse ();
447- this . initializer . onApplicationEvent (new ContextClosedEvent (this .context ));
449+ multicastEvent (new ContextClosedEvent (this .context ));
448450 assertThat (loggingSystem .cleanedUp ).isTrue ();
449451 childContext .close ();
450452 }
@@ -491,17 +493,26 @@ public void logFilePropertiesCanReferenceSystemProperties() {
491493 public void applicationFailedEventCleansUpLoggingSystem () {
492494 System .setProperty (LoggingSystem .SYSTEM_PROPERTY ,
493495 TestCleanupLoggingSystem .class .getName ());
494- this . initializer . onApplicationEvent (
496+ multicastEvent (
495497 new ApplicationStartedEvent (this .springApplication , new String [0 ]));
496498 TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem ) ReflectionTestUtils
497499 .getField (this .initializer , "loggingSystem" );
498500 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 ()));
502503 assertThat (loggingSystem .cleanedUp ).isTrue ();
503504 }
504505
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+
505516 private boolean bridgeHandlerInstalled () {
506517 Logger rootLogger = LogManager .getLogManager ().getLogger ("" );
507518 Handler [] handlers = rootLogger .getHandlers ();
0 commit comments