Home / Class/ EventPublishingRunListenerTests Class — spring-boot Architecture

EventPublishingRunListenerTests Class — spring-boot Architecture

Architecture documentation for the EventPublishingRunListenerTests class in EventPublishingRunListenerTests.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/event/EventPublishingRunListenerTests.java lines 47–113

class EventPublishingRunListenerTests {

	private static final String[] NO_ARGS = new String[0];

	@Test
	void shouldPublishLifecycleEvents() {
		DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
		StaticApplicationContext context = new StaticApplicationContext();
		TestApplicationListener applicationListener = new TestApplicationListener();
		SpringApplication application = mock(SpringApplication.class);
		given(application.getListeners()).willReturn(Collections.singleton(applicationListener));
		EventPublishingRunListener publishingListener = new EventPublishingRunListener(application, NO_ARGS);
		applicationListener.assertReceivedNoEvents();
		publishingListener.starting(bootstrapContext);
		applicationListener.assertReceivedEvent(ApplicationStartingEvent.class);
		publishingListener.environmentPrepared(bootstrapContext, new MockEnvironment());
		applicationListener.assertReceivedEvent(ApplicationEnvironmentPreparedEvent.class);
		publishingListener.contextPrepared(context);
		applicationListener.assertReceivedEvent(ApplicationContextInitializedEvent.class);
		publishingListener.contextLoaded(context);
		applicationListener.assertReceivedEvent(ApplicationPreparedEvent.class);
		context.refresh();
		publishingListener.started(context, null);
		applicationListener.assertReceivedEvent(ApplicationStartedEvent.class, AvailabilityChangeEvent.class);
		publishingListener.ready(context, null);
		applicationListener.assertReceivedEvent(ApplicationReadyEvent.class, AvailabilityChangeEvent.class);
	}

	@Test
	void initialEventListenerCanAddAdditionalListenersToApplication() {
		SpringApplication application = new SpringApplication();
		DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
		ConfigurableEnvironment environment = new StandardEnvironment();
		TestApplicationListener lateAddedApplicationListener = new TestApplicationListener();
		ApplicationListener<ApplicationStartingEvent> listener = (event) -> event.getSpringApplication()
			.addListeners(lateAddedApplicationListener);
		application.addListeners(listener);
		EventPublishingRunListener runListener = new EventPublishingRunListener(application, NO_ARGS);
		runListener.starting(bootstrapContext);
		runListener.environmentPrepared(bootstrapContext, environment);
		lateAddedApplicationListener.assertReceivedEvent(ApplicationEnvironmentPreparedEvent.class);
	}

	static class TestApplicationListener implements ApplicationListener<ApplicationEvent> {

		private final Deque<ApplicationEvent> events = new ArrayDeque<>();

		@Override
		public void onApplicationEvent(ApplicationEvent event) {
			this.events.add(event);
		}

		void assertReceivedNoEvents() {
			assertThat(this.events).isEmpty();
		}

		void assertReceivedEvent(Class<?>... eventClasses) {
			List<ApplicationEvent> receivedEvents = new ArrayList<>();
			while (!this.events.isEmpty()) {
				receivedEvents.add(this.events.pollFirst());
			}
			assertThat(receivedEvents).extracting("class").contains((Object[]) eventClasses);
		}

	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free