From e0acf8254d7e1edc59e61dbbc2e6ed1c14359f67 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 31 May 2018 12:51:20 -0700 Subject: [PATCH] Unit test GemFire Mock Object destruction at the end of test execution. --- .../tests/mock/GemFireMockObjectsSupport.java | 2 +- ...ireMockObjectsSupportIntegrationTests.java | 60 ++++++++++++++++--- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/spring-test-data-geode/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java b/spring-test-data-geode/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java index 2278895..25ccab7 100644 --- a/spring-test-data-geode/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java +++ b/spring-test-data-geode/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java @@ -247,7 +247,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { /** * Destroys all {@link DisposableBean} based {@link Object GemFire objects}. */ - private static void destroyGemFireObjects() { + static void destroyGemFireObjects() { cachedGemFireObjects.stream() .filter(gemfireObject -> gemfireObject instanceof DisposableBean) diff --git a/spring-test-data-geode/src/test/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupportIntegrationTests.java b/spring-test-data-geode/src/test/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupportIntegrationTests.java index 012f149..41c2a6f 100644 --- a/spring-test-data-geode/src/test/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupportIntegrationTests.java +++ b/spring-test-data-geode/src/test/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupportIntegrationTests.java @@ -25,6 +25,7 @@ import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheFactory; import org.junit.After; import org.junit.Test; +import org.springframework.beans.factory.DisposableBean; import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; import org.springframework.data.gemfire.tests.support.AbstractSecurityManager; @@ -44,22 +45,52 @@ public class GemFireMockObjectsSupportIntegrationTests extends IntegrationTestsS @After public void tearDown() { + GemFireMockObjectsSupport.destroy(); + + TestSecurityManager.constructed.set(false); + TestSecurityManager.destroyed.set(false); + + TestSecurityPostProcessor.constructed.set(false); } @Test - public void instantiatesGemFireObjectsFromPropertiesSuccessfully() { + public void constructsGemFireObjectsFromPropertiesSuccessfully() { Properties gemfireProperties = new Properties(); - gemfireProperties.setProperty("name", "TestInstantiatesGemFireObjectsFromPropertiesSuccessfully"); + gemfireProperties.setProperty("name", "TestConstructsGemFireObjectsFromPropertiesSuccessfully"); gemfireProperties.setProperty("security-manager", TestSecurityManager.class.getName()); - assertThat(TestSecurityManager.CONSTRUCTED.get()).isFalse(); + assertThat(TestSecurityManager.constructed.get()).isFalse(); GemFireMockObjectsSupport.spyOn(new CacheFactory(gemfireProperties)).create(); - assertThat(TestSecurityManager.CONSTRUCTED.get()).isTrue(); + assertThat(TestSecurityManager.constructed.get()).isTrue(); + } + + @Test + public void destroysConstructedGemFireObjectsFromPropertiesSuccessfully() { + + Properties gemfireProperties = new Properties(); + + gemfireProperties.setProperty("name", "TestConstructsGemFireObjectsFromPropertiesSuccessfully"); + gemfireProperties.setProperty("security-manager", TestSecurityManager.class.getName()); + gemfireProperties.setProperty("security-post-processor", TestSecurityPostProcessor.class.getName()); + + assertThat(TestSecurityManager.constructed.get()).isFalse(); + assertThat(TestSecurityManager.destroyed.get()).isFalse(); + assertThat(TestSecurityPostProcessor.constructed.get()).isFalse(); + + GemFireMockObjectsSupport.spyOn(new CacheFactory(gemfireProperties)).create(); + + assertThat(TestSecurityManager.constructed.get()).isTrue(); + assertThat(TestSecurityManager.destroyed.get()).isFalse(); + assertThat(TestSecurityPostProcessor.constructed.get()).isTrue(); + + GemFireMockObjectsSupport.destroyGemFireObjects(); + + assertThat(TestSecurityManager.destroyed.get()).isTrue(); } @Test @@ -107,12 +138,27 @@ public class GemFireMockObjectsSupportIntegrationTests extends IntegrationTestsS } } - public static final class TestSecurityManager extends AbstractSecurityManager { + public static final class TestSecurityManager extends AbstractSecurityManager implements DisposableBean { - private static final AtomicBoolean CONSTRUCTED = new AtomicBoolean(false); + private static final AtomicBoolean constructed = new AtomicBoolean(false); + private static final AtomicBoolean destroyed = new AtomicBoolean(false); public TestSecurityManager() { - CONSTRUCTED.set(true); + constructed.set(true); + } + + @Override + public void destroy() throws Exception { + destroyed.set(true); + } + } + + public static final class TestSecurityPostProcessor { + + private static final AtomicBoolean constructed = new AtomicBoolean(false); + + public TestSecurityPostProcessor() { + constructed.set(true); } } }