From 770b3c85cf7dabccf597e40a08080ab36f2daf1b Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 14 Sep 2021 11:52:14 -0700 Subject: [PATCH] Add getters and setter for the autowiored/injected Spring ConfigurableApplicationContext object reference in IntegrationTestsSupport. --- .../integration/IntegrationTestsSupport.java | 69 ++++++++++++++++++- ...icationContextIntegrationTestsSupport.java | 38 ++-------- 2 files changed, 75 insertions(+), 32 deletions(-) diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/IntegrationTestsSupport.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/IntegrationTestsSupport.java index 0877f50..8a4b467 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/IntegrationTestsSupport.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/IntegrationTestsSupport.java @@ -18,6 +18,7 @@ package org.springframework.data.gemfire.tests.integration; import static java.util.Arrays.stream; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; import java.io.File; import java.io.IOException; @@ -80,14 +81,18 @@ import org.springframework.util.ReflectionUtils; * @see java.io.File * @see java.time.LocalDateTime * @see java.util.concurrent.TimeUnit - * @see java.util.concurrent.atomic.AtomicBoolean * @see java.util.function.Predicate * @see org.apache.geode.DataSerializer * @see org.apache.geode.cache.GemFireCache * @see org.apache.geode.distributed.Locator + * @see org.springframework.context.ApplicationContext * @see org.springframework.context.ApplicationEvent * @see org.springframework.context.ApplicationEventPublisher * @see org.springframework.context.ApplicationEventPublisherAware + * @see org.springframework.context.ConfigurableApplicationContext + * @see org.springframework.core.env.ConfigurableEnvironment + * @see org.springframework.core.env.PropertySource + * @see org.springframework.core.env.StandardEnvironment * @see org.springframework.data.gemfire.support.GemfireBeanFactoryLocator * @see org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport * @since 1.0.0 @@ -148,6 +153,68 @@ public abstract class IntegrationTestsSupport { @Autowired(required = false) private ConfigurableApplicationContext applicationContext; + /** + * Sets a reference to the configured Spring {@link ConfigurableApplicationContext}. + * + * @param specific {@link Class type} of {@link ConfigurableApplicationContext}. + * @param applicationContext reference to the current, configured Spring {@link ConfigurableApplicationContext}. + * @return the given reference to the Spring {@link ConfigurableApplicationContext}. + * @see org.springframework.context.ConfigurableApplicationContext + * @see #getOptionalApplicationContext() + * @see #getApplicationContext() + */ + protected @Nullable T setApplicationContext( + @Nullable T applicationContext) { + + this.applicationContext = applicationContext; + + return applicationContext; + } + + /** + * Gets a reference to the configured Spring {@link ConfigurableApplicationContext}. + * + * @param specific {@link Class type} of {@link ConfigurableApplicationContext}. + * @return a reference to the configured Spring {@link ConfigurableApplicationContext}; maybe {@literal null}. + * @see org.springframework.context.ConfigurableApplicationContext + * @see #setApplicationContext(ConfigurableApplicationContext) + * @see #getOptionalApplicationContext() + */ + @SuppressWarnings("unchecked") + protected @Nullable T getApplicationContext() { + return (T) this.applicationContext; + } + + /** + * Gets an {@link Optional} reference to the configured Spring {@link ConfigurableApplicationContext}. + * + * @param specific {@link Class type} of {@link ConfigurableApplicationContext}. + * @return an {@link Optional} reference to the configured Spring {@link ConfigurableApplicationContext}. + * @see org.springframework.context.ConfigurableApplicationContext + * @see #setApplicationContext(ConfigurableApplicationContext) + * @see #getApplicationContext() + * @see java.util.Optional + */ + protected Optional getOptionalApplicationContext() { + return Optional.ofNullable(getApplicationContext()); + } + + /** + * Gets a required reference to the configured Spring {@link ConfigurableApplicationContext}. + * + * @param specific {@link Class type} of {@link ConfigurableApplicationContext}. + * @return an {@literal non-null} reference to the configured Spring {@link ConfigurableApplicationContext}. + * @throws IllegalStateException if the Spring {@link ConfigurableApplicationContext} was not initialized. + * @see org.springframework.context.ConfigurableApplicationContext + * @see #setApplicationContext(ConfigurableApplicationContext) + * @see #getOptionalApplicationContext() + * @see #getApplicationContext() + */ + protected T requireApplicationContext() { + return this.getOptionalApplicationContext() + .orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized")); + } + @BeforeClass public static void closeAnyGemFireCacheInstanceBeforeTestExecution() { closeGemFireCacheWaitOnCacheClosedEvent(); diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java index 1851b8a..6fb89e4 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/integration/SpringApplicationContextIntegrationTestsSupport.java @@ -15,10 +15,6 @@ */ package org.springframework.data.gemfire.tests.integration; -import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; - -import java.util.Optional; - import org.junit.After; import org.springframework.context.ApplicationContext; @@ -28,7 +24,6 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.gemfire.util.ArrayUtils; import org.springframework.lang.NonNull; -import org.springframework.lang.Nullable; /** * The {@link SpringApplicationContextIntegrationTestsSupport} class is an extension of {@link IntegrationTestsSupport} @@ -51,11 +46,9 @@ import org.springframework.lang.Nullable; public abstract class SpringApplicationContextIntegrationTestsSupport extends IntegrationTestsSupport implements ApplicationEventPublisherAware { - private volatile ConfigurableApplicationContext applicationContext; - @After public void closeApplicationContext() { - closeApplicationContext(this.applicationContext); + closeApplicationContext(getApplicationContext()); } protected ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { @@ -78,23 +71,6 @@ public abstract class SpringApplicationContextIntegrationTestsSupport extends In return applicationContext; } - protected @Nullable T setApplicationContext( - @Nullable T applicationContext) { - - this.applicationContext = applicationContext; - - return applicationContext; - } - - @SuppressWarnings("unchecked") - protected @Nullable T getApplicationContext() { - return (T) this.applicationContext; - } - - protected Optional getOptionalApplicationContext() { - return Optional.ofNullable(getApplicationContext()); - } - /** * @inheritDoc */ @@ -109,15 +85,15 @@ public abstract class SpringApplicationContextIntegrationTestsSupport extends In protected T getBean(Class requiredType) { - return getOptionalApplicationContext() - .map(applicationContext -> applicationContext.getBean(requiredType)) - .orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized")); + ConfigurableApplicationContext applicationContext = requireApplicationContext(); + + return applicationContext.getBean(requiredType); } protected T getBean(String beanName, Class requiredType) { - return getOptionalApplicationContext() - .map(applicationContext -> applicationContext.getBean(beanName, requiredType)) - .orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized")); + ConfigurableApplicationContext applicationContext = requireApplicationContext(); + + return applicationContext.getBean(beanName, requiredType); } }