Search in sources :

Example 1 with ApplicationStartingEvent

use of org.springframework.boot.context.event.ApplicationStartingEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method applicationFailedEventCleansUpLoggingSystem.

@Test
public void applicationFailedEventCleansUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils.getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ApplicationFailedEvent(this.springApplication, new String[0], new GenericApplicationContext(), new Exception()));
    assertThat(loggingSystem.cleanedUp).isTrue();
}
Also used : ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) Matchers.containsString(org.hamcrest.Matchers.containsString) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with ApplicationStartingEvent

use of org.springframework.boot.context.event.ApplicationStartingEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method shutdownHookIsNotRegisteredByDefault.

@Test
public void shutdownHookIsNotRegisteredByDefault() throws Exception {
    TestLoggingApplicationListener listener = new TestLoggingApplicationListener();
    System.setProperty(LoggingSystem.class.getName(), TestShutdownHandlerLoggingSystem.class.getName());
    multicastEvent(listener, new ApplicationStartingEvent(new SpringApplication(), NO_ARGS));
    listener.initialize(this.context.getEnvironment(), this.context.getClassLoader());
    assertThat(listener.shutdownHook).isNull();
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) LoggingSystem(org.springframework.boot.logging.LoggingSystem) JavaLoggingSystem(org.springframework.boot.logging.java.JavaLoggingSystem) AbstractLoggingSystem(org.springframework.boot.logging.AbstractLoggingSystem) Test(org.junit.Test)

Example 3 with ApplicationStartingEvent

use of org.springframework.boot.context.event.ApplicationStartingEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method shutdownHookCanBeRegistered.

@Test
public void shutdownHookCanBeRegistered() throws Exception {
    TestLoggingApplicationListener listener = new TestLoggingApplicationListener();
    System.setProperty(LoggingSystem.class.getName(), TestShutdownHandlerLoggingSystem.class.getName());
    TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.register_shutdown_hook=true");
    multicastEvent(listener, new ApplicationStartingEvent(new SpringApplication(), NO_ARGS));
    listener.initialize(this.context.getEnvironment(), this.context.getClassLoader());
    assertThat(listener.shutdownHook).isNotNull();
    listener.shutdownHook.start();
    assertThat(TestShutdownHandlerLoggingSystem.shutdownLatch.await(30, TimeUnit.SECONDS)).isTrue();
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) LoggingSystem(org.springframework.boot.logging.LoggingSystem) JavaLoggingSystem(org.springframework.boot.logging.java.JavaLoggingSystem) AbstractLoggingSystem(org.springframework.boot.logging.AbstractLoggingSystem) Test(org.junit.Test)

Example 4 with ApplicationStartingEvent

use of org.springframework.boot.context.event.ApplicationStartingEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method closingChildContextDoesNotCleanUpLoggingSystem.

@Test
public void closingChildContextDoesNotCleanUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils.getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    GenericApplicationContext childContext = new GenericApplicationContext();
    childContext.setParent(this.context);
    multicastEvent(new ContextClosedEvent(childContext));
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(loggingSystem.cleanedUp).isTrue();
    childContext.close();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) Matchers.containsString(org.hamcrest.Matchers.containsString) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Example 5 with ApplicationStartingEvent

use of org.springframework.boot.context.event.ApplicationStartingEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerIntegrationTests method loggingPerformedDuringChildApplicationStartIsNotLost.

@Test
public void loggingPerformedDuringChildApplicationStartIsNotLost() {
    new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).child(Config.class).web(WebApplicationType.NONE).listeners(new ApplicationListener<ApplicationStartingEvent>() {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        @Override
        public void onApplicationEvent(ApplicationStartingEvent event) {
            this.logger.info("Child application starting");
        }
    }).run();
    assertThat(this.outputCapture.toString()).contains("Child application starting");
}
Also used : SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) Logger(org.slf4j.Logger) Test(org.junit.Test)

Aggregations

ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)10 Test (org.junit.Test)8 SpringApplication (org.springframework.boot.SpringApplication)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 JavaLoggingSystem (org.springframework.boot.logging.java.JavaLoggingSystem)3 File (java.io.File)2 ApplicationFailedEvent (org.springframework.boot.context.event.ApplicationFailedEvent)2 AbstractLoggingSystem (org.springframework.boot.logging.AbstractLoggingSystem)2 LoggingSystem (org.springframework.boot.logging.LoggingSystem)2 ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Before (org.junit.Before)1 ExpectedException (org.junit.rules.ExpectedException)1 Logger (org.slf4j.Logger)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationPreparedEvent)1 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)1 LogFile (org.springframework.boot.logging.LogFile)1