diff --git a/src/main/java/org/springframework/data/gemfire/config/EvictionAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java similarity index 96% rename from src/main/java/org/springframework/data/gemfire/config/EvictionAttributesFactoryBean.java rename to src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java index 54b02cad..71ea2dc5 100644 --- a/src/main/java/org/springframework/data/gemfire/config/EvictionAttributesFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.data.gemfire.config; +package org.springframework.data.gemfire; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; @@ -36,7 +36,7 @@ import com.gemstone.gemfire.internal.cache.lru.MemLRUCapacityController; * @see com.gemstone.gemfire.cache.util.ObjectSizer */ @SuppressWarnings("unused") -class EvictionAttributesFactoryBean implements FactoryBean, InitializingBean { +public class EvictionAttributesFactoryBean implements FactoryBean, InitializingBean { // TODO remove this reference to the GemFire internal class when the Gem team fixes the EvictionAttributes bug!!! protected static final int DEFAULT_LRU_MAXIMUM_ENTRIES = LRUCapacityController.DEFAULT_MAXIMUM_ENTRIES; diff --git a/src/main/java/org/springframework/data/gemfire/EvictionType.java b/src/main/java/org/springframework/data/gemfire/EvictionType.java new file mode 100644 index 00000000..fd618a16 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/EvictionType.java @@ -0,0 +1,94 @@ +/* + * 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.EvictionAlgorithm; + +/** + * Simple enumeration for the various GemFire Eviction types. + * + * @author Costin Leau + * @author John Blum + * @see com.gemstone.gemfire.cache.EvictionAlgorithm + */ +@SuppressWarnings("unused") +public enum EvictionType { + ENTRY_COUNT(EvictionAlgorithm.LRU_ENTRY), + HEAP_PERCENTAGE(EvictionAlgorithm.LRU_HEAP), + MEMORY_SIZE(EvictionAlgorithm.LRU_MEMORY), + NONE(EvictionAlgorithm.NONE); + + private final EvictionAlgorithm evictionAlgorithm; + + /** + * Constructs an instance (enumeration) of the EvictionType enumerated type initialized with the corresponding + * GemFire EvictionAlgorithm. + * + * @param evictionAlgorithm the GemFire EvictionAlgorithm represented by this EvictionType. + * @see com.gemstone.gemfire.cache.EvictionAlgorithm + */ + EvictionType(final EvictionAlgorithm evictionAlgorithm) { + this.evictionAlgorithm = evictionAlgorithm; + } + + /** + * Returns the corresponding SDG EvictionType enumerated value for the GemFire EvictionAlgorithm. + * + * @param evictionAlgorithm the GemFire EvictionAlgorithm used to lookup the corresponding EvictionType. + * @return a EvictionType representing the specified GemFire EvictionAlgorithm. Returns null if no EvictionType + * represents the given GemFire EvictionAlgorithm. + * @see #getEvictionAlgorithm() + * @see com.gemstone.gemfire.cache.EvictionAlgorithm + */ + public static EvictionType valueOf(final EvictionAlgorithm evictionAlgorithm) { + for (EvictionType evictionType : values()) { + if (evictionType.getEvictionAlgorithm().equals(evictionAlgorithm)) { + return evictionType; + } + } + + return null; + } + + /** + * Returns an EvictionType enumerated value for the given, named EvictionType, ignoring case. + * + * @param value a String value indicating the name of the desired EvictionType enumerated value. + * @return an EvictionType corresponding to the given, named enumerated value. + * @see java.lang.Enum#name() + */ + public static EvictionType valueOfIgnoreCase(final String value) { + for (EvictionType evictionType : values()) { + if (evictionType.name().equalsIgnoreCase(value)) { + return evictionType; + } + } + + return null; + } + + /** + * Gets the GemFire EvictionAlgorithm represented by this enumerated value. + * + * @return the corresponding GemFire EvictionAlgorithm. + * @see com.gemstone.gemfire.cache.EvictionAlgorithm + */ + public EvictionAlgorithm getEvictionAlgorithm() { + return evictionAlgorithm; + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java b/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java new file mode 100644 index 00000000..5a316721 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java @@ -0,0 +1,69 @@ +/* + * 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 java.beans.PropertyEditorSupport; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.util.Assert; + +/** + * The EvictionTypeConverter class is a Spring Converter used to convert a String value into + * a corresponding EvictionType enumerated value. + * + * @author John Blum + * @see java.beans.PropertyEditorSupport + * @see org.springframework.core.convert.converter.Converter + * @see org.springframework.data.gemfire.EvictionType + * @since 1.6.0 + */ +public class EvictionTypeConverter extends PropertyEditorSupport implements Converter { + + /* non-javadoc */ + private EvictionType assertConverted(final String source, final EvictionType evictionType) { + Assert.notNull(evictionType, String.format("Source (%1$s) is not a valid EvictionType!", source)); + return evictionType; + } + + /** + * Converts the given String value into an appropriate ExpirationActionType. + * + * @param source the String value to convert into an ExpirationActionType. + * @return an ExpirationActionType for the given String value. + * @throws java.lang.IllegalArgumentException if the String value does not represent a valid ExpirationActionType. + * @see org.springframework.data.gemfire.EvictionType#valueOfIgnoreCase(String) + */ + @Override + public EvictionType convert(final String source) { + return assertConverted(source, EvictionType.valueOfIgnoreCase(source)); + } + + /** + * Sets the EvictionType 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 an EvictionType. + * + * @param text the String value to express as (convert to) an EvictionType. + * @throws java.lang.IllegalArgumentException if the String value does not represent a valid EvictionType. + * @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/SubscriptionAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java similarity index 69% rename from src/main/java/org/springframework/data/gemfire/config/SubscriptionAttributesFactoryBean.java rename to src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java index 474cf12d..37bf1cff 100644 --- a/src/main/java/org/springframework/data/gemfire/config/SubscriptionAttributesFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBean.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.gemfire.config; +package org.springframework.data.gemfire; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; @@ -25,58 +25,43 @@ 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. * * @author Lyndon Adams - * @since 12 March 2013 + * @author John Blum + * @since 1.3.0 */ public class SubscriptionAttributesFactoryBean implements FactoryBean, InitializingBean { - SubscriptionAttributes subscriptionAttri; - InterestPolicy policy; - SubscriptionType type = SubscriptionType.ALL; - - + private SubscriptionAttributes subscriptionAttributes; + + private SubscriptionType type; + /* (non-Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ @Override public void afterPropertiesSet() throws Exception { - if( policy == null ){ - policy = InterestPolicy.DEFAULT; - } - subscriptionAttri = createAttributes(); + subscriptionAttributes = new SubscriptionAttributes(getPolicy()); } - private SubscriptionAttributes createAttributes(){ - switch( type ){ - case ALL : - policy = InterestPolicy.ALL; - break; - case CACHE_CONTENT : - policy = InterestPolicy.CACHE_CONTENT; - break; - default : - policy = InterestPolicy.DEFAULT; - } - return new SubscriptionAttributes( policy ); - } - - - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#getObject() */ @Override public SubscriptionAttributes getObject() throws Exception { - return subscriptionAttri; + return subscriptionAttributes; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ @Override public Class getObjectType() { - return ( subscriptionAttri != null ) ? subscriptionAttri.getClass() : SubscriptionAttributes.class; + return (subscriptionAttributes != null ? subscriptionAttributes.getClass() : SubscriptionAttributes.class); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#isSingleton() */ @Override @@ -84,19 +69,20 @@ 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/EvictionType.java b/src/main/java/org/springframework/data/gemfire/config/EvictionType.java deleted file mode 100644 index 8f52a364..00000000 --- a/src/main/java/org/springframework/data/gemfire/config/EvictionType.java +++ /dev/null @@ -1,27 +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.config; - -/** - * Simple enumeration for the various GemFire eviction types. - * - * @author Costin Leau - */ -enum EvictionType { - - ENTRY_COUNT, MEMORY_SIZE, HEAP_PERCENTAGE -} diff --git a/src/main/java/org/springframework/data/gemfire/config/GemfireConstants.java b/src/main/java/org/springframework/data/gemfire/config/GemfireConstants.java index 65e73cf7..6dfcfcbe 100644 --- a/src/main/java/org/springframework/data/gemfire/config/GemfireConstants.java +++ b/src/main/java/org/springframework/data/gemfire/config/GemfireConstants.java @@ -12,15 +12,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.data.gemfire.config; + /** - * - * @author David Turanski + * The GemfireConstants class define constants for Spring GemFire component bean names. * + * @author David Turanski + * @author John Blum */ +@SuppressWarnings("unused") public interface GemfireConstants { - static final String DEFAULT_GEMFIRE_POOL_NAME = "gemfirePool"; + static final String DEFAULT_GEMFIRE_CACHE_NAME = "gemfireCache"; - static final String DEFAULT_GEMFIRE_TXMANAGER_NAME = "gemfireTransactionManager"; static final String DEFAULT_GEMFIRE_FUNCTION_SERVICE_NAME = "gemfireFunctionService"; + static final String DEFAULT_GEMFIRE_POOL_NAME = "gemfirePool"; + static final String DEFAULT_GEMFIRE_TRANSACTION_MANAGER_NAME = "gemfireTransactionManager"; + + @Deprecated + static final String DEFAULT_GEMFIRE_TXMANAGER_NAME = DEFAULT_GEMFIRE_TRANSACTION_MANAGER_NAME; + } 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 17f17c99..fd195dc6 100644 --- a/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java +++ b/src/main/java/org/springframework/data/gemfire/config/ParsingUtils.java @@ -25,7 +25,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.ManagedList; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.core.Conventions; +import org.springframework.data.gemfire.EvictionAttributesFactoryBean; +import org.springframework.data.gemfire.EvictionType; import org.springframework.data.gemfire.GemfireUtils; +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; @@ -236,10 +240,11 @@ abstract class ParsingUtils { String type = subscriptionElement.getAttribute("type"); if (StringUtils.hasText(type)) { - subscriptionAttributesBuilder.addPropertyValue("type", SubscriptionType.valueOf(type.toUpperCase())); + subscriptionAttributesBuilder.addPropertyValue("type", SubscriptionType.valueOfIgnoreCase(type)); } - regionAttributesBuilder.addPropertyValue("subscriptionAttributes", subscriptionAttributesBuilder.getBeanDefinition()); + regionAttributesBuilder.addPropertyValue("subscriptionAttributes", + subscriptionAttributesBuilder.getBeanDefinition()); return true; } diff --git a/src/main/java/org/springframework/data/gemfire/config/SubscriptionType.java b/src/main/java/org/springframework/data/gemfire/config/SubscriptionType.java deleted file mode 100644 index 83cc8439..00000000 --- a/src/main/java/org/springframework/data/gemfire/config/SubscriptionType.java +++ /dev/null @@ -1,26 +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.config; - -/** - * Simple enumeration for the various GemFire subscription types. - * - * @author Lyndon Adams - * - */ -enum SubscriptionType { - ALL, CACHE_CONTENT; -} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/gemfire/config/TransactionManagerParser.java b/src/main/java/org/springframework/data/gemfire/config/TransactionManagerParser.java index f767976e..8670e5e8 100644 --- a/src/main/java/org/springframework/data/gemfire/config/TransactionManagerParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/TransactionManagerParser.java @@ -26,9 +26,10 @@ import org.springframework.util.StringUtils; import org.w3c.dom.Element; /** - * Parser for <transaction-manager> definitions. + * Parser for <transaction-manager> element bean definitions. * * @author Costin Leau + * @author John Blum */ class TransactionManagerParser extends AbstractSingleBeanDefinitionParser { @@ -53,11 +54,15 @@ class TransactionManagerParser extends AbstractSingleBeanDefinitionParser { protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException { String name = super.resolveId(element, definition, parserContext); + if (!StringUtils.hasText(name)) { - name = GemfireConstants.DEFAULT_GEMFIRE_TXMANAGER_NAME; + name = GemfireConstants.DEFAULT_GEMFIRE_TRANSACTION_MANAGER_NAME; //For backward compatibility - parserContext.getRegistry().registerAlias(GemfireConstants.DEFAULT_GEMFIRE_TXMANAGER_NAME, "gemfire-transaction-manager"); + parserContext.getRegistry().registerAlias(GemfireConstants.DEFAULT_GEMFIRE_TRANSACTION_MANAGER_NAME, + "gemfire-transaction-manager"); } + return name; } + } diff --git a/src/test/java/org/springframework/data/gemfire/config/EvictionAttributesFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java similarity index 98% rename from src/test/java/org/springframework/data/gemfire/config/EvictionAttributesFactoryBeanTest.java rename to src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java index 6599a7e3..5d34b8d7 100644 --- a/src/test/java/org/springframework/data/gemfire/config/EvictionAttributesFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.data.gemfire.config; +package org.springframework.data.gemfire; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -37,7 +37,7 @@ import com.gemstone.gemfire.cache.util.ObjectSizer; * * @author John Blum * @see org.junit.Test - * @see org.springframework.data.gemfire.config.EvictionAttributesFactoryBean + * @see org.springframework.data.gemfire.EvictionAttributesFactoryBean * @see com.gemstone.gemfire.cache.EvictionAttributes * @since 1.3.4 */ diff --git a/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java b/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java new file mode 100644 index 00000000..a60ae85c --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java @@ -0,0 +1,82 @@ +/* + * 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.assertNull; + +import org.junit.After; +import org.junit.Test; + +/** + * The EvictionTypeConverterTest class is a test suite of test cases testing the contract and functionality + * of the EvictionTypeConverter class. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.EvictionTypeConverter + * @since 1.6.0 + */ +public class EvictionTypeConverterTest { + + private final EvictionTypeConverter converter = new EvictionTypeConverter(); + + @After + public void tearDown() { + converter.setValue(null); + } + + @Test + public void testConvert() { + assertEquals(EvictionType.ENTRY_COUNT, converter.convert("entry_count")); + assertEquals(EvictionType.HEAP_PERCENTAGE, converter.convert("Heap_Percentage")); + assertEquals(EvictionType.MEMORY_SIZE, converter.convert("MEMorY_SiZe")); + assertEquals(EvictionType.NONE, converter.convert("NONE")); + } + + @Test(expected = IllegalArgumentException.class) + public void testConvertWithIllegalValue() { + try { + converter.convert("LIFO_MEMORY"); + } + catch (IllegalArgumentException expected) { + assertEquals("Source (LIFO_MEMORY) is not a valid EvictionType!", expected.getMessage()); + throw expected; + } + } + + @Test + public void testSetAsText() { + assertNull(converter.getValue()); + converter.setAsText("heap_percentage"); + assertEquals(EvictionType.HEAP_PERCENTAGE, converter.getValue()); + converter.setAsText("NOne"); + assertEquals(EvictionType.NONE, converter.getValue()); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetAsTextWithIllegalValue() { + try { + converter.setAsText("LRU_COUNT"); + } + catch (IllegalArgumentException expected) { + assertEquals("Source (LRU_COUNT) is not a valid EvictionType!", expected.getMessage()); + throw expected; + } + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java b/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java new file mode 100644 index 00000000..d4d89132 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java @@ -0,0 +1,67 @@ +/* + * 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.assertNull; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.EvictionAlgorithm; + +/** + * The EvictionTypeTest class is a test suite of test cases testing the contract and functionality + * of the EvictionType enum. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.EvictionType + * @since 1.6.0 + */ +public class EvictionTypeTest { + + @Test + @SuppressWarnings("deprecation") + public void testValueOfEvictionAlgorithms() { + assertEquals(EvictionType.ENTRY_COUNT, EvictionType.valueOf(EvictionAlgorithm.LRU_ENTRY)); + assertEquals(EvictionType.HEAP_PERCENTAGE, EvictionType.valueOf(EvictionAlgorithm.LRU_HEAP)); + assertEquals(EvictionType.MEMORY_SIZE, EvictionType.valueOf(EvictionAlgorithm.LRU_MEMORY)); + assertEquals(EvictionType.NONE, EvictionType.valueOf(EvictionAlgorithm.NONE)); + assertNull(EvictionType.valueOf(EvictionAlgorithm.LIFO_ENTRY)); + assertNull(EvictionType.valueOf(EvictionAlgorithm.LIFO_MEMORY)); + assertNull(EvictionType.valueOf((EvictionAlgorithm) null)); + } + + @Test + public void testValueOfIgnoreCase() { + assertEquals(EvictionType.ENTRY_COUNT, EvictionType.valueOfIgnoreCase("entry_count")); + assertEquals(EvictionType.HEAP_PERCENTAGE, EvictionType.valueOfIgnoreCase("Heap_Percentage")); + assertEquals(EvictionType.MEMORY_SIZE, EvictionType.valueOfIgnoreCase("MEMorY_SiZe")); + assertEquals(EvictionType.NONE, EvictionType.valueOfIgnoreCase("NONE")); + } + + @Test + public void testValueOfIgnoreCaseWithInvalidValues() { + assertNull(EvictionType.valueOfIgnoreCase("heap_%")); + assertNull(EvictionType.valueOfIgnoreCase("mem_size")); + assertNull(EvictionType.valueOfIgnoreCase("memory_space")); + assertNull(EvictionType.valueOfIgnoreCase(" ")); + assertNull(EvictionType.valueOfIgnoreCase("")); + assertNull(EvictionType.valueOfIgnoreCase(null)); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java new file mode 100644 index 00000000..fa320d98 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/SubscriptionAttributesFactoryBeanTest.java @@ -0,0 +1,112 @@ +/* + * 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.assertTrue; + +import org.junit.Test; + +import com.gemstone.gemfire.cache.InterestPolicy; +import com.gemstone.gemfire.cache.SubscriptionAttributes; + +/** + * The SubscriptionAttributesFactoryBeanTest class is a test suite of test cases testing the contract and functionality + * of the SubscriptionAttributesFactoryBean class. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.SubscriptionAttributesFactoryBean + * @since 1.6.0 + */ +public class SubscriptionAttributesFactoryBeanTest { + + @Test + public void testIsSingleton() { + assertTrue(new SubscriptionAttributesFactoryBean().isSingleton()); + } + + @Test + public void testGetObjectAndObjectTypeForSubscriptionType() throws Exception { + SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); + + factoryBean.setType(SubscriptionType.CACHE_CONTENT); + factoryBean.afterPropertiesSet(); + + assertEquals(SubscriptionType.CACHE_CONTENT, factoryBean.getType()); + assertEquals(InterestPolicy.CACHE_CONTENT, factoryBean.getPolicy()); + + SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.CACHE_CONTENT, subscriptionAttributes.getInterestPolicy()); + assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); + } + + @Test + public void testGetObjectAndObjectTypeForDefaultSubscriptionType() throws Exception { + SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); + + factoryBean.setType(null); + 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()); + + SubscriptionAttributes subscriptionAttributes = factoryBean.getObject(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.ALL, subscriptionAttributes.getInterestPolicy()); + assertTrue(SubscriptionAttributes.class.isAssignableFrom(factoryBean.getObjectType())); + } + + @Test + public void testGetObjectAndObjectTypeForNullInterestPolicy() throws Exception { + SubscriptionAttributesFactoryBean factoryBean = new SubscriptionAttributesFactoryBean(); + + factoryBean.setPolicy(null); + 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())); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/SubscriptionTypeConverterTest.java b/src/test/java/org/springframework/data/gemfire/SubscriptionTypeConverterTest.java new file mode 100644 index 00000000..4e872c39 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/SubscriptionTypeConverterTest.java @@ -0,0 +1,79 @@ +/* + * 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 org.junit.After; +import org.junit.Test; + +/** + * The SubscriptionTypeConverterTest class is a test suite of test cases testing the contract and functionality + * of the SubscriptionTypeConverter class. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.SubscriptionTypeConverter + * @since 1.5.0 + */ +public class SubscriptionTypeConverterTest { + + private SubscriptionTypeConverter converter = new SubscriptionTypeConverter(); + + @After + public void tearDown() { + converter.setValue(null); + } + + @Test + public void testConvert() { + assertEquals(SubscriptionType.ALL, converter.convert("all")); + assertEquals(SubscriptionType.CACHE_CONTENT, converter.convert("Cache_Content")); + assertEquals(SubscriptionType.DEFAULT, converter.convert("DeFault")); + } + + @Test(expected = IllegalArgumentException.class) + public void testConvertWithInvalidValue() { + try { + converter.convert("invalid"); + } + catch (IllegalArgumentException expected) { + assertEquals("Source (invalid) is not a valid SubscriptionType!", expected.getMessage()); + throw expected; + } + } + + @Test + public void testSetAsText() { + converter.setAsText("aLl"); + assertEquals(SubscriptionType.ALL, converter.getValue()); + converter.setAsText("CACHE_content"); + assertEquals(SubscriptionType.CACHE_CONTENT, converter.getValue()); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetAsTextWithInvalidValue() { + try { + converter.setAsText("none"); + } + catch (IllegalArgumentException expected) { + assertEquals("Source (none) is not a valid SubscriptionType!", expected.getMessage()); + throw expected; + } + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java b/src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java new file mode 100644 index 00000000..537665da --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/SubscriptionTypeTest.java @@ -0,0 +1,70 @@ +/* + * 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/CacheSubscriptionTest.java b/src/test/java/org/springframework/data/gemfire/config/CacheSubscriptionTest.java index adb57af0..28f2d32e 100644 --- a/src/test/java/org/springframework/data/gemfire/config/CacheSubscriptionTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/CacheSubscriptionTest.java @@ -16,7 +16,8 @@ package org.springframework.data.gemfire.config; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -36,45 +37,60 @@ import com.gemstone.gemfire.cache.SubscriptionAttributes; * Test to ensure subscription policy can be applied to server regions. * * @author Lyndon Adams - * @since 13 March 2013 + * @author John Blum + * @since 1.3.0 */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("/org/springframework/data/gemfire/config/subscription-ns.xml") +@ContextConfiguration("subscription-ns.xml") +@SuppressWarnings("unused") public class CacheSubscriptionTest{ - @Autowired ApplicationContext context; + @Autowired + private ApplicationContext context; - @SuppressWarnings("rawtypes") @Test - public void testRRSubscriptionAllPolicy() throws Exception { + public void testReplicatedRegionSubscriptionAllPolicy() throws Exception { assertTrue(context.containsBean("replicALL")); - RegionFactoryBean fb = context.getBean("&replicALL", RegionFactoryBean.class); - RegionAttributes attrs = TestUtils.readField("attributes", fb); - - SubscriptionAttributes sa = attrs.getSubscriptionAttributes(); - assertEquals(InterestPolicy.ALL, sa.getInterestPolicy() ); + + RegionFactoryBean regionFactoryBean = context.getBean("&replicALL", RegionFactoryBean.class); + RegionAttributes regionAttributes = TestUtils.readField("attributes", regionFactoryBean); + + assertNotNull(regionAttributes); + + SubscriptionAttributes subscriptionAttributes = regionAttributes.getSubscriptionAttributes(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.ALL, subscriptionAttributes.getInterestPolicy()); } - @SuppressWarnings("rawtypes") @Test - public void testPRSubscriptionCacheContentPolicy() throws Exception { + public void testPartitionRegionSubscriptionCacheContentPolicy() throws Exception { assertTrue(context.containsBean("partCACHE_CONTENT")); - RegionFactoryBean fb = context.getBean("&partCACHE_CONTENT", RegionFactoryBean.class); - RegionAttributes attrs = TestUtils.readField("attributes", fb); - - SubscriptionAttributes sa = attrs.getSubscriptionAttributes(); - assertEquals(InterestPolicy.CACHE_CONTENT, sa.getInterestPolicy() ); + + RegionFactoryBean regionFactoryBean = context.getBean("&partCACHE_CONTENT", RegionFactoryBean.class); + RegionAttributes regionAttributes = TestUtils.readField("attributes", regionFactoryBean); + + assertNotNull(regionAttributes); + + SubscriptionAttributes subscriptionAttributes = regionAttributes.getSubscriptionAttributes(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.CACHE_CONTENT, subscriptionAttributes.getInterestPolicy()); } - @SuppressWarnings("rawtypes") @Test - public void testPRSubscriptionDefaultPolicy() throws Exception { + public void testPartitionRegionSubscriptionDefaultPolicy() throws Exception { assertTrue(context.containsBean("partDEFAULT")); - RegionFactoryBean fb = context.getBean("&partDEFAULT", RegionFactoryBean.class); - RegionAttributes attrs = TestUtils.readField("attributes", fb); - - SubscriptionAttributes sa = attrs.getSubscriptionAttributes(); - assertEquals(InterestPolicy.ALL, sa.getInterestPolicy() ); + + RegionFactoryBean regionFactoryBean = context.getBean("&partDEFAULT", RegionFactoryBean.class); + RegionAttributes regionAttributes = TestUtils.readField("attributes", regionFactoryBean); + + assertNotNull(regionAttributes); + + SubscriptionAttributes subscriptionAttributes = regionAttributes.getSubscriptionAttributes(); + + assertNotNull(subscriptionAttributes); + assertEquals(InterestPolicy.DEFAULT, subscriptionAttributes.getInterestPolicy()); } }