Search in sources :

Example 1 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method overridePidFileWithSpring.

@Test
public void overridePidFileWithSpring() throws Exception {
    File file = this.temporaryFolder.newFile();
    SpringApplicationEvent event = createPreparedEvent("spring.pid.file", file.getAbsolutePath());
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
    listener.onApplicationEvent(event);
    assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
}
Also used : SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 2 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method tryEnvironmentPreparedEvent.

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

Example 3 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method tryReadyEvent.

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

Example 4 with SpringApplicationEvent

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

the class ApplicationPidFileWriterTests method throwWhenPidFileIsReadOnlyWithSpring.

@Test
public void throwWhenPidFileIsReadOnlyWithSpring() throws Exception {
    File file = this.temporaryFolder.newFile();
    file.setReadOnly();
    SpringApplicationEvent event = createPreparedEvent("spring.pid.fail-on-write-error", "true");
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
    this.exception.expect(IllegalStateException.class);
    this.exception.expectMessage("Cannot create pid file");
    listener.onApplicationEvent(event);
}
Also used : SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) File(java.io.File) Test(org.junit.Test)

Example 5 with SpringApplicationEvent

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

the class SpringApplicationTests method applicationRunnerFailureCausesApplicationFailedEventToBePublished.

@Test
void applicationRunnerFailureCausesApplicationFailedEventToBePublished() throws Exception {
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    @SuppressWarnings("unchecked") ApplicationListener<SpringApplicationEvent> listener = mock(ApplicationListener.class);
    application.addListeners(listener);
    ApplicationRunner runner = mock(ApplicationRunner.class);
    Exception failure = new Exception();
    willThrow(failure).given(runner).run(isA(ApplicationArguments.class));
    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)

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