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 5507c0e..8cd4593 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 @@ -135,7 +135,9 @@ import org.springframework.data.gemfire.server.SubscriptionEvictionPolicy; import org.springframework.data.gemfire.tests.mock.support.MockObjectInvocationException; import org.springframework.data.gemfire.tests.util.FileSystemUtils; import org.springframework.data.gemfire.tests.util.ObjectUtils; +import org.springframework.data.util.ReflectionUtils; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -146,6 +148,7 @@ import org.springframework.util.StringUtils; * @see java.io.File * @see java.net.InetSocketAddress * @see java.util.Properties + * @see java.util.UUID * @see org.apache.geode.cache.AttributesMutator * @see org.apache.geode.cache.Cache * @see org.apache.geode.cache.CacheFactory @@ -216,6 +219,12 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { private static final String FROM_KEYWORD = "FROM"; private static final String WHERE_KEYWORD = "WHERE"; + private static final String[] GEMFIRE_OBJECT_BASED_PROPERTIES = { + "security-client-auth-init", + "security-manager", + "security-post-processor", + }; + private static final String REPEATING_REGION_SEPARATOR = Region.SEPARATOR + "{2,}"; /** @@ -312,6 +321,28 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { }).orElse(DataPolicy.DEFAULT); } + /** + * Instantiates all Apache Geode/Pivotal GemFire objects which have been declared + * via {@link System#getProperties() System properties}. + * + * @param {@link Class type} of the {@link GemFireCache}. + * @param gemfireCache reference to the {@link GemFireCache} instance. + * @return the given {@link GemFireCache} instance. + * @see org.apache.geode.cache.GemFireCache + */ + private static T instantiateGemFireObjects(T gemfireCache) { + + Properties localGemfireProperties = gemfireProperties.get(); + + Arrays.stream(GEMFIRE_OBJECT_BASED_PROPERTIES) + .map(localGemfireProperties::getProperty) + .filter(StringUtils::hasText) + .filter(className -> ClassUtils.isPresent(className, ClassUtils.getDefaultClassLoader())) + .forEach(className -> ReflectionUtils.createInstanceIfPresent(className, null)); + + return gemfireCache; + } + /** * Determines whether the given {@link Region} is a root {@link Region}. * @@ -335,6 +366,23 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { return regionPath.lastIndexOf(Region.SEPARATOR) <= 0; } + /** + * Normalizes the {@link String name} of the Apache Geode/Pivotal GemFire System property by stripping off + * the {@literal gemfire.} prefix. + * + * @param propertyName {@link String name} of the property to normalize. + * @return the {@link String normalized form} of the Apache Geode/Pivotal GemFire System property. + * @see GemFire Properties + */ + private static String normalizeGemFirePropertyName(String propertyName) { + + return Optional.ofNullable(propertyName) + .filter(StringUtils::hasText) + .filter(it -> it.startsWith(GEMFIRE_SYSTEM_PROPERTIES_PREFIX)) + .map(it -> it.substring(GEMFIRE_SYSTEM_PROPERTIES_PREFIX.length())) + .orElse(propertyName); + } + /** * Normalizes the given {@link Region#getFullPath() Regon path} by removing all duplicate, repeating * {@link Region#SEPARATOR} characters between path segments as well as removing the trailing @@ -2615,7 +2663,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { doAnswer(invocation -> { storeConfiguration(cacheFactory); - return rememberMockedGemFireCache(resolvedMockCache, useSingletonCache); + return rememberMockedGemFireCache(instantiateGemFireObjects(resolvedMockCache), useSingletonCache); }).when(cacheFactorySpy).create(); return cacheFactorySpy; @@ -2782,7 +2830,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { doAnswer(invocation -> { storeConfiguration(clientCacheFactory); - return rememberMockedGemFireCache(resolvedMockedClientCache, useSingletonCache); + return rememberMockedGemFireCache(instantiateGemFireObjects(resolvedMockedClientCache), useSingletonCache); }).when(clientCacheFactorySpy).create(); return clientCacheFactorySpy; @@ -2844,15 +2892,6 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { return gemfireSystemProperties; } - private static String normalizeGemFirePropertyName(String propertyName) { - - return Optional.ofNullable(propertyName) - .filter(StringUtils::hasText) - .filter(it -> it.startsWith(GEMFIRE_SYSTEM_PROPERTIES_PREFIX)) - .map(it -> it.substring(GEMFIRE_SYSTEM_PROPERTIES_PREFIX.length())) - .orElse(propertyName); - } - public static class LuceneIndexKey { private final String indexName; 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 cd0a7b3..012f149 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 @@ -19,12 +19,14 @@ package org.springframework.data.gemfire.tests.mock; import static org.assertj.core.api.Assertions.assertThat; import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheFactory; import org.junit.After; import org.junit.Test; import org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport; +import org.springframework.data.gemfire.tests.support.AbstractSecurityManager; /** * Integration tests for {@link GemFireMockObjectsSupport}. @@ -45,10 +47,26 @@ public class GemFireMockObjectsSupportIntegrationTests extends IntegrationTestsS GemFireMockObjectsSupport.destroy(); } + @Test + public void instantiatesGemFireObjectsFromPropertiesSuccessfully() { + + Properties gemfireProperties = new Properties(); + + gemfireProperties.setProperty("name", "TestInstantiatesGemFireObjectsFromPropertiesSuccessfully"); + gemfireProperties.setProperty("security-manager", TestSecurityManager.class.getName()); + + assertThat(TestSecurityManager.CONSTRUCTED.get()).isFalse(); + + GemFireMockObjectsSupport.spyOn(new CacheFactory(gemfireProperties)).create(); + + assertThat(TestSecurityManager.CONSTRUCTED.get()).isTrue(); + } + @Test public void storesGemFirePropertiesSuccessfully() { try { + System.setProperty("gemfire.name", "TestStoresGemFirePropertiesSuccessfully"); System.setProperty("gemfire.log-level", "config"); System.setProperty("gemfire.locators", "skullbox[12345]"); @@ -88,4 +106,13 @@ public class GemFireMockObjectsSupportIntegrationTests extends IntegrationTestsS System.clearProperty("non-gemfire.property"); } } + + public static final class TestSecurityManager extends AbstractSecurityManager { + + private static final AtomicBoolean CONSTRUCTED = new AtomicBoolean(false); + + public TestSecurityManager() { + CONSTRUCTED.set(true); + } + } }