Search in sources :

Example 1 with ApplicationPreparedEvent

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

the class ApplicationPidFileWriterTests method createPreparedEvent.

private SpringApplicationEvent createPreparedEvent(String propName, String propValue) {
    ConfigurableEnvironment environment = createEnvironment(propName, propValue);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    given(context.getEnvironment()).willReturn(environment);
    return new ApplicationPreparedEvent(new SpringApplication(), new String[] {}, context);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent)

Example 2 with ApplicationPreparedEvent

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

the class RestartApplicationListenerTests method testInitialize.

private void testInitialize(boolean failed) {
    Restarter.clearInstance();
    RestartApplicationListener listener = new RestartApplicationListener();
    SpringApplication application = new SpringApplication();
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    listener.onApplicationEvent(new ApplicationStartingEvent(application, ARGS));
    assertThat(Restarter.getInstance()).isNotEqualTo(nullValue());
    assertThat(Restarter.getInstance().isFinished()).isFalse();
    listener.onApplicationEvent(new ApplicationPreparedEvent(application, ARGS, context));
    if (failed) {
        listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS, context, new RuntimeException()));
    } else {
        listener.onApplicationEvent(new ApplicationReadyEvent(application, ARGS, context));
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) SpringApplication(org.springframework.boot.SpringApplication) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent)

Example 3 with ApplicationPreparedEvent

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

the class ConfigFileApplicationListenerTests method validateProfilePrecedence.

private void validateProfilePrecedence(String... profiles) {
    ApplicationPreparedEvent event = new ApplicationPreparedEvent(new SpringApplication(), new String[0], new AnnotationConfigApplicationContext());
    this.initializer.onApplicationEvent(event);
    String log = this.out.toString();
    // First make sure that each profile got processed only once
    for (String profile : profiles) {
        String reason = "Wrong number of occurrences for profile '" + profile + "' --> " + log;
        assertThat(StringUtils.countOccurrencesOf(log, createLogForProfile(profile))).as(reason).isEqualTo(1);
    }
    // Make sure the order of loading is the right one
    for (String profile : profiles) {
        String line = createLogForProfile(profile);
        int index = log.indexOf(line);
        assertThat(index).as("Loading profile '" + profile + "' not found in '" + log + "'").isNotEqualTo(-1);
        log = log.substring(index + line.length());
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent)

Aggregations

SpringApplication (org.springframework.boot.SpringApplication)3 ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationPreparedEvent)3 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 ApplicationFailedEvent (org.springframework.boot.context.event.ApplicationFailedEvent)1 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)1 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1