diff --git a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfiguration.java b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfiguration.java index 6cc4f7a..6c601ee 100644 --- a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfiguration.java +++ b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfiguration.java @@ -536,10 +536,10 @@ public class GemFireHttpSessionConfiguration extends AbstractGemFireHttpSessionC GemFireCacheTypeAwareRegionFactoryBean sessionRegion = new GemFireCacheTypeAwareRegionFactoryBean<>(); + sessionRegion.setAttributes(sessionRegionAttributes); + sessionRegion.setCache(gemfireCache); sessionRegion.setClientRegionShortcut(getClientRegionShortcut()); - sessionRegion.setGemfireCache(gemfireCache); sessionRegion.setPoolName(getPoolName()); - sessionRegion.setRegionAttributes(sessionRegionAttributes); sessionRegion.setRegionName(getSessionRegionName()); sessionRegion.setServerRegionShortcut(getServerRegionShortcut()); diff --git a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBean.java b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBean.java index fa882d2..38f0ead 100644 --- a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBean.java +++ b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBean.java @@ -18,7 +18,7 @@ package org.springframework.session.data.gemfire.config.annotation.web.http.supp import java.util.Optional; -import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.Cache; import org.apache.geode.cache.InterestResultPolicy; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionAttributes; @@ -26,17 +26,12 @@ import org.apache.geode.cache.RegionShortcut; import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.client.Pool; -import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.SmartLifecycle; -import org.springframework.data.gemfire.GenericRegionFactoryBean; -import org.springframework.data.gemfire.client.ClientRegionFactoryBean; import org.springframework.data.gemfire.client.Interest; -import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport; +import org.springframework.data.gemfire.config.annotation.support.CacheTypeAwareRegionFactoryBean; +import org.springframework.session.Session; import org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration; import org.springframework.session.data.gemfire.support.GemFireUtils; -import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -62,8 +57,7 @@ import org.springframework.util.StringUtils; * @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration * @since 1.1.0 */ -public class GemFireCacheTypeAwareRegionFactoryBean extends AbstractFactoryBeanSupport> - implements InitializingBean, SmartLifecycle { +public class GemFireCacheTypeAwareRegionFactoryBean extends CacheTypeAwareRegionFactoryBean { protected static final ClientRegionShortcut DEFAULT_CLIENT_REGION_SHORTCUT = GemFireHttpSessionConfiguration.DEFAULT_CLIENT_REGION_SHORTCUT; @@ -77,112 +71,25 @@ public class GemFireCacheTypeAwareRegionFactoryBean extends AbstractFactor protected static final String DEFAULT_SESSION_REGION_NAME = GemFireHttpSessionConfiguration.DEFAULT_SESSION_REGION_NAME; - private ClientRegionShortcut clientRegionShortcut; - - private GemFireCache gemfireCache; - - private Region region; - - private RegionAttributes regionAttributes; - - private RegionShortcut serverRegionShortcut; - - private volatile SmartLifecycle smartLifecycleComponent; - - private String poolName; private String regionName; - /** - * Post-construction initialization callback to create, configure and initialize the - * Pivotal GemFire cache {@link Region} used to store, replicate (distribute) and manage - * Session state. This method intelligently handles both client-server and - * peer-to-peer (p2p) Pivotal GemFire supported distributed system topologies. - * - * @throws Exception if the initialization of the Pivotal GemFire cache {@link Region} fails. - * @see org.springframework.session.data.gemfire.support.GemFireUtils#isClient(GemFireCache) - * @see #getGemfireCache() - * @see #newClientRegion(GemFireCache) - * @see #newServerRegion(GemFireCache) - */ - public void afterPropertiesSet() throws Exception { - - GemFireCache gemfireCache = getGemfireCache(); - - this.region = GemFireUtils.isClient(gemfireCache) - ? newClientRegion(gemfireCache) - : newServerRegion(gemfireCache); + @Override + public void setClientRegionShortcut(ClientRegionShortcut clientRegionShortcut) { + super.setClientRegionShortcut(clientRegionShortcut != null ? clientRegionShortcut + : DEFAULT_CLIENT_REGION_SHORTCUT); } - /** - * Constructs a Pivotal GemFire cache {@link Region} using a peer-to-peer (p2p) GemFire - * topology to store and manage Session state in a Pivotal GemFire server cluster accessible - * from a Pivotal GemFire cache client. - * - * @param gemfireCache a reference to the GemFire - * {@link org.apache.geode.cache.Cache}. - * @return a peer-to-peer-based Pivotal GemFire cache {@link Region} to store and manage - * Session state. - * @throws Exception if the instantiation, configuration and initialization of the - * Pivotal GemFire cache {@link Region} fails. - * @see org.springframework.data.gemfire.GenericRegionFactoryBean - * @see org.apache.geode.cache.GemFireCache - * @see org.apache.geode.cache.Region - * @see #getRegionAttributes() - * @see #getRegionName() - * @see #getServerRegionShortcut() - */ - protected Region newServerRegion(GemFireCache gemfireCache) throws Exception { - - GenericRegionFactoryBean serverRegion = new GenericRegionFactoryBean(); - - serverRegion.setAttributes(getRegionAttributes()); - serverRegion.setCache(gemfireCache); - serverRegion.setRegionName(getRegionName()); - serverRegion.setShortcut(getServerRegionShortcut()); - serverRegion.afterPropertiesSet(); - - this.smartLifecycleComponent = serverRegion; - - return serverRegion.getObject(); + @Override + public ClientRegionShortcut getClientRegionShortcut() { + return super.getClientRegionShortcut(); } - /** - * Constructs a Pivotal GemFire cache {@link Region} using the client-server Pivotal GemFire topology - * to store and manage Session state in a Pivotal GemFire server cluster accessible from a - * Pivotal GemFire cache client. - * - * @param gemfireCache a reference to the GemFire - * {@link org.apache.geode.cache.Cache}. - * @return a client-server-based Pivotal GemFire cache {@link Region} to store and manage - * Session state. - * @throws Exception if the instantiation, configuration and initialization of the - * Pivotal GemFire cache {@link Region} fails. - * @see org.springframework.data.gemfire.client.ClientRegionFactoryBean - * @see org.apache.geode.cache.GemFireCache - * @see org.apache.geode.cache.Region - * @see #getClientRegionShortcut() - * @see #getRegionAttributes() - * @see #getRegionName() - * @see #registerInterests(boolean) - */ - protected Region newClientRegion(GemFireCache gemfireCache) throws Exception { + @Override + protected Interest[] getInterests() { - ClientRegionFactoryBean clientRegion = new ClientRegionFactoryBean(); + ClientRegionShortcut clientRegionShortcut = getClientRegionShortcut(); - ClientRegionShortcut shortcut = getClientRegionShortcut(); - - clientRegion.setAttributes(getRegionAttributes()); - clientRegion.setBeanFactory(getBeanFactory()); - clientRegion.setCache(gemfireCache); - clientRegion.setInterests(registerInterests(!GemFireUtils.isLocal(shortcut))); - clientRegion.setPoolName(getPoolName()); - clientRegion.setRegionName(getRegionName()); - clientRegion.setShortcut(shortcut); - clientRegion.afterPropertiesSet(); - - this.smartLifecycleComponent = clientRegion; - - return clientRegion.getObject(); + return registerInterests(!(clientRegionShortcut == null || GemFireUtils.isLocal(clientRegionShortcut))); } /** @@ -201,123 +108,6 @@ public class GemFireCacheTypeAwareRegionFactoryBean extends AbstractFactor : new Interest[0]; } - /** - * Returns a reference to the constructed Pivotal GemFire cache {@link Region} used to store - * and manage Session state. - * - * @return the {@link Region} used to store and manage Session state. - * @throws Exception if the {@link Region} reference cannot be obtained. - * @see org.apache.geode.cache.Region - */ - public Region getObject() throws Exception { - return this.region; - } - - /** - * Returns the specific type of Pivotal GemFire cache {@link Region} this factory creates when - * initialized or Region.class when uninitialized. - * - * @return the Pivotal GemFire cache {@link Region} class type constructed by this factory. - * @see org.apache.geode.cache.Region - * @see java.lang.Class - */ - @SuppressWarnings("unchecked") - public Class getObjectType() { - - return Optional.ofNullable(this.region) - .map(Object::getClass) - .orElse((Class) Region.class); - } - - /** - * Sets a reference to the Spring {@link BeanFactory} responsible for - * creating Apache Geode/Pivotal GemFire components. - * - * @param beanFactory reference to the Spring {@link BeanFactory} - * @throws IllegalArgumentException if the {@link BeanFactory} reference is null. - * @see org.springframework.beans.factory.BeanFactory - */ - @Override - public void setBeanFactory(BeanFactory beanFactory) { - Assert.notNull(beanFactory, "BeanFactory is required"); - super.setBeanFactory(beanFactory); - } - - /** - * Gets a reference to the Spring {@link BeanFactory} responsible for - * creating Apache Geode/Pivotal GemFire components. - * - * @return a reference to the Spring {@link BeanFactory} - * @throws IllegalStateException if the {@link BeanFactory} reference - * is null. - * @see org.springframework.beans.factory.BeanFactory - */ - @Override - public BeanFactory getBeanFactory() { - return Optional.ofNullable(super.getBeanFactory()).orElseThrow(() -> - new IllegalStateException("A reference to the BeanFactory was not properly configured")); - } - - /** - * Sets the {@link Region} data policy used by the Pivotal GemFire cache client to manage - * Session state. - * - * @param clientRegionShortcut a {@link ClientRegionShortcut} to specify the client - * {@link Region} data management policy. - * @see org.apache.geode.cache.client.ClientRegionShortcut - */ - public void setClientRegionShortcut(ClientRegionShortcut clientRegionShortcut) { - this.clientRegionShortcut = clientRegionShortcut; - } - - /** - * Returns the {@link Region} data policy used by the Pivotal GemFire cache client to manage - * Session state. Defaults to {@link ClientRegionShortcut#PROXY}. - * - * @return a {@link ClientRegionShortcut} specifying the client {@link Region} data - * management policy. - * @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration#DEFAULT_CLIENT_REGION_SHORTCUT - * @see org.apache.geode.cache.client.ClientRegionShortcut - */ - protected ClientRegionShortcut getClientRegionShortcut() { - return Optional.ofNullable(this.clientRegionShortcut).orElse(DEFAULT_CLIENT_REGION_SHORTCUT); - } - - /** - * Sets a reference to the Pivotal GemFire cache used to construct the appropriate - * {@link Region}. - * - * @param gemfireCache a reference to the Pivotal GemFire cache. - * @throws IllegalArgumentException if the {@link GemFireCache} reference is null. - */ - public void setGemfireCache(GemFireCache gemfireCache) { - this.gemfireCache = Optional.ofNullable(gemfireCache).orElseThrow(() -> - new IllegalArgumentException("GemFireCache is required")); - } - - /** - * Returns a reference to the Pivotal GemFire cache used to construct the appropriate - * {@link Region}. - * - * @return a reference to the Pivotal GemFire cache. - * @throws IllegalStateException if the {@link GemFireCache} reference is null. - */ - protected GemFireCache getGemfireCache() { - return Optional.ofNullable(this.gemfireCache).orElseThrow(() -> - new IllegalStateException("A reference to the GemFireCache was not properly configured")); - } - - /** - * Sets the name of the Pivotal GemFire {@link Pool} used by the client Region for managing Sessions - * during cache operations involving the server. - * - * @param poolName the name of a Pivotal GemFire {@link Pool}. - * @see Pool#getName() - */ - public void setPoolName(final String poolName) { - this.poolName = poolName; - } - /** * Returns the name of the Pivotal GemFire {@link Pool} used by the client Region for managing Sessions * during cache operations involving the server. @@ -325,8 +115,9 @@ public class GemFireCacheTypeAwareRegionFactoryBean extends AbstractFactor * @return the name of a Pivotal GemFire {@link Pool}. * @see Pool#getName() */ - protected String getPoolName() { - return Optional.ofNullable(this.poolName).filter(StringUtils::hasText).orElse(DEFAULT_POOL_NAME); + @Override + protected Optional getPoolName() { + return Optional.of(super.getPoolName().orElse(DEFAULT_POOL_NAME)); } /** @@ -336,9 +127,11 @@ public class GemFireCacheTypeAwareRegionFactoryBean extends AbstractFactor * @param regionAttributes the Pivotal GemFire {@link RegionAttributes} used to configure the * Pivotal GemFire cache {@link Region}. * @see org.apache.geode.cache.RegionAttributes + * @deprecated use {@link #setAttributes(RegionAttributes)}. */ + @Deprecated public void setRegionAttributes(RegionAttributes regionAttributes) { - this.regionAttributes = regionAttributes; + setAttributes(regionAttributes); } /** @@ -348,96 +141,52 @@ public class GemFireCacheTypeAwareRegionFactoryBean extends AbstractFactor * @return the Pivotal GemFire {@link RegionAttributes} used to configure the Pivotal GemFire cache * {@link Region}. * @see org.apache.geode.cache.RegionAttributes + * @deprecated use {@link #getAttributes()}. */ + @Deprecated protected RegionAttributes getRegionAttributes() { - return this.regionAttributes; + return getAttributes(); } /** - * Sets the name of the Pivotal GemFire cache {@link Region} use to store and manage Session - * state. + * Sets the {@link String name} of the {@link Region} used to store and manage {@link Session} state. * - * @param regionName a String specifying the name of the Pivotal GemFire cache {@link Region}. + * @param regionName {@link String} containing the name of the {@link Region} used to store + * and manage {@link Session} state. */ - public void setRegionName(final String regionName) { + public void setRegionName(String regionName) { this.regionName = regionName; } /** - * Returns the configured name of the Pivotal GemFire cache {@link Region} use to store and - * manage Session state. Defaults to "ClusteredSpringSessions" + * Returns the configured {@link String name} of the {@link Region} used to store and manage {@link Session} state. * - * @return a String specifying the name of the Pivotal GemFire cache {@link Region}. + * Defaults to {@literal ClusteredSpringSessions}. + * + * @return a {@link String} containing the name of the {@link Region} used to store + * and manage {@link Session} state. * @see org.apache.geode.cache.Region#getName() */ protected String getRegionName() { return Optional.ofNullable(this.regionName).filter(StringUtils::hasText).orElse(DEFAULT_SESSION_REGION_NAME); } - /** - * Sets the {@link Region} data policy used by the Pivotal GemFire peer cache to manage - * Session state. - * - * @param serverRegionShortcut a {@link RegionShortcut} to specify the peer - * {@link Region} data management policy. - * @see org.apache.geode.cache.RegionShortcut - */ - public void setServerRegionShortcut(RegionShortcut serverRegionShortcut) { - this.serverRegionShortcut = serverRegionShortcut; + @Override + public String resolveRegionName() { + return getRegionName(); } /** - * Returns the {@link Region} data policy used by the Pivotal GemFire peer cache to manage - * Session state. Defaults to {@link RegionShortcut#PARTITION}. + * Returns the {@link Region} data policy used by the Apache Geode/Pivotal GemFire peer {@link Cache} + * to manage {@link Session} state. * - * @return a {@link RegionShortcut} specifying the peer {@link Region} data management - * policy. + * Defaults to {@link RegionShortcut#PARTITION}. + * + * @return a {@link RegionShortcut} specifying the peer {@link Region} data management policy. * @see org.apache.geode.cache.RegionShortcut */ - protected RegionShortcut getServerRegionShortcut() { - return Optional.ofNullable(this.serverRegionShortcut).orElse(DEFAULT_SERVER_REGION_SHORTCUT); - } - - protected Optional getSmartLifecycleComponent() { - return Optional.ofNullable(this.smartLifecycleComponent); - } - @Override - public boolean isAutoStartup() { - - return getSmartLifecycleComponent() - .map(SmartLifecycle::isAutoStartup) - .orElse(false); - } - - @Override - public boolean isRunning() { - - return getSmartLifecycleComponent() - .map(SmartLifecycle::isRunning) - .orElse(false); - } - - @Override - public void start() { - getSmartLifecycleComponent().ifPresent(SmartLifecycle::start); - } - - @Override - public void stop() { - getSmartLifecycleComponent().ifPresent(SmartLifecycle::stop); - } - - @Override - public void stop(Runnable callback) { - getSmartLifecycleComponent().ifPresent(it -> it.stop(callback)); - } - - @Override - public int getPhase() { - - return getSmartLifecycleComponent() - .map(SmartLifecycle::getPhase) - .orElse(0); + public RegionShortcut getServerRegionShortcut() { + return Optional.ofNullable(super.getServerRegionShortcut()).orElse(DEFAULT_SERVER_REGION_SHORTCUT); } } diff --git a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfigurationTests.java b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfigurationTests.java index 6f1d5ee..f06f101 100644 --- a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfigurationTests.java +++ b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/GemFireHttpSessionConfigurationTests.java @@ -54,6 +54,7 @@ import org.springframework.session.Session; import org.springframework.session.data.gemfire.GemFireOperationsSessionRepository; import org.springframework.session.data.gemfire.config.annotation.web.http.support.GemFireCacheTypeAwareRegionFactoryBean; import org.springframework.session.data.gemfire.config.annotation.web.http.support.SpringSessionGemFireConfigurer; +import org.springframework.util.ReflectionUtils; /** * Unit tests for {@link GemFireHttpSessionConfiguration} class. @@ -83,21 +84,37 @@ public class GemFireHttpSessionConfigurationTests { @SuppressWarnings("unchecked") protected T getField(Object obj, String fieldName) { + try { - Field field = obj.getClass().getDeclaredField(fieldName); + Field field = resolveField(obj, fieldName); field.setAccessible(true); return (T) field.get(obj); } - catch (NoSuchFieldException e) { - throw new IllegalArgumentException(String.format( - "field with name [%1$s] was not found in class [%2$s]", fieldName, obj), e); + catch (NoSuchFieldException cause) { + throw new IllegalArgumentException(cause); } - catch (IllegalAccessException e) { - throw new Error(String.format("unable to access field [%1$s] on object of type [%2$s]", - fieldName, obj.getClass().getName()), e); + catch (IllegalAccessException cause) { + throw new Error(String.format("Unable to access field [%1$s] on object of type [%2$s]", + fieldName, obj.getClass().getName()), cause); } } + private static Field resolveField(Object obj, String fieldName) throws NoSuchFieldException { + return resolveField(obj.getClass(), fieldName); + } + + private static Field resolveField(Class type, String fieldName) throws NoSuchFieldException { + + Field field = ReflectionUtils.findField(type, fieldName); + + if (field == null) { + throw new NoSuchFieldException(String.format("Field with name [%1$s] was not found in class [%2$s]", + fieldName, type)); + } + + return field; + } + @SafeVarargs private static T[] toArray(T... array) { return array; @@ -405,16 +422,13 @@ public class GemFireHttpSessionConfigurationTests { this.gemfireConfiguration.sessionRegion(mockGemFireCache, mockRegionAttributes); assertThat(sessionRegionFactoryBean).isNotNull(); - assertThat(this.getField(sessionRegionFactoryBean, "clientRegionShortcut")) - .isEqualTo(ClientRegionShortcut.CACHING_PROXY); - assertThat(this.getField(sessionRegionFactoryBean, "gemfireCache")) - .isEqualTo(mockGemFireCache); + assertThat(sessionRegionFactoryBean.getClientRegionShortcut()).isEqualTo(ClientRegionShortcut.CACHING_PROXY); + assertThat(sessionRegionFactoryBean.getCache()).isEqualTo(mockGemFireCache); assertThat(this.getField(sessionRegionFactoryBean, "poolName")).isEqualTo("TestPool"); assertThat(this.>getField(sessionRegionFactoryBean, "regionAttributes")).isEqualTo(mockRegionAttributes); assertThat(this.getField(sessionRegionFactoryBean, "regionName")).isEqualTo("TestRegion"); - assertThat(this.getField(sessionRegionFactoryBean, "serverRegionShortcut")) - .isEqualTo(RegionShortcut.REPLICATE_PERSISTENT); + assertThat(sessionRegionFactoryBean.getServerRegionShortcut()).isEqualTo(RegionShortcut.REPLICATE_PERSISTENT); verifyZeroInteractions(mockGemFireCache); verifyZeroInteractions(mockRegionAttributes); diff --git a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBeanTests.java b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBeanTests.java index a3db163..dc1c384 100644 --- a/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBeanTests.java +++ b/spring-session-data-geode/src/test/java/org/springframework/session/data/gemfire/config/annotation/web/http/support/GemFireCacheTypeAwareRegionFactoryBeanTests.java @@ -19,6 +19,8 @@ package org.springframework.session.data.gemfire.config.annotation.web.http.supp import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import java.util.Arrays; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,6 +37,7 @@ import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientRegionShortcut; import org.springframework.beans.factory.BeanFactory; +import org.springframework.data.gemfire.client.ClientRegionShortcutWrapper; import org.springframework.data.gemfire.client.Interest; import org.springframework.session.Session; @@ -81,28 +84,28 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { this.regionFactoryBean = new GemFireCacheTypeAwareRegionFactoryBean<>(); } - protected void afterPropertiesSetCreatesCorrectRegionForGemFireCacheType(final GemFireCache expectedCache, - Region expectedRegion) throws Exception { + protected void afterPropertiesSetCreatesCorrectRegionForGemFireCacheType(GemFireCache expectedCache, + Region expectedRegion) throws Exception { this.regionFactoryBean = new GemFireCacheTypeAwareRegionFactoryBean() { @Override - protected Region newClientRegion(GemFireCache gemfireCache) throws Exception { + protected Region newClientRegion(GemFireCache gemfireCache, String name) throws Exception { assertThat(gemfireCache).isSameAs(expectedCache); return GemFireCacheTypeAwareRegionFactoryBeanTests.this.mockClientRegion; } @Override - protected Region newServerRegion(GemFireCache gemfireCache) throws Exception { + protected Region newServerRegion(GemFireCache gemfireCache, String name) throws Exception { assertThat(gemfireCache).isSameAs(expectedCache); return GemFireCacheTypeAwareRegionFactoryBeanTests.this.mockServerRegion; } }; - this.regionFactoryBean.setGemfireCache(expectedCache); + this.regionFactoryBean.setCache(expectedCache); this.regionFactoryBean.afterPropertiesSet(); - assertThat(this.regionFactoryBean.getGemfireCache()).isSameAs(expectedCache); + assertThat(this.regionFactoryBean.getCache()).isSameAs(expectedCache); assertThat(this.regionFactoryBean.getObject()).isEqualTo(expectedRegion); assertThat(this.regionFactoryBean.getObjectType()).isEqualTo(expectedRegion.getClass()); } @@ -136,7 +139,49 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { Interest[] interests = this.regionFactoryBean.registerInterests(false); assertThat(interests).isNotNull(); - assertThat(interests.length).isEqualTo(0); + assertThat(interests).isEmpty(); + } + + @Test + public void getInterestsWhenClientRegionShortcutIsNotLocalReturnsAllKeys() { + + Arrays.stream(ClientRegionShortcut.values()) + .filter(clientRegionShortcut -> !clientRegionShortcut.name().contains("LOCAL")) + .forEach(clientRegionShortcut -> { + + this.regionFactoryBean.setClientRegionShortcut(clientRegionShortcut); + + assertThat(this.regionFactoryBean.getClientRegionShortcut()).isEqualTo(clientRegionShortcut); + assertThat(ClientRegionShortcutWrapper.valueOf(clientRegionShortcut).isLocal()).isFalse(); + + Interest[] interests = this.regionFactoryBean.getInterests(); + + assertThat(interests).isNotNull(); + assertThat(interests).hasSize(1); + assertThat(interests[0].isDurable()).isFalse(); + assertThat(interests[0].getKey().toString()).isEqualTo("ALL_KEYS"); + assertThat(interests[0].getPolicy()).isEqualTo(InterestResultPolicy.KEYS); + assertThat(interests[0].isReceiveValues()).isTrue(); + }); + } + + @Test + public void getInterestsWhenClientRegionShortcutIsLocalReturnsNoKeys() { + + Arrays.stream(ClientRegionShortcut.values()) + .filter(clientRegionShortcut -> clientRegionShortcut.name().contains("LOCAL")) + .forEach(clientRegionShortcut -> { + + this.regionFactoryBean.setClientRegionShortcut(clientRegionShortcut); + + assertThat(this.regionFactoryBean.getClientRegionShortcut()).isEqualTo(clientRegionShortcut); + assertThat(ClientRegionShortcutWrapper.valueOf(clientRegionShortcut).isLocal()).isTrue(); + + Interest[] interests = this.regionFactoryBean.getInterests(); + + assertThat(interests).isNotNull(); + assertThat(interests).isEmpty(); + }); } @Test @@ -159,38 +204,11 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { assertThat(this.regionFactoryBean.getBeanFactory()).isEqualTo(mockBeanFactory); } - @Test(expected = IllegalArgumentException.class) - @SuppressWarnings("all") - public void setBeanFactoryToNullThrowsIllegalArgumentException() { - try { - this.regionFactoryBean.setBeanFactory(null); - } - catch (IllegalArgumentException expected) { - assertThat(expected).hasMessage("BeanFactory is required"); - assertThat(expected).hasNoCause(); - - throw expected; - } - } - - @Test(expected = IllegalStateException.class) - public void getBeanFactoryWhenNullThrowsIllegalStateException() { - try { - this.regionFactoryBean.getBeanFactory(); - } - catch (IllegalStateException expected) { - assertThat(expected).hasMessage("A reference to the BeanFactory was not properly configured"); - assertThat(expected).hasNoCause(); - - throw expected; - } - } - @Test public void setAndGetClientRegionShortcut() { - assertThat(this.regionFactoryBean.getClientRegionShortcut()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_CLIENT_REGION_SHORTCUT); + assertThat(this.regionFactoryBean.getClientRegionShortcut()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_CLIENT_REGION_SHORTCUT); this.regionFactoryBean.setClientRegionShortcut(ClientRegionShortcut.LOCAL_PERSISTENT); @@ -208,65 +226,39 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { Cache mockCache = mock(Cache.class); - this.regionFactoryBean.setGemfireCache(mockCache); + this.regionFactoryBean.setCache(mockCache); - assertThat(this.regionFactoryBean.getGemfireCache()).isEqualTo(mockCache); - } - - @Test(expected = IllegalArgumentException.class) - public void setGemfireCacheToNullThrowsIllegalArgumentException() { - try { - this.regionFactoryBean.setGemfireCache(null); - } - catch (IllegalArgumentException expected) { - assertThat(expected).hasMessage("GemFireCache is required"); - assertThat(expected).hasNoCause(); - - throw expected; - } - } - - @Test(expected = IllegalStateException.class) - public void getGemfireCacheWhenNullThrowsIllegalStateException() { - try { - this.regionFactoryBean.getGemfireCache(); - } - catch (IllegalStateException expected) { - assertThat(expected).hasMessage("A reference to the GemFireCache was not properly configured"); - assertThat(expected).hasNoCause(); - - throw expected; - } + assertThat(this.regionFactoryBean.getCache()).isEqualTo(mockCache); } @Test public void setAndGetPoolName() { - assertThat(this.regionFactoryBean.getPoolName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); + assertThat(this.regionFactoryBean.getPoolName().orElse(null)) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); this.regionFactoryBean.setPoolName("TestPoolName"); - assertThat(this.regionFactoryBean.getPoolName()).isEqualTo("TestPoolName"); + assertThat(this.regionFactoryBean.getPoolName().orElse(null)).isEqualTo("TestPoolName"); this.regionFactoryBean.setPoolName(" "); - assertThat(this.regionFactoryBean.getPoolName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); + assertThat(this.regionFactoryBean.getPoolName().orElse(null)) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); this.regionFactoryBean.setPoolName(""); - assertThat(this.regionFactoryBean.getPoolName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); + assertThat(this.regionFactoryBean.getPoolName().orElse(null)) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); this.regionFactoryBean.setPoolName(null); - assertThat(this.regionFactoryBean.getPoolName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); + assertThat(this.regionFactoryBean.getPoolName().orElse(null)) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_POOL_NAME); } @Test - @SuppressWarnings("unchecked") + @SuppressWarnings({ "deprecation", "unchecked" }) public void setAndGetRegionAttributes() { RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); @@ -285,8 +277,8 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { @Test public void setAndGetRegionName() { - assertThat(this.regionFactoryBean.getRegionName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); + assertThat(this.regionFactoryBean.getRegionName()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); this.regionFactoryBean.setRegionName("Example"); @@ -294,25 +286,25 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { this.regionFactoryBean.setRegionName(" "); - assertThat(this.regionFactoryBean.getRegionName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); + assertThat(this.regionFactoryBean.getRegionName()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); this.regionFactoryBean.setRegionName(""); - assertThat(this.regionFactoryBean.getRegionName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); + assertThat(this.regionFactoryBean.getRegionName()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); this.regionFactoryBean.setRegionName(null); - assertThat(this.regionFactoryBean.getRegionName()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); + assertThat(this.regionFactoryBean.getRegionName()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SESSION_REGION_NAME); } @Test public void setAndGetServerRegionShortcut() { - assertThat(this.regionFactoryBean.getServerRegionShortcut()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SERVER_REGION_SHORTCUT); + assertThat(this.regionFactoryBean.getServerRegionShortcut()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SERVER_REGION_SHORTCUT); this.regionFactoryBean.setServerRegionShortcut(RegionShortcut.LOCAL_PERSISTENT); @@ -320,7 +312,7 @@ public class GemFireCacheTypeAwareRegionFactoryBeanTests { this.regionFactoryBean.setServerRegionShortcut(null); - assertThat(this.regionFactoryBean.getServerRegionShortcut()).isEqualTo( - GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SERVER_REGION_SHORTCUT); + assertThat(this.regionFactoryBean.getServerRegionShortcut()) + .isEqualTo(GemFireCacheTypeAwareRegionFactoryBean.DEFAULT_SERVER_REGION_SHORTCUT); } }