diff --git a/src/main/java/org/springframework/data/gemfire/LocalRegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/LocalRegionFactoryBean.java index f2012a3e..9e8dcca3 100644 --- a/src/main/java/org/springframework/data/gemfire/LocalRegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/LocalRegionFactoryBean.java @@ -15,8 +15,8 @@ */ package org.springframework.data.gemfire; +import org.springframework.data.gemfire.support.RegionShortcutWrapper; import org.springframework.util.Assert; -import org.springframework.util.StringUtils; import com.gemstone.gemfire.cache.DataPolicy; import com.gemstone.gemfire.cache.RegionFactory; @@ -39,6 +39,28 @@ public class LocalRegionFactoryBean extends RegionFactoryBean { super.afterPropertiesSet(); } + @Override + protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, DataPolicy dataPolicy) { + if (dataPolicy == null || DataPolicy.NORMAL.equals(dataPolicy)) { + // NOTE this is safe since a LOCAL Scoped NORMAL Region requiring persistence can be satisfied with + // PERSISTENT_REPLICATE, per the RegionShortcut.LOCAL_PERSISTENT + regionFactory.setDataPolicy(isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.NORMAL); + } + else if (DataPolicy.PRELOADED.equals(dataPolicy)) { + // NOTE this is safe since a LOCAL Scoped PRELOADED Region requiring persistence can be satisfied with + // PERSISTENT_REPLICATE, per the RegionShortcut.LOCAL_PERSISTENT + regionFactory.setDataPolicy(isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.PRELOADED); + } + else if (DataPolicy.PERSISTENT_REPLICATE.equals(dataPolicy) + && RegionShortcutWrapper.valueOf(getShortcut()).isPersistent()) { + regionFactory.setDataPolicy(dataPolicy); + } + else { + throw new IllegalArgumentException(String.format("Data Policy '%1$s' is not supported for Local Regions.", + dataPolicy)); + } + } + /** * Resolves the Data Policy used by this "local" GemFire Region (i.e. locally Scoped; Scope.LOCAL) based on the * enumerated value from com.gemstone.gemfire.cache.RegionShortcuts (LOCAL, LOCAL_PERSISTENT, LOCAL_HEAP_LRU, @@ -53,25 +75,14 @@ public class LocalRegionFactoryBean extends RegionFactoryBean { */ @Override protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, String dataPolicy) { - DataPolicy resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy); + DataPolicy resolvedDataPolicy = null; - Assert.isTrue(resolvedDataPolicy != null || !StringUtils.hasText(dataPolicy), - String.format("Data Policy '%1$s' is invalid.", dataPolicy)); + if (dataPolicy != null) { + resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy); + Assert.notNull(resolvedDataPolicy, String.format("Data Policy '%1$s' is invalid.", dataPolicy)); + } - if (resolvedDataPolicy == null || DataPolicy.NORMAL.equals(resolvedDataPolicy)) { - // NOTE this is safe since a LOCAL Scoped NORMAL Region requiring persistence can be satisfied with - // PERSISTENT_REPLICATE, per the RegionShortcut.LOCAL_PERSISTENT - regionFactory.setDataPolicy(isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.NORMAL); - } - else if (DataPolicy.PRELOADED.equals(resolvedDataPolicy)) { - // NOTE this is safe since a LOCAL Scoped PRELOADED Region requiring persistence can be satisfied with - // PERSISTENT_REPLICATE, per the RegionShortcut.LOCAL_PERSISTENT - regionFactory.setDataPolicy(isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.PRELOADED); - } - else { - throw new IllegalArgumentException(String.format("Data Policy '%1$s' is not supported in Local Regions.", - dataPolicy)); - } + resolveDataPolicy(regionFactory, persistent, resolvedDataPolicy); } } diff --git a/src/main/java/org/springframework/data/gemfire/PartitionedRegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/PartitionedRegionFactoryBean.java index 85dd5586..4d3f98a9 100644 --- a/src/main/java/org/springframework/data/gemfire/PartitionedRegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/PartitionedRegionFactoryBean.java @@ -28,32 +28,38 @@ import com.gemstone.gemfire.cache.RegionFactory; public class PartitionedRegionFactoryBean extends RegionFactoryBean { @Override - protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, String dataPolicy) { - if (dataPolicy != null) { - DataPolicy resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy); + protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, DataPolicy dataPolicy) { + // First, verify the GemFire version is 6.5 or Higher when Persistence is specified... + Assert.isTrue(!DataPolicy.PERSISTENT_PARTITION.equals(dataPolicy) || GemfireUtils.isGemfireVersion65OrAbove(), + String.format("Persistent PARTITION Regions can only be used from GemFire 6.5 onwards; current version is [%1$s].", + CacheFactory.getVersion())); - Assert.notNull(resolvedDataPolicy, String.format("Data Policy '%1$s' is invalid.", dataPolicy)); - - // Validate that the user-defined Data Policy matches the appropriate Spring GemFire XML namespace - // configuration meta-data element for Region (i.e. )! - Assert.isTrue(resolvedDataPolicy.withPartitioning(), String.format( - "Data Policy '%1$s' is not supported in Partitioned Regions.", resolvedDataPolicy)); - - // Validate that the data-policy and persistent attributes are compatible when specified! - assertDataPolicyAndPersistentAttributesAreCompatible(resolvedDataPolicy); - - regionFactory.setDataPolicy(resolvedDataPolicy); - } - else if (isPersistent()) { - // first, check the presence of GemFire 6.5 or Higher - Assert.isTrue(GemfireUtils.isGemfireVersion65OrAbove(), String.format( - "Can define Persistent Partitioned Regions only from GemFire 6.5 onwards; current version is [%1$s]", - CacheFactory.getVersion())); - regionFactory.setDataPolicy(DataPolicy.PERSISTENT_PARTITION); + if (dataPolicy == null) { + dataPolicy = (isPersistent() ? DataPolicy.PERSISTENT_PARTITION : DataPolicy.PARTITION); } else { - regionFactory.setDataPolicy(DataPolicy.PARTITION); + // Validate that the user-defined Data Policy matches the appropriate Spring GemFire XML namespace + // configuration meta-data element for Region (i.e. )! + Assert.isTrue(dataPolicy.withPartitioning(), String.format( + "Data Policy '%1$s' is not supported in Partitioned Regions.", dataPolicy)); } + + // Validate the data-policy and persistent attributes are compatible when specified! + assertDataPolicyAndPersistentAttributesAreCompatible(dataPolicy); + + regionFactory.setDataPolicy(dataPolicy); + } + + @Override + protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, String dataPolicy) { + DataPolicy resolvedDataPolicy = null; + + if (dataPolicy != null) { + resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy); + Assert.notNull(resolvedDataPolicy, String.format("Data Policy '%1$s' is invalid.", dataPolicy)); + } + + resolveDataPolicy(regionFactory, persistent, resolvedDataPolicy); } } diff --git a/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java index 24570fea..8a625552 100644 --- a/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/RegionFactoryBean.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.context.SmartLifecycle; import org.springframework.core.io.Resource; import org.springframework.data.gemfire.client.ClientRegionFactoryBean; +import org.springframework.data.gemfire.support.RegionShortcutWrapper; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; @@ -35,12 +36,16 @@ import com.gemstone.gemfire.cache.CacheLoader; import com.gemstone.gemfire.cache.CacheWriter; import com.gemstone.gemfire.cache.DataPolicy; import com.gemstone.gemfire.cache.GemFireCache; +import com.gemstone.gemfire.cache.PartitionAttributes; +import com.gemstone.gemfire.cache.PartitionAttributesFactory; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.RegionAttributes; import com.gemstone.gemfire.cache.RegionFactory; +import com.gemstone.gemfire.cache.RegionShortcut; import com.gemstone.gemfire.cache.Scope; import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue; import com.gemstone.gemfire.cache.wan.GatewaySender; +import com.gemstone.gemfire.internal.cache.UserSpecifiedRegionAttributes; /** * Base class for FactoryBeans used to create GemFire {@link Region}s. Will try @@ -56,6 +61,7 @@ import com.gemstone.gemfire.cache.wan.GatewaySender; * @author David Turanski * @author John Blum */ +@SuppressWarnings("unused") public class RegionFactoryBean extends RegionLookupFactoryBean implements DisposableBean, SmartLifecycle { protected final Log log = LogFactory.getLog(getClass()); @@ -74,16 +80,19 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple private CacheWriter cacheWriter; + private DataPolicy dataPolicy; + private Object[] asyncEventQueues; private Object[] gatewaySenders; private RegionAttributes attributes; + private RegionShortcut shortcut; + private Resource snapshot; private Scope scope; - private String dataPolicy; private String diskStoreName; private String hubId; @@ -94,27 +103,17 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple } @Override + @SuppressWarnings("deprecation") protected Region lookupFallback(GemFireCache gemfireCache, String regionName) throws Exception { Assert.isTrue(gemfireCache instanceof Cache, "Unable to create Regions from " + gemfireCache); Cache cache = (Cache) gemfireCache; - RegionFactory regionFactory; - if (attributes != null) { - // TODO refactor... AttributesFactory extended by the SDG RegionAttributesFactoryBean and used by all - // RegionFactoryBeans subclasses calls AttributesFactory.validateAttributes(..) before the RegionAttributes - // are created in the AttributesFactory.create() method, which is called by - // RegionAttributesFactoryBean.afterPropertiesSet() method. - AttributesFactory.validateAttributes(attributes); - regionFactory = cache.createRegionFactory(attributes); - } - else { - regionFactory = cache.createRegionFactory(); - } + RegionFactory regionFactory = createRegionFactory(cache); if (hubId != null) { enableGateway = (enableGateway == null || enableGateway); - Assert.isTrue(enableGateway, "hubId requires the enableGateway property to be true"); + Assert.isTrue(enableGateway, "The 'hubId' requires the 'enableGateway' property to be true"); regionFactory.setGatewayHubId(hubId); } @@ -154,12 +153,12 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple regionFactory.setCacheWriter(cacheWriter); } + resolveDataPolicy(regionFactory, persistent, dataPolicy); + if (diskStoreName != null) { regionFactory.setDiskStoreName(diskStoreName); } - resolveDataPolicy(regionFactory, persistent, dataPolicy); - if (scope != null) { regionFactory.setScope(scope); } @@ -169,8 +168,7 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple "Lock Grantor only applies to a global scoped region."); } - // get underlying AttributesFactory - postProcess(findAttributesFactory(regionFactory)); + postProcess(regionFactory); Region region = (getParent() != null ? regionFactory.createSubregion(getParent(), regionName) : regionFactory.create(regionName)); @@ -196,12 +194,279 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple return region; } + /** + * Validates that the settings for Data Policy and the 'persistent' attribute in elements + * are compatible. + *

+ * @param resolvedDataPolicy the GemFire Data Policy resolved form the Spring GemFire XML namespace configuration + * meta-data. + * @see #isPersistent() + * @see #isNotPersistent() + * @see com.gemstone.gemfire.cache.DataPolicy + */ + protected void assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy resolvedDataPolicy) { + if (resolvedDataPolicy.withPersistence()) { + Assert.isTrue(isPersistentUnspecified() || isPersistent(), String.format( + "Data Policy '%1$s' is invalid when persistent is false.", resolvedDataPolicy)); + } + else { + // NOTE otherwise, the Data Policy is not persistent, so... + Assert.isTrue(isPersistentUnspecified() || isNotPersistent(), String.format( + "Data Policy '%1$s' is invalid when persistent is true.", resolvedDataPolicy)); + } + } + + /** + * Determines whether the user explicitly set the 'persistent' attribute or not. + *

+ * @return a boolean value indicating whether the user explicitly set the 'persistent' attribute to true or false. + * @see #isPersistent() + * @see #isNotPersistent() + */ + protected boolean isPersistentUnspecified() { + return (persistent == null); + } + + /** + * Returns true when the user explicitly specified a value for the persistent attribute and it is true. If the + * persistent attribute was not explicitly specified, then the persistence setting is implicitly undefined + * and will be determined by the Data Policy. + *

+ * @return true when the user specified an explicit value for the persistent attribute and it is true; + * false otherwise. + * @see #isNotPersistent() + * @see #isPersistentUnspecified() + */ + protected boolean isPersistent() { + return Boolean.TRUE.equals(persistent); + } + + /** + * Returns true when the user explicitly specified a value for the persistent attribute and it is false. If the + * persistent attribute was not explicitly specified, then the persistence setting is implicitly undefined + * and will be determined by the Data Policy. + *

+ * @return true when the user specified an explicit value for the persistent attribute and it is false; + * false otherwise. + * @see #isPersistent() + * @see #isPersistentUnspecified() + */ + protected boolean isNotPersistent() { + return Boolean.FALSE.equals(persistent); + } + + /** + * Creates an instance of RegionFactory using the given Cache instance used to configure and construct the Region + * created by this FactoryBean. + *

+ * @param cache the GemFire Cache instance. + * @return a RegionFactory used to configure and construct the Region created by this FactoryBean. + * @see com.gemstone.gemfire.cache.Cache#createRegionFactory() + * @see com.gemstone.gemfire.cache.Cache#createRegionFactory(com.gemstone.gemfire.cache.RegionAttributes) + * @see com.gemstone.gemfire.cache.Cache#createRegionFactory(com.gemstone.gemfire.cache.RegionShortcut) + * @see com.gemstone.gemfire.cache.RegionFactory + */ + protected RegionFactory createRegionFactory(final Cache cache) { + if (shortcut != null) { + RegionFactory regionFactory = mergeRegionAttributes( + cache.createRegionFactory(shortcut), attributes); + setDataPolicy(getDataPolicy(regionFactory)); + return regionFactory; + } + else if (attributes != null) { + return cache.createRegionFactory(attributes); + } + else { + return cache.createRegionFactory(); + } + } + + /* + * (non-Javadoc) - this method is meant strictly to be overridden for testing purposes! + * @see com.gemstone.gemfire.cache.RegionFactory#attrsFactory + * @see com.gemstone.gemfire.cache.AttributesFactory#regionAttributes + * @see com.gemstone.gemfire.cache.RegionAttributes#getDataPolicy + * @see com.gemstone.gemfire.cache.DataPolicy + */ + @SuppressWarnings({ "deprecation", "unchecked"}) + DataPolicy getDataPolicy(final RegionFactory regionFactory) { + // NOTE cannot pass RegionAttributes.class as the "targetType" on the second invocation of getFieldValue(..) + // since the "regionAttributes" field is naively of the implementation class type rather than the interface + // type... so much for programming to interfaces. + return ((RegionAttributes) getFieldValue(getFieldValue(regionFactory, "attrsFactory", AttributesFactory.class), + "regionAttributes", null)).getDataPolicy(); + } + + /* + * (non-Javadoc) + */ + @SuppressWarnings("unchecked") + private T getFieldValue(final Object source, final String fieldName, final Class targetType) { + Field field = ReflectionUtils.findField(source.getClass(), fieldName, targetType); + ReflectionUtils.makeAccessible(field); + return (T) ReflectionUtils.getField(field, source); + } + + /** + * Intelligently merges the given RegionAttributes with the configuration setting of the RegionFactory. This method + * is used to merge the RegionAttributes and PartitionAttributes with the RegionFactory that is created when the + * user specified a RegionShortcut. This method gets called by the createRegionFactory method depending upon + * the value passed to the Cache.createRegionFactory() method (i.e. whether there was a RegionShortcut specified + * or not). + *

+ * @param the Class type fo the Region key. + * @param the Class type of the Region value. + * @param regionFactory the GemFire RegionFactory used to configure and create the Region that is the product + * of this RegionFactoryBean. + * @param regionAttributes the RegionAttributes containing the Region configuration settings to merge to the + * RegionFactory. + * @return the RegionFactory with the configuration settings of the RegionAttributes merged. + * @see #hasUserSpecifiedEvictionAttributes(com.gemstone.gemfire.cache.RegionAttributes) + * @see #validateRegionAttributes(com.gemstone.gemfire.cache.RegionAttributes) + * @see com.gemstone.gemfire.cache.RegionAttributes + * @see com.gemstone.gemfire.cache.RegionFactory + */ + @SuppressWarnings("unchecked") + protected RegionFactory mergeRegionAttributes(final RegionFactory regionFactory, + final RegionAttributes regionAttributes) { + + if (regionAttributes != null) { + // NOTE this validation may not be strictly required depending on how the RegionAttributes were "created", + // but... + validateRegionAttributes(regionAttributes); + + regionFactory.setCloningEnabled(regionAttributes.getCloningEnabled()); + regionFactory.setConcurrencyChecksEnabled(regionAttributes.getConcurrencyChecksEnabled()); + regionFactory.setConcurrencyLevel(regionAttributes.getConcurrencyLevel()); + regionFactory.setCustomEntryIdleTimeout(regionAttributes.getCustomEntryIdleTimeout()); + regionFactory.setCustomEntryTimeToLive(regionAttributes.getCustomEntryTimeToLive()); + regionFactory.setDiskSynchronous(regionAttributes.isDiskSynchronous()); + regionFactory.setEnableAsyncConflation(regionAttributes.getEnableAsyncConflation()); + regionFactory.setEnableSubscriptionConflation(regionAttributes.getEnableSubscriptionConflation()); + regionFactory.setEntryIdleTimeout(regionAttributes.getEntryIdleTimeout()); + regionFactory.setEntryTimeToLive(regionAttributes.getEntryTimeToLive()); + + // NOTE EvictionAttributes are created by certain RegionShortcuts; need the null check! + if (hasUserSpecifiedEvictionAttributes(regionAttributes)) { + regionFactory.setEvictionAttributes(regionAttributes.getEvictionAttributes()); + } + + regionFactory.setIgnoreJTA(regionAttributes.getIgnoreJTA()); + regionFactory.setIndexMaintenanceSynchronous(regionAttributes.getIndexMaintenanceSynchronous()); + regionFactory.setInitialCapacity(regionAttributes.getInitialCapacity()); + regionFactory.setKeyConstraint(regionAttributes.getKeyConstraint()); + regionFactory.setLoadFactor(regionAttributes.getLoadFactor()); + regionFactory.setLockGrantor(regionAttributes.isLockGrantor()); + regionFactory.setMembershipAttributes(regionAttributes.getMembershipAttributes()); + regionFactory.setMulticastEnabled(regionAttributes.getMulticastEnabled()); + mergePartitionAttributes(regionFactory, regionAttributes); + regionFactory.setPoolName(regionAttributes.getPoolName()); + regionFactory.setRegionIdleTimeout(regionAttributes.getRegionIdleTimeout()); + regionFactory.setRegionTimeToLive(regionAttributes.getRegionTimeToLive()); + regionFactory.setStatisticsEnabled(regionAttributes.getStatisticsEnabled()); + regionFactory.setSubscriptionAttributes(regionAttributes.getSubscriptionAttributes()); + regionFactory.setValueConstraint(regionAttributes.getValueConstraint()); + } + + return regionFactory; + } + + protected void mergePartitionAttributes(final RegionFactory regionFactory, final RegionAttributes regionAttributes) { + // NOTE PartitionAttributes are created by certain RegionShortcuts; need the null check since RegionAttributes + // can technically return null! + // NOTE most likely, the PartitionAttributes will never be null since the PartitionRegionFactoryBean always + // sets a PartitionAttributesFactoryBean BeanBuilder on the RegionAttributesFactoryBean "partitionAttributes" + // property. + if (regionAttributes.getPartitionAttributes() != null) { + PartitionAttributes partitionAttributes = regionAttributes.getPartitionAttributes(); + PartitionAttributesFactory partitionAttributesFactory = new PartitionAttributesFactory(partitionAttributes); + RegionShortcutWrapper shortcutWrapper = RegionShortcutWrapper.valueOf(shortcut); + + // NOTE however, since the default value of redundancy is 0, we need to account for 'redundant' + // RegionShortcut types, which specify a redundancy of 1. + if (shortcutWrapper.isRedundant() && partitionAttributes.getRedundantCopies() == 0) { + partitionAttributesFactory.setRedundantCopies(1); + } + + // NOTE and, since the default value of localMaxMemory is based on the system memory, we need to account for + // 'proxy' RegionShortcut types, which specify a local max memory of 0. + if (shortcutWrapper.isProxy()) { + partitionAttributesFactory.setLocalMaxMemory(0); + } + + // NOTE internally, RegionFactory.setPartitionAttributes handles merging the PartitionAttributes, hooray! + regionFactory.setPartitionAttributes(partitionAttributesFactory.create()); + } + } + + /* + * (non-Javadoc) - this method is meant strictly to be overridden for testing purposes! + * NOTE unfortunately, must resort to using a GemFire internal class, ugh! + * @see com.gemstone.gemfire.internal.cache.UserSpecifiedRegionAttributes#hasEvictionAttributes + */ + boolean hasUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) { + return (regionAttributes instanceof UserSpecifiedRegionAttributes + && ((UserSpecifiedRegionAttributes) regionAttributes).hasEvictionAttributes()); + } + + /* + * (non-Javadoc) - this method is meant strictly to be overridden for testing purposes! + * @see com.gemstone.gemfire.cache.AttributesFactory#validateAttributes(:RegionAttributes) + */ + @SuppressWarnings("deprecation") + void validateRegionAttributes(final RegionAttributes regionAttributes) { + AttributesFactory.validateAttributes(regionAttributes); + } + + /** + * Post-process the RegionFactory used to create the GemFire Region for this factory bean during the initialization + * process. The RegionFactory is already configured and initialized by the factory bean before this method + * is invoked. + *

+ * @param regionFactory the GemFire RegionFactory used to create the Region for post-processing. + * @see com.gemstone.gemfire.cache.RegionFactory + */ + protected void postProcess(RegionFactory regionFactory) { + } + + /** + * Post-process the Region for this factory bean during the initialization process. The Region is + * already configured and initialized by the factory bean before this method is invoked. + *

+ * @param region the GemFire Region to post-process. + * @see com.gemstone.gemfire.cache.Region + */ + protected void postProcess(Region region) { + } + + /** + * Validates and sets the Data Policy on the RegionFactory used to create and configure the Region from this + * FactoryBean. + *

+ * @param regionFactory the RegionFactory used by this FactoryBean to create and configure the Region. + * @param persistent a boolean value indicating whether the Region should be persistent and persist it's + * data to disk. + * @param dataPolicy the configured Data Policy for the Region. + * @see #resolveDataPolicy(com.gemstone.gemfire.cache.RegionFactory, Boolean, String) + * @see com.gemstone.gemfire.cache.DataPolicy + * @see com.gemstone.gemfire.cache.RegionFactory + */ + protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, DataPolicy dataPolicy) { + if (dataPolicy != null) { + assertDataPolicyAndPersistentAttributesAreCompatible(dataPolicy); + regionFactory.setDataPolicy(dataPolicy); + } + else { + resolveDataPolicy(regionFactory, persistent, (String) null); + } + } + /** * Validates the configured Data Policy and may override it, taking into account the 'persistent' attribute * and constraints for the Region type. *

- * @param regionFactory the GemFire RegionFactory used to created the Local Region. - * @param persistent a boolean value indicating whether the Local Region should persist it's data. + * @param regionFactory the GemFire RegionFactory used to create the desired Region. + * @param persistent a boolean value indicating whether the Region should persist it's data to disk. * @param dataPolicy requested Data Policy as set by the user in the Spring GemFire configuration meta-data. * @see com.gemstone.gemfire.cache.DataPolicy * @see com.gemstone.gemfire.cache.RegionFactory @@ -220,40 +485,6 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple } } - @SuppressWarnings("unchecked") - private AttributesFactory findAttributesFactory(RegionFactory regionFactory) { - Field attrsFactoryField = ReflectionUtils.findField(RegionFactory.class, "attrsFactory", - AttributesFactory.class); - ReflectionUtils.makeAccessible(attrsFactoryField); - return (AttributesFactory) ReflectionUtils.getField(attrsFactoryField, regionFactory); - } - - /** - * Post-process the attribute factory object used for configuring the region - * of this factory bean during the initialization process. The object is - * already initialized and configured by the factory bean before this method - * is invoked. - * - * @param attributesFactory attribute factory - * @deprecated as of GemFire 6.5, the use of {@link AttributesFactory} has - * been deprecated - */ - @Deprecated - @SuppressWarnings("unused") - protected void postProcess(AttributesFactory attributesFactory) { - } - - /** - * Post-process the region object for this factory bean during the - * initialization process. The object is already initialized and configured - * by the factory bean before this method is invoked. - * - * @param region - */ - @SuppressWarnings("unused") - protected void postProcess(Region region) { - } - @Override public void destroy() throws Exception { if (getRegion() != null) { @@ -274,9 +505,9 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple } /** - * - * @param asyncEventQueues defined as Object for backward compatibility with - * Gemfire 6 + * The list of AsyncEventQueues to use with this Region. + *

+ * @param asyncEventQueues defined as Object for backwards compatibility with Gemfire 6. */ public void setAsyncEventQueues(Object[] asyncEventQueues) { this.asyncEventQueues = asyncEventQueues; @@ -293,14 +524,6 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple this.attributes = attributes; } - /** - * Indicates whether the region referred by this factory bean, will be - * closed on shutdown (default true). - */ - public void setClose(boolean close) { - this.close = close; - } - /** * Sets the cache listeners used for the region used by this factory. Used * only when a new region is created.Overrides the settings specified @@ -334,6 +557,14 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple this.cacheWriter = cacheWriter; } + /** + * Indicates whether the region referred by this factory bean, will be + * closed on shutdown (default true). + */ + public void setClose(boolean close) { + this.close = close; + } + /** * Indicates whether the region referred by this factory bean, will be * destroyed on shutdown (default false). @@ -343,17 +574,30 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple } /** - * Sets the dataPolicy as a String. Required to support property - * placeholders - * @param dataPolicyName the dataPolicy name (NORMAL, PRELOADED, etc) + * Sets the DataPolicy of the Region. + *

+ * @param dataPolicy the GemFire DataPolicy to use when configuring the Region. + * @since 1.4.0 */ - public void setDataPolicy(String dataPolicyName) { - this.dataPolicy = dataPolicyName; + public void setDataPolicy(DataPolicy dataPolicy) { + this.dataPolicy = dataPolicy; } /** - * Sets the name of disk store to use for overflow and persistence - * @param diskStoreName + * Sets the DataPolicy of the Region as a String. + *

+ * @param dataPolicyName the name of the DataPolicy (e.g. REPLICATE, PARTITION) + * @see #setDataPolicy(com.gemstone.gemfire.cache.DataPolicy) + * @deprecated as of 1.4.0, use setDataPolicy(:DataPolicy) instead. + */ + public void setDataPolicy(String dataPolicyName) { + this.dataPolicy = new DataPolicyConverter().convert(dataPolicyName); + } + + /** + * Sets the name of Disk Store used for either overflow or persistence, or both. + *

+ * @param diskStoreName the name of the Disk Store bean in context used for overflow/persistence. */ public void setDiskStoreName(String diskStoreName) { this.diskStoreName = diskStoreName; @@ -376,7 +620,7 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple this.hubId = hubId; } - public void setPersistent(boolean persistent) { + public void setPersistent(Boolean persistent) { this.persistent = persistent; } @@ -391,6 +635,24 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple this.scope = scope; } + /* + * (non-Javadoc) + */ + protected final RegionShortcut getShortcut() { + return shortcut; + } + + /** + * Configures the Region with a RegionShortcut. + *

+ * @param shortcut the RegionShortcut used to configure pre-defined default for the Region created + * by this FactoryBean. + * @see com.gemstone.gemfire.cache.RegionShortcut + */ + public void setShortcut(RegionShortcut shortcut) { + this.shortcut = shortcut; + } + /** * Sets the snapshots used for loading a newly created region. That * is, the snapshot will be used only when a new region is created - @@ -403,57 +665,8 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple this.snapshot = snapshot; } - /** - * Validates that the settings for Data Policy and the 'persistent' attribute in elements - * are compatible. - *

- * @param resolvedDataPolicy the GemFire Data Policy resolved form the Spring GemFire XML namespace configuration - * meta-data. - * @see #isPersistent() - * @see #isNotPersistent() - * @see com.gemstone.gemfire.cache.DataPolicy - */ - protected void assertDataPolicyAndPersistentAttributesAreCompatible(final DataPolicy resolvedDataPolicy) { - final boolean persistentNotSpecified = (this.persistent == null); - - if (resolvedDataPolicy.withPersistence()) { - Assert.isTrue(persistentNotSpecified || isPersistent(), String.format( - "Data Policy '%1$s' is invalid when persistent is false.", resolvedDataPolicy)); - } - else { - // NOTE otherwise, the Data Policy is with persistence, so... - Assert.isTrue(persistentNotSpecified || isNotPersistent(), String.format( - "Data Policy '%1$s' is invalid when persistent is true.", resolvedDataPolicy)); - } - } - - /** - * Returns true when the user explicitly specified a value for the persistent attribute and it is true. If the - * persistent attribute was not explicitly specified, then the persistence setting is implicitly undefined - * and will be determined by the Data Policy. - *

- * @return true when the user specified an explicit value for the persistent attribute and it is true; - * false otherwise. - * @see #isNotPersistent() - */ - protected boolean isPersistent() { - return Boolean.TRUE.equals(persistent); - } - - /** - * Returns true when the user explicitly specified a value for the persistent attribute and it is false. If the - * persistent attribute was not explicitly specified, then the persistence setting is implicitly undefined - * and will be determined by the Data Policy. - *

- * @return true when the user specified an explicit value for the persistent attribute and it is false; - * false otherwise. - * @see #isPersistent() - */ - protected boolean isNotPersistent() { - return Boolean.FALSE.equals(persistent); - } - - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.context.Lifecycle#start() */ @Override @@ -461,9 +674,9 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple if (!ObjectUtils.isEmpty(gatewaySenders)) { synchronized (gatewaySenders) { for (Object obj : gatewaySenders) { - GatewaySender gws = (GatewaySender) obj; - if (!(gws.isManualStart() || gws.isRunning())) { - gws.start(); + GatewaySender gatewaySender = (GatewaySender) obj; + if (!(gatewaySender.isManualStart() || gatewaySender.isRunning())) { + gatewaySender.start(); } } } @@ -471,22 +684,25 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple this.running = true; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.context.Lifecycle#stop() */ @Override public void stop() { if (!ObjectUtils.isEmpty(gatewaySenders)) { synchronized (gatewaySenders) { - for (Object obj : gatewaySenders) { - ((GatewaySender) obj).stop(); + for (Object gatewaySender : gatewaySenders) { + ((GatewaySender) gatewaySender).stop(); } } } + this.running = false; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.context.Lifecycle#isRunning() */ @Override @@ -494,7 +710,8 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple return this.running; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.context.Phased#getPhase() */ @Override @@ -502,7 +719,8 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple return Integer.MAX_VALUE; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.context.SmartLifecycle#isAutoStartup() */ @Override @@ -510,7 +728,8 @@ public class RegionFactoryBean extends RegionLookupFactoryBean imple return this.autoStartup; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.context.SmartLifecycle#stop(java.lang.Runnable) */ @Override diff --git a/src/main/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBean.java index 642f8d85..2794678c 100644 --- a/src/main/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBean.java @@ -27,30 +27,36 @@ import com.gemstone.gemfire.cache.RegionFactory; public class ReplicatedRegionFactoryBean extends RegionFactoryBean { @Override - protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, String dataPolicy) { - if (dataPolicy != null) { - DataPolicy resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy); - - Assert.notNull(resolvedDataPolicy, String.format("Data Policy '%1$s' is invalid.", dataPolicy)); - - if (DataPolicy.EMPTY.equals(resolvedDataPolicy)) { - resolvedDataPolicy = DataPolicy.EMPTY; - } - else { - // Validate that the user-defined Data Policy matches the appropriate Spring GemFire XML namespace - // configuration meta-data element for Region (i.e. )! - Assert.isTrue(resolvedDataPolicy.withReplication(), String.format( - "Data Policy '%1$s' is not supported in Replicated Regions.", resolvedDataPolicy)); - } - - // Validate that the data-policy and persistent attributes are compatible when specified! - assertDataPolicyAndPersistentAttributesAreCompatible(resolvedDataPolicy); - - regionFactory.setDataPolicy(resolvedDataPolicy); + protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, DataPolicy dataPolicy) { + if (dataPolicy == null) { + dataPolicy = (isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.REPLICATE); + } + else if (DataPolicy.EMPTY.equals(dataPolicy)) { + dataPolicy = DataPolicy.EMPTY; } else { - regionFactory.setDataPolicy(isPersistent() ? DataPolicy.PERSISTENT_REPLICATE : DataPolicy.REPLICATE); + // Validate that the user-defined Data Policy matches the appropriate Spring GemFire XML namespace + // configuration meta-data element for the Region (i.e. )! + Assert.isTrue(dataPolicy.withReplication(), String.format( + "Data Policy '%1$s' is not supported in Replicated Regions.", dataPolicy)); } + + // Validate that the data-policy and persistent attributes are compatible when both are specified! + assertDataPolicyAndPersistentAttributesAreCompatible(dataPolicy); + + regionFactory.setDataPolicy(dataPolicy); + } + + @Override + protected void resolveDataPolicy(RegionFactory regionFactory, Boolean persistent, String dataPolicy) { + DataPolicy resolvedDataPolicy = null; + + if (dataPolicy != null) { + resolvedDataPolicy = new DataPolicyConverter().convert(dataPolicy); + Assert.notNull(resolvedDataPolicy, String.format("Data Policy '%1$s' is invalid.", dataPolicy)); + } + + resolveDataPolicy(regionFactory, persistent, resolvedDataPolicy); } } diff --git a/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java index 6e09502d..ff41c1c0 100644 --- a/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/AbstractRegionParser.java @@ -80,7 +80,7 @@ abstract class AbstractRegionParser extends AbstractSingleBeanDefinitionParser { ParsingUtils.setPropertyValue(element, builder, "name"); ParsingUtils.setPropertyValue(element, builder, "data-policy"); ParsingUtils.setPropertyValue(element, builder, "persistent"); - ParsingUtils.setPropertyValue(element, regionAttributesBuilder, "publisher"); + ParsingUtils.setPropertyValue(element, builder, "shortcut"); if (StringUtils.hasText(element.getAttribute("disk-store-ref"))) { ParsingUtils.setPropertyValue(element, builder, "disk-store-ref", "diskStoreName"); @@ -147,8 +147,8 @@ abstract class AbstractRegionParser extends AbstractSingleBeanDefinitionParser { private void parseCollectionOfCustomSubElements(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String className, String subElementName, String propertyName) { - List subElements = DomUtils.getChildElementsByTagName(element, - new String[] { subElementName, subElementName + "-ref" }); + List subElements = DomUtils.getChildElementsByTagName(element, subElementName, + subElementName + "-ref"); if (!CollectionUtils.isEmpty(subElements)) { ManagedArray array = new ManagedArray(className, subElements.size()); @@ -224,4 +224,13 @@ abstract class AbstractRegionParser extends AbstractSingleBeanDefinitionParser { return parentPath; } + protected void validateDataPolicyShortcutAttributesMutualExclusion(final Element element, + final ParserContext parserContext) { + if (element.hasAttribute("data-policy") && element.hasAttribute("shortcut")) { + parserContext.getReaderContext().error(String.format( + "Only one of [data-policy, shortcut] may be specified with element '%1$s'.", element.getTagName()), + element); + } + } + } diff --git a/src/main/java/org/springframework/data/gemfire/config/ClientRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/ClientRegionParser.java index 67f9bab2..75734ba1 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ClientRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/ClientRegionParser.java @@ -49,7 +49,7 @@ class ClientRegionParser extends AbstractRegionParser { protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, boolean subRegion) { - validateDataPolicyShortcutMutualExclusion(element, parserContext); + validateDataPolicyShortcutAttributesMutualExclusion(element, parserContext); String resolvedCacheRef = ParsingUtils.resolveCacheReference(element.getAttribute("cache-ref")); @@ -114,14 +114,6 @@ class ClientRegionParser extends AbstractRegionParser { } } - private void validateDataPolicyShortcutMutualExclusion(final Element element, final ParserContext parserContext) { - if (element.hasAttribute("data-policy") && element.hasAttribute("shortcut")) { - parserContext.getReaderContext().error(String.format( - "Only one of [data-policy, shortcut] may be specified with element '%1$s'.", element.getTagName()), - element); - } - } - private void parseDiskStoreAttribute(final Element element, final BeanDefinitionBuilder builder) { String diskStoreRefAttribute = element.getAttribute("disk-store-ref"); diff --git a/src/main/java/org/springframework/data/gemfire/config/LocalRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/LocalRegionParser.java index b76f3e5c..8298a591 100644 --- a/src/main/java/org/springframework/data/gemfire/config/LocalRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/LocalRegionParser.java @@ -39,6 +39,8 @@ class LocalRegionParser extends AbstractRegionParser { protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, boolean subRegion) { + validateDataPolicyShortcutAttributesMutualExclusion(element, parserContext); + BeanDefinitionBuilder regionAttributesBuilder = BeanDefinitionBuilder.genericBeanDefinition( RegionAttributesFactoryBean.class); diff --git a/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java b/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java index f086f245..c13de78f 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java +++ b/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java @@ -303,6 +303,7 @@ abstract class ParsingUtils { setPropertyValue(element, regionAttributesBuilder, "cloning-enabled"); setPropertyValue(element, regionAttributesBuilder, "concurrency-level"); + setPropertyValue(element, regionAttributesBuilder, "disk-synchronous"); setPropertyValue(element, regionAttributesBuilder, "enable-async-conflation"); setPropertyValue(element, regionAttributesBuilder, "enable-subscription-conflation"); setPropertyValue(element, regionAttributesBuilder, "ignore-jta", "ignoreJTA"); @@ -311,6 +312,7 @@ abstract class ParsingUtils { setPropertyValue(element, regionAttributesBuilder, "key-constraint"); setPropertyValue(element, regionAttributesBuilder, "load-factor"); setPropertyValue(element, regionAttributesBuilder, "multicast-enabled"); + setPropertyValue(element, regionAttributesBuilder, "publisher"); setPropertyValue(element, regionAttributesBuilder, "value-constraint"); String indexUpdateType = element.getAttribute("index-update-type"); diff --git a/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java index 7065f410..010df399 100644 --- a/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/PartitionedRegionParser.java @@ -46,10 +46,13 @@ class PartitionedRegionParser extends AbstractRegionParser { return PartitionedRegionFactoryBean.class; } - @SuppressWarnings("unchecked") @Override + @SuppressWarnings("unchecked") protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, boolean subRegion) { + + validateDataPolicyShortcutAttributesMutualExclusion(element, parserContext); + super.doParse(element, builder); BeanDefinitionBuilder regionAttributesBuilder = BeanDefinitionBuilder.genericBeanDefinition( @@ -63,11 +66,11 @@ class PartitionedRegionParser extends AbstractRegionParser { PartitionAttributesFactoryBean.class); parseColocatedWith(element, builder, partitionAttributesBuilder, "colocated-with"); - ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "copies","redundantCopies"); + ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "copies", "redundantCopies"); ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "local-max-memory"); ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "recovery-delay"); ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "startup-recovery-delay"); - ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "total-buckets","totalNumBuckets"); + ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "total-buckets", "totalNumBuckets"); ParsingUtils.setPropertyValue(element, partitionAttributesBuilder, "total-max-memory"); Element partitionResolverSubElement = DomUtils.getChildElementByTagName(element, "partition-resolver"); diff --git a/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java b/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java index 58eed613..f42806dd 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/ReplicatedRegionParser.java @@ -40,13 +40,14 @@ class ReplicatedRegionParser extends AbstractRegionParser { protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, boolean subRegion) { + validateDataPolicyShortcutAttributesMutualExclusion(element, parserContext); + ParsingUtils.parseScope(element, builder); BeanDefinitionBuilder regionAttributesBuilder = BeanDefinitionBuilder.genericBeanDefinition( RegionAttributesFactoryBean.class); - super.doParseCommonRegionConfiguration(element, parserContext, builder, regionAttributesBuilder, - subRegion); + super.doParseCommonRegionConfiguration(element, parserContext, builder, regionAttributesBuilder, subRegion); builder.addPropertyValue("attributes", regionAttributesBuilder.getBeanDefinition()); } diff --git a/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java b/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java index 0f6cb9e7..93a6b2f3 100644 --- a/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java +++ b/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java @@ -129,8 +129,7 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw Object instance = instantiator.createInstance(entity, provider); - final BeanWrapper, Object> wrapper = BeanWrapper - .create(instance, conversionService); + final BeanWrapper wrapper = BeanWrapper.create(instance, conversionService); entity.doWithProperties(new PropertyHandler() { @Override @@ -168,7 +167,7 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw @Override public boolean toData(Object value, final PdxWriter writer) { GemfirePersistentEntity entity = mappingContext.getPersistentEntity(value.getClass()); - final BeanWrapper, Object> wrapper = BeanWrapper.create(value, conversionService); + final BeanWrapper wrapper = BeanWrapper.create(value, conversionService); entity.doWithProperties(new PropertyHandler() { @SuppressWarnings({ "unchecked", "rawtypes" }) diff --git a/src/main/java/org/springframework/data/gemfire/support/ClientRegionShortcutWrapper.java b/src/main/java/org/springframework/data/gemfire/support/ClientRegionShortcutWrapper.java new file mode 100644 index 00000000..2b2fc10c --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/support/ClientRegionShortcutWrapper.java @@ -0,0 +1,92 @@ +/* + * Copyright 2010-2013 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 + * + * http://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.support; + +import org.springframework.util.ObjectUtils; + +import com.gemstone.gemfire.cache.client.ClientRegionShortcut; + +/** + * The ClientRegionShortcutWrapper enum is a Java enumerated type that wraps GemFire's ClientRegionShortcuts + * with Spring Data GemFire ClientRegionShortcutWrapper enumerated values. + *

+ * @author John Blum + * @see com.gemstone.gemfire.cache.client.ClientRegionShortcut + * @since 1.4.0 + */ +@SuppressWarnings("unused") +public enum ClientRegionShortcutWrapper { + CACHING_PROXY(ClientRegionShortcut.CACHING_PROXY), + CACHING_PROXY_HEAP_LRU(ClientRegionShortcut.CACHING_PROXY_HEAP_LRU), + CACHING_PROXY_OVERFLOW(ClientRegionShortcut.CACHING_PROXY_OVERFLOW), + LOCAL(ClientRegionShortcut.LOCAL), + LOCAL_HEAP_LRU(ClientRegionShortcut.LOCAL_HEAP_LRU), + LOCAL_OVERFLOW(ClientRegionShortcut.LOCAL_OVERFLOW), + LOCAL_PERSISTENT(ClientRegionShortcut.LOCAL_PERSISTENT), + LOCAL_PERSISTENT_OVERFLOW(ClientRegionShortcut.LOCAL_PERSISTENT_OVERFLOW), + PROXY(ClientRegionShortcut.PROXY), + UNSPECIFIED(null); + + private final ClientRegionShortcut clientRegionShortcut; + + ClientRegionShortcutWrapper(final ClientRegionShortcut clientRegionShortcut) { + this.clientRegionShortcut = clientRegionShortcut; + } + + public static ClientRegionShortcutWrapper valueOf(final ClientRegionShortcut clientRegionShortcut) { + for (ClientRegionShortcutWrapper wrapper : values()) { + if (ObjectUtils.nullSafeEquals(wrapper.getClientRegionShortcut(), clientRegionShortcut)) { + return wrapper; + } + } + + return ClientRegionShortcutWrapper.UNSPECIFIED; + } + + public ClientRegionShortcut getClientRegionShortcut() { + return clientRegionShortcut; + } + + public boolean isCaching() { + return name().contains("CACHING"); + } + + public boolean isHeapLru() { + return name().contains("HEAP_LRU"); + } + + public boolean isLocal() { + return name().contains("LOCAL"); + } + + public boolean isOverflow() { + return name().contains("OVERFLOW"); + } + + public boolean isPersistent() { + return name().contains("PERSISTENT"); + } + + public boolean isPersistentOverflow() { + return name().contains("PERSISTENT_OVERFLOW"); + } + + public boolean isProxy() { + return name().contains("PROXY"); + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/support/RegionShortcutWrapper.java b/src/main/java/org/springframework/data/gemfire/support/RegionShortcutWrapper.java new file mode 100644 index 00000000..669214fa --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/support/RegionShortcutWrapper.java @@ -0,0 +1,115 @@ +/* + * Copyright 2010-2013 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 + * + * http://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.support; + +import org.springframework.util.ObjectUtils; + +import com.gemstone.gemfire.cache.RegionShortcut; + +/** + * The RegionShortcutWrapper enum is a Java enumerated type that wraps GemFire's RegionShortcuts + * with Spring Data GemFire RegionShortcutWrapper enumerated values. + *

+ * + * @author John Blum + * @see com.gemstone.gemfire.cache.RegionShortcut + * @since 1.4.0 + */ +@SuppressWarnings("unused") +public enum RegionShortcutWrapper { + LOCAL(RegionShortcut.LOCAL), + LOCAL_HEAP_LRU(RegionShortcut.LOCAL_HEAP_LRU), + LOCAL_OVERFLOW(RegionShortcut.LOCAL_OVERFLOW), + LOCAL_PERSISTENT(RegionShortcut.LOCAL_PERSISTENT), + LOCAL_PERSISTENT_OVERFLOW(RegionShortcut.LOCAL_PERSISTENT_OVERFLOW), + PARTITION(RegionShortcut.PARTITION), + PARTITION_HEAP_LRU(RegionShortcut.PARTITION_HEAP_LRU), + PARTITION_OVERFLOW(RegionShortcut.PARTITION_OVERFLOW), + PARTITION_PERSISTENT(RegionShortcut.PARTITION_PERSISTENT), + PARTITION_PERSISTENT_OVERFLOW(RegionShortcut.PARTITION_PERSISTENT_OVERFLOW), + PARTITION_PROXY(RegionShortcut.PARTITION_PROXY), + PARTITION_PROXY_REDUNDANT(RegionShortcut.PARTITION_PROXY_REDUNDANT), + PARTITION_REDUNDANT(RegionShortcut.PARTITION_REDUNDANT), + PARTITION_REDUNDANT_HEAP_LRU(RegionShortcut.PARTITION_REDUNDANT_HEAP_LRU), + PARTITION_REDUNDANT_OVERFLOW(RegionShortcut.PARTITION_REDUNDANT_OVERFLOW), + PARTITION_REDUNDANT_PERSISTENT(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT), + PARTITION_REDUNDANT_PERSISTENT_OVERFLOW(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW), + REPLICATE(RegionShortcut.REPLICATE), + REPLICATE_HEAP_LRU(RegionShortcut.REPLICATE_HEAP_LRU), + REPLICATE_OVERFLOW(RegionShortcut.REPLICATE_OVERFLOW), + REPLICATE_PERSISTENT(RegionShortcut.REPLICATE_PERSISTENT), + REPLICATE_PERSISTENT_OVERFLOW(RegionShortcut.REPLICATE_PERSISTENT_OVERFLOW), + REPLICATE_PROXY(RegionShortcut.REPLICATE_PROXY), + UNSPECIFIED(null); + + private final RegionShortcut regionShortcut; + + RegionShortcutWrapper(final RegionShortcut regionShortcut) { + this.regionShortcut = regionShortcut; + } + + public static RegionShortcutWrapper valueOf(final RegionShortcut regionShortcut) { + for (RegionShortcutWrapper wrapper : values()) { + if (ObjectUtils.nullSafeEquals(wrapper.getRegionShortcut(), regionShortcut)) { + return wrapper; + } + } + + return RegionShortcutWrapper.UNSPECIFIED; + } + + public boolean isHeapLru() { + return name().contains("HEAP_LRU"); + } + + public boolean isLocal() { + return name().contains("LOCAL"); + } + + public boolean isOverflow() { + return name().contains("OVERFLOW"); + } + + public boolean isPartition() { + return name().contains("PARTITION"); + } + + public boolean isPersistent() { + return name().contains("PERSISTENT"); + } + + public boolean isPersistentOverflow() { + return (isOverflow() && isPersistent()); + } + + public boolean isProxy() { + return name().contains("PROXY"); + } + + public boolean isRedundant() { + return name().contains("REDUNDANT"); + } + + public boolean isReplicate() { + return name().contains("REPLICATE"); + } + + public RegionShortcut getRegionShortcut() { + return regionShortcut; + } + +} diff --git a/src/main/resources/META-INF/spring.schemas b/src/main/resources/META-INF/spring.schemas index 1361f7a9..bf98a37d 100644 --- a/src/main/resources/META-INF/spring.schemas +++ b/src/main/resources/META-INF/spring.schemas @@ -2,7 +2,7 @@ http\://www.springframework.org/schema/gemfire/spring-gemfire-1.0.xsd=org/spring http\://www.springframework.org/schema/gemfire/spring-gemfire-1.1.xsd=org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd http\://www.springframework.org/schema/gemfire/spring-gemfire-1.2.xsd=org/springframework/data/gemfire/config/spring-gemfire-1.2.xsd http\://www.springframework.org/schema/gemfire/spring-gemfire-1.3.xsd=org/springframework/data/gemfire/config/spring-gemfire-1.3.xsd -http\://www.springframework.org/schema/gemfire/spring-gemfire-1.3.xsd=org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd +http\://www.springframework.org/schema/gemfire/spring-gemfire-1.4.xsd=org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd http\://www.springframework.org/schema/gemfire/spring-gemfire.xsd=org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd http\://www.springframework.org/schema/data/gemfire/spring-data-gemfire-1.2.xsd=org/springframework/data/gemfire/config/spring-data-gemfire-1.2.xsd http\://www.springframework.org/schema/data/gemfire/spring-data-gemfire-1.3.xsd=org/springframework/data/gemfire/config/spring-data-gemfire-1.3.xsd diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd index 1b852859..8d0db360 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd @@ -1,14 +1,16 @@ - + + + + + + + + + + + + + + + + + + - + - - - - - +The RegionShortcut for this region. Allows easy initialization of the region based on pre-defined defaults. + ]]> + + + + + + + + + + @@ -1086,13 +1112,6 @@ The action to take when performing eviction. - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1257,16 +1306,28 @@ redundancy. Each copy provides extra backup at the expense of extra storages. ]]> - + - + + + + + + + + + + + + - + + ]]> - + - + - + + + + + + + + + + + + + + + + + + + + + + - - - + + - - + ]]> + + + + + + + + @@ -1771,20 +1857,20 @@ The name of the pool used by this client. If not set, a default pool (initialize - - - - - - - - - + + + + + + + + + diff --git a/src/test/java/org/springframework/data/gemfire/InvalidRegionDataPolicyShortcutsTest.java b/src/test/java/org/springframework/data/gemfire/InvalidRegionDataPolicyShortcutsTest.java new file mode 100644 index 00000000..f6cf6557 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/InvalidRegionDataPolicyShortcutsTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2010-2013 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 + * + * http://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; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * The InvalidRegionDataPolicyShortcutsIntegrationTest class is a test suite of test cases testing and setting up + * some invalid, or illegal uses of the Region data-policy and/or shortcut XML namespace attributes. + *

+ * @author John Blum + * @see org.junit.Test + * @since 1.4.0 + */ +public class InvalidRegionDataPolicyShortcutsTest { + + @Test(expected = BeanCreationException.class) + public void testInvalidRegionShortcutWithPersistentAttribute() { + try { + new ClassPathXmlApplicationContext( + "/org/springframework/data/gemfire/invalid-region-shortcut-with-persistent-attribute.xml"); + } + catch (BeanCreationException expected) { + //expected.printStackTrace(System.err); + assertTrue(expected.getMessage().contains("Error creating bean with name 'InvalidReplicate'")); + throw expected; + } + } + + @Test(expected = BeanDefinitionParsingException.class) + public void testInvalidUseOfRegionDataPolicyAndShortcut() { + try { + new ClassPathXmlApplicationContext( + "/org/springframework/data/gemfire/invalid-use-of-region-datapolicy-and-shortcut.xml"); + } + catch (BeanDefinitionParsingException expected) { + //expected.printStackTrace(System.err); + assertTrue(expected.getMessage().contains( + "Only one of [data-policy, shortcut] may be specified with element 'gfe:partitioned-region'")); + throw expected; + } + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/LocalRegionFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/LocalRegionFactoryBeanTest.java index 1a4b2702..2b3556b5 100644 --- a/src/test/java/org/springframework/data/gemfire/LocalRegionFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/LocalRegionFactoryBeanTest.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.data.gemfire; import static org.junit.Assert.assertEquals; @@ -28,11 +29,13 @@ import org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest; import com.gemstone.gemfire.cache.DataPolicy; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.RegionFactory; +import com.gemstone.gemfire.cache.RegionShortcut; /** * The PartitionedRegionFactoryBeanTest class is a test suite of test cases testing the component functionality * and correct behavior of the PartitionedRegionFactoryBean class. *

+ * * @author David Turanski * @author John Blum * @see org.mockito.Mockito @@ -49,31 +52,31 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { private RegionFactoryBeanConfig defaultConfig() { return new RegionFactoryBeanConfig(new LocalRegionFactoryBean(), "default") { + @Override + public void configureRegionFactoryBean() { + } + @Override public void verify() { Region region = regionFactoryBean.getRegion(); assertNotNull(region); assertEquals(DataPolicy.DEFAULT, region.getAttributes().getDataPolicy()); } - - @Override - public void configureRegionFactoryBean() { - } }; } - @SuppressWarnings("rawtypes") + @SuppressWarnings({ "deprecation", "rawtypes" }) private RegionFactoryBeanConfig invalidConfig() { return new RegionFactoryBeanConfig(new LocalRegionFactoryBean(), "local-replicate") { @Override - public void verify() { - assertNotNull(this.exception); + public void configureRegionFactoryBean() { + regionFactoryBean.setDataPolicy("replicate"); } @Override - public void configureRegionFactoryBean() { - regionFactoryBean.setDataPolicy("replicate"); + public void verify() { + assertNotNull(this.exception); } }; } @@ -88,6 +91,67 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { return mock(RegionFactory.class); } + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, (String) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL)); + } + + @Test + public void testResolveDataPolicyWhenNotPersistentAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, (String) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL)); + } + + @Test + public void testResolveDataPolicyWhenPersistentAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, (String) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWithBlankDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.resolveDataPolicy(mockRegionFactory, null, " "); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy ' ' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWithEmptyDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.resolveDataPolicy(mockRegionFactory, null, ""); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy '' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED)); + } + } + @Test(expected = IllegalArgumentException.class) public void testResolveDataPolicyWithInvalidDataPolicyName() { RegionFactory mockRegionFactory = createMockRegionFactory(); @@ -108,14 +172,14 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWithInvalidDataPolicyRegionType() { + public void testResolveDataPolicyWithInvalidDataPolicyType() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { factoryBean.resolveDataPolicy(mockRegionFactory, null, "PARTITION"); } catch (IllegalArgumentException e) { - assertEquals("Data Policy 'PARTITION' is not supported in Local Regions.", e.getMessage()); + assertEquals("Data Policy 'PARTITION' is not supported for Local Regions.", e.getMessage()); throw e; } finally { @@ -127,14 +191,7 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.resolveDataPolicy(mockRegionFactory, null, null); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL)); - } - - @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndNormalDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndNormalDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "Normal"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL)); @@ -157,7 +214,7 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndPreloadedDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPreloadedDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "preloaded"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PRELOADED)); @@ -179,4 +236,54 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenShortcutIsNullAndPersistentReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setShortcut(null); + factoryBean.resolveDataPolicy(mockRegionFactory, null, DataPolicy.PERSISTENT_REPLICATE); + } + catch (IllegalArgumentException expected) { + assertEquals("Data Policy 'PERSISTENT_REPLICATE' is not supported for Local Regions.", + expected.getMessage()); + throw expected; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(DataPolicy.NORMAL); + verify(mockRegionFactory, never()).setDataPolicy(DataPolicy.PRELOADED); + verify(mockRegionFactory, never()).setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenShortcutNotPersistentAndPersistentReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setShortcut(RegionShortcut.LOCAL_OVERFLOW); + factoryBean.resolveDataPolicy(mockRegionFactory, true, DataPolicy.PERSISTENT_REPLICATE); + } + catch (IllegalArgumentException expected) { + assertEquals("Data Policy 'PERSISTENT_REPLICATE' is not supported for Local Regions.", + expected.getMessage()); + throw expected; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(DataPolicy.NORMAL); + verify(mockRegionFactory, never()).setDataPolicy(DataPolicy.PRELOADED); + verify(mockRegionFactory, never()).setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); + } + } + + @Test + public void testResolveDataPolicyWhenPersistentPersistentReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setShortcut(RegionShortcut.LOCAL_PERSISTENT_OVERFLOW); + factoryBean.resolveDataPolicy(mockRegionFactory, false, DataPolicy.PERSISTENT_REPLICATE); + verify(mockRegionFactory).setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); + } + } diff --git a/src/test/java/org/springframework/data/gemfire/PartitionedRegionFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/PartitionedRegionFactoryBeanTest.java index f8238e85..f4fb5713 100644 --- a/src/test/java/org/springframework/data/gemfire/PartitionedRegionFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/PartitionedRegionFactoryBeanTest.java @@ -32,9 +32,11 @@ import com.gemstone.gemfire.cache.RegionFactory; * and correct behavior of the PartitionedRegionFactoryBean class. *

* @author John Blum - * @see org.mockito.Mockito * @see org.junit.Test + * @see org.mockito.Mockito * @see org.springframework.data.gemfire.PartitionedRegionFactoryBean + * @see com.gemstone.gemfire.cache.DataPolicy + * @see com.gemstone.gemfire.cache.RegionFactory * @since 1.3.3 */ @SuppressWarnings("unchecked") @@ -46,6 +48,67 @@ public class PartitionedRegionFactoryBeanTest { return mock(RegionFactory.class); } + @Test + public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, (String) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); + } + + @Test + public void testResolveDataPolicyWhenNotPersistentAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, (String) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); + } + + @Test + public void testResolveDataPolicyWhenPersistentAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, (String) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWithBlankDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.resolveDataPolicy(mockRegionFactory, null, " "); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy ' ' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWithEmptyDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.resolveDataPolicy(mockRegionFactory, null, ""); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy '' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED)); + } + } + @Test(expected = IllegalArgumentException.class) public void testResolveDataPolicyWithInvalidDataPolicyName() { RegionFactory mockRegionFactory = createMockRegionFactory(); @@ -59,11 +122,13 @@ public class PartitionedRegionFactoryBeanTest { } finally { verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); } } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWithInvalidDataPolicyRegionType() { + public void testResolveDataPolicyWithInvalidDataPolicyType() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { @@ -74,49 +139,22 @@ public class PartitionedRegionFactoryBeanTest { throw e; } finally { + verify(mockRegionFactory, never()).setDataPolicy(null); verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); } } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.resolveDataPolicy(mockRegionFactory, null, null); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); - } - - @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndPartitionedDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPartitionDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "PARTITION"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndPersistentPartitionedDataPolicy() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_PARTITION"); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); - } - - @Test - public void testResolveDataPolicyWithPersistentFalseAndDataPolicyUnspecified() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.setPersistent(false); - factoryBean.resolveDataPolicy(mockRegionFactory, false, null); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); - } - - @Test - public void testResolveDataPolicyWithPersistentTrueAndDataPolicyUnspecified() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.setPersistent(true); - factoryBean.resolveDataPolicy(mockRegionFactory, true, null); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); - } - - @Test - public void testResolveDataPolicyWithPersistentFalseAndPartitionedDataPolicy() { + public void testResolveDataPolicyWhenNotPersistentAndPartitionDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(false); factoryBean.resolveDataPolicy(mockRegionFactory, false, "PARTITION"); @@ -124,24 +162,7 @@ public class PartitionedRegionFactoryBeanTest { } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWithPersistentFalseAndPersistentPartitionedDataPolicy() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - - try { - factoryBean.setPersistent(false); - factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_PARTITION"); - } - catch (IllegalArgumentException e) { - assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", e.getMessage()); - throw e; - } - finally { - verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); - } - } - - @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWithPersistentTrueAndPartitionedDataPolicy() { + public void testResolveDataPolicyWhenPersistentAndPartitionDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { @@ -153,12 +174,40 @@ public class PartitionedRegionFactoryBeanTest { throw e; } finally { + verify(mockRegionFactory, never()).setDataPolicy(null); verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); } } @Test - public void testResolveDataPolicyWithPersistentTrueAndPersistentPartitionedDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPersistentPartitionDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_PARTITION"); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenNotPersistentAndPersistentPartitionedDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_PARTITION"); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); + } + } + + @Test + public void testResolveDataPolicyWhenPersistentAndPersistentPartitionedDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(true); factoryBean.resolveDataPolicy(mockRegionFactory, true, "PERSISTENT_PARTITION"); diff --git a/src/test/java/org/springframework/data/gemfire/RegionDataPolicyShortcutsIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/RegionDataPolicyShortcutsIntegrationTest.java new file mode 100644 index 00000000..88eee25c --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/RegionDataPolicyShortcutsIntegrationTest.java @@ -0,0 +1,183 @@ +/* + * Copyright 2010-2013 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 + * + * http://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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.gemstone.gemfire.cache.DataPolicy; +import com.gemstone.gemfire.cache.EvictionAction; +import com.gemstone.gemfire.cache.EvictionAlgorithm; +import com.gemstone.gemfire.cache.Region; + +/** + * The RegionShortcutsIntegrationTest class is a test suite of test cases testing the use of RegionShortcuts in the + * Spring Data GemFire XML Namespace! + *

+ * @author John Blum + * @see org.junit.Test + * @see org.junit.runner.RunWith + * @see org.springframework.test.context.ContextConfiguration + * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner + * @see com.gemstone.gemfire.cache.Region + * @since 1.4.0 + */ +@ContextConfiguration("region-datapolicy-shortcuts.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class RegionDataPolicyShortcutsIntegrationTest { + + @Resource(name = "LocalWithDataPolicy") + private Region localWithDataPolicy; + + @Resource(name = "LocalWithShortcut") + private Region localWithShortcut; + + @Resource(name = "PartitionWithDataPolicy") + private Region partitionWithDataPolicy; + + @Resource(name = "PartitionWithShortcut") + private Region partitionWithShortcut; + + @Resource(name = "ReplicateWithDataPolicy") + private Region replicateWithDataPolicy; + + @Resource(name = "ReplicateWithShortcut") + private Region replicateWithShortcut; + + @Resource(name = "ShortcutDefaults") + private Region shortcutDefaults; + + @Resource(name = "ShortcutOverrides") + private Region shortcutOverrides; + + @Test + public void testLocalRegionWithDataPolicy() { + assertNotNull("A reference to the 'LocalWithDataPolicy' Region was not property configured!", localWithDataPolicy); + assertEquals("LocalWithDataPolicy", localWithDataPolicy.getName()); + assertEquals("/LocalWithDataPolicy", localWithDataPolicy.getFullPath()); + assertNotNull(localWithDataPolicy.getAttributes()); + assertEquals(DataPolicy.NORMAL, localWithDataPolicy.getAttributes().getDataPolicy()); + } + + @Test + public void testLocalRegionWithShortcut() { + assertNotNull("A reference to the 'LocalWithShortcut' Region was not property configured!", localWithShortcut); + assertEquals("LocalWithShortcut", localWithShortcut.getName()); + assertEquals("/LocalWithShortcut", localWithShortcut.getFullPath()); + assertNotNull(localWithShortcut.getAttributes()); + assertEquals(DataPolicy.PERSISTENT_REPLICATE, localWithShortcut.getAttributes().getDataPolicy()); + } + + @Test + public void testPartitionRegionWithDataPolicy() { + assertNotNull("A reference to the 'PartitionWithDataPolicy' Region was not property configured!", partitionWithDataPolicy); + assertEquals("PartitionWithDataPolicy", partitionWithDataPolicy.getName()); + assertEquals("/PartitionWithDataPolicy", partitionWithDataPolicy.getFullPath()); + assertNotNull(partitionWithDataPolicy.getAttributes()); + assertEquals(DataPolicy.PARTITION, partitionWithDataPolicy.getAttributes().getDataPolicy()); + } + + @Test + public void testPartitionRegionWithShortcut() { + assertNotNull("A reference to the 'PartitionWithShortcut' Region was not property configured!", partitionWithShortcut); + assertEquals("PartitionWithShortcut", partitionWithShortcut.getName()); + assertEquals("/PartitionWithShortcut", partitionWithShortcut.getFullPath()); + assertNotNull(partitionWithShortcut.getAttributes()); + assertEquals(DataPolicy.PERSISTENT_PARTITION, partitionWithShortcut.getAttributes().getDataPolicy()); + } + + @Test + public void testReplicateRegionWithDataPolicy() { + assertNotNull("A reference to the 'ReplicateWithDataPolicy' Region was not property configured!", replicateWithDataPolicy); + assertEquals("ReplicateWithDataPolicy", replicateWithDataPolicy.getName()); + assertEquals("/ReplicateWithDataPolicy", replicateWithDataPolicy.getFullPath()); + assertNotNull(replicateWithDataPolicy.getAttributes()); + assertEquals(DataPolicy.REPLICATE, replicateWithDataPolicy.getAttributes().getDataPolicy()); + } + + @Test + public void testReplicateRegionWithShortcut() { + assertNotNull("A reference to the 'ReplicateWithShortcut' Region was not property configured!", replicateWithShortcut); + assertEquals("ReplicateWithShortcut", replicateWithShortcut.getName()); + assertEquals("/ReplicateWithShortcut", replicateWithShortcut.getFullPath()); + assertNotNull(replicateWithShortcut.getAttributes()); + assertEquals(DataPolicy.PERSISTENT_REPLICATE, replicateWithShortcut.getAttributes().getDataPolicy()); + } + + @Test + public void testShortcutDefaultsRegion() { + assertNotNull("A reference to the 'ShortcutDefaults' Region was not properly configured!", shortcutDefaults); + assertEquals("ShortcutDefaults", shortcutDefaults.getName()); + assertEquals("/ShortcutDefaults", shortcutDefaults.getFullPath()); + + assertNotNull(shortcutDefaults.getAttributes()); + assertFalse(shortcutDefaults.getAttributes().getCloningEnabled()); + assertTrue(shortcutDefaults.getAttributes().getConcurrencyChecksEnabled()); + assertEquals(DataPolicy.PERSISTENT_PARTITION, shortcutDefaults.getAttributes().getDataPolicy()); + assertFalse(shortcutDefaults.getAttributes().isDiskSynchronous()); + assertTrue(shortcutDefaults.getAttributes().getIgnoreJTA()); + assertEquals(101, shortcutDefaults.getAttributes().getInitialCapacity()); + assertEquals(new Float(0.85f), new Float(shortcutDefaults.getAttributes().getLoadFactor())); + assertEquals(Long.class, shortcutDefaults.getAttributes().getKeyConstraint()); + assertEquals(String.class, shortcutDefaults.getAttributes().getValueConstraint()); + + assertNotNull(shortcutDefaults.getAttributes().getEvictionAttributes()); + assertEquals(EvictionAction.OVERFLOW_TO_DISK, shortcutDefaults.getAttributes().getEvictionAttributes().getAction()); + assertEquals(EvictionAlgorithm.LRU_HEAP, shortcutDefaults.getAttributes().getEvictionAttributes().getAlgorithm()); + + assertNotNull(shortcutDefaults.getAttributes().getPartitionAttributes()); + assertEquals(1, shortcutDefaults.getAttributes().getPartitionAttributes().getRedundantCopies()); + assertEquals(177, shortcutDefaults.getAttributes().getPartitionAttributes().getTotalNumBuckets()); + } + + @Test + public void testShortcutOverridesRegion() { + assertNotNull("A reference to the 'ShortcutOverrides' Region was not properly configured!", shortcutOverrides); + assertEquals("ShortcutOverrides", shortcutOverrides.getName()); + assertEquals("/ShortcutOverrides", shortcutOverrides.getFullPath()); + + assertNotNull(shortcutOverrides.getAttributes()); + assertTrue(shortcutOverrides.getAttributes().getCloningEnabled()); + assertFalse(shortcutOverrides.getAttributes().getConcurrencyChecksEnabled()); + assertEquals(DataPolicy.PARTITION, shortcutOverrides.getAttributes().getDataPolicy()); + assertTrue(shortcutOverrides.getAttributes().isDiskSynchronous()); + assertFalse(shortcutOverrides.getAttributes().getIgnoreJTA()); + assertEquals(51, shortcutOverrides.getAttributes().getInitialCapacity()); + assertEquals(new Float(0.72f), new Float(shortcutOverrides.getAttributes().getLoadFactor())); + assertEquals(String.class, shortcutOverrides.getAttributes().getKeyConstraint()); + assertEquals(Object.class, shortcutOverrides.getAttributes().getValueConstraint()); + + assertNotNull(shortcutOverrides.getAttributes().getEvictionAttributes()); + assertEquals(EvictionAction.LOCAL_DESTROY, shortcutOverrides.getAttributes().getEvictionAttributes().getAction()); + assertEquals(8192, shortcutOverrides.getAttributes().getEvictionAttributes().getMaximum()); + assertEquals(EvictionAlgorithm.LRU_ENTRY, shortcutOverrides.getAttributes().getEvictionAttributes().getAlgorithm()); + + assertNotNull(shortcutOverrides.getAttributes().getPartitionAttributes()); + assertEquals(3, shortcutOverrides.getAttributes().getPartitionAttributes().getRedundantCopies()); + assertEquals(111, shortcutOverrides.getAttributes().getPartitionAttributes().getTotalNumBuckets()); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/RegionFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/RegionFactoryBeanTest.java index 0c12941d..dfa0893f 100644 --- a/src/test/java/org/springframework/data/gemfire/RegionFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/RegionFactoryBeanTest.java @@ -16,28 +16,68 @@ package org.springframework.data.gemfire; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.same; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.After; import org.junit.Test; import org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest; +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.CustomExpiry; import com.gemstone.gemfire.cache.DataPolicy; +import com.gemstone.gemfire.cache.EvictionAttributes; +import com.gemstone.gemfire.cache.ExpirationAction; +import com.gemstone.gemfire.cache.ExpirationAttributes; +import com.gemstone.gemfire.cache.MembershipAttributes; +import com.gemstone.gemfire.cache.PartitionAttributes; import com.gemstone.gemfire.cache.Region; +import com.gemstone.gemfire.cache.RegionAttributes; import com.gemstone.gemfire.cache.RegionFactory; +import com.gemstone.gemfire.cache.RegionShortcut; +import com.gemstone.gemfire.cache.SubscriptionAttributes; +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; /** + * The RegionFactoryBeanTest class is a test suite of test cases testing the contract and functionality of the + * RegionFactoryBean class. + *

* @author David Turanski * @author John Blum + * @see org.springframework.data.gemfire.RegionFactoryBean + * @see com.gemstone.gemfire.cache.Cache + * @see com.gemstone.gemfire.cache.DataPolicy + * @see com.gemstone.gemfire.cache.PartitionAttributes + * @see com.gemstone.gemfire.cache.Region + * @see com.gemstone.gemfire.cache.RegionAttributes + * @see com.gemstone.gemfire.cache.RegionFactory + * @see com.gemstone.gemfire.cache.RegionShortcut */ @SuppressWarnings("unchecked") public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { private final RegionFactoryBean factoryBean = new RegionFactoryBean(); + @After + public void tearDown() { + factoryBean.setDataPolicy((DataPolicy) null); + factoryBean.setShortcut(null); + } + @SuppressWarnings("rawtypes") private RegionFactoryBeanConfig defaultConfig() { return new RegionFactoryBeanConfig(new RegionFactoryBean(), "default") { @@ -50,14 +90,13 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { @Override public void configureRegionFactoryBean() { - // TODO Auto-generated method stub } }; } @SuppressWarnings("rawtypes") - private RegionFactoryBeanConfig persistent() { + private RegionFactoryBeanConfig persistentConfig() { return new RegionFactoryBeanConfig(new RegionFactoryBean(), "persistent") { @Override @@ -73,8 +112,8 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { }; } - @SuppressWarnings("rawtypes") - private RegionFactoryBeanConfig invalidPersistence() { + @SuppressWarnings({ "deprecation", "rawtypes" }) + private RegionFactoryBeanConfig invalidPersistentConfig() { return new RegionFactoryBeanConfig(new RegionFactoryBean(), "invalid-persistence") { @Override @@ -95,18 +134,561 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { @Override protected void createRegionFactoryBeanConfigs() { addRFBConfig(defaultConfig()); - addRFBConfig(persistent()); - addRFBConfig(invalidPersistence()); + addRFBConfig(persistentConfig()); + addRFBConfig(invalidPersistentConfig()); + } + + protected PartitionAttributes createPartitionAttributes(final String colocatedWith, final int localMaxMemory, + final long recoveryDelay, final int redundantCopies, final long startupRecoveryDelay, + final long totalMaxMemory, final int totalNumberOfBuckets) throws Exception { + PartitionAttributesFactoryBean partitionAttributesFactoryBean = new PartitionAttributesFactoryBean(); + + partitionAttributesFactoryBean.setColocatedWith(colocatedWith); + partitionAttributesFactoryBean.setLocalMaxMemory(localMaxMemory); + partitionAttributesFactoryBean.setRecoveryDelay(recoveryDelay); + partitionAttributesFactoryBean.setRedundantCopies(redundantCopies); + partitionAttributesFactoryBean.setStartupRecoveryDelay(startupRecoveryDelay); + partitionAttributesFactoryBean.setTotalMaxMemory(totalMaxMemory); + partitionAttributesFactoryBean.setTotalNumBuckets(totalNumberOfBuckets); + partitionAttributesFactoryBean.afterPropertiesSet(); + + return partitionAttributesFactoryBean.getObject(); } protected RegionFactory createMockRegionFactory() { return mock(RegionFactory.class); } + protected RegionFactory createTestRegionFactory() { + return new TestRegionFactory(); + } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() { + public void testAssertDataPolicyAndPersistentAttributesAreCompatible() { + RegionFactoryBean factoryBean = new RegionFactoryBean(); + + factoryBean.setPersistent(null); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PARTITION); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.REPLICATE); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PERSISTENT_PARTITION); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PERSISTENT_REPLICATE); + factoryBean.setPersistent(false); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PARTITION); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.REPLICATE); + factoryBean.setPersistent(true); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PERSISTENT_PARTITION); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PERSISTENT_REPLICATE); + } + + @Test(expected = IllegalArgumentException.class) + public void testAssertNonPersistentDataPolicyWithPersistentAttribute() { + try { + RegionFactoryBean factoryBean = new RegionFactoryBean(); + factoryBean.setPersistent(true); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.REPLICATE); + } + catch (IllegalArgumentException expected) { + assertEquals("Data Policy 'REPLICATE' is invalid when persistent is true.", expected.getMessage()); + throw expected; + } + } + + @Test(expected = IllegalArgumentException.class) + public void testAssertPersistentDataPolicyWithNotPersistentAttribute() { + try { + RegionFactoryBean factoryBean = new RegionFactoryBean(); + factoryBean.setPersistent(false); + factoryBean.assertDataPolicyAndPersistentAttributesAreCompatible(DataPolicy.PERSISTENT_PARTITION); + } + catch (IllegalArgumentException expected) { + assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", + expected.getMessage()); + throw expected; + } + } + + @Test + public void testIsPersistentUnspecified() { + RegionFactoryBean factoryBean = new RegionFactoryBean(); + + assertTrue(factoryBean.isPersistentUnspecified()); + + factoryBean.setPersistent(false); + + assertFalse(factoryBean.isPersistentUnspecified()); + + factoryBean.setPersistent(true); + + assertFalse(factoryBean.isPersistentUnspecified()); + } + + @Test + public void testIsPersistent() { + RegionFactoryBean factoryBean = new RegionFactoryBean(); + + assertFalse(factoryBean.isPersistent()); + + factoryBean.setPersistent(false); + + assertFalse(factoryBean.isPersistent()); + + factoryBean.setPersistent(true); + + assertTrue(factoryBean.isPersistent()); + } + + @Test + public void testIsNotPersistent() { + RegionFactoryBean factoryBean = new RegionFactoryBean(); + + assertFalse(factoryBean.isNotPersistent()); + + factoryBean.setPersistent(true); + + assertFalse(factoryBean.isNotPersistent()); + + factoryBean.setPersistent(false); + + assertTrue(factoryBean.isNotPersistent()); + } + + @Test + public void testCreateRegionFactoryWithShortcut() { + Cache mockCache = mock(Cache.class); + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + + final RegionFactory mockRegionFactory = createMockRegionFactory(); + + when(mockCache.createRegionFactory(eq(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW))) + .thenReturn(mockRegionFactory); + + final AtomicBoolean setDataPolicyCalled = new AtomicBoolean(false); + + RegionFactoryBean factoryBean = new RegionFactoryBean() { + @Override + DataPolicy getDataPolicy(final RegionFactory regionFactory) { + return DataPolicy.PERSISTENT_PARTITION; + } + + @Override + public void setDataPolicy(final DataPolicy dataPolicy) { + assertEquals(DataPolicy.PERSISTENT_PARTITION, dataPolicy); + super.setDataPolicy(dataPolicy); + setDataPolicyCalled.set(true); + } + + @Override + protected RegionFactory mergeRegionAttributes(RegionFactory regionFactory, RegionAttributes regionAttributes) { + return mockRegionFactory; + } + }; + + factoryBean.setAttributes(mockRegionAttributes); + factoryBean.setShortcut(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW); + + assertSame(mockRegionFactory, factoryBean.createRegionFactory(mockCache)); + assertTrue(setDataPolicyCalled.get()); + + verify(mockCache).createRegionFactory(eq(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW)); + } + + @Test + public void testCreateRegionFactoryWithAttributes() { + Cache mockCache = mock(Cache.class); + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + + final RegionFactory mockRegionFactory = createMockRegionFactory(); + + when(mockCache.createRegionFactory(eq(mockRegionAttributes))).thenReturn(mockRegionFactory); + + RegionFactoryBean factoryBean = new RegionFactoryBean() { + @Override + protected RegionFactory mergeRegionAttributes(RegionFactory regionFactory, RegionAttributes regionAttributes) { + return mockRegionFactory; + } + }; + + factoryBean.setAttributes(mockRegionAttributes); + factoryBean.setShortcut(null); + + assertSame(mockRegionFactory, factoryBean.createRegionFactory(mockCache)); + + verify(mockCache).createRegionFactory(eq(mockRegionAttributes)); + } + + @Test + public void testCreateRegionFactory() { + Cache mockCache = mock(Cache.class); + + final RegionFactory mockRegionFactory = createMockRegionFactory(); + + when(mockCache.createRegionFactory()).thenReturn(mockRegionFactory); + + RegionFactoryBean factoryBean = new RegionFactoryBean() { + @Override + protected RegionFactory mergeRegionAttributes(RegionFactory regionFactory, RegionAttributes regionAttributes) { + return mockRegionFactory; + } + }; + + factoryBean.setAttributes(null); + factoryBean.setShortcut(null); + + assertSame(mockRegionFactory, factoryBean.createRegionFactory(mockCache)); + + verify(mockCache).createRegionFactory(); + } + + @Test + public void testMergeRegionAttributes() throws Exception { + EvictionAttributes testEvictionAttributes = EvictionAttributes.createLRUEntryAttributes(); + ExpirationAttributes testExpirationAttributes = new ExpirationAttributes(120, ExpirationAction.LOCAL_DESTROY); + MembershipAttributes testMembershipAttributes = new MembershipAttributes(); + PartitionAttributes testPartitionAttributes = createPartitionAttributes("TestRegion", 1024000, 15000l, 0, + 45000l, 2048000000l, 97); + SubscriptionAttributes testSubscriptionAttributes = new SubscriptionAttributes(); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = (RegionFactory) createMockRegionFactory(); + + when(mockRegionAttributes.getCloningEnabled()).thenReturn(false); + when(mockRegionAttributes.getConcurrencyChecksEnabled()).thenReturn(true); + when(mockRegionAttributes.getConcurrencyLevel()).thenReturn(51); + when(mockRegionAttributes.getCustomEntryIdleTimeout()).thenReturn(null); + when(mockRegionAttributes.getCustomEntryTimeToLive()).thenReturn(null); + when(mockRegionAttributes.isDiskSynchronous()).thenReturn(true); + when(mockRegionAttributes.getEnableAsyncConflation()).thenReturn(false); + when(mockRegionAttributes.getEnableSubscriptionConflation()).thenReturn(false); + when(mockRegionAttributes.getEntryIdleTimeout()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getEntryTimeToLive()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getEvictionAttributes()).thenReturn(testEvictionAttributes); + when(mockRegionAttributes.getIgnoreJTA()).thenReturn(false); + when(mockRegionAttributes.getIndexMaintenanceSynchronous()).thenReturn(false); + when(mockRegionAttributes.getInitialCapacity()).thenReturn(1024); + when(mockRegionAttributes.getKeyConstraint()).thenReturn(Long.class); + when(mockRegionAttributes.getLoadFactor()).thenReturn(0.90f); + when(mockRegionAttributes.isLockGrantor()).thenReturn(true); + when(mockRegionAttributes.getMembershipAttributes()).thenReturn(testMembershipAttributes); + when(mockRegionAttributes.getMulticastEnabled()).thenReturn(false); + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(testPartitionAttributes); + when(mockRegionAttributes.getPoolName()).thenReturn("swimming"); + when(mockRegionAttributes.getRegionIdleTimeout()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getRegionTimeToLive()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getStatisticsEnabled()).thenReturn(true); + when(mockRegionAttributes.getSubscriptionAttributes()).thenReturn(testSubscriptionAttributes); + + RegionFactoryBean factoryBean = new RegionFactoryBean() { + @Override boolean hasUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) { + return true; + } + + @Override void validateRegionAttributes(final RegionAttributes regionAttributes) { + // no-op! + } + }; + + factoryBean.mergeRegionAttributes(mockRegionFactory, mockRegionAttributes); + + verify(mockRegionFactory).setCloningEnabled(eq(false)); + verify(mockRegionFactory).setConcurrencyChecksEnabled(eq(true)); + verify(mockRegionFactory).setConcurrencyLevel(eq(51)); + verify(mockRegionFactory).setCustomEntryIdleTimeout(null); + verify(mockRegionFactory).setCustomEntryTimeToLive(null); + verify(mockRegionFactory).setDiskSynchronous(eq(true)); + verify(mockRegionFactory).setEnableAsyncConflation(eq(false)); + verify(mockRegionFactory).setEnableSubscriptionConflation(eq(false)); + verify(mockRegionFactory).setEntryIdleTimeout(same(testExpirationAttributes)); + verify(mockRegionFactory).setEntryTimeToLive(same(testExpirationAttributes)); + verify(mockRegionFactory).setEvictionAttributes(same(testEvictionAttributes)); + verify(mockRegionFactory).setIgnoreJTA(eq(false)); + verify(mockRegionFactory).setIndexMaintenanceSynchronous(eq(false)); + verify(mockRegionFactory).setInitialCapacity(eq(1024)); + verify(mockRegionFactory).setKeyConstraint(Long.class); + verify(mockRegionFactory).setLoadFactor(eq(0.90f)); + verify(mockRegionFactory).setLockGrantor(eq(true)); + verify(mockRegionFactory).setMembershipAttributes(same(testMembershipAttributes)); + verify(mockRegionFactory).setMulticastEnabled(eq(false)); + verify(mockRegionFactory).setPartitionAttributes(eq(testPartitionAttributes)); + verify(mockRegionFactory).setPoolName(eq("swimming")); + verify(mockRegionFactory).setRegionIdleTimeout(same(testExpirationAttributes)); + verify(mockRegionFactory).setRegionTimeToLive(same(testExpirationAttributes)); + verify(mockRegionFactory).setStatisticsEnabled(eq(true)); + verify(mockRegionFactory).setSubscriptionAttributes(same(testSubscriptionAttributes)); + } + + @Test + public void testMergeRegionAttributesWithNull() { RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.resolveDataPolicy(mockRegionFactory, null, null); + + factoryBean.mergeRegionAttributes(mockRegionFactory, null); + + verify(mockRegionFactory, never()).setCloningEnabled(false); + verify(mockRegionFactory, never()).setConcurrencyChecksEnabled(true); + verify(mockRegionFactory, never()).setConcurrencyLevel(2); + verify(mockRegionFactory, never()).setCustomEntryIdleTimeout(any(CustomExpiry.class)); + verify(mockRegionFactory, never()).setCustomEntryTimeToLive(any(CustomExpiry.class)); + verify(mockRegionFactory, never()).setDiskSynchronous(true); + verify(mockRegionFactory, never()).setEnableAsyncConflation(true); + verify(mockRegionFactory, never()).setEnableSubscriptionConflation(false); + verify(mockRegionFactory, never()).setEntryIdleTimeout(any(ExpirationAttributes.class)); + verify(mockRegionFactory, never()).setEntryTimeToLive(any(ExpirationAttributes.class)); + verify(mockRegionFactory, never()).setEvictionAttributes(any(EvictionAttributes.class)); + verify(mockRegionFactory, never()).setIgnoreJTA(false); + verify(mockRegionFactory, never()).setIndexMaintenanceSynchronous(false); + verify(mockRegionFactory, never()).setInitialCapacity(51); + verify(mockRegionFactory, never()).setKeyConstraint(any(Class.class)); + verify(mockRegionFactory, never()).setLoadFactor(0.75f); + verify(mockRegionFactory, never()).setLockGrantor(true); + verify(mockRegionFactory, never()).setMembershipAttributes(any(MembershipAttributes.class)); + verify(mockRegionFactory, never()).setMulticastEnabled(false); + verify(mockRegionFactory, never()).setPartitionAttributes(any(PartitionAttributes.class)); + verify(mockRegionFactory, never()).setPoolName(any(String.class)); + verify(mockRegionFactory, never()).setRegionIdleTimeout(any(ExpirationAttributes.class)); + verify(mockRegionFactory, never()).setRegionTimeToLive(any(ExpirationAttributes.class)); + verify(mockRegionFactory, never()).setStatisticsEnabled(true); + verify(mockRegionFactory, never()).setSubscriptionAttributes(any(SubscriptionAttributes.class)); + verify(mockRegionFactory, never()).setValueConstraint(any(Class.class)); + } + + @Test + public void testPartialMergeRegionAttributes() { + ExpirationAttributes testExpirationAttributes = new ExpirationAttributes(300, ExpirationAction.LOCAL_INVALIDATE); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = (RegionFactory) createMockRegionFactory(); + + when(mockRegionAttributes.getCloningEnabled()).thenReturn(true); + when(mockRegionAttributes.getConcurrencyChecksEnabled()).thenReturn(false); + when(mockRegionAttributes.getConcurrencyLevel()).thenReturn(8); + when(mockRegionAttributes.getCustomEntryIdleTimeout()).thenReturn(null); + when(mockRegionAttributes.getCustomEntryTimeToLive()).thenReturn(null); + when(mockRegionAttributes.isDiskSynchronous()).thenReturn(false); + when(mockRegionAttributes.getEnableAsyncConflation()).thenReturn(true); + when(mockRegionAttributes.getEnableSubscriptionConflation()).thenReturn(true); + when(mockRegionAttributes.getEntryIdleTimeout()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getEntryTimeToLive()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getEvictionAttributes()).thenReturn(null); + when(mockRegionAttributes.getIgnoreJTA()).thenReturn(true); + when(mockRegionAttributes.getIndexMaintenanceSynchronous()).thenReturn(true); + when(mockRegionAttributes.getInitialCapacity()).thenReturn(512); + when(mockRegionAttributes.getKeyConstraint()).thenReturn(Long.class); + when(mockRegionAttributes.getLoadFactor()).thenReturn(0.60f); + when(mockRegionAttributes.isLockGrantor()).thenReturn(false); + when(mockRegionAttributes.getMembershipAttributes()).thenReturn(null); + when(mockRegionAttributes.getMulticastEnabled()).thenReturn(true); + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(null); + when(mockRegionAttributes.getPoolName()).thenReturn("swimming"); + when(mockRegionAttributes.getRegionIdleTimeout()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getRegionTimeToLive()).thenReturn(testExpirationAttributes); + when(mockRegionAttributes.getStatisticsEnabled()).thenReturn(true); + when(mockRegionAttributes.getSubscriptionAttributes()).thenReturn(null); + + RegionFactoryBean factoryBean = new RegionFactoryBean() { + @Override boolean hasUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) { + return false; + } + + @Override void validateRegionAttributes(final RegionAttributes regionAttributes) { + // no-op! + } + }; + + factoryBean.mergeRegionAttributes(mockRegionFactory, mockRegionAttributes); + + verify(mockRegionFactory).setCloningEnabled(eq(true)); + verify(mockRegionFactory).setConcurrencyChecksEnabled(eq(false)); + verify(mockRegionFactory).setConcurrencyLevel(eq(8)); + verify(mockRegionFactory).setCustomEntryIdleTimeout(null); + verify(mockRegionFactory).setCustomEntryTimeToLive(null); + verify(mockRegionFactory).setDiskSynchronous(eq(false)); + verify(mockRegionFactory).setEnableAsyncConflation(eq(true)); + verify(mockRegionFactory).setEnableSubscriptionConflation(eq(true)); + verify(mockRegionFactory).setEntryIdleTimeout(same(testExpirationAttributes)); + verify(mockRegionFactory).setEntryTimeToLive(same(testExpirationAttributes)); + verify(mockRegionFactory, never()).setEvictionAttributes(any(EvictionAttributes.class)); + verify(mockRegionFactory).setIgnoreJTA(eq(true)); + verify(mockRegionFactory).setIndexMaintenanceSynchronous(eq(true)); + verify(mockRegionFactory).setInitialCapacity(eq(512)); + verify(mockRegionFactory).setKeyConstraint(Long.class); + verify(mockRegionFactory).setLoadFactor(eq(0.60f)); + verify(mockRegionFactory).setLockGrantor(eq(false)); + verify(mockRegionFactory).setMembershipAttributes(null); + verify(mockRegionFactory).setMulticastEnabled(eq(true)); + verify(mockRegionFactory, never()).setPartitionAttributes(any(PartitionAttributes.class)); + verify(mockRegionFactory).setPoolName(eq("swimming")); + verify(mockRegionFactory).setRegionIdleTimeout(same(testExpirationAttributes)); + verify(mockRegionFactory).setRegionTimeToLive(same(testExpirationAttributes)); + verify(mockRegionFactory).setStatisticsEnabled(eq(true)); + verify(mockRegionFactory).setSubscriptionAttributes(null); + } + + @Test + public void testMergePartitionAttributesWithPartitionRedundantProxy() throws Exception { + PartitionAttributes testPartitionAttributes = createPartitionAttributes("TestRegion", 512000, 15000l, 0, + 30000l, 1024000l, 51); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = createTestRegionFactory(); + + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(testPartitionAttributes); + + factoryBean.setShortcut(RegionShortcut.PARTITION_PROXY_REDUNDANT); + factoryBean.mergePartitionAttributes(mockRegionFactory, mockRegionAttributes); + + RegionAttributes regionAttributes = TestUtils.readField("regionAttributes", + TestUtils.readField("attrsFactory", mockRegionFactory)); + PartitionAttributes actualPartitionAttributes = regionAttributes.getPartitionAttributes(); + + assertNotNull(actualPartitionAttributes); + assertNotSame(testPartitionAttributes, actualPartitionAttributes); + assertEquals("TestRegion", actualPartitionAttributes.getColocatedWith()); + assertEquals(0, actualPartitionAttributes.getLocalMaxMemory()); + assertEquals(15000l, actualPartitionAttributes.getRecoveryDelay()); + assertEquals(1, actualPartitionAttributes.getRedundantCopies()); + assertEquals(30000l, actualPartitionAttributes.getStartupRecoveryDelay()); + assertEquals(1024000l, actualPartitionAttributes.getTotalMaxMemory()); + assertEquals(51, actualPartitionAttributes.getTotalNumBuckets()); + + verify(mockRegionAttributes, times(2)).getPartitionAttributes(); + } + + @Test + public void testMergePartitionAttributesWithPartitionRedundant() throws Exception { + PartitionAttributes testPartitionAttributes = createPartitionAttributes("TestRegion", 512000, 15000l, 0, + 30000l, 1024000l, 51); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = createTestRegionFactory(); + + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(testPartitionAttributes); + + factoryBean.setShortcut(RegionShortcut.PARTITION_REDUNDANT); + factoryBean.mergePartitionAttributes(mockRegionFactory, mockRegionAttributes); + + RegionAttributes regionAttributes = TestUtils.readField("regionAttributes", + TestUtils.readField("attrsFactory", mockRegionFactory)); + PartitionAttributes actualPartitionAttributes = regionAttributes.getPartitionAttributes(); + + assertNotNull(actualPartitionAttributes); + assertNotSame(testPartitionAttributes, actualPartitionAttributes); + assertEquals("TestRegion", actualPartitionAttributes.getColocatedWith()); + assertEquals(512000, actualPartitionAttributes.getLocalMaxMemory()); + assertEquals(15000l, actualPartitionAttributes.getRecoveryDelay()); + assertEquals(1, actualPartitionAttributes.getRedundantCopies()); + assertEquals(30000l, actualPartitionAttributes.getStartupRecoveryDelay()); + assertEquals(1024000l, actualPartitionAttributes.getTotalMaxMemory()); + assertEquals(51, actualPartitionAttributes.getTotalNumBuckets()); + + verify(mockRegionAttributes, times(2)).getPartitionAttributes(); + } + + @Test + public void testMergePartitionAttributesWithPartitionRedundantPersistentOverflow() throws Exception { + PartitionAttributes testPartitionAttributes = createPartitionAttributes("TestRegion", 512000, 15000l, 3, + 30000l, 1024000l, 51); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = createTestRegionFactory(); + + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(testPartitionAttributes); + + factoryBean.setShortcut(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW); + factoryBean.mergePartitionAttributes(mockRegionFactory, mockRegionAttributes); + + RegionAttributes regionAttributes = TestUtils.readField("regionAttributes", + TestUtils.readField("attrsFactory", mockRegionFactory)); + PartitionAttributes actualPartitionAttributes = regionAttributes.getPartitionAttributes(); + + assertNotNull(actualPartitionAttributes); + assertNotSame(testPartitionAttributes, actualPartitionAttributes); + assertEquals("TestRegion", actualPartitionAttributes.getColocatedWith()); + assertEquals(512000, actualPartitionAttributes.getLocalMaxMemory()); + assertEquals(15000l, actualPartitionAttributes.getRecoveryDelay()); + assertEquals(3, actualPartitionAttributes.getRedundantCopies()); + assertEquals(30000l, actualPartitionAttributes.getStartupRecoveryDelay()); + assertEquals(1024000l, actualPartitionAttributes.getTotalMaxMemory()); + assertEquals(51, actualPartitionAttributes.getTotalNumBuckets()); + + verify(mockRegionAttributes, times(2)).getPartitionAttributes(); + } + + @Test + public void testMergePartitionAttributesWithPartitionProxy() throws Exception { + PartitionAttributes testPartitionAttributes = createPartitionAttributes("TestRegion", 512000, 15000l, 0, + 30000l, 1024000l, 51); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = createTestRegionFactory(); + + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(testPartitionAttributes); + + factoryBean.setShortcut(RegionShortcut.PARTITION_PROXY); + factoryBean.mergePartitionAttributes(mockRegionFactory, mockRegionAttributes); + + RegionAttributes regionAttributes = TestUtils.readField("regionAttributes", + TestUtils.readField("attrsFactory", mockRegionFactory)); + PartitionAttributes actualPartitionAttributes = regionAttributes.getPartitionAttributes(); + + assertNotNull(actualPartitionAttributes); + assertNotSame(testPartitionAttributes, actualPartitionAttributes); + assertEquals("TestRegion", actualPartitionAttributes.getColocatedWith()); + assertEquals(0, actualPartitionAttributes.getLocalMaxMemory()); + assertEquals(15000l, actualPartitionAttributes.getRecoveryDelay()); + assertEquals(0, actualPartitionAttributes.getRedundantCopies()); + assertEquals(30000l, actualPartitionAttributes.getStartupRecoveryDelay()); + assertEquals(1024000l, actualPartitionAttributes.getTotalMaxMemory()); + assertEquals(51, actualPartitionAttributes.getTotalNumBuckets()); + + verify(mockRegionAttributes, times(2)).getPartitionAttributes(); + } + + @Test + public void testMergePartitionAttributesWithPartition() throws Exception { + PartitionAttributes testPartitionAttributes = createPartitionAttributes("TestRegion", 512000, 15000l, 0, + 30000l, 1024000l, 51); + + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = createTestRegionFactory(); + + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(testPartitionAttributes); + + factoryBean.setShortcut(RegionShortcut.PARTITION); + factoryBean.mergePartitionAttributes(mockRegionFactory, mockRegionAttributes); + + RegionAttributes regionAttributes = TestUtils.readField("regionAttributes", + TestUtils.readField("attrsFactory", mockRegionFactory)); + PartitionAttributes actualPartitionAttributes = regionAttributes.getPartitionAttributes(); + + assertNotNull(actualPartitionAttributes); + assertNotSame(testPartitionAttributes, actualPartitionAttributes); + assertEquals("TestRegion", actualPartitionAttributes.getColocatedWith()); + assertEquals(512000, actualPartitionAttributes.getLocalMaxMemory()); + assertEquals(15000l, actualPartitionAttributes.getRecoveryDelay()); + assertEquals(0, actualPartitionAttributes.getRedundantCopies()); + assertEquals(30000l, actualPartitionAttributes.getStartupRecoveryDelay()); + assertEquals(1024000l, actualPartitionAttributes.getTotalMaxMemory()); + assertEquals(51, actualPartitionAttributes.getTotalNumBuckets()); + + verify(mockRegionAttributes, times(2)).getPartitionAttributes(); + } + + @Test + public void testMergePartitionAttributes() { + RegionAttributes mockRegionAttributes = mock(RegionAttributes.class); + RegionFactory mockRegionFactory = createMockRegionFactory(); + + when(mockRegionAttributes.getPartitionAttributes()).thenReturn(null); + + factoryBean.setShortcut(null); + factoryBean.mergePartitionAttributes(mockRegionFactory, mockRegionAttributes); + + verify(mockRegionAttributes, times(1)).getPartitionAttributes(); + verify(mockRegionFactory, never()).setPartitionAttributes(any(PartitionAttributes.class)); + } + + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndDataPolicyUnspecified() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, (String) null); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT)); } @@ -114,7 +696,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { public void testResolveDataPolicyWhenNotPersistentAndDataPolicyUnspecified() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(false); - factoryBean.resolveDataPolicy(mockRegionFactory, false, null); + factoryBean.resolveDataPolicy(mockRegionFactory, false, (String) null); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT)); } @@ -122,13 +704,52 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { public void testResolveDataPolicyWhenPersistentAndDataPolicyUnspecified() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(true); - factoryBean.resolveDataPolicy(mockRegionFactory, true, null); + factoryBean.resolveDataPolicy(mockRegionFactory, true, (String) null); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWithBlankDataPolicyName() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, " "); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy ' ' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWithEmptyDataPolicyName() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, ""); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy '' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + } + @Test(expected = IllegalArgumentException.class) public void testResolveDataPolicyWithInvalidDataPolicyName() { RegionFactory mockRegionFactory = createMockRegionFactory(); + try { factoryBean.setPersistent(true); factoryBean.resolveDataPolicy(mockRegionFactory, true, "CSV"); @@ -145,14 +766,14 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndNormalDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndNormalDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "NORMAL"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL)); } @Test - public void testResolveDataPolicyWhenNotPersistentUnspecifiedAndPreloadedDataPolicy() { + public void testResolveDataPolicyWhenNotPersistentAndPreloadedDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(false); factoryBean.resolveDataPolicy(mockRegionFactory, false, "PRELOADED"); @@ -160,8 +781,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWhenPersistentAndPersistentEmptyDataPolicy() { + public void testResolveDataPolicyWhenPersistentAndEmptyDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); + try { factoryBean.setPersistent(true); factoryBean.resolveDataPolicy(mockRegionFactory, true, "EMPTY"); @@ -179,18 +801,27 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndPersistentPartitionedDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPartitionDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, "PARTITION"); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); + } + + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPersistentPartitionDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_PARTITION"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWhenNotPersistentAndPersistentPartitionedDataPolicy() { + public void testResolveDataPolicyWhenNotPersistentAndPersistentPartitionDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); + try { factoryBean.setPersistent(false); factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_PARTITION"); + fail("Setting the 'persistent' attribute to FALSE and 'Data Policy' to PERSISTENT_PARTITION should have thrown an IllegalArgumentException!"); } catch (IllegalArgumentException e) { assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", e.getMessage()); @@ -204,12 +835,145 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest { } } + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenPersistentAndPartitionDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, "PARTITION"); + fail("Setting the 'persistent' attribute to TRUE and 'Data Policy' to PARTITION should have thrown an IllegalArgumentException!"); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy 'PARTITION' is invalid when persistent is true.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + } + @Test - public void testResolveDataPolicyWhenPersistentAndPersistentPartitionedDataPolicy() { + public void testResolveDataPolicyWhenNotPersistentAndPartitionDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, "PARTITION"); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION)); + } + + @Test + public void testResolveDataPolicyWhenPersistentAndPersistentPartitionDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(true); factoryBean.resolveDataPolicy(mockRegionFactory, true, "PERSISTENT_PARTITION"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION)); } + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndUnspecifiedDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(null); + factoryBean.resolveDataPolicy(mockRegionFactory, null, (DataPolicy) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT)); + } + + @Test + public void testResolveDataPolicyWhenNotPersistentAndUnspecifiedDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, (DataPolicy) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT)); + } + + @Test + public void testResolveDataPolicyWhenPersistentAndUnspecifiedDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, (DataPolicy) null); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, DataPolicy.REPLICATE); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE)); + } + + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPersistentReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, DataPolicy.PERSISTENT_REPLICATE); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenNotPersistentAndPersistentReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, DataPolicy.PERSISTENT_REPLICATE); + fail("Setting the 'persistent' attribute to FALSE and 'Data Policy' to PERSISTENT_REPLICATE should have thrown an IllegalArgumentException!"); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy 'PERSISTENT_REPLICATE' is invalid when persistent is false.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenPersistentAndReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, "REPLICATE"); + fail("Setting the 'persistent' attribute to TRUE and 'Data Policy' to REPLICATE should have thrown an IllegalArgumentException!"); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy 'REPLICATE' is invalid when persistent is true.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE)); + } + } + + @Test + public void testResolveDataPolicyWhenNotPersistentAndReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, DataPolicy.REPLICATE); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE)); + } + + @Test + public void testResolveDataPolicyWhenPersistentAndPersistentReplicateDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, DataPolicy.PERSISTENT_REPLICATE); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + + protected static class TestRegionFactory extends RegionFactory { + + protected TestRegionFactory() { + super((GemFireCacheImpl) null); + } + } + } diff --git a/src/test/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBeanTest.java index 5553f596..8e28194a 100644 --- a/src/test/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/ReplicatedRegionFactoryBeanTest.java @@ -32,9 +32,11 @@ import com.gemstone.gemfire.cache.RegionFactory; * and correct behavior of the ReplicatedRegionFactoryBean class. *

* @author John Blum - * @see org.mockito.Mockito * @see org.junit.Test + * @see org.mockito.Mockito * @see org.springframework.data.gemfire.ReplicatedRegionFactoryBean + * @see com.gemstone.gemfire.cache.DataPolicy + * @see com.gemstone.gemfire.cache.RegionFactory * @since 1.3.3 */ @SuppressWarnings("unchecked") @@ -49,7 +51,7 @@ public class ReplicatedRegionFactoryBeanTest { @Test public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() { RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.resolveDataPolicy(mockRegionFactory, null, null); + factoryBean.resolveDataPolicy(mockRegionFactory, null, (String) null); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE)); } @@ -57,7 +59,7 @@ public class ReplicatedRegionFactoryBeanTest { public void testResolveDataPolicyWhenNotPersistentAndDataPolicyUnspecified() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(false); - factoryBean.resolveDataPolicy(mockRegionFactory, false, null); + factoryBean.resolveDataPolicy(mockRegionFactory, false, (String) null); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE)); } @@ -65,43 +67,50 @@ public class ReplicatedRegionFactoryBeanTest { public void testResolveDataPolicyWhenPersistentAndDataPolicyUnspecified() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(true); - factoryBean.resolveDataPolicy(mockRegionFactory, true, null); + factoryBean.resolveDataPolicy(mockRegionFactory, true, (String) null); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } - @Test - public void testResolveDataPolicyWithPersistentUnspecifiedAndEmptyDataPolicy() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.resolveDataPolicy(mockRegionFactory, null, "EMPTY"); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.EMPTY)); - } - - @Test - public void testResolveDataPolicyWhenNotPersistentAndEmptyDataPolicy() { - RegionFactory mockRegionFactory = createMockRegionFactory(); - factoryBean.setPersistent(false); - factoryBean.resolveDataPolicy(mockRegionFactory, false, "empty"); - verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.EMPTY)); - } - @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWhenPersistentAndEmptyDataPolicy() { + public void testResolveDataPolicyWithBlankDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); + try { - factoryBean.setPersistent(true); - factoryBean.resolveDataPolicy(mockRegionFactory, true, "empty"); + factoryBean.resolveDataPolicy(mockRegionFactory, null, " "); } catch (IllegalArgumentException e) { - assertEquals("Data Policy 'EMPTY' is invalid when persistent is true.", e.getMessage()); + assertEquals("Data Policy ' ' is invalid.", e.getMessage()); throw e; } finally { - verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.EMPTY)); + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED)); } } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWithPersistentUnspecifiedAndInvalidDataPolicyName() { + public void testResolveDataPolicyWithEmptyDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.resolveDataPolicy(mockRegionFactory, null, ""); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy '' is invalid.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED)); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenPersistentUnspecifiedAndInvalidDataPolicyName() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { @@ -113,13 +122,14 @@ public class ReplicatedRegionFactoryBeanTest { } finally { verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.EMPTY)); verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE)); verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWithPersistentUnspecifiedAndInvalidDataPolicyRegionType() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndInvalidDataPolicyType() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { @@ -136,14 +146,48 @@ public class ReplicatedRegionFactoryBeanTest { } @Test - public void testResolveDataPolicyWhenPersistentUnspecifiedAndReplicatedDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndEmptyDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.resolveDataPolicy(mockRegionFactory, null, "EMPTY"); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.EMPTY)); + } + + @Test + public void testResolveDataPolicyWhenNotPersistentAndEmptyDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + factoryBean.setPersistent(false); + factoryBean.resolveDataPolicy(mockRegionFactory, false, "empty"); + verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.EMPTY)); + } + + @Test(expected = IllegalArgumentException.class) + public void testResolveDataPolicyWhenPersistentAndEmptyDataPolicy() { + RegionFactory mockRegionFactory = createMockRegionFactory(); + + try { + factoryBean.setPersistent(true); + factoryBean.resolveDataPolicy(mockRegionFactory, true, "empty"); + } + catch (IllegalArgumentException e) { + assertEquals("Data Policy 'EMPTY' is invalid when persistent is true.", e.getMessage()); + throw e; + } + finally { + verify(mockRegionFactory, never()).setDataPolicy(null); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.EMPTY)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); + } + } + + @Test + public void testResolveDataPolicyWhenPersistentUnspecifiedAndReplicateDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "REPLICATE"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE)); } @Test - public void testResolveDataPolicyWhenNotPersistentAndReplicatedDataPolicy() { + public void testResolveDataPolicyWhenNotPersistentAndReplicateDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(false); factoryBean.resolveDataPolicy(mockRegionFactory, false, "REPLICATE"); @@ -151,7 +195,7 @@ public class ReplicatedRegionFactoryBeanTest { } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWhenPersistentAndReplicatedDataPolicy() { + public void testResolveDataPolicyWhenPersistentAndReplicateDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { @@ -165,18 +209,19 @@ public class ReplicatedRegionFactoryBeanTest { finally { verify(mockRegionFactory, never()).setDataPolicy(null); verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } } @Test - public void testResolveDataPolicyWhenPersistentUnspecifiedAndPersistentReplicatedDataPolicy() { + public void testResolveDataPolicyWhenPersistentUnspecifiedAndPersistentReplicateDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_REPLICATE"); verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } @Test(expected = IllegalArgumentException.class) - public void testResolveDataPolicyWhenNotPersistentAndPersistentReplicatedDataPolicy() { + public void testResolveDataPolicyWhenNotPersistentAndPersistentReplicateDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); try { @@ -190,11 +235,12 @@ public class ReplicatedRegionFactoryBeanTest { finally { verify(mockRegionFactory, never()).setDataPolicy(null); verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE)); + verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE)); } } @Test - public void testResolveDataPolicyWhenPersistentAndPersistentReplicatedDataPolicy() { + public void testResolveDataPolicyWhenPersistentAndPersistentReplicateDataPolicy() { RegionFactory mockRegionFactory = createMockRegionFactory(); factoryBean.setPersistent(true); factoryBean.resolveDataPolicy(mockRegionFactory, true, "PERSISTENT_REPLICATE"); diff --git a/src/test/java/org/springframework/data/gemfire/config/AbstractRegionParserTest.java b/src/test/java/org/springframework/data/gemfire/config/AbstractRegionParserTest.java index a7a08016..47bec9d1 100644 --- a/src/test/java/org/springframework/data/gemfire/config/AbstractRegionParserTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/AbstractRegionParserTest.java @@ -18,12 +18,17 @@ package org.springframework.data.gemfire.config; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.matches; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.junit.Test; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.beans.factory.xml.XmlReaderContext; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -74,6 +79,63 @@ public class AbstractRegionParserTest { assertFalse(regionParser.isSubRegion(mockElement)); } + @Test + public void testValidateDataPolicyShortcutAttributesMutualExclusion() { + Element mockElement = mock(Element.class); + + when(mockElement.hasAttribute(matches("data-policy"))).thenReturn(false); + when(mockElement.hasAttribute(matches("shortcut"))).thenReturn(false); + + new TestRegionParser().validateDataPolicyShortcutAttributesMutualExclusion(mockElement, null); + + verify(mockElement).hasAttribute(eq("data-policy")); + verify(mockElement, never()).hasAttribute(eq("shortcut")); + } + + @Test + public void testValidateDataPolicyShortcutAttributesMutualExclusionWithDataPolicy() { + Element mockElement = mock(Element.class); + + when(mockElement.hasAttribute(matches("data-policy"))).thenReturn(true); + when(mockElement.hasAttribute(matches("shortcut"))).thenReturn(false); + + new TestRegionParser().validateDataPolicyShortcutAttributesMutualExclusion(mockElement, null); + + verify(mockElement).hasAttribute(eq("data-policy")); + verify(mockElement).hasAttribute(eq("shortcut")); + } + + @Test + public void testValidateDataPolicyShortcutAttributesMutualExclusionWithShortcut() { + Element mockElement = mock(Element.class); + + when(mockElement.hasAttribute(matches("data-policy"))).thenReturn(false); + when(mockElement.hasAttribute(matches("shortcut"))).thenReturn(true); + + new TestRegionParser().validateDataPolicyShortcutAttributesMutualExclusion(mockElement, null); + + verify(mockElement).hasAttribute(eq("data-policy")); + verify(mockElement, never()).hasAttribute(eq("shortcut")); + } + + @Test + public void testValidateDataPolicyShortcutAttributesMutualExclusionWithDataPolicyAndShortcut() { + Element mockElement = mock(Element.class); + XmlReaderContext mockReaderContext = mock(XmlReaderContext.class); + + ParserContext mockParserContext = new ParserContext(mockReaderContext, null); + + when(mockElement.hasAttribute(matches("data-policy"))).thenReturn(true); + when(mockElement.hasAttribute(matches("shortcut"))).thenReturn(true); + when(mockElement.getTagName()).thenReturn("local-region"); + + new TestRegionParser().validateDataPolicyShortcutAttributesMutualExclusion(mockElement, mockParserContext); + + verify(mockReaderContext).error( + eq("Only one of [data-policy, shortcut] may be specified with element 'local-region'."), + eq(mockElement)); + } + protected static class TestRegionParser extends AbstractRegionParser { @Override diff --git a/src/test/java/org/springframework/data/gemfire/config/SubRegionWithInvalidDataPolicyTest.java b/src/test/java/org/springframework/data/gemfire/config/SubRegionWithInvalidDataPolicyTest.java index 8f6f4350..55ecece9 100644 --- a/src/test/java/org/springframework/data/gemfire/config/SubRegionWithInvalidDataPolicyTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/SubRegionWithInvalidDataPolicyTest.java @@ -21,7 +21,9 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.xml.sax.SAXParseException; /** * The SubRegionWithInvalidDataPolicyTest class is a test suite of test cases testing the data-policy and persistent @@ -35,17 +37,15 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; */ public class SubRegionWithInvalidDataPolicyTest { - @Test(expected = BeanCreationException.class) + @Test(expected = XmlBeanDefinitionStoreException.class) public void testSubRegionBeanDefinitionWithInconsistentDataPolicy() { try { new ClassPathXmlApplicationContext("/org/springframework/data/gemfire/config/subregion-with-invalid-datapolicy.xml"); } - catch (BeanCreationException expected) { + catch (XmlBeanDefinitionStoreException expected) { //expected.printStackTrace(System.err); - assertTrue(expected.getMessage().contains("Error creating bean with name '/Parent/Child'")); - assertTrue(expected.getCause() instanceof IllegalArgumentException); - assertEquals("Data Policy 'PERSISTENT_PARTITION' is not supported in Replicated Regions.", - expected.getCause().getMessage()); + assertTrue(expected.getCause() instanceof SAXParseException); + assertTrue(expected.getCause().getMessage().contains("PERSISTENT_PARTITION")); throw expected; } } diff --git a/src/test/java/org/springframework/data/gemfire/support/ClientRegionShortcutWrapperTest.java b/src/test/java/org/springframework/data/gemfire/support/ClientRegionShortcutWrapperTest.java new file mode 100644 index 00000000..17b4a828 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/support/ClientRegionShortcutWrapperTest.java @@ -0,0 +1,152 @@ +/* + * Copyright 2010-2013 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 + * + * http://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.support; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.client.ClientRegionShortcut; + +/** + * The ClientRegionShortcutWrapperTest class is a test suite of test cases testing the contract and functionality of the + * ClientRegionShortcutWrapper enum class type. + *

+ * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.support.ClientRegionShortcutWrapper + * @see com.gemstone.gemfire.cache.client.ClientRegionShortcut + * @since 1.4.0 + */ +public class ClientRegionShortcutWrapperTest { + + @Test + public void testOneToOneMapping() { + for (ClientRegionShortcut shortcut : ClientRegionShortcut.values()) { + assertNotNull(ClientRegionShortcutWrapper.valueOf(shortcut.name())); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.equals(ClientRegionShortcutWrapper.valueOf(shortcut))); + } + } + + @Test + public void testClientRegionShortcutUnspecified() { + assertEquals(ClientRegionShortcutWrapper.UNSPECIFIED, ClientRegionShortcutWrapper.valueOf( + (ClientRegionShortcut) null)); + } + + @Test + public void testIsCaching() { + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY.isCaching()); + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isCaching()); + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isCaching()); + assertFalse(ClientRegionShortcutWrapper.LOCAL.isCaching()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isCaching()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isCaching()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isCaching()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isCaching()); + assertFalse(ClientRegionShortcutWrapper.PROXY.isCaching()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isCaching()); + } + + @Test + public void testIsHeapLru() { + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY.isHeapLru()); + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.LOCAL.isHeapLru()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.PROXY.isHeapLru()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isHeapLru()); + } + + @Test + public void testIsLocal() { + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY.isLocal()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isLocal()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isLocal()); + assertTrue(ClientRegionShortcutWrapper.LOCAL.isLocal()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isLocal()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isLocal()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isLocal()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isLocal()); + assertFalse(ClientRegionShortcutWrapper.PROXY.isLocal()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isLocal()); + } + + @Test + public void testIsOverflow() { + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY.isOverflow()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isOverflow()); + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL.isOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isOverflow()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isOverflow()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isOverflow()); + assertFalse(ClientRegionShortcutWrapper.PROXY.isOverflow()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isOverflow()); + } + + @Test + public void testIsPersistent() { + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.LOCAL.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isPersistent()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isPersistent()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.PROXY.isPersistent()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isPersistent()); + } + + @Test + public void testIsPersistentOverflow() { + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isPersistentOverflow()); + assertTrue(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.PROXY.isPersistentOverflow()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isPersistentOverflow()); + } + + @Test + public void testIsProxy() { + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY.isProxy()); + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY_HEAP_LRU.isProxy()); + assertTrue(ClientRegionShortcutWrapper.CACHING_PROXY_OVERFLOW.isProxy()); + assertFalse(ClientRegionShortcutWrapper.LOCAL.isProxy()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_HEAP_LRU.isProxy()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_OVERFLOW.isProxy()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT.isProxy()); + assertFalse(ClientRegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isProxy()); + assertTrue(ClientRegionShortcutWrapper.PROXY.isProxy()); + assertFalse(ClientRegionShortcutWrapper.UNSPECIFIED.isProxy()); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/support/RegionShortcutWrapperTest.java b/src/test/java/org/springframework/data/gemfire/support/RegionShortcutWrapperTest.java new file mode 100644 index 00000000..dd63db8f --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/support/RegionShortcutWrapperTest.java @@ -0,0 +1,303 @@ +/* + * Copyright 2010-2013 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 + * + * http://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.support; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.RegionShortcut; + +/** + * The RegionShortcutWrapperTest class is a test suite of test cases testing the contract and functionality of the + * RegionShortcutWrapper enum class type. + *

+ * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.support.RegionShortcutWrapper + * @see com.gemstone.gemfire.cache.RegionShortcut + * @since 1.4.0 + */ +public class RegionShortcutWrapperTest { + + @Test + public void testOneForOneMapping() { + for (RegionShortcut shortcut : RegionShortcut.values()) { + assertNotNull(RegionShortcutWrapper.valueOf(shortcut.name())); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.equals(RegionShortcutWrapper.valueOf(shortcut))); + } + } + + @Test + public void testRegionShortcutUnspecified() { + assertEquals(RegionShortcutWrapper.UNSPECIFIED, RegionShortcutWrapper.valueOf((RegionShortcut) null)); + } + + @Test + public void testIsHeapLru() { + assertFalse(RegionShortcutWrapper.LOCAL.isHeapLru()); + assertTrue(RegionShortcutWrapper.LOCAL_HEAP_LRU.isHeapLru()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isHeapLru()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION.isHeapLru()); + assertTrue(RegionShortcutWrapper.PARTITION_HEAP_LRU.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isHeapLru()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isHeapLru()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.REPLICATE.isHeapLru()); + assertTrue(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isHeapLru()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isHeapLru()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isHeapLru()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isHeapLru()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isHeapLru()); + } + + @Test + public void testIsLocal() { + assertTrue(RegionShortcutWrapper.LOCAL.isLocal()); + assertTrue(RegionShortcutWrapper.LOCAL_HEAP_LRU.isLocal()); + assertTrue(RegionShortcutWrapper.LOCAL_OVERFLOW.isLocal()); + assertTrue(RegionShortcutWrapper.LOCAL_PERSISTENT.isLocal()); + assertTrue(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isLocal()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.REPLICATE.isLocal()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isLocal()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isLocal()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isLocal()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isLocal()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isLocal()); + } + + @Test + public void testIsOverflow() { + assertFalse(RegionShortcutWrapper.LOCAL.isOverflow()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isOverflow()); + assertTrue(RegionShortcutWrapper.LOCAL_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isOverflow()); + assertTrue(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isOverflow()); + assertTrue(RegionShortcutWrapper.PARTITION_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isOverflow()); + assertTrue(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isOverflow()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isOverflow()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE.isOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isOverflow()); + assertTrue(RegionShortcutWrapper.REPLICATE_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isOverflow()); + assertTrue(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isOverflow()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isOverflow()); + } + + @Test + public void testIsPartition() { + assertFalse(RegionShortcutWrapper.LOCAL.isPartition()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isPartition()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isPartition()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isPartition()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_HEAP_LRU.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_OVERFLOW.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_PERSISTENT.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_PROXY.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isPartition()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isPartition()); + assertFalse(RegionShortcutWrapper.REPLICATE.isPartition()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isPartition()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isPartition()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isPartition()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isPartition()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isPartition()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isPartition()); + } + + @Test + public void testIsPersistent() { + assertFalse(RegionShortcutWrapper.LOCAL.isPersistent()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isPersistent()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isPersistent()); + assertTrue(RegionShortcutWrapper.LOCAL_PERSISTENT.isPersistent()); + assertTrue(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isPersistent()); + assertTrue(RegionShortcutWrapper.PARTITION_PERSISTENT.isPersistent()); + assertTrue(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isPersistent()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isPersistent()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isPersistent()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isPersistent()); + assertFalse(RegionShortcutWrapper.REPLICATE.isPersistent()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isPersistent()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isPersistent()); + assertTrue(RegionShortcutWrapper.REPLICATE_PERSISTENT.isPersistent()); + assertTrue(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isPersistent()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isPersistent()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isPersistent()); + } + + @Test + public void testIsPersistentOverflow() { + assertFalse(RegionShortcutWrapper.LOCAL.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isPersistentOverflow()); + assertTrue(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isPersistentOverflow()); + assertTrue(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isPersistentOverflow()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isPersistentOverflow()); + assertTrue(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isPersistentOverflow()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isPersistentOverflow()); + } + @Test + public void testIsProxy() { + assertFalse(RegionShortcutWrapper.LOCAL.isProxy()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isProxy()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isProxy()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isProxy()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isProxy()); + assertTrue(RegionShortcutWrapper.PARTITION_PROXY.isProxy()); + assertTrue(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isProxy()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isProxy()); + assertFalse(RegionShortcutWrapper.REPLICATE.isProxy()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isProxy()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isProxy()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isProxy()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isProxy()); + assertTrue(RegionShortcutWrapper.REPLICATE_PROXY.isProxy()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isProxy()); + } + @Test + public void testIsRedundant() { + assertFalse(RegionShortcutWrapper.LOCAL.isRedundant()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isRedundant()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isRedundant()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.PARTITION.isRedundant()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isRedundant()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isRedundant()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isRedundant()); + assertTrue(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isRedundant()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT.isRedundant()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isRedundant()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isRedundant()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isRedundant()); + assertTrue(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.REPLICATE.isRedundant()); + assertFalse(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isRedundant()); + assertFalse(RegionShortcutWrapper.REPLICATE_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT.isRedundant()); + assertFalse(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isRedundant()); + assertFalse(RegionShortcutWrapper.REPLICATE_PROXY.isRedundant()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isRedundant()); + } + + @Test + public void testIsReplicate() { + assertFalse(RegionShortcutWrapper.LOCAL.isReplicate()); + assertFalse(RegionShortcutWrapper.LOCAL_HEAP_LRU.isReplicate()); + assertFalse(RegionShortcutWrapper.LOCAL_OVERFLOW.isReplicate()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT.isReplicate()); + assertFalse(RegionShortcutWrapper.LOCAL_PERSISTENT_OVERFLOW.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_HEAP_LRU.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_OVERFLOW.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_PERSISTENT_OVERFLOW.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_PROXY_REDUNDANT.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_HEAP_LRU.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_OVERFLOW.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT.isReplicate()); + assertFalse(RegionShortcutWrapper.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW.isReplicate()); + assertTrue(RegionShortcutWrapper.REPLICATE.isReplicate()); + assertTrue(RegionShortcutWrapper.REPLICATE_HEAP_LRU.isReplicate()); + assertTrue(RegionShortcutWrapper.REPLICATE_OVERFLOW.isReplicate()); + assertTrue(RegionShortcutWrapper.REPLICATE_PERSISTENT.isReplicate()); + assertTrue(RegionShortcutWrapper.REPLICATE_PERSISTENT_OVERFLOW.isReplicate()); + assertTrue(RegionShortcutWrapper.REPLICATE_PROXY.isReplicate()); + assertFalse(RegionShortcutWrapper.UNSPECIFIED.isReplicate()); + } + +} diff --git a/src/test/resources/org/springframework/data/gemfire/colocated-region.xml b/src/test/resources/org/springframework/data/gemfire/colocated-region.xml index 9e565672..2206b09b 100644 --- a/src/test/resources/org/springframework/data/gemfire/colocated-region.xml +++ b/src/test/resources/org/springframework/data/gemfire/colocated-region.xml @@ -3,8 +3,8 @@ xmlns:gfe="http://www.springframework.org/schema/gemfire" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.3.xsd"> + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd"> diff --git a/src/test/resources/org/springframework/data/gemfire/config/client-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/client-ns.xml index ab1444da..1c3bc2a7 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/client-ns.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/client-ns.xml @@ -13,7 +13,7 @@ - + diff --git a/src/test/resources/org/springframework/data/gemfire/invalid-region-shortcut-with-persistent-attribute.xml b/src/test/resources/org/springframework/data/gemfire/invalid-region-shortcut-with-persistent-attribute.xml new file mode 100644 index 00000000..5cef86a7 --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/invalid-region-shortcut-with-persistent-attribute.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/src/test/resources/org/springframework/data/gemfire/invalid-use-of-region-datapolicy-and-shortcut.xml b/src/test/resources/org/springframework/data/gemfire/invalid-use-of-region-datapolicy-and-shortcut.xml new file mode 100644 index 00000000..2e3fa76e --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/invalid-use-of-region-datapolicy-and-shortcut.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/src/test/resources/org/springframework/data/gemfire/region-datapolicy-shortcuts.xml b/src/test/resources/org/springframework/data/gemfire/region-datapolicy-shortcuts.xml new file mode 100644 index 00000000..6903943f --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/region-datapolicy-shortcuts.xml @@ -0,0 +1,41 @@ + + + + + springGemFireRegionDataPolicyShortcutsIntegrationTest + config + 0 + + + + + + + + + + + + + + + + + + + +