From b1fdb5962ee07656a652db5c74b1331677f2df10 Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 22 Mar 2021 20:08:41 -0700 Subject: [PATCH] Add TestContextApplicationEventPublisherAdapter class adapting the Spring TestContext framework and API as an instance of the ApplicationEventPublisher interface. --- ...ntextApplicationEventPublisherAdapter.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/extensions/spring/test/context/TestContextApplicationEventPublisherAdapter.java diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/extensions/spring/test/context/TestContextApplicationEventPublisherAdapter.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/extensions/spring/test/context/TestContextApplicationEventPublisherAdapter.java new file mode 100644 index 0000000..daaf912 --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/extensions/spring/test/context/TestContextApplicationEventPublisherAdapter.java @@ -0,0 +1,92 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.data.gemfire.tests.extensions.spring.test.context; + +import java.util.Optional; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.lang.NonNull; +import org.springframework.test.context.TestContext; +import org.springframework.util.Assert; + +/** + * {@literal Adapter} used to adapt the {@link TestContext} API as an {@link ApplicationEventPublisher}. + * + * @author John Blum + * @see org.springframework.context.ApplicationEventPublisher + * @see org.springframework.test.context.TestContext + * @since 0.0.23 + */ +@SuppressWarnings("unused") +public class TestContextApplicationEventPublisherAdapter implements ApplicationEventPublisher { + + protected static @NonNull TestContextApplicationEventPublisherAdapter from(@NonNull TestContext testContext) { + return new TestContextApplicationEventPublisherAdapter(testContext); + } + + private final TestContext testContext; + + /** + * Constructs a new instance of {@link TestContextApplicationEventPublisherAdapter} initialized with the given, + * required {@link TestContext}. + * + * @param testContext Spring {@link TestContext} to be adapted as a Spring {@link ApplicationEventPublisher}. + * @throws IllegalArgumentException if {@link TestContext} is {@literal null}. + * @see org.springframework.test.context.TestContext + */ + protected TestContextApplicationEventPublisherAdapter(@NonNull TestContext testContext) { + Assert.notNull(testContext, "TestContext must not be null"); + this.testContext = testContext; + } + + /** + * Returns a reference to the configured {@link TestContext}. + * + * @return a reference to the configured {@link TestContext}; never {@literal null}. + * @see org.springframework.test.context.TestContext + */ + protected @NonNull TestContext getTestContext() { + return this.testContext; + } + + /** + * Returns an {@link Optional} reference to the {@link ApplicationEventPublisher} if available. + * + * The {@link ApplicationEventPublisher}, or rather Spring {@link ApplicationContext}, is resolved from + * the {@link TestContext} depending on whether the {@link ApplicationContext} has been initialized yet. + * + * @return an {@link Optional} reference to the {@link ApplicationEventPublisher} if available. + * @see org.springframework.context.ApplicationEventPublisher + * @see java.util.Optional + * @see #getTestContext() + */ + protected Optional getApplicationEventPublisher() { + + return Optional.ofNullable(getTestContext()) + .filter(TestContext::hasApplicationContext) + .map(TestContext::getApplicationContext); + } + + /** + * @inheritDoc + */ + @Override + public void publishEvent(@NonNull Object event) { + getApplicationEventPublisher().ifPresent(applicationEventPublisher -> + applicationEventPublisher.publishEvent(event)); + } +}