From ecbd4fd2326ea0714a1aabc6b0301aa9c6bbce33 Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 23 Jan 2015 16:15:27 -0800 Subject: [PATCH] SGF-289 - Enumeration restrictions (xsd:enumeration) should be avoided in the XML schema. Removed the 'subscriptionPolicyType' XSD simple-type definition and subsequently, all Subscription policy enumeration restrictions on SDG Region elements in the SDG XML namespace (e.g. gfe:partitioned-region) that declare and define a Subscription policy. Renamed SubscriptionType to InterestPolicyType along with the associated test suite class. Renamed the SubscriptionTypeConverter class to InterestPolicyConverter along with the associated test suite class. Created the RegionSubscriptionAttributesNamespaceTest class to test the configuration of Subscription on GemFire Regions in the SDG XML namespace using various settings and property placeholders. --- .../data/gemfire/CacheFactoryBean.java | 3 +- .../data/gemfire/InterestPolicyConverter.java | 51 +++++++++ .../data/gemfire/InterestPolicyType.java | 106 ++++++++++++++++++ .../SubscriptionAttributesFactoryBean.java | 49 ++++++-- .../data/gemfire/SubscriptionType.java | 94 ---------------- .../gemfire/SubscriptionTypeConverter.java | 71 ------------ .../data/gemfire/config/ParsingUtils.java | 9 +- .../gemfire/config/spring-gemfire-1.6.xsd | 38 +------ .../data/gemfire/EvictionActionTypeTest.java | 4 +- .../gemfire/ExpirationActionTypeTest.java | 2 +- ....java => InterestPolicyConverterTest.java} | 39 ++++--- .../data/gemfire/InterestPolicyTypeTest.java | 100 +++++++++++++++++ ...SubscriptionAttributesFactoryBeanTest.java | 76 +++++++------ .../data/gemfire/SubscriptionTypeTest.java | 70 ------------ ...onSubscriptionAttributesNamespaceTest.java | 97 ++++++++++++++++ ...riptionAttributesNamespaceTest-context.xml | 42 +++++++ 16 files changed, 511 insertions(+), 340 deletions(-) create mode 100644 src/main/java/org/springframework/data/gemfire/InterestPolicyConverter.java create mode 100644 src/main/java/org/springframework/data/gemfire/InterestPolicyType.java delete mode 100644 src/main/java/org/springframework/data/gemfire/SubscriptionType.java delete mode 100644 src/main/java/org/springframework/data/gemfire/SubscriptionTypeConverter.java rename src/test/java/org/springframework/data/gemfire/{SubscriptionTypeConverterTest.java => InterestPolicyConverterTest.java} (52%) create mode 100644 src/test/java/org/springframework/data/gemfire/InterestPolicyTypeTest.java delete mode 100644 src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java create mode 100644 src/test/java/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest.java create mode 100644 src/test/resources/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest-context.xml diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index e429939d..c208d946 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -50,6 +50,7 @@ import com.gemstone.gemfire.cache.DynamicRegionFactory; import com.gemstone.gemfire.cache.EvictionAction; import com.gemstone.gemfire.cache.ExpirationAction; import com.gemstone.gemfire.cache.GemFireCache; +import com.gemstone.gemfire.cache.InterestPolicy; import com.gemstone.gemfire.cache.TransactionListener; import com.gemstone.gemfire.cache.TransactionWriter; import com.gemstone.gemfire.cache.util.GatewayConflictResolver; @@ -921,7 +922,7 @@ public class CacheFactoryBean implements BeanClassLoaderAware, BeanFactoryAware, beanFactory.registerCustomEditor(ExpirationAction.class, ExpirationActionConverter.class); beanFactory.registerCustomEditor(IndexMaintenanceType.class, IndexMaintenanceTypeConverter.class); beanFactory.registerCustomEditor(IndexType.class, IndexTypeConverter.class); - beanFactory.registerCustomEditor(SubscriptionType.class, SubscriptionTypeConverter.class); + beanFactory.registerCustomEditor(InterestPolicy.class, InterestPolicyConverter.class); } } diff --git a/src/main/java/org/springframework/data/gemfire/InterestPolicyConverter.java b/src/main/java/org/springframework/data/gemfire/InterestPolicyConverter.java new file mode 100644 index 00000000..4e038915 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/InterestPolicyConverter.java @@ -0,0 +1,51 @@ +/* + * 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 org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport; + +import com.gemstone.gemfire.cache.InterestPolicy; + +/** + * The InterestPolicyConverter class is a Spring Converter implementation and Java PropertyEditor handling + * the conversion between Strings and GemFire InterestPolicy values. + * + * @author John Blum + * @see org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport + * @see com.gemstone.gemfire.cache.InterestPolicy + * @since 1.6.0 + */ +@SuppressWarnings("unused") +public class InterestPolicyConverter extends AbstractPropertyEditorConverterSupport { + + /** + * Converts the given String into a GemFire InterestPolicy value. + * + * @param source the String value to convert into a GemFire InterestPolicy value. + * @return a GemFire InterestPolicy value for the given String description of the GemFire InterestPolicy + * @throws java.lang.IllegalArgumentException if the String is not a valid GemFire InterestPolicy. + * @see org.springframework.data.gemfire.InterestPolicyType#getInterestPolicy(InterestPolicyType) + * @see org.springframework.data.gemfire.InterestPolicyType#valueOfIgnoreCase(String) + * @see #assertConverted(String, Object, Class) + */ + @Override + public InterestPolicy convert(final String source) { + return assertConverted(source, InterestPolicyType.getInterestPolicy( + InterestPolicyType.valueOfIgnoreCase(source)), InterestPolicy.class); + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/InterestPolicyType.java b/src/main/java/org/springframework/data/gemfire/InterestPolicyType.java new file mode 100644 index 00000000..a04d56a4 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/InterestPolicyType.java @@ -0,0 +1,106 @@ +/* + * 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 com.gemstone.gemfire.cache.InterestPolicy; + +/** + * The InterestPolicyType enum is an enumeration of all the GemFire Subscription, InterestPolicy values. + * + * @author Lyndon Adams + * @author John Blum + * @since 1.3.0 + */ +@SuppressWarnings("unused") +public enum InterestPolicyType { + ALL(InterestPolicy.ALL), + CACHE_CONTENT(InterestPolicy.CACHE_CONTENT); + + public static final InterestPolicyType DEFAULT = InterestPolicyType.valueOf(InterestPolicy.DEFAULT); + + private final InterestPolicy interestPolicy; + + /** + * Constructs an instance of the SubscriptionType enum initialized with the matching GemFire InterestPolicy. + * + * @param interestPolicy a GemFire InterestPolicy corresponding to this SubscriptionType. + * @see com.gemstone.gemfire.cache.InterestPolicy + */ + InterestPolicyType(final InterestPolicy interestPolicy) { + this.interestPolicy = interestPolicy; + } + + /** + * Null-safe operation to extract the GemFire InterestPolicy from the InterPolicyType enumerated value. + * + * @param interestPolicyType the InterestPolicyType enum from which to extract GemFire's InterestPolicy + * @return a GemFire InterestPolicy for the given InterestPolicyType enumerated value + * or null if InterestPolicyType is null. + * @see com.gemstone.gemfire.cache.InterestPolicy + */ + public static InterestPolicy getInterestPolicy(final InterestPolicyType interestPolicyType) { + return (interestPolicyType != null ? interestPolicyType.getInterestPolicy() : null); + } + + /** + * Returns a SubscriptionType enumerated value for the given GemFire InterestPolicy. + * + * @param interestPolicy the GemFire InterestPolicy used to lookup and match a SubscriptionType. + * @return a SubscriptionType enumerated value matching the given GemFire InterestPolicy + * or null if no matching value was found. + * @see com.gemstone.gemfire.cache.InterestPolicy + * @see #getInterestPolicy() + */ + public static InterestPolicyType valueOf(final InterestPolicy interestPolicy) { + for (InterestPolicyType interestPolicyType : values()) { + if (interestPolicyType.getInterestPolicy().equals(interestPolicy)) { + return interestPolicyType; + } + } + + return null; + } + + /** + * Returns a SubscriptionType enumerated value for the case-insensitive, named Subscription (InterestsPolicy). + * + * @param value a String name used to look and match the SubscriptionType. + * @return a SubscriptionType enumerated value for the given case-insensitive named Subscription + * or null if no match was found. + * @see java.lang.String#equalsIgnoreCase(String) + * @see #name() + */ + public static InterestPolicyType valueOfIgnoreCase(final String value) { + for (InterestPolicyType interestPolicyType : values()) { + if (interestPolicyType.name().equalsIgnoreCase(value)) { + return interestPolicyType; + } + } + + return null; + } + + /** + * Returns the GemFire InterestPolicy corresponding to this SubscriptionType enumerated value. + * + * @return the GemFire InterestPolicy corresponding to this SubscriptionType. + * @see com.gemstone.gemfire.cache.InterestPolicy + */ + public InterestPolicy getInterestPolicy() { + return interestPolicy; + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java index 37bf1cff..17f80c03 100644 --- a/src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java @@ -22,24 +22,31 @@ import com.gemstone.gemfire.cache.InterestPolicy; import com.gemstone.gemfire.cache.SubscriptionAttributes; /** - * Simple utility class used for defining nested factory-method like definitions w/o polluting the container with useless beans. + * The SubscriptionAttributesFactoryBean class is a Spring FactoryBean used for defining and constructing + * a GemFire SubscriptionAttributes object, which determines the Subscription policy used by Regions to + * declared their data interests. * * @author Lyndon Adams * @author John Blum + * @see org.springframework.beans.factory.FactoryBean + * @see org.springframework.beans.factory.InitializingBean + * @see com.gemstone.gemfire.cache.InterestPolicy + * @see com.gemstone.gemfire.cache.SubscriptionAttributes * @since 1.3.0 */ public class SubscriptionAttributesFactoryBean implements FactoryBean, InitializingBean { + private InterestPolicy interestPolicy; + private SubscriptionAttributes subscriptionAttributes; - private SubscriptionType type; - - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ @Override public void afterPropertiesSet() throws Exception { - subscriptionAttributes = new SubscriptionAttributes(getPolicy()); + subscriptionAttributes = new SubscriptionAttributes(getInterestPolicy()); } /* @@ -69,20 +76,40 @@ public class SubscriptionAttributesFactoryBean implements FactoryBean { - - /* non-Javadoc */ - private SubscriptionType assertConverted(final String source, final SubscriptionType subscriptionType) { - Assert.notNull(subscriptionType, String.format("Source (%1$s) is not a valid SubscriptionType!", source)); - return subscriptionType; - } - - /** - * Converts the given String into a SubscriptionType enumerated value. - * - * @param source the String value to convert into a corresponding SubscriptionType enumerated value. - * @return a SubscriptionType enumerated value given a String representation. - * @throws java.lang.IllegalArgumentException if the String does not represent a valid SubscriptionType. - * @see #assertConverted(String, SubscriptionType) - * @see org.springframework.data.gemfire.SubscriptionType#valueOfIgnoreCase(String) - */ - @Override - public SubscriptionType convert(final String source) { - return assertConverted(source, SubscriptionType.valueOfIgnoreCase(source)); - } - - /** - * Sets the SubscriptionType by parsing the given text String. May throw a java.lang.IllegalArgumentException - * if either the text is badly formatted or the text cannot be expressed as a SubscriptionType. - * - * @param text the String value to express as (convert to) a SubscriptionType. - * @throws java.lang.IllegalArgumentException if the String value does not represent a valid SubscriptionType. - * @see #convert(String) - * @see #setValue(Object) - */ - @Override - public void setAsText(final String text) throws IllegalArgumentException { - setValue(convert(text)); - } - -} 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 53d69890..49245c7c 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java +++ b/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java @@ -28,8 +28,8 @@ import org.springframework.core.Conventions; import org.springframework.data.gemfire.EvictionAttributesFactoryBean; import org.springframework.data.gemfire.ExpirationAttributesFactoryBean; import org.springframework.data.gemfire.GemfireUtils; +import org.springframework.data.gemfire.InterestPolicyType; import org.springframework.data.gemfire.SubscriptionAttributesFactoryBean; -import org.springframework.data.gemfire.SubscriptionType; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; @@ -230,12 +230,7 @@ abstract class ParsingUtils { BeanDefinitionBuilder subscriptionAttributesBuilder = BeanDefinitionBuilder.genericBeanDefinition( SubscriptionAttributesFactoryBean.class); - // do manual conversion since the enum is not public - String type = subscriptionElement.getAttribute("type"); - - if (StringUtils.hasText(type)) { - subscriptionAttributesBuilder.addPropertyValue("type", SubscriptionType.valueOfIgnoreCase(type)); - } + setPropertyValue(subscriptionElement, subscriptionAttributesBuilder, "type", "interestPolicy"); regionAttributesBuilder.addPropertyValue("subscriptionAttributes", subscriptionAttributesBuilder.getBeanDefinition()); diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd index 1a8481f5..115d05cc 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.6.xsd @@ -1029,7 +1029,7 @@ Subscription policy for the replicated region. ]]> - + @@ -1262,7 +1262,7 @@ Subscription policy for the partitioned region. ]]> - + @@ -1635,38 +1635,6 @@ The threshold (or limit) against which the eviction algorithm runs. Once the thr - - - - - - - - - - - - - - - @@ -1969,9 +1937,9 @@ The ClientRegionShortcut for this region. Allows easy initialization of the regi - + diff --git a/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java b/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java index b3bc0493..dac3c92f 100644 --- a/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java +++ b/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java @@ -64,7 +64,7 @@ public class EvictionActionTypeTest { } @Test - public void testValueOfWithInvalidValue() { + public void testValueOfWithNull() { assertNull(EvictionActionType.valueOf((EvictionAction) null)); } @@ -77,7 +77,7 @@ public class EvictionActionTypeTest { } @Test - public void testValueOfIgnoreCaseWithInvalidValue() { + public void testValueOfIgnoreCaseWithInvalidValues() { assertNull(EvictionActionType.valueOfIgnoreCase("REMOTE_DESTROY")); assertNull(EvictionActionType.valueOfIgnoreCase("All")); assertNull(EvictionActionType.valueOfIgnoreCase(" none ")); diff --git a/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java b/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java index 1296a7d3..55d65a6d 100644 --- a/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java +++ b/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java @@ -57,8 +57,8 @@ public class ExpirationActionTypeTest { @Test public void testDefault() { - assertEquals(ExpirationActionType.INVALIDATE, ExpirationActionType.DEFAULT); assertEquals(ExpirationAction.INVALIDATE, ExpirationActionType.DEFAULT.getExpirationAction()); + assertEquals(ExpirationActionType.INVALIDATE, ExpirationActionType.DEFAULT); } @Test diff --git a/src/test/java/org/springframework/data/gemfire/SubscriptionTypeConverterTest.java b/src/test/java/org/springframework/data/gemfire/InterestPolicyConverterTest.java similarity index 52% rename from src/test/java/org/springframework/data/gemfire/SubscriptionTypeConverterTest.java rename to src/test/java/org/springframework/data/gemfire/InterestPolicyConverterTest.java index 4e872c39..245988d1 100644 --- a/src/test/java/org/springframework/data/gemfire/SubscriptionTypeConverterTest.java +++ b/src/test/java/org/springframework/data/gemfire/InterestPolicyConverterTest.java @@ -17,22 +17,26 @@ package org.springframework.data.gemfire; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import org.junit.After; import org.junit.Test; +import com.gemstone.gemfire.cache.InterestPolicy; + /** - * The SubscriptionTypeConverterTest class is a test suite of test cases testing the contract and functionality - * of the SubscriptionTypeConverter class. + * The InterestPolicyConverterTest class is a test suite of test cases testing the contract and functionality + * of the InterestPolicyConverter class. * * @author John Blum * @see org.junit.Test - * @see org.springframework.data.gemfire.SubscriptionTypeConverter - * @since 1.5.0 + * @see org.springframework.data.gemfire.InterestPolicyConverter + * @see com.gemstone.gemfire.cache.InterestPolicy + * @since 1.6.0 */ -public class SubscriptionTypeConverterTest { +public class InterestPolicyConverterTest { - private SubscriptionTypeConverter converter = new SubscriptionTypeConverter(); + private InterestPolicyConverter converter = new InterestPolicyConverter(); @After public void tearDown() { @@ -41,18 +45,19 @@ public class SubscriptionTypeConverterTest { @Test public void testConvert() { - assertEquals(SubscriptionType.ALL, converter.convert("all")); - assertEquals(SubscriptionType.CACHE_CONTENT, converter.convert("Cache_Content")); - assertEquals(SubscriptionType.DEFAULT, converter.convert("DeFault")); + assertEquals(InterestPolicy.ALL, converter.convert("all")); + assertEquals(InterestPolicy.CACHE_CONTENT, converter.convert("Cache_Content")); + assertEquals(InterestPolicy.CACHE_CONTENT, converter.convert("CACHE_ConTent")); + assertEquals(InterestPolicy.ALL, converter.convert("ALL")); } @Test(expected = IllegalArgumentException.class) - public void testConvertWithInvalidValue() { + public void testConvertIllegalValueValue() { try { converter.convert("invalid"); } catch (IllegalArgumentException expected) { - assertEquals("Source (invalid) is not a valid SubscriptionType!", expected.getMessage()); + assertEquals("(invalid) is not a valid InterestPolicy!", expected.getMessage()); throw expected; } } @@ -60,20 +65,24 @@ public class SubscriptionTypeConverterTest { @Test public void testSetAsText() { converter.setAsText("aLl"); - assertEquals(SubscriptionType.ALL, converter.getValue()); - converter.setAsText("CACHE_content"); - assertEquals(SubscriptionType.CACHE_CONTENT, converter.getValue()); + assertEquals(InterestPolicy.ALL, converter.getValue()); + converter.setAsText("Cache_CoNTeNT"); + assertEquals(InterestPolicy.CACHE_CONTENT, converter.getValue()); } @Test(expected = IllegalArgumentException.class) public void testSetAsTextWithInvalidValue() { try { + assertNull(converter.getValue()); converter.setAsText("none"); } catch (IllegalArgumentException expected) { - assertEquals("Source (none) is not a valid SubscriptionType!", expected.getMessage()); + assertEquals("(none) is not a valid InterestPolicy!", expected.getMessage()); throw expected; } + finally { + assertNull(converter.getValue()); + } } } diff --git a/src/test/java/org/springframework/data/gemfire/InterestPolicyTypeTest.java b/src/test/java/org/springframework/data/gemfire/InterestPolicyTypeTest.java new file mode 100644 index 00000000..be76b563 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/InterestPolicyTypeTest.java @@ -0,0 +1,100 @@ +/* + * 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.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.InterestPolicy; + +/** + * The InterestPolicyTypeTest class is a test suite of test cases testing the contract and functionality + * of the InterestPolicyType enum. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.InterestPolicyType + * @see com.gemstone.gemfire.cache.InterestPolicy + * @since 1.6.0 + */ +public class InterestPolicyTypeTest { + + @Test + public void testStaticGetInterestPolicy() { + assertEquals(InterestPolicy.ALL, InterestPolicyType.getInterestPolicy(InterestPolicyType.ALL)); + assertEquals(InterestPolicy.CACHE_CONTENT, InterestPolicyType.getInterestPolicy(InterestPolicyType.CACHE_CONTENT)); + } + + @Test + public void testStaticGetInterestPolicyWithNull() { + assertNull(InterestPolicyType.getInterestPolicy(null)); + } + + @Test + public void testGetInterestPolicy() { + assertEquals(InterestPolicy.ALL, InterestPolicyType.ALL.getInterestPolicy()); + assertEquals(InterestPolicy.CACHE_CONTENT, InterestPolicyType.CACHE_CONTENT.getInterestPolicy()); + } + + @Test + public void testDefault() { + assertEquals(InterestPolicy.DEFAULT, InterestPolicyType.valueOf(InterestPolicy.DEFAULT).getInterestPolicy()); + assertEquals(InterestPolicyType.CACHE_CONTENT, InterestPolicyType.DEFAULT); + } + + @Test + public void testValueOfInterestPolicies() { + try { + for (byte ordinal = 0; ordinal < Byte.MAX_VALUE; ordinal++) { + InterestPolicy interestPolicy = InterestPolicy.fromOrdinal(ordinal); + InterestPolicyType interestPolicyType = InterestPolicyType.valueOf(interestPolicy); + assertNotNull(interestPolicyType); + assertEquals(interestPolicy, interestPolicyType.getInterestPolicy()); + } + } + catch (ArrayIndexOutOfBoundsException ignore) { + } + } + + @Test + public void testValueOfInterestPolicyWithNull() { + assertNull(InterestPolicyType.valueOf((InterestPolicy) null)); + } + + @Test + public void testValueOfIgnoreCase() { + assertEquals(InterestPolicyType.ALL, InterestPolicyType.valueOfIgnoreCase("all")); + assertEquals(InterestPolicyType.CACHE_CONTENT, InterestPolicyType.valueOfIgnoreCase("Cache_Content")); + assertEquals(InterestPolicyType.ALL, InterestPolicyType.valueOfIgnoreCase("ALL")); + assertEquals(InterestPolicyType.CACHE_CONTENT, InterestPolicyType.valueOfIgnoreCase("CACHE_ConTent")); + } + + @Test + public void testValueOfIgnoreCaseWithInvalidValues() { + assertNull(InterestPolicyType.valueOfIgnoreCase("@11")); + assertNull(InterestPolicyType.valueOfIgnoreCase("CACHE_KEYS")); + assertNull(InterestPolicyType.valueOfIgnoreCase("invalid")); + assertNull(InterestPolicyType.valueOfIgnoreCase("test")); + assertNull(InterestPolicyType.valueOfIgnoreCase(" ")); + assertNull(InterestPolicyType.valueOfIgnoreCase("")); + assertNull(InterestPolicyType.valueOfIgnoreCase(null)); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java index fa320d98..cd17c080 100644 --- a/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java @@ -42,48 +42,28 @@ public class SubscriptionAttributesFactoryBeanTest { } @Test - public void testGetObjectAndObjectTypeForSubscriptionType() throws Exception { + public void testSetAndGetInterestPolicy() { SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); - factoryBean.setType(SubscriptionType.CACHE_CONTENT); - factoryBean.afterPropertiesSet(); + assertEquals(InterestPolicy.DEFAULT, factoryBean.getInterestPolicy()); - assertEquals(SubscriptionType.CACHE_CONTENT, factoryBean.getType()); - assertEquals(InterestPolicy.CACHE_CONTENT, factoryBean.getPolicy()); + factoryBean.setInterestPolicy(InterestPolicy.CACHE_CONTENT); - SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); + assertEquals(InterestPolicy.CACHE_CONTENT, factoryBean.getInterestPolicy()); - assertNotNull(subscriptionAttributes); - assertEquals(InterestPolicy.CACHE_CONTENT, subscriptionAttributes.getInterestPolicy()); - assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); + factoryBean.setInterestPolicy(null); + + assertEquals(InterestPolicy.DEFAULT, factoryBean.getInterestPolicy()); } @Test - public void testGetObjectAndObjectTypeForDefaultSubscriptionType() throws Exception { + public void testGetObjectAndObjectTypeForAllInterestPolicy() throws Exception { SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); - factoryBean.setType(null); + factoryBean.setInterestPolicy(InterestPolicy.ALL); factoryBean.afterPropertiesSet(); - assertEquals(SubscriptionType.DEFAULT, factoryBean.getType()); - assertEquals(InterestPolicy.DEFAULT, factoryBean.getPolicy()); - - SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); - - assertNotNull(subscriptionAttributes); - assertEquals(InterestPolicy.DEFAULT, subscriptionAttributes.getInterestPolicy()); - assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); - } - - @Test - public void testGetObjectAndObjectTypeForInterestPolicy() throws Exception { - SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); - - factoryBean.setPolicy(InterestPolicy.ALL); - factoryBean.afterPropertiesSet(); - - assertEquals(SubscriptionType.ALL, factoryBean.getType()); - assertEquals(InterestPolicy.ALL, factoryBean.getPolicy()); + assertEquals(InterestPolicy.ALL, factoryBean.getInterestPolicy()); SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); @@ -92,15 +72,45 @@ public class SubscriptionAttributesFactoryBeanTest { assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); } + @Test + public void testGetObjectAndObjectTypeForCacheContentInterestPolicy() throws Exception { + SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); + + factoryBean.setInterestPolicy(InterestPolicy.CACHE_CONTENT); + factoryBean.afterPropertiesSet(); + + assertEquals(InterestPolicy.CACHE_CONTENT, factoryBean.getInterestPolicy()); + + SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.CACHE_CONTENT, subscriptionAttributes.getInterestPolicy()); + assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); + } + + @Test + public void testGetObjectAndObjectTypeForDefaultInterestPolicy() throws Exception { + SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); + + factoryBean.afterPropertiesSet(); + + assertEquals(InterestPolicy.DEFAULT, factoryBean.getInterestPolicy()); + + SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.DEFAULT, subscriptionAttributes.getInterestPolicy()); + assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); + } + @Test public void testGetObjectAndObjectTypeForNullInterestPolicy() throws Exception { SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); - factoryBean.setPolicy(null); + factoryBean.setInterestPolicy(null); factoryBean.afterPropertiesSet(); - assertEquals(SubscriptionType.DEFAULT, factoryBean.getType()); - assertEquals(InterestPolicy.DEFAULT, factoryBean.getPolicy()); + assertEquals(InterestPolicy.DEFAULT, factoryBean.getInterestPolicy()); SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); diff --git a/src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java b/src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java deleted file mode 100644 index 537665da..00000000 --- a/src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.assertNotNull; -import static org.junit.Assert.assertNull; - -import org.junit.Test; - -import com.gemstone.gemfire.cache.InterestPolicy; - -/** - * The SubscriptionTypeTest class is a test suite of test cases testing the contract and functionality - * of the SubscriptionType enumerated type. - * - * @author John Blum - * @see org.junit.Test - * @see org.springframework.data.gemfire.SubscriptionType - * @since 1.6.0 - */ -public class SubscriptionTypeTest { - - @Test - public void testValueOfInterestPolicies() { - try { - for (byte ordinal = 0; ordinal < Byte.MAX_VALUE; ordinal++) { - InterestPolicy interestPolicy = InterestPolicy.fromOrdinal(ordinal); - SubscriptionType subscriptionType = SubscriptionType.valueOf(interestPolicy); - assertNotNull(subscriptionType); - assertEquals(interestPolicy, subscriptionType.getInterestPolicy()); - } - } - catch (ArrayIndexOutOfBoundsException ignore) { - } - } - - @Test - public void testValueOfIgnoreCase() { - assertEquals(SubscriptionType.ALL, SubscriptionType.valueOfIgnoreCase("all")); - assertEquals(SubscriptionType.CACHE_CONTENT, SubscriptionType.valueOfIgnoreCase("Cache_Content")); - assertEquals(SubscriptionType.DEFAULT, SubscriptionType.valueOfIgnoreCase("DeFault")); - } - - @Test - public void testValueOfIgnoreCaseWithInvalidValue() { - assertNull(SubscriptionType.valueOfIgnoreCase(null)); - assertNull(SubscriptionType.valueOfIgnoreCase("")); - assertNull(SubscriptionType.valueOfIgnoreCase(" ")); - assertNull(SubscriptionType.valueOfIgnoreCase("@11")); - assertNull(SubscriptionType.valueOfIgnoreCase("CACHE_KEYS")); - assertNull(SubscriptionType.valueOfIgnoreCase("invalid")); - assertNull(SubscriptionType.valueOfIgnoreCase("test")); - } - -} diff --git a/src/test/java/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest.java new file mode 100644 index 00000000..51cd3913 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest.java @@ -0,0 +1,97 @@ +/* + * 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.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.gemstone.gemfire.cache.DataPolicy; +import com.gemstone.gemfire.cache.InterestPolicy; +import com.gemstone.gemfire.cache.Region; + +/** + * The RegionSubscriptionAttributesNamespaceTest class is a test suite of test cases testing the contract + * and functionality of declaring and defining Subscription Attributes for a Region in the Spring Data GemFire + * XML namespace (XSD) configuration meta-data. + * + * @author John Blum + * @see org.junit.Test + * @see org.junit.runner.RunWith + * @see org.springframework.data.gemfire.SubscriptionAttributesFactoryBean + * @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer + * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner + * @since 1.6.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(initializers = GemfireTestApplicationContextInitializer.class) +@SuppressWarnings("unused") +public class RegionSubscriptionAttributesNamespaceTest { + + @Resource(name = "NoSubscriptionRegion") + private Region noSubscriptionRegion; + + @Resource(name = "AllSubscriptionRegion") + private Region allSubscriptionRegion; + + @Resource(name = "CacheContentSubscriptionRegion") + private Region cacheContentSubscriptionRegion; + + @Resource(name = "DefaultSubscriptionRegion") + private Region defaultSubscriptionRegion; + + protected void assertSubscription(final Region region, final String expectedRegionName, + final DataPolicy expectedDataPolicy, final InterestPolicy expectedInterestedPolicy) { + assertNotNull(String.format("The '%1$s' Region was not properly configured an initialized!", + expectedRegionName), region); + assertEquals(expectedRegionName, region.getName()); + assertNotNull(region.getAttributes()); + assertEquals(expectedDataPolicy, region.getAttributes().getDataPolicy()); + assertNotNull(region.getAttributes().getSubscriptionAttributes()); + assertEquals(expectedInterestedPolicy, region.getAttributes().getSubscriptionAttributes().getInterestPolicy()); + } + + @Test + public void testNoSubscriptionRegion() { + assertSubscription(noSubscriptionRegion, "NoSubscriptionRegion", DataPolicy.REPLICATE, InterestPolicy.DEFAULT); + } + + @Test + public void testAllSubscriptionRegion() { + assertSubscription(allSubscriptionRegion, "AllSubscriptionRegion", DataPolicy.REPLICATE, InterestPolicy.ALL); + } + + @Test + public void testCacheContentSubscriptionRegion() { + assertSubscription(cacheContentSubscriptionRegion, "CacheContentSubscriptionRegion", DataPolicy.PARTITION, + InterestPolicy.CACHE_CONTENT); + } + + @Test + public void testDefaultSubscriptionRegion() { + assertSubscription(defaultSubscriptionRegion, "DefaultSubscriptionRegion", DataPolicy.PARTITION, + InterestPolicy.DEFAULT); + } + +} diff --git a/src/test/resources/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest-context.xml b/src/test/resources/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest-context.xml new file mode 100644 index 00000000..73f696f3 --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/config/RegionSubscriptionAttributesNamespaceTest-context.xml @@ -0,0 +1,42 @@ + + + + + ALL + + + + + + RegionSubscriptionAttributesNamespaceTest + 0 + config + + + + + + + + + + + + + + + + + + +