From 2729e537f654b852c761d65900f45b41b04edf65 Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 29 Mar 2021 14:16:04 -0700 Subject: [PATCH] Add @AfterClass test logic to cleanup non-Standard Spring container Environment PropertySources if available. --- .../integration/IntegrationTestsSupport.java | 50 +++++++ ...ropertySourcesCleanupIntegrationTests.java | 129 ++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/integration/SpringEnvironmentPropertySourcesCleanupIntegrationTests.java 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 60b90d6..6a17459 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 @@ -27,6 +27,7 @@ import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.WeakHashMap; import java.util.concurrent.TimeUnit; @@ -50,9 +51,16 @@ import org.apache.geode.internal.cache.InternalCache; import org.apache.geode.internal.net.SSLConfigurationFactory; import org.apache.geode.internal.net.SocketCreatorFactory; +import org.apache.shiro.util.StringUtils; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEvent; 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.PropertySource; +import org.springframework.core.env.StandardEnvironment; import org.springframework.data.gemfire.GemfireUtils; import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator; import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport; @@ -104,6 +112,9 @@ public abstract class IntegrationTestsSupport { protected static final String SYSTEM_PROPERTIES_LOG_FILE = "system-properties.log"; protected static final String TEST_GEMFIRE_LOG_LEVEL = "error"; + private static final AtomicReference applicationContextReference = + new AtomicReference<>(null); + private static final Predicate JAVAX_NET_SSL_NAME_PREDICATE = propertyName -> String.valueOf(propertyName).toLowerCase().startsWith("javax.net.ssl"); @@ -121,9 +132,21 @@ public abstract class IntegrationTestsSupport { .or(GEODE_DOT_SYSTEM_PROPERTY_NAME_PREDICATE) .or(SPRING_DOT_SYSTEM_PROPERTY_NAME_PREDICATE); + private static final Predicate SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME::equals; + + private static final Predicate SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = + StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME::equals; + + private static final Predicate RETAINED_PROPERTY_SOURCE_NAMES = SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME + .or(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); + private static final TestContextCacheLifecycleListenerAdapter cacheLifecycleListener = TestContextCacheLifecycleListenerAdapter.getInstance(); + @Autowired(required = false) + private ConfigurableApplicationContext applicationContext; + @BeforeClass public static void closeAnyGemFireCacheInstanceBeforeTestExecution() { closeGemFireCacheWaitOnCacheClosedEvent(); @@ -170,6 +193,11 @@ public abstract class IntegrationTestsSupport { } } + @Before + public void referenceApplicationContext() { + applicationContextReference.set(this.applicationContext); + } + @AfterClass public static void clearAllJavaGemFireGeodeAndSpringDotPrefixedSystemProperties() { @@ -180,6 +208,28 @@ public abstract class IntegrationTestsSupport { allSystemPropertyNames.forEach(System::clearProperty); } + @AfterClass + public static void clearNonStandardSpringEnvironmentPropertySources() { + + Optional.ofNullable(applicationContextReference.get()) + .map(ConfigurableApplicationContext::getEnvironment) + .map(ConfigurableEnvironment::getPropertySources) + .ifPresent(propertySources -> { + for (PropertySource propertySource : propertySources) { + if (Objects.nonNull(propertySource)) { + + String propertySourceName = propertySource.getName(); + + if (StringUtils.hasText(propertySourceName)) { + if (!RETAINED_PROPERTY_SOURCE_NAMES.test(propertySource.getName())) { + propertySources.remove(propertySourceName); + } + } + } + } + }); + } + @AfterClass public static void closeAllBeanFactoryLocators() { GemfireBeanFactoryLocator.clear(); diff --git a/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/integration/SpringEnvironmentPropertySourcesCleanupIntegrationTests.java b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/integration/SpringEnvironmentPropertySourcesCleanupIntegrationTests.java new file mode 100644 index 0000000..a0b57e5 --- /dev/null +++ b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/tests/integration/SpringEnvironmentPropertySourcesCleanupIntegrationTests.java @@ -0,0 +1,129 @@ +/* + * 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.integration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; + +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.ExternalResource; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySources; +import org.springframework.lang.NonNull; +import org.springframework.mock.env.MockPropertySource; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration Tests testing and asserting the cleanup of the Spring container {@link ConfigurableEnvironment} + * and {@link PropertySources} linked to Unit & Integration testing. + * + * @author John Blum + * @see org.springframework.context.ConfigurableApplicationContext + * @see org.springframework.context.annotation.Configuration + * @see org.springframework.core.env.ConfigurableEnvironment + * @see org.springframework.core.env.PropertySources + * @see org.springframework.mock.env.MockPropertySource + * @see org.springframework.test.context.ContextConfiguration + * @see org.springframework.test.context.junit4.SpringRunner + * @since 0.0.24 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration +@SuppressWarnings("unused") +public class SpringEnvironmentPropertySourcesCleanupIntegrationTests extends IntegrationTestsSupport { + + private static final AtomicReference applicationContextReference = + new AtomicReference<>(null); + + private static final String MOCK_PROPERTY_SOURCE_NAME = "MockPropertySource"; + + @ClassRule + public static final ExternalResource SPRING_ENVIRONMENT_RESOURCE_CLEANUP = new ExternalResource() { + + @Override + protected void after() { + + super.after(); + + Optional.ofNullable(applicationContextReference.get()) + .map(ConfigurableApplicationContext::getEnvironment) + .map(ConfigurableEnvironment::getPropertySources) + .filter(propertySources -> Boolean.FALSE.equals(propertySources.contains(MOCK_PROPERTY_SOURCE_NAME))) + //.map(success -> { System.err.println("SUCCESS!"); return success; }) + .orElseThrow(() -> newIllegalStateException(String.format( + "Spring Environment should not contain the [%s] PropertySource", MOCK_PROPERTY_SOURCE_NAME))); + } + }; + + @Autowired + private ConfigurableApplicationContext applicationContext; + + @Before + public void setup() { + + assertThat(this.applicationContext).isNotNull(); + assertThat(this.applicationContext.isActive()).isTrue(); + + applicationContextReference.set(this.applicationContext); + } + + @Test + public void applicationContextEnvironmentContainsMockPropertySource() { + + ConfigurableEnvironment environment = this.applicationContext.getEnvironment(); + + assertThat(environment).isNotNull(); + + PropertySources propertySources = environment.getPropertySources(); + + assertThat(propertySources).isNotNull(); + assertThat(propertySources.contains(MOCK_PROPERTY_SOURCE_NAME)).isTrue(); + } + + @Configuration + static class TestConfiguration { + + @EventListener(classes = ContextRefreshedEvent.class) + public void addTestPropertySource(@NonNull ContextRefreshedEvent event) { + + Optional.ofNullable(event) + .map(ContextRefreshedEvent::getApplicationContext) + .filter(ConfigurableApplicationContext.class::isInstance) + .map(ConfigurableApplicationContext.class::cast) + .map(ConfigurableApplicationContext::getEnvironment) + .map(ConfigurableEnvironment::getPropertySources) + .ifPresent(propertySources -> { + + MockPropertySource mockPropertySource = new MockPropertySource("MockPropertySource"); + + propertySources.addLast(mockPropertySource); + }); + } + } +}