Search in sources :

Example 6 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method overridePidFileWithSpring.

@Test
void overridePidFileWithSpring() {
    File file = new File(this.tempDir, "pid");
    SpringApplicationEvent event = createPreparedEvent("spring.pid.file", file.getAbsolutePath());
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
    listener.onApplicationEvent(event);
    assertThat(contentOf(file)).isNotEmpty();
}
Also used : SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 7 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method tryReadyEvent.

@Test
void tryReadyEvent() throws Exception {
    File file = new File(this.tempDir, "pid");
    file.createNewFile();
    SpringApplicationEvent event = createReadyEvent("spring.pid.file", file.getAbsolutePath());
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
    listener.onApplicationEvent(event);
    assertThat(contentOf(file)).isEmpty();
    listener.setTriggerEventType(ApplicationReadyEvent.class);
    listener.onApplicationEvent(event);
    assertThat(contentOf(file)).isNotEmpty();
}
Also used : SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 8 with SpringApplicationEvent

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

the class SpringApplicationTests method commandLineRunnerFailureCausesApplicationFailedEventToBePublished.

@Test
void commandLineRunnerFailureCausesApplicationFailedEventToBePublished() throws Exception {
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    @SuppressWarnings("unchecked") ApplicationListener<SpringApplicationEvent> listener = mock(ApplicationListener.class);
    application.addListeners(listener);
    CommandLineRunner runner = mock(CommandLineRunner.class);
    Exception failure = new Exception();
    willThrow(failure).given(runner).run();
    application.addInitializers((context) -> context.getBeanFactory().registerSingleton("runner", runner));
    assertThatIllegalStateException().isThrownBy(application::run).withCause(failure);
    then(listener).should().onApplicationEvent(isA(ApplicationStartedEvent.class));
    then(listener).should().onApplicationEvent(isA(ApplicationFailedEvent.class));
    then(listener).should(never()).onApplicationEvent(isA(ApplicationReadyEvent.class));
}
Also used : ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ApplicationStartedEvent(org.springframework.boot.context.event.ApplicationStartedEvent) ApplicationContextException(org.springframework.context.ApplicationContextException) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) BeanDefinitionOverrideException(org.springframework.beans.factory.support.BeanDefinitionOverrideException) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Test(org.junit.jupiter.api.Test)

Example 9 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method throwWhenPidFileIsReadOnlyWithSpring.

@Test
void throwWhenPidFileIsReadOnlyWithSpring() throws Exception {
    withReadOnlyPidFile((file) -> {
        SpringApplicationEvent event = createPreparedEvent("spring.pid.fail-on-write-error", "true");
        ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
        assertThatIllegalStateException().isThrownBy(() -> listener.onApplicationEvent(event)).withMessageContaining("Cannot create pid file");
    });
}
Also used : SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 10 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method tryEnvironmentPreparedEvent.

@Test
void tryEnvironmentPreparedEvent() throws Exception {
    File file = new File(this.tempDir, "pid");
    file.createNewFile();
    SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file", file.getAbsolutePath());
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
    listener.onApplicationEvent(event);
    assertThat(contentOf(file)).isEmpty();
    listener.setTriggerEventType(ApplicationEnvironmentPreparedEvent.class);
    listener.onApplicationEvent(event);
    assertThat(contentOf(file)).isNotEmpty();
}
Also used : SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

SpringApplicationEvent (org.springframework.boot.context.event.SpringApplicationEvent)10 File (java.io.File)7 Test (org.junit.jupiter.api.Test)6 Test (org.junit.Test)4 FileReader (java.io.FileReader)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2 Assertions.assertThatNoException (org.assertj.core.api.Assertions.assertThatNoException)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)2 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)2 BeanDefinitionOverrideException (org.springframework.beans.factory.support.BeanDefinitionOverrideException)2 ApplicationFailedEvent (org.springframework.boot.context.event.ApplicationFailedEvent)2 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)2 ApplicationStartedEvent (org.springframework.boot.context.event.ApplicationStartedEvent)2 ApplicationContextException (org.springframework.context.ApplicationContextException)2