Add getters and setter for the autowiored/injected Spring ConfigurableApplicationContext object reference in IntegrationTestsSupport.

This commit is contained in:
John Blum
2021-09-14 11:52:14 -07:00
parent b1de07519b
commit 770b3c85cf
2 changed files with 75 additions and 32 deletions

View File

@@ -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 <T> 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 extends ConfigurableApplicationContext> T setApplicationContext(
@Nullable T applicationContext) {
this.applicationContext = applicationContext;
return applicationContext;
}
/**
* Gets a reference to the configured Spring {@link ConfigurableApplicationContext}.
*
* @param <T> 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 extends ConfigurableApplicationContext> T getApplicationContext() {
return (T) this.applicationContext;
}
/**
* Gets an {@link Optional} reference to the configured Spring {@link ConfigurableApplicationContext}.
*
* @param <T> 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 <T extends ConfigurableApplicationContext> Optional<T> getOptionalApplicationContext() {
return Optional.ofNullable(getApplicationContext());
}
/**
* Gets a required reference to the configured Spring {@link ConfigurableApplicationContext}.
*
* @param <T> 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 extends ConfigurableApplicationContext> T requireApplicationContext() {
return this.<T>getOptionalApplicationContext()
.orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized"));
}
@BeforeClass
public static void closeAnyGemFireCacheInstanceBeforeTestExecution() {
closeGemFireCacheWaitOnCacheClosedEvent();

View File

@@ -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 extends ConfigurableApplicationContext> T setApplicationContext(
@Nullable T applicationContext) {
this.applicationContext = applicationContext;
return applicationContext;
}
@SuppressWarnings("unchecked")
protected @Nullable <T extends ConfigurableApplicationContext> T getApplicationContext() {
return (T) this.applicationContext;
}
protected <T extends ConfigurableApplicationContext> Optional<T> getOptionalApplicationContext() {
return Optional.ofNullable(getApplicationContext());
}
/**
* @inheritDoc
*/
@@ -109,15 +85,15 @@ public abstract class SpringApplicationContextIntegrationTestsSupport extends In
protected <T> T getBean(Class<T> requiredType) {
return getOptionalApplicationContext()
.map(applicationContext -> applicationContext.getBean(requiredType))
.orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized"));
ConfigurableApplicationContext applicationContext = requireApplicationContext();
return applicationContext.getBean(requiredType);
}
protected <T> T getBean(String beanName, Class<T> requiredType) {
return getOptionalApplicationContext()
.map(applicationContext -> applicationContext.getBean(beanName, requiredType))
.orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized"));
ConfigurableApplicationContext applicationContext = requireApplicationContext();
return applicationContext.getBean(beanName, requiredType);
}
}