Change all JUnit declared @BeforeClass Spring and Apache Geode object and resource cleanup to occur after test (class/suite) execution using JUnit's @AfterClass.

Edit Javadoc.
This commit is contained in:
John Blum
2021-09-16 22:49:48 -07:00
parent e188e9f48e
commit 61316b8631
2 changed files with 114 additions and 56 deletions

View File

@@ -15,9 +15,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;
@@ -26,6 +24,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -40,7 +39,6 @@ import java.util.stream.Collectors;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.apache.geode.DataSerializer;
import org.apache.geode.cache.CacheClosedException;
@@ -62,6 +60,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.data.gemfire.GemfireUtils;
@@ -69,6 +68,7 @@ import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport;
import org.springframework.data.gemfire.tests.util.FileSystemUtils;
import org.springframework.data.gemfire.tests.util.FileUtils;
import org.springframework.data.gemfire.util.ArrayUtils;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
@@ -217,57 +217,12 @@ public abstract class IntegrationTestsSupport {
.orElseThrow(() -> newIllegalStateException("An ApplicationContext was not initialized"));
}
@BeforeClass
public static void closeAnyGemFireCacheBeforeTestExecution() {
closeGemFireCacheWaitOnCacheClosedEvent();
}
@BeforeClass
public static void closeAnySocketConfigurationBeforeTestExecution() {
SocketCreatorFactory.close();
}
@BeforeClass
public static void closeAnySslConfigurationBeforeTestExecution() {
//SSLConfigurationFactory.close();
synchronized (SSLConfigurationFactory.class) {
try {
Field instance = ReflectionUtils.findField(SSLConfigurationFactory.class, "instance",
SSLConfigurationFactory.class);
Optional.ofNullable(instance)
.ifPresent(field -> {
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, null, null);
});
}
catch (Throwable ignore) {
// Not much we can do about it now!
}
}
}
@BeforeClass
public static void stopAnyGemFireLocatorBeforeTestExecution() {
stopGemFireLocatorWaitOnStopEvent();
}
@Before
public void configureQueryDebugging() {
if (isQueryDebuggingEnabled()) {
System.setProperty(GEMFIRE_QUERY_VERBOSE_PROPERTY, Boolean.TRUE.toString());
}
}
@Before
public void referenceApplicationContext() {
applicationContextReference.set(this.applicationContext);
}
/**
* Clears all Java, VMware Tanzu GemFire, Apache Geode and Spring {@link System#getProperties() System Properties}
* after test (class/suite) execution.
*
* @see java.lang.System#getProperties()
*/
@AfterClass
public static void clearAllJavaGemFireGeodeAndSpringDotPrefixedSystemProperties() {
@@ -278,6 +233,14 @@ public abstract class IntegrationTestsSupport {
allSystemPropertyNames.forEach(System::clearProperty);
}
/**
* Clears (removes) all non-standard Spring {@link PropertySource PropertySources} from the Spring
* {@link Environment} after test (class/suite) execution.
*
* Only {@link System#getProperties() System Properties} and {@literal Environment Variables} are standard.
*
* @see org.springframework.core.env.Environment
*/
@AfterClass
public static void clearNonStandardSpringEnvironmentPropertySources() {
@@ -300,11 +263,75 @@ public abstract class IntegrationTestsSupport {
});
}
/**
* Closes the use of all Spring {@literal BeanFactoryLocators} after test (class/suite) execution.
*/
@AfterClass
public static void closeAllBeanFactoryLocators() {
GemfireBeanFactoryLocator.clear();
}
/**
* Closes any Apache Geode {@link GemFireCache} after test (class/suite) execution.
*
* @see org.apache.geode.cache.GemFireCache
*/
@AfterClass
public static void closeAnyGemFireCache() {
closeGemFireCacheWaitOnCacheClosedEvent();
}
/**
* Closes any Apache Geode {@link Locator} after test (class/suite) execution.
*
* @see org.apache.geode.distributed.Locator
*/
@AfterClass
public static void closeAnyGemFireLocator() {
stopGemFireLocatorWaitOnStopEvent();
}
/**
* Closes any Apache Geode {@link java.net.Socket} configuration after test (class/suite) execution.
*
* @see java.net.Socket
*/
@AfterClass
public static void closeAnySocketConfiguration() {
SocketCreatorFactory.close();
}
/**
* Closes any Apache Geode {@literal SSL} configuration after test (class/suite) execution.
*/
@AfterClass
public static void closeAnySslConfiguration() {
//SSLConfigurationFactory.close();
synchronized (SSLConfigurationFactory.class) {
try {
Field instance = ReflectionUtils.findField(SSLConfigurationFactory.class, "instance",
SSLConfigurationFactory.class);
Optional.ofNullable(instance)
.ifPresent(field -> {
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, null, null);
});
}
catch (Throwable ignore) {
// Not much we can do about it now!
}
}
}
/**
* Deletes any Apache Geode process ID ({@literal PID}) {@link File Files} after test (class/suite) execution.
*
* @see java.io.File
*/
@AfterClass
public static void deleteAllGemFireProcessIdFiles() {
@@ -315,19 +342,50 @@ public abstract class IntegrationTestsSupport {
FileSystemUtils.deleteRecursive(FileSystemUtils.WORKING_DIRECTORY, fileFilter);
}
/**
* Destroys the use of all Apache Geode / VMware Tanzu GemFire mock objects and resources held by the mock objects
* after test (class/suite) execution.
*/
@AfterClass
public static void destroyAllGemFireMockObjects() {
GemFireMockObjectsSupport.destroy();
}
/**
* Unregisters all Apache Geode {@link DataSerializer DataSerializers} from Apache Geode's serialization framework
* (subsystem) after test (class/suite) execution.
*
* @see org.apache.geode.DataSerializer
*/
@AfterClass
public static void unregisterAllDataSerializers() {
stream(nullSafeArray(InternalDataSerializer.getSerializers(), DataSerializer.class))
Arrays.stream(ArrayUtils.nullSafeArray(InternalDataSerializer.getSerializers(), DataSerializer.class))
.map(DataSerializer::getId)
.forEach(InternalDataSerializer::unregister);
}
/**
* Configures (eanbles) Apache Geode {@literal OQL} query debugging before test (case/method) execution.
*/
@Before
public void configureQueryDebugging() {
if (isQueryDebuggingEnabled()) {
System.setProperty(GEMFIRE_QUERY_VERBOSE_PROPERTY, Boolean.TRUE.toString());
}
}
/**
* Stores a reference to the optionally autowired/injected Spring {@link ApplicationContext} in the global context.
*
* @see org.springframework.context.ConfigurableApplicationContext
*/
@Before
public void referenceApplicationContext() {
applicationContextReference.set(this.applicationContext);
}
public static void closeApplicationContext(@Nullable ApplicationContext applicationContext) {
Optional.ofNullable(applicationContext)

View File

@@ -59,7 +59,7 @@ public class SslConfigurationIntegrationTests {
assertThat(sslConfigurationFactoryOne).isNotNull();
assertThat(getInstance()).isSameAs(sslConfigurationFactoryOne);
IntegrationTestsSupport.closeAnySslConfigurationBeforeTestExecution();
IntegrationTestsSupport.closeAnySslConfiguration();
SSLConfigurationFactory sslConfigurationFactoryTwo = getInstance();