From 319442b446a8a0d211936c4a6ee3759025180dc8 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 22 Jan 2015 21:53:58 -0800 Subject: [PATCH] SGF-289 - Enumeration restrictions (xsd:enumeration) should be avoided in the XML schema. Removed the 'evictionActionType' XSD simple-type definition and subsequently, all EvictionAction enumeration restrictions on all SDG Region element types in the SDG XML namespace (e.g. gfe:partitioned-region) that define and declare an eviction policy and corresponding 'action'. In addition, performed refactoring and cleanup on serveral new SDG enumeration types, like EvictionType and ExpirationActionType, along with the corresponding Converter implementations and bean properties on their respective FactoryBeans (EvictionAttributesFactoryBean and ExpirationAttributesFactoryBean). --- .../data/gemfire/CacheFactoryBean.java | 5 +- .../data/gemfire/EvictionActionConverter.java | 51 +++++++++ .../data/gemfire/EvictionActionType.java | 108 ++++++++++++++++++ .../EvictionAttributesFactoryBean.java | 92 +++++++++------ .../data/gemfire/EvictionType.java | 40 ++++--- .../data/gemfire/EvictionTypeConverter.java | 41 ++----- .../gemfire/ExpirationActionConverter.java | 50 ++++++++ .../data/gemfire/ExpirationActionType.java | 43 ++++--- .../ExpirationActionTypeConverter.java | 72 ------------ .../ExpirationAttributesFactoryBean.java | 16 +-- .../data/gemfire/config/ParsingUtils.java | 20 +--- ...bstractPropertyEditorConverterSupport.java | 66 +++++++++++ .../gemfire/config/spring-gemfire-1.6.xsd | 64 +++++------ .../gemfire/EvictionActionConverterTest.java | 87 ++++++++++++++ .../data/gemfire/EvictionActionTypeTest.java | 90 +++++++++++++++ .../EvictionAttributesFactoryBeanTest.java | 108 +++++++++--------- .../gemfire/EvictionTypeConverterTest.java | 10 +- .../data/gemfire/EvictionTypeTest.java | 27 ++++- ...ava => ExpirationActionConverterTest.java} | 33 +++--- .../gemfire/ExpirationActionTypeTest.java | 35 +++++- .../ExpirationAttributesFactoryBeanTest.java | 6 +- ...victionAttributesNamespaceTest-context.xml | 3 +- 22 files changed, 767 insertions(+), 300 deletions(-) create mode 100644 src/main/java/org/springframework/data/gemfire/EvictionActionConverter.java create mode 100644 src/main/java/org/springframework/data/gemfire/EvictionActionType.java create mode 100644 src/main/java/org/springframework/data/gemfire/ExpirationActionConverter.java delete mode 100644 src/main/java/org/springframework/data/gemfire/ExpirationActionTypeConverter.java create mode 100644 src/main/java/org/springframework/data/gemfire/support/AbstractPropertyEditorConverterSupport.java create mode 100644 src/test/java/org/springframework/data/gemfire/EvictionActionConverterTest.java create mode 100644 src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java rename src/test/java/org/springframework/data/gemfire/{ExpirationActionTypeConverterTest.java => ExpirationActionConverterTest.java} (59%) diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index d368d562..e429939d 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -47,6 +47,8 @@ import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.CacheClosedException; import com.gemstone.gemfire.cache.CacheFactory; 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.TransactionListener; import com.gemstone.gemfire.cache.TransactionWriter; @@ -914,8 +916,9 @@ public class CacheFactoryBean implements BeanClassLoaderAware, BeanFactoryAware, if (getBeanFactory() instanceof ConfigurableBeanFactory) { ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) getBeanFactory(); + beanFactory.registerCustomEditor(EvictionAction.class, EvictionActionConverter.class); beanFactory.registerCustomEditor(EvictionType.class, EvictionTypeConverter.class); - beanFactory.registerCustomEditor(ExpirationActionType.class, ExpirationActionTypeConverter.class); + beanFactory.registerCustomEditor(ExpirationAction.class, ExpirationActionConverter.class); beanFactory.registerCustomEditor(IndexMaintenanceType.class, IndexMaintenanceTypeConverter.class); beanFactory.registerCustomEditor(IndexType.class, IndexTypeConverter.class); beanFactory.registerCustomEditor(SubscriptionType.class, SubscriptionTypeConverter.class); diff --git a/src/main/java/org/springframework/data/gemfire/EvictionActionConverter.java b/src/main/java/org/springframework/data/gemfire/EvictionActionConverter.java new file mode 100644 index 00000000..8fadd2fd --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/EvictionActionConverter.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.EvictionAction; + +/** + * The EvictionActionConverter class is a Spring Converter and JavaBeans PropertyEditor that converts + * an Object value into an instance of GemFire EvictionAction. + * + * @author John Blum + * @see org.springframework.data.gemfire.EvictionActionType + * @see org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport + * @see com.gemstone.gemfire.cache.EvictionAction + * @since 1.6.0 + */ +@SuppressWarnings("unused") +public class EvictionActionConverter extends AbstractPropertyEditorConverterSupport { + + /** + * Converts the given String into a GemFire EvictionAction value. + * + * @param source the String to convert. + * @return the GemFire EvictionAction value matching the given String. + * @throws java.lang.IllegalArgumentException if the String could not be converted into + * an instance of GemFire EvictionAction. + * @see com.gemstone.gemfire.cache.EvictionAction + */ + @Override + public EvictionAction convert(final String source) { + return assertConverted(source, EvictionActionType.getEvictionAction( + EvictionActionType.valueOfIgnoreCase(source)), EvictionAction.class); + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/EvictionActionType.java b/src/main/java/org/springframework/data/gemfire/EvictionActionType.java new file mode 100644 index 00000000..eddd6dff --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/EvictionActionType.java @@ -0,0 +1,108 @@ +/* + * 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.EvictionAction; + +/** + * The EvictionActionType enum is an enumeration of all the GemFire EvictionAction values. + * + * @author John Blum + * @see com.gemstone.gemfire.cache.EvictionAction + * @since 1.6.0 + */ +@SuppressWarnings("unused") +public enum EvictionActionType { + LOCAL_DESTROY(EvictionAction.LOCAL_DESTROY), + NONE(EvictionAction.NONE), + OVERFLOW_TO_DISK(EvictionAction.OVERFLOW_TO_DISK); + + public static final EvictionActionType DEFAULT = EvictionActionType.valueOf(EvictionAction.DEFAULT_EVICTION_ACTION); + + private final EvictionAction evictionAction; + + /** + * Constructs an instance of the EvictionActionType enum initialized with the matching GemFire EvictionAction. + * + * @param evictionAction the matching GemFire EvictionAction value for this enumerated value. + * @see com.gemstone.gemfire.cache.EvictionAction + */ + EvictionActionType(final EvictionAction evictionAction) { + this.evictionAction = evictionAction; + } + + /** + * A null-safe operation to extract the GemFire EvictionAction from the EvictionActionType enumerated value. + * + * @param evictionActionType the EvictionActionType enumerated value from which to extract + * the matching GemFire EvictionAction value. + * @return a GemFire EvictionAction given a EvictionActionType enumerated value. + * @see #getEvictionAction() + */ + public static EvictionAction getEvictionAction(final EvictionActionType evictionActionType) { + return (evictionActionType != null ? evictionActionType.getEvictionAction() : null); + } + + /** + * Returns an EvictionActionType enumerated value matching the given GemFire EvictionAction. + * + * @param evictionAction the GemFire EvictionAction used to lookup and match the appropriate EvictionActionType. + * @return an EvictionActionType enumerated value matching the given GemFire EvictionAction + * or null if no match was found. + * @see com.gemstone.gemfire.cache.EvictionAction + * @see #getEvictionAction() + */ + public static EvictionActionType valueOf(final EvictionAction evictionAction) { + for (EvictionActionType evictionActionType : values()) { + if (evictionActionType.getEvictionAction().equals(evictionAction)) { + return evictionActionType; + } + } + + return null; + } + + /** + * Returns an EvictionActionType enumerated value given the named, case-insensitive eviction action. + * + * @param name a String value indicating the name the eviction action used to match EvictionActionType. + * @return an EvictionActionType enumerated value matching the given named, case-insensitive eviction action + * or null if not match was found. + * @see java.lang.String#equalsIgnoreCase(String) + * @see #name() + */ + public static EvictionActionType valueOfIgnoreCase(final String name) { + for (EvictionActionType evictionActionType : values()) { + if (evictionActionType.name().equalsIgnoreCase(name)) { + return evictionActionType; + } + } + + return null; + } + + /** + * Gets the matching GemFire EvictionAction represented by this enumerated value. + * + * @return the GemFire EvictionAction represented by this enum. + * @see com.gemstone.gemfire.cache.EvictionAction + */ + public EvictionAction getEvictionAction() { + return evictionAction; + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java index 71ea2dc5..0793bb31 100644 --- a/src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/EvictionAttributesFactoryBean.java @@ -54,7 +54,6 @@ public class EvictionAttributesFactoryBean implements FactoryBean getObjectType() { return (evictionAttributes != null ? evictionAttributes.getClass() : EvictionAttributes.class); } + /* non-Javadoc */ public boolean isSingleton() { return true; } /** - * @return the action + * Sets the action to perform on the Region when Eviction occurs. + * + * @param action the specified EvictionAction taken on the Region. + * @see com.gemstone.gemfire.cache.EvictionAction */ - public EvictionAction getAction() { - return action; - } - - /** - * @param action the action to set - */ - public void setAction(EvictionAction action) { + public void setAction(final EvictionAction action) { this.action = action; } /** - * @return the objectSizer + * Gets the action performed on the Region when Eviction occurs. + * + * @return the EvictionAction taken on the Region. + * @see com.gemstone.gemfire.cache.EvictionAction + */ + public EvictionAction getAction() { + return (action != null ? action : EvictionAction.DEFAULT_EVICTION_ACTION); + } + + /** + * Sets the GemFire ObjectSizer used in determining object sizes of data stored in the Cache. + * + * @param objectSizer the ObjectSizer used in sizing object data stored in the Cache. + * @see com.gemstone.gemfire.cache.util.ObjectSizer + */ + public void setObjectSizer(final ObjectSizer objectSizer) { + this.objectSizer = objectSizer; + } + + /** + * Gets the GemFire ObjectSizer used in determining object sizes of data stored in the Cache. + * + * @return the ObjectSizer used in sizing object data stored in the Cache. + * @see com.gemstone.gemfire.cache.util.ObjectSizer */ public ObjectSizer getObjectSizer() { return objectSizer; } /** - * @param objectSizer the objectSizer to set + * Set the threshold used by the LRU algorithm in ENTRY_COUNT and MEMORY_SIZE eviction policy. + * + * @param threshold an Integer value specifying the threshold used by the LRU algorithm + * when enforcing the eviction policy. */ - public void setObjectSizer(ObjectSizer objectSizer) { - this.objectSizer = objectSizer; + public void setThreshold(final Integer threshold) { + this.threshold = threshold; } /** - * @return the threshold + * Get the threshold used by the LRU algorithm in ENTRY_COUNT and MEMORY_SIZE eviction policy. + * + * @return an Integer value specifying the threshold used by the LRU algorithm when enforcing the eviction policy. */ public Integer getThreshold() { return threshold; } /** - * @param threshold the threshold to set + * Sets the type of eviction policy and algorithm (e.g. LRU on Entry Count, Heap % or Memory Size) + * to implement on the Region. + * + * @param type the type of eviction policy/algorithm to implement on the Region. + * @see org.springframework.data.gemfire.EvictionType */ - public void setThreshold(Integer threshold) { - this.threshold = threshold; + public void setType(final EvictionType type) { + this.type = type; } /** - * @return the type + * Gets the eviction policy and algorithm used by the Region. + * + * @return the eviction policy and algorithm in use by the Region. + * @see org.springframework.data.gemfire.EvictionType */ public EvictionType getType() { return type; } - /** - * @param type the type to set - */ - public void setType(EvictionType type) { - this.type = type; - } - } diff --git a/src/main/java/org/springframework/data/gemfire/EvictionType.java b/src/main/java/org/springframework/data/gemfire/EvictionType.java index fd618a16..bf43648e 100644 --- a/src/main/java/org/springframework/data/gemfire/EvictionType.java +++ b/src/main/java/org/springframework/data/gemfire/EvictionType.java @@ -19,7 +19,7 @@ package org.springframework.data.gemfire; import com.gemstone.gemfire.cache.EvictionAlgorithm; /** - * Simple enumeration for the various GemFire Eviction types. + * The EvictionType enum is an enumeration of all GemFire Eviction policy types. * * @author Costin Leau * @author John Blum @@ -35,10 +35,9 @@ public enum EvictionType { private final EvictionAlgorithm evictionAlgorithm; /** - * Constructs an instance (enumeration) of the EvictionType enumerated type initialized with the corresponding - * GemFire EvictionAlgorithm. + * Constructs an instance of the EvictionType enum initialized with the matching GemFire EvictionAlgorithm. * - * @param evictionAlgorithm the GemFire EvictionAlgorithm represented by this EvictionType. + * @param evictionAlgorithm the GemFire EvictionAlgorithm represented by this EvictionType enumerated value. * @see com.gemstone.gemfire.cache.EvictionAlgorithm */ EvictionType(final EvictionAlgorithm evictionAlgorithm) { @@ -46,13 +45,23 @@ public enum EvictionType { } /** - * Returns the corresponding SDG EvictionType enumerated value for the GemFire EvictionAlgorithm. + * A null-safe operation to extract the GemFire EvictionAlgorithm from the given EvictionType enumerated value. * - * @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. + * @param evictionType the EvictionType from which to extract the GemFire EvictionAlgorithm. + * @return the GemFire EvictionAlgorithm for the corresponding EvictionType or null if evictionType is null. * @see #getEvictionAlgorithm() + */ + public static EvictionAlgorithm getEvictionAlgorithm(final EvictionType evictionType) { + return (evictionType != null ? evictionType.getEvictionAlgorithm() : null); + } + + /** + * Returns an EvictionType enumerated value matching the given GemFire EvictionAlgorithm. + * + * @param evictionAlgorithm the GemFire EvictionAlgorithm used to lookup and match the EvictionType. + * @return an EvictionType matching the specified GemFire EvictionAlgorithm or null if no match was found. * @see com.gemstone.gemfire.cache.EvictionAlgorithm + * @see #getEvictionAlgorithm() */ public static EvictionType valueOf(final EvictionAlgorithm evictionAlgorithm) { for (EvictionType evictionType : values()) { @@ -65,15 +74,16 @@ public enum EvictionType { } /** - * Returns an EvictionType enumerated value for the given, named EvictionType, ignoring case. + * Returns an EvictionType enumerated value given the named, case-insensitive eviction policy. * - * @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() + * @param name a String indicating the name of the eviction policy used to match EvictionType. + * @return an EvictionType matching the given the named, case-insensitive eviction policy. + * @see java.lang.String#equalsIgnoreCase(String) + * @see #name() */ - public static EvictionType valueOfIgnoreCase(final String value) { + public static EvictionType valueOfIgnoreCase(final String name) { for (EvictionType evictionType : values()) { - if (evictionType.name().equalsIgnoreCase(value)) { + if (evictionType.name().equalsIgnoreCase(name)) { return evictionType; } } @@ -84,7 +94,7 @@ public enum EvictionType { /** * Gets the GemFire EvictionAlgorithm represented by this enumerated value. * - * @return the corresponding GemFire EvictionAlgorithm. + * @return the GemFire EvictionAlgorithm represented by this enum. * @see com.gemstone.gemfire.cache.EvictionAlgorithm */ public EvictionAlgorithm getEvictionAlgorithm() { diff --git a/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java b/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java index 5a316721..f5d8489f 100644 --- a/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java +++ b/src/main/java/org/springframework/data/gemfire/EvictionTypeConverter.java @@ -16,54 +16,31 @@ package org.springframework.data.gemfire; -import java.beans.PropertyEditorSupport; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.util.Assert; +import org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport; /** * 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 + * @see org.springframework.data.gemfire.support.AbstractPropertyEditorConverterSupport * @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; - } +public class EvictionTypeConverter extends AbstractPropertyEditorConverterSupport { /** - * Converts the given String value into an appropriate ExpirationActionType. + * Converts the given String into a matching EvictionType. * - * @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. + * @param source the String value to convert into an EvictionType. + * @return the EvictionType matching the given String. + * @throws java.lang.IllegalArgumentException if the String value does not represent a valid EvictionType. * @see org.springframework.data.gemfire.EvictionType#valueOfIgnoreCase(String) + * @see #assertConverted(String, Object, Class) */ @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)); + return assertConverted(source, EvictionType.valueOfIgnoreCase(source), EvictionType.class); } } diff --git a/src/main/java/org/springframework/data/gemfire/ExpirationActionConverter.java b/src/main/java/org/springframework/data/gemfire/ExpirationActionConverter.java new file mode 100644 index 00000000..615e6beb --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/ExpirationActionConverter.java @@ -0,0 +1,50 @@ +/* + * 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.ExpirationAction; + +/** + * The ExpirationActionTypeConverter class is a Spring Converter used to convert a String value into + * a corresponding ExpirationActionType enumerated value. + * + * @author John Blum + * @see java.beans.PropertyEditorSupport + * @see org.springframework.core.convert.converter.Converter + * @see org.springframework.data.gemfire.ExpirationActionType + * @since 1.6.0 + */ +public class ExpirationActionConverter extends AbstractPropertyEditorConverterSupport { + + /** + * Converts the given String into an appropriate GemFire ExpirationAction. + * + * @param source the String to convert into an GemFire ExpirationAction. + * @return an GemFire ExpirationAction value for the given String. + * @throws java.lang.IllegalArgumentException if the String is not a valid GemFire ExpirationAction. + * @see org.springframework.data.gemfire.ExpirationActionType#valueOfIgnoreCase(String) + * @see com.gemstone.gemfire.cache.ExpirationAction + */ + @Override + public ExpirationAction convert(final String source) { + return assertConverted(source, ExpirationActionType.getExpirationAction( + ExpirationActionType.valueOfIgnoreCase(source)), ExpirationAction.class); + } + +} diff --git a/src/main/java/org/springframework/data/gemfire/ExpirationActionType.java b/src/main/java/org/springframework/data/gemfire/ExpirationActionType.java index 6fdbbc50..ae73ec6a 100644 --- a/src/main/java/org/springframework/data/gemfire/ExpirationActionType.java +++ b/src/main/java/org/springframework/data/gemfire/ExpirationActionType.java @@ -19,7 +19,7 @@ package org.springframework.data.gemfire; import com.gemstone.gemfire.cache.ExpirationAction; /** - * The ExpirationActionType enum is a enumeration of GemFire Expiration Actions on expired Cache Region entries. + * The ExpirationActionType enum is a enumeration of GemFire ExpirationActions on expired Cache Region entries. * * @author John Blum * @see com.gemstone.gemfire.cache.ExpirationAction @@ -37,10 +37,9 @@ public enum ExpirationActionType { private final ExpirationAction expirationAction; /** - * Constructs an instance of the ExpirationActionType enum initialized with the corresponding GemFire - * ExpirationAction value. + * Constructs an instance of the ExpirationActionType enum initialized with the matching GemFire ExpirationAction. * - * @param expirationAction the GemFire ExpirationAction value. + * @param expirationAction the matching GemFire ExpirationAction for this enumerated value. * @see com.gemstone.gemfire.cache.ExpirationAction */ ExpirationActionType(final ExpirationAction expirationAction) { @@ -48,10 +47,23 @@ public enum ExpirationActionType { } /** - * Returns the corresponding SDG ExpirationActionType enumerated value given a GemFire ExpirationAction. + * A null-safe operation to extract the corresponding GemFire ExpirationAction for the ExpirationActionType. * - * @param expirationAction the GemFire ExpirationAction instance used to determine the ExpirationActionType. - * @return a ExpirationActionType enumerated value given a GemFire ExpirationAction. + * @param expirationActionType the ExpirationActionType enumerated value from which to extract + * the corresponding GemFire ExpirationAction. + * @return a GemFire ExpirationAction given the ExpirationActionType enumerated value. + * @see com.gemstone.gemfire.cache.ExpirationAction + */ + public static ExpirationAction getExpirationAction(final ExpirationActionType expirationActionType) { + return (expirationActionType != null ? expirationActionType.getExpirationAction() : null); + } + + /** + * Returns the ExpirationActionType enumerated value matching the given GemFire ExpirationAction. + * + * @param expirationAction the GemFire ExpirationAction used to match the ExpirationActionType. + * @return a matching ExpirationActionType enumerated value given a GemFire ExpirationAction + * or null if no match was found. * @see com.gemstone.gemfire.cache.ExpirationAction * @see #getExpirationAction() */ @@ -66,16 +78,17 @@ public enum ExpirationActionType { } /** - * Returns an ExpirationActionType for the given String value describing the enumerated value. + * Returns an ExpirationActionType enumerated value given a named, case-insensitive expiration action. * - * @param value a String value describing the desired ExpirationActionType that is returned. - * @return an ExpirationActionType for the given String. - * @see java.lang.Enum#name() + * @param name a String name for the expiration action matching the ExpirationActionType. + * @return a matching ExpirationActionType for the named, case-insensitive expiration action + * or null if no match could be found. * @see java.lang.String#equalsIgnoreCase(String) + * @see #name() */ - public static ExpirationActionType valueOfIgnoreCase(final String value) { + public static ExpirationActionType valueOfIgnoreCase(final String name) { for (ExpirationActionType expirationActionType : values()) { - if (expirationActionType.name().equalsIgnoreCase(value)) { + if (expirationActionType.name().equalsIgnoreCase(name)) { return expirationActionType; } } @@ -84,9 +97,9 @@ public enum ExpirationActionType { } /** - * Gets the corresponding GemFire ExpirationAction for this enumerated value. + * Gets the matching GemFire ExpirationAction for this enumerated value. * - * @return a GemFire ExpirationAction instance corresponding to this enumerated value. + * @return the GemFire ExpirationAction instance corresponding to this enumerated value. * @see com.gemstone.gemfire.cache.ExpirationAction */ public ExpirationAction getExpirationAction() { diff --git a/src/main/java/org/springframework/data/gemfire/ExpirationActionTypeConverter.java b/src/main/java/org/springframework/data/gemfire/ExpirationActionTypeConverter.java deleted file mode 100644 index b95609c6..00000000 --- a/src/main/java/org/springframework/data/gemfire/ExpirationActionTypeConverter.java +++ /dev/null @@ -1,72 +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 java.beans.PropertyEditorSupport; - -import org.springframework.core.convert.converter.Converter; - -/** - * The ExpirationActionTypeConverter class is a Spring Converter used to convert a String value into - * a corresponding ExpirationActionType enumerated value. - * - * @author John Blum - * @see java.beans.PropertyEditorSupport - * @see org.springframework.core.convert.converter.Converter - * @see org.springframework.data.gemfire.ExpirationActionType - * @since 1.6.0 - */ -public class ExpirationActionTypeConverter extends PropertyEditorSupport implements Converter { - - /* non-Javadoc */ - private ExpirationActionType assertConverted(final String source, final ExpirationActionType expirationActionType) { - if (expirationActionType == null) { - throw new IllegalArgumentException(String.format("Source (%1$s) is not a valid ExpirationActionType!", - source)); - } - - return expirationActionType; - } - - /** - * Sets the ExpirationActionType by parsing a given String. May raise a java.lang.IllegalArgumentException - * if either the String is badly formatted or the text cannot be expressed as a ExpirationActionType. - * - * @param text the String value to express as (convert to) a ExpirationActionType. - * @throws java.lang.IllegalArgumentException if the String value does not represent a valid ExpirationActionType. - * @see #convert(String) - * @see #setValue(Object) - */ - @Override - public void setAsText(final String text) throws IllegalArgumentException { - setValue(convert(text)); - } - - /** - * 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.ExpirationActionType#valueOfIgnoreCase(String) - */ - @Override - public ExpirationActionType convert(final String source) { - return assertConverted(source, ExpirationActionType.valueOfIgnoreCase(source)); - } - -} diff --git a/src/main/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBean.java b/src/main/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBean.java index 96ccb313..d1068c83 100644 --- a/src/main/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBean.java @@ -19,6 +19,7 @@ package org.springframework.data.gemfire; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import com.gemstone.gemfire.cache.ExpirationAction; import com.gemstone.gemfire.cache.ExpirationAttributes; /** @@ -37,14 +38,15 @@ public class ExpirationAttributesFactoryBean implements FactoryBean extends PropertyEditorSupport + implements Converter { + + /** + * Asserts that the given String was successfully converted into an instance of Class type T. + * + * @param source the String to convert. + * @param convertedValue the value of the String converted into instance of Class type T. + * @param type the target Class type of the converted value. + * @return the converted value of Class type T if not null. + * @throws java.lang.IllegalArgumentException if the String could not be converted into + * an instance of Class type T. + */ + protected T assertConverted(final String source, final T convertedValue, final Class type) { + Assert.notNull(convertedValue, String.format("(%1$s) is not a valid %2$s!", source, type.getSimpleName())); + return convertedValue; + } + + /** + * Sets the value of this PropertyEditor with the given String converted to the appropriate Class type. + * + * @param text the String to convert. + * @throws java.lang.IllegalArgumentException if the String could not be converted into + * an instance of Class type T. + * @see #convert(Object) + * @see #setValue(Object) + */ + @Override + public void setAsText(final String text) throws IllegalArgumentException { + setValue(convert(text)); + } + +} 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 612d7b80..1a8481f5 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 @@ -1041,11 +1041,14 @@ Eviction policy for the replicated region. - + +Set to the following Eviction Actions (Note LOCAL_DESTROY is not available for Replicated Regions): + +OVERFLOW_TO_DISK - Entry is overflowed to disk and the value set to null in memory. For Partitioned Regions, +this provides the most reliable read behavior across the region. + ]]> @@ -1271,11 +1274,16 @@ Eviction policy for the partitioned region. - + +Set to one of the following Eviction Actions: + +LOCAL_DESTROY - Entry is destroyed locally. Not available for Replicated Regions. + +OVERFLOW_TO_DISK - Entry is overflowed to disk and the value set to null in memory. For Partitioned Regions, +this provides the most reliable read behavior across the region. + ]]> @@ -1456,11 +1464,16 @@ Eviction policy for the replicated region. - + +Set to one of the following Eviction Actions: + +LOCAL_DESTROY - Entry is destroyed locally. Not available for Replicated Regions. + +OVERFLOW_TO_DISK - Entry is overflowed to disk and the value set to null in memory. For Partitioned Regions, +this provides the most reliable read behavior across the region. + ]]> @@ -1622,28 +1635,6 @@ The threshold (or limit) against which the eviction algorithm runs. Once the thr - - - - - - - - - - - - - - - @@ -1909,11 +1900,16 @@ Eviction policy for the partitioned region. - + +Set to one of the following Eviction Actions: + +LOCAL_DESTROY - Entry is destroyed locally. Not available for Replicated Regions. + +OVERFLOW_TO_DISK - Entry is overflowed to disk and the value set to null in memory. For Partitioned Regions, +this provides the most reliable read behavior across the region. + ]]> diff --git a/src/test/java/org/springframework/data/gemfire/EvictionActionConverterTest.java b/src/test/java/org/springframework/data/gemfire/EvictionActionConverterTest.java new file mode 100644 index 00000000..db9d76be --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/EvictionActionConverterTest.java @@ -0,0 +1,87 @@ +/* + * 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; + +import com.gemstone.gemfire.cache.EvictionAction; + +/** + * The EvictionActionTypeConverterTest class is a test suite of test cases testing the contract and functionality + * of the EvictionActionTypeConverter. + * + * @author John Blum + * @see org.junit.Test + * @see EvictionActionConverter + * @since 1.6.0 + */ +public class EvictionActionConverterTest { + + private EvictionActionConverter converter = new EvictionActionConverter(); + + @After + public void tearDown() { + converter.setValue(null); + } + + @Test + public void testConvert() { + assertEquals(EvictionAction.LOCAL_DESTROY, converter.convert("local_destroy")); + assertEquals(EvictionAction.NONE, converter.convert("None")); + assertEquals(EvictionAction.OVERFLOW_TO_DISK, converter.convert("OverFlow_TO_dIsk")); + } + + @Test(expected = IllegalArgumentException.class) + public void testConvertIllegalValue() { + try { + converter.convert("invalid_value"); + } + catch (IllegalArgumentException expected) { + assertEquals("(invalid_value) is not a valid EvictionAction!", expected.getMessage()); + throw expected; + } + } + + @Test + public void testSetAsText() { + assertNull(converter.getValue()); + converter.setAsText("Local_Destroy"); + assertEquals(EvictionAction.LOCAL_DESTROY, converter.getValue()); + converter.setAsText("overflow_to_disk"); + assertEquals(EvictionAction.OVERFLOW_TO_DISK, converter.getValue()); + } + + @Test(expected = IllegalArgumentException.class) + public void testSetAsTextWithIllegalValue() { + try { + assertNull(converter.getValue()); + converter.setAsText("destroy"); + } + catch (IllegalArgumentException expected) { + assertEquals("(destroy) is not a valid EvictionAction!", expected.getMessage()); + throw expected; + } + finally { + assertNull(converter.getValue()); + } + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java b/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java new file mode 100644 index 00000000..b3bc0493 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/EvictionActionTypeTest.java @@ -0,0 +1,90 @@ +/* + * 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.EvictionAction; + +/** + * The EvictionActionTypeTest class is a test suite of test cases testing the contract and functionality + * of the EvictionActionType enum. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.EvictionActionType + * @see com.gemstone.gemfire.cache.EvictionAction + * @since 1.6.0 + */ +public class EvictionActionTypeTest { + + @Test + public void testStaticGetEvictionAction() { + assertEquals(EvictionAction.LOCAL_DESTROY, EvictionActionType.getEvictionAction( + EvictionActionType.LOCAL_DESTROY)); + assertEquals(EvictionAction.OVERFLOW_TO_DISK, EvictionActionType.getEvictionAction( + EvictionActionType.OVERFLOW_TO_DISK)); + } + + @Test + public void testStaticGetEvictionActionWithNull() { + assertNull(EvictionActionType.getEvictionAction(null)); + } + + @Test + public void testGetEvictionAction() { + assertEquals(EvictionAction.LOCAL_DESTROY, EvictionActionType.LOCAL_DESTROY.getEvictionAction()); + assertEquals(EvictionAction.NONE, EvictionActionType.NONE.getEvictionAction()); + assertEquals(EvictionAction.OVERFLOW_TO_DISK, EvictionActionType.OVERFLOW_TO_DISK.getEvictionAction()); + assertEquals(EvictionAction.DEFAULT_EVICTION_ACTION, EvictionActionType.DEFAULT.getEvictionAction()); + } + + @Test + public void testValueOf() { + assertEquals(EvictionActionType.LOCAL_DESTROY, EvictionActionType.valueOf(EvictionAction.LOCAL_DESTROY)); + assertEquals(EvictionActionType.NONE, EvictionActionType.valueOf(EvictionAction.NONE)); + assertEquals(EvictionActionType.OVERFLOW_TO_DISK, EvictionActionType.valueOf(EvictionAction.OVERFLOW_TO_DISK)); + } + + @Test + public void testValueOfWithInvalidValue() { + assertNull(EvictionActionType.valueOf((EvictionAction) null)); + } + + @Test + public void testValueOfIgnoreCase() { + assertEquals(EvictionActionType.LOCAL_DESTROY, EvictionActionType.valueOfIgnoreCase("Local_Destroy")); + assertEquals(EvictionActionType.NONE, EvictionActionType.valueOfIgnoreCase("none")); + assertEquals(EvictionActionType.NONE, EvictionActionType.valueOfIgnoreCase("NONE")); + assertEquals(EvictionActionType.OVERFLOW_TO_DISK, EvictionActionType.valueOfIgnoreCase("OverFlow_TO_DiSk")); + } + + @Test + public void testValueOfIgnoreCaseWithInvalidValue() { + assertNull(EvictionActionType.valueOfIgnoreCase("REMOTE_DESTROY")); + assertNull(EvictionActionType.valueOfIgnoreCase("All")); + assertNull(EvictionActionType.valueOfIgnoreCase(" none ")); + assertNull(EvictionActionType.valueOfIgnoreCase("underflow_from_disk")); + assertNull(EvictionActionType.valueOfIgnoreCase(" ")); + assertNull(EvictionActionType.valueOfIgnoreCase("")); + assertNull(EvictionActionType.valueOfIgnoreCase(null)); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java index 5d34b8d7..f98b5108 100644 --- a/src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/EvictionAttributesFactoryBeanTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import org.junit.After; @@ -59,6 +60,11 @@ public class EvictionAttributesFactoryBeanTest { mockObjectSizer = null; } + @Test + public void testIsSingleton() { + assertTrue(new EvictionAttributesFactoryBean().isSingleton()); + } + @Test public void testCreateEntryCountEvictionAttributesWithNullAction() { factoryBean.setAction(null); @@ -76,23 +82,6 @@ public class EvictionAttributesFactoryBeanTest { assertEquals(EvictionAlgorithm.LRU_ENTRY, evictionAttributes.getAlgorithm()); } - @Test - public void testCreateEntryCountEvictionAttributesWithNone() { - factoryBean.setAction(EvictionAction.NONE); - factoryBean.setObjectSizer(mockObjectSizer); - factoryBean.setThreshold(null); - factoryBean.setType(EvictionType.ENTRY_COUNT); - factoryBean.afterPropertiesSet(); - - EvictionAttributes evictionAttributes = factoryBean.getObject(); - - assertNotNull(evictionAttributes); - assertEquals(EvictionAction.NONE, evictionAttributes.getAction()); - assertNull(evictionAttributes.getObjectSizer()); - assertEquals(EvictionAttributesFactoryBean.DEFAULT_LRU_MAXIMUM_ENTRIES, evictionAttributes.getMaximum()); - assertEquals(EvictionAlgorithm.LRU_ENTRY, evictionAttributes.getAlgorithm()); - } - @Test public void testCreateEntryCountEvictionAttributesWithLocalDestroy() { factoryBean.setAction(EvictionAction.LOCAL_DESTROY); @@ -110,6 +99,23 @@ public class EvictionAttributesFactoryBeanTest { assertEquals(EvictionAlgorithm.LRU_ENTRY, evictionAttributes.getAlgorithm()); } + @Test + public void testCreateEntryCountEvictionAttributesWithNone() { + factoryBean.setAction(EvictionAction.NONE); + factoryBean.setObjectSizer(mockObjectSizer); + factoryBean.setThreshold(null); + factoryBean.setType(EvictionType.ENTRY_COUNT); + factoryBean.afterPropertiesSet(); + + EvictionAttributes evictionAttributes = factoryBean.getObject(); + + assertNotNull(evictionAttributes); + assertEquals(EvictionAction.NONE, evictionAttributes.getAction()); + assertNull(evictionAttributes.getObjectSizer()); + assertEquals(EvictionAttributesFactoryBean.DEFAULT_LRU_MAXIMUM_ENTRIES, evictionAttributes.getMaximum()); + assertEquals(EvictionAlgorithm.LRU_ENTRY, evictionAttributes.getAlgorithm()); + } + @Test public void testCreateEntryCountEvictionAttributesWithOverflowToDisk() { factoryBean.setAction(EvictionAction.OVERFLOW_TO_DISK); @@ -142,22 +148,6 @@ public class EvictionAttributesFactoryBeanTest { assertEquals(EvictionAlgorithm.LRU_HEAP, evictionAttributes.getAlgorithm()); } - @Test - public void testCreateHeapPercentageEvictionAttributesWithNone() { - factoryBean.setAction(EvictionAction.NONE); - factoryBean.setObjectSizer(mockObjectSizer); - factoryBean.setThreshold(null); - factoryBean.setType(EvictionType.HEAP_PERCENTAGE); - factoryBean.afterPropertiesSet(); - - EvictionAttributes evictionAttributes = factoryBean.getObject(); - - assertNotNull(evictionAttributes); - assertEquals(EvictionAction.NONE, evictionAttributes.getAction()); - assertSame(mockObjectSizer, evictionAttributes.getObjectSizer()); - assertEquals(EvictionAlgorithm.LRU_HEAP, evictionAttributes.getAlgorithm()); - } - @Test public void testCreateHeapPercentageEvictionAttributesWithLocalDestroy() { factoryBean.setAction(EvictionAction.LOCAL_DESTROY); @@ -174,6 +164,22 @@ public class EvictionAttributesFactoryBeanTest { assertEquals(EvictionAlgorithm.LRU_HEAP, evictionAttributes.getAlgorithm()); } + @Test + public void testCreateHeapPercentageEvictionAttributesWithNone() { + factoryBean.setAction(EvictionAction.NONE); + factoryBean.setObjectSizer(mockObjectSizer); + factoryBean.setThreshold(null); + factoryBean.setType(EvictionType.HEAP_PERCENTAGE); + factoryBean.afterPropertiesSet(); + + EvictionAttributes evictionAttributes = factoryBean.getObject(); + + assertNotNull(evictionAttributes); + assertEquals(EvictionAction.NONE, evictionAttributes.getAction()); + assertSame(mockObjectSizer, evictionAttributes.getObjectSizer()); + assertEquals(EvictionAlgorithm.LRU_HEAP, evictionAttributes.getAlgorithm()); + } + @Test public void testCreateHeapPercentageEvictionAttributesWithOverflowToDisk() { factoryBean.setAction(EvictionAction.OVERFLOW_TO_DISK); @@ -200,7 +206,7 @@ public class EvictionAttributesFactoryBeanTest { factoryBean.afterPropertiesSet(); } catch (IllegalArgumentException expected) { - assertEquals("The HEAP_PERCENTAGE (LRU_HEAP algorithm) does not support threshold (a.k.a. maximum)!", + assertEquals("HEAP_PERCENTAGE (LRU_HEAP algorithm) does not support threshold (a.k.a. maximum)!", expected.getMessage()); assertEquals(85, factoryBean.getThreshold().intValue()); assertEquals(EvictionType.HEAP_PERCENTAGE, factoryBean.getType()); @@ -225,23 +231,6 @@ public class EvictionAttributesFactoryBeanTest { assertEquals(EvictionAlgorithm.LRU_MEMORY, evictionAttributes.getAlgorithm()); } - @Test - public void testCreateMemorySizeEvictionAttributesWithNone() { - factoryBean.setAction(EvictionAction.NONE); - factoryBean.setObjectSizer(null); - factoryBean.setThreshold(256); - factoryBean.setType(EvictionType.MEMORY_SIZE); - factoryBean.afterPropertiesSet(); - - EvictionAttributes evictionAttributes = factoryBean.getObject(); - - assertNotNull(evictionAttributes); - assertEquals(EvictionAction.NONE, evictionAttributes.getAction()); - assertNull(evictionAttributes.getObjectSizer()); - assertEquals(256, evictionAttributes.getMaximum()); - assertEquals(EvictionAlgorithm.LRU_MEMORY, evictionAttributes.getAlgorithm()); - } - @Test public void testCreateMemorySizeEvictionAttributesWithLocalDestroy() { factoryBean.setAction(EvictionAction.LOCAL_DESTROY); @@ -259,6 +248,23 @@ public class EvictionAttributesFactoryBeanTest { assertEquals(EvictionAlgorithm.LRU_MEMORY, evictionAttributes.getAlgorithm()); } + @Test + public void testCreateMemorySizeEvictionAttributesWithNone() { + factoryBean.setAction(EvictionAction.NONE); + factoryBean.setObjectSizer(null); + factoryBean.setThreshold(256); + factoryBean.setType(EvictionType.MEMORY_SIZE); + factoryBean.afterPropertiesSet(); + + EvictionAttributes evictionAttributes = factoryBean.getObject(); + + assertNotNull(evictionAttributes); + assertEquals(EvictionAction.NONE, evictionAttributes.getAction()); + assertNull(evictionAttributes.getObjectSizer()); + assertEquals(256, evictionAttributes.getMaximum()); + assertEquals(EvictionAlgorithm.LRU_MEMORY, evictionAttributes.getAlgorithm()); + } + @Test public void testCreateMemorySizeEvictionAttributesWithOverflowToDisk() { factoryBean.setAction(EvictionAction.OVERFLOW_TO_DISK); diff --git a/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java b/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java index a60ae85c..695091fc 100644 --- a/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java +++ b/src/test/java/org/springframework/data/gemfire/EvictionTypeConverterTest.java @@ -49,12 +49,12 @@ public class EvictionTypeConverterTest { } @Test(expected = IllegalArgumentException.class) - public void testConvertWithIllegalValue() { + public void testConvertIllegalValue() { try { converter.convert("LIFO_MEMORY"); } catch (IllegalArgumentException expected) { - assertEquals("Source (LIFO_MEMORY) is not a valid EvictionType!", expected.getMessage()); + assertEquals("(LIFO_MEMORY) is not a valid EvictionType!", expected.getMessage()); throw expected; } } @@ -71,12 +71,16 @@ public class EvictionTypeConverterTest { @Test(expected = IllegalArgumentException.class) public void testSetAsTextWithIllegalValue() { try { + assertNull(converter.getValue()); converter.setAsText("LRU_COUNT"); } catch (IllegalArgumentException expected) { - assertEquals("Source (LRU_COUNT) is not a valid EvictionType!", expected.getMessage()); + assertEquals("(LRU_COUNT) is not a valid EvictionType!", expected.getMessage()); throw expected; } + finally { + assertNull(converter.getValue()); + } } } diff --git a/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java b/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java index d4d89132..52ecbe50 100644 --- a/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java +++ b/src/test/java/org/springframework/data/gemfire/EvictionTypeTest.java @@ -30,17 +30,41 @@ import com.gemstone.gemfire.cache.EvictionAlgorithm; * @author John Blum * @see org.junit.Test * @see org.springframework.data.gemfire.EvictionType + * @see com.gemstone.gemfire.cache.EvictionAlgorithm * @since 1.6.0 */ public class EvictionTypeTest { @Test - @SuppressWarnings("deprecation") + public void testStaticGetEvictionAlgorithm() { + assertEquals(EvictionAlgorithm.LRU_HEAP, EvictionType.getEvictionAlgorithm(EvictionType.HEAP_PERCENTAGE)); + assertEquals(EvictionAlgorithm.LRU_MEMORY, EvictionType.getEvictionAlgorithm(EvictionType.MEMORY_SIZE)); + } + + @Test + public void testStaticGetEvictionAlgorithmWithNull() { + assertNull(EvictionType.getEvictionAlgorithm(null)); + } + + @Test + public void testGetEvictionAlgorithm() { + assertEquals(EvictionAlgorithm.LRU_ENTRY, EvictionType.ENTRY_COUNT.getEvictionAlgorithm()); + assertEquals(EvictionAlgorithm.LRU_HEAP, EvictionType.HEAP_PERCENTAGE.getEvictionAlgorithm()); + assertEquals(EvictionAlgorithm.LRU_MEMORY, EvictionType.MEMORY_SIZE.getEvictionAlgorithm()); + assertEquals(EvictionAlgorithm.NONE, EvictionType.NONE.getEvictionAlgorithm()); + } + + @Test 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)); + } + + @Test + @SuppressWarnings("deprecation") + public void testValueOfInvalidEvictionAlgorithms() { assertNull(EvictionType.valueOf(EvictionAlgorithm.LIFO_ENTRY)); assertNull(EvictionType.valueOf(EvictionAlgorithm.LIFO_MEMORY)); assertNull(EvictionType.valueOf((EvictionAlgorithm) null)); @@ -56,6 +80,7 @@ public class EvictionTypeTest { @Test public void testValueOfIgnoreCaseWithInvalidValues() { + assertNull(EvictionType.valueOfIgnoreCase("number_of_entries")); assertNull(EvictionType.valueOfIgnoreCase("heap_%")); assertNull(EvictionType.valueOfIgnoreCase("mem_size")); assertNull(EvictionType.valueOfIgnoreCase("memory_space")); diff --git a/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeConverterTest.java b/src/test/java/org/springframework/data/gemfire/ExpirationActionConverterTest.java similarity index 59% rename from src/test/java/org/springframework/data/gemfire/ExpirationActionTypeConverterTest.java rename to src/test/java/org/springframework/data/gemfire/ExpirationActionConverterTest.java index 671dfae3..2e014bd2 100644 --- a/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeConverterTest.java +++ b/src/test/java/org/springframework/data/gemfire/ExpirationActionConverterTest.java @@ -17,22 +17,25 @@ 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.ExpirationAction; + /** * The ExpirationActionTypeConverterTest class is a test suite of test cases testing the contract and functionality * of the ExpirationActionTypeConverter class. * * @author John Blum * @see org.junit.Test - * @see org.springframework.data.gemfire.ExpirationActionTypeConverter + * @see ExpirationActionConverter * @since 1.6.0 */ -public class ExpirationActionTypeConverterTest { +public class ExpirationActionConverterTest { - private final ExpirationActionTypeConverter converter = new ExpirationActionTypeConverter(); + private final ExpirationActionConverter converter = new ExpirationActionConverter(); @After public void tearDown() { @@ -41,19 +44,19 @@ public class ExpirationActionTypeConverterTest { @Test public void testConvert() { - assertEquals(ExpirationActionType.DESTROY, converter.convert("destroy")); - assertEquals(ExpirationActionType.INVALIDATE, converter.convert("InvalidAte")); - assertEquals(ExpirationActionType.LOCAL_DESTROY, converter.convert("LOCAL_dEsTrOy")); - assertEquals(ExpirationActionType.LOCAL_INVALIDATE, converter.convert("Local_Invalidate")); + assertEquals(ExpirationAction.DESTROY, converter.convert("destroy")); + assertEquals(ExpirationAction.INVALIDATE, converter.convert("inValidAte")); + assertEquals(ExpirationAction.LOCAL_DESTROY, converter.convert("LOCAL_dEsTrOy")); + assertEquals(ExpirationAction.LOCAL_INVALIDATE, converter.convert("Local_Invalidate")); } @Test(expected = IllegalArgumentException.class) - public void testConvertThrowsIllegalArgumentException() { + public void testConvertIllegalValue() { try { - converter.convert("blow_up"); + converter.convert("invalid_value"); } catch (IllegalArgumentException expected) { - assertEquals("Source (blow_up) is not a valid ExpirationActionType!", expected.getMessage()); + assertEquals("(invalid_value) is not a valid ExpirationAction!", expected.getMessage()); throw expected; } } @@ -61,20 +64,24 @@ public class ExpirationActionTypeConverterTest { @Test public void testSetAsText() { converter.setAsText("InValidAte"); - assertEquals(ExpirationActionType.INVALIDATE, converter.getValue()); + assertEquals(ExpirationAction.INVALIDATE, converter.getValue()); converter.setAsText("Local_Destroy"); - assertEquals(ExpirationActionType.LOCAL_DESTROY, converter.getValue()); + assertEquals(ExpirationAction.LOCAL_DESTROY, converter.getValue()); } @Test(expected = IllegalArgumentException.class) public void testSetAsTextThrowsIllegalArgumentException() { try { + assertNull(converter.getValue()); converter.setAsText("destruction"); } catch (IllegalArgumentException expected) { - assertEquals("Source (destruction) is not a valid ExpirationActionType!", expected.getMessage()); + assertEquals("(destruction) is not a valid ExpirationAction!", expected.getMessage()); throw expected; } + finally { + assertNull(converter.getValue()); + } } } diff --git a/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java b/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java index e5c325e6..1296a7d3 100644 --- a/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java +++ b/src/test/java/org/springframework/data/gemfire/ExpirationActionTypeTest.java @@ -17,7 +17,6 @@ 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; @@ -31,10 +30,31 @@ import com.gemstone.gemfire.cache.ExpirationAction; * @author John Blum * @see org.junit.Test * @see org.springframework.data.gemfire.ExpirationActionType + * @see com.gemstone.gemfire.cache.ExpirationAction * @since 1.6.0 */ public class ExpirationActionTypeTest { + @Test + public void testStaticGetExpirationAction() { + assertEquals(ExpirationAction.DESTROY, ExpirationActionType.getExpirationAction(ExpirationActionType.DESTROY)); + assertEquals(ExpirationAction.LOCAL_DESTROY, ExpirationActionType.getExpirationAction( + ExpirationActionType.LOCAL_DESTROY)); + } + + @Test + public void testStaticGetExpirationActionWithNull() { + assertNull(ExpirationActionType.getExpirationAction(null)); + } + + @Test + public void testGetExpirationAction() { + assertEquals(ExpirationAction.DESTROY, ExpirationActionType.DESTROY.getExpirationAction()); + assertEquals(ExpirationAction.INVALIDATE, ExpirationActionType.INVALIDATE.getExpirationAction()); + assertEquals(ExpirationAction.LOCAL_DESTROY, ExpirationActionType.LOCAL_DESTROY.getExpirationAction()); + assertEquals(ExpirationAction.LOCAL_INVALIDATE, ExpirationActionType.LOCAL_INVALIDATE.getExpirationAction()); + } + @Test public void testDefault() { assertEquals(ExpirationActionType.INVALIDATE, ExpirationActionType.DEFAULT); @@ -54,13 +74,19 @@ public class ExpirationActionTypeTest { public void testValueOfExpirationActionOrdinalValues() { try { for (int ordinal = 0; ordinal < Integer.MAX_VALUE; ordinal++) { - assertNotNull(ExpirationActionType.valueOf(ExpirationAction.fromOrdinal(ordinal))); + ExpirationAction expirationAction = ExpirationAction.fromOrdinal(ordinal); + assertEquals(expirationAction, ExpirationActionType.valueOf(expirationAction).getExpirationAction()); } } catch (ArrayIndexOutOfBoundsException ignore) { } } + @Test + public void testValueOfWithNull() { + assertNull(ExpirationActionType.valueOf((ExpirationAction) null)); + } + @Test public void testValueOfIgnoreCase() { assertEquals(ExpirationActionType.DESTROY, ExpirationActionType.valueOfIgnoreCase("destroy")); @@ -70,9 +96,12 @@ public class ExpirationActionTypeTest { } @Test - public void testValueOfIgnoreCaseWithInvalidStringValues() { + public void testValueOfIgnoreCaseWithInvalidValues() { assertNull(ExpirationActionType.valueOfIgnoreCase("Invalid")); assertNull(ExpirationActionType.valueOfIgnoreCase("local destroy")); + assertNull(ExpirationActionType.valueOfIgnoreCase(" ")); + assertNull(ExpirationActionType.valueOfIgnoreCase("")); + assertNull(ExpirationActionType.valueOfIgnoreCase(null)); } } diff --git a/src/test/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBeanTest.java index 8445cb10..5e02c845 100644 --- a/src/test/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/ExpirationAttributesFactoryBeanTest.java @@ -49,9 +49,9 @@ public class ExpirationAttributesFactoryBeanTest { assertEquals(ExpirationAttributesFactoryBean.DEFAULT_EXPIRATION_ACTION, expirationAttributesFactoryBean.getAction()); - expirationAttributesFactoryBean.setAction(ExpirationActionType.LOCAL_DESTROY); + expirationAttributesFactoryBean.setAction(ExpirationAction.LOCAL_DESTROY); - assertEquals(ExpirationActionType.LOCAL_DESTROY, expirationAttributesFactoryBean.getAction()); + assertEquals(ExpirationAction.LOCAL_DESTROY, expirationAttributesFactoryBean.getAction()); expirationAttributesFactoryBean.setAction(null); @@ -81,7 +81,7 @@ public class ExpirationAttributesFactoryBeanTest { assertNull(expirationAttributesFactoryBean.getObject()); assertEquals(ExpirationAttributes.class, expirationAttributesFactoryBean.getObjectType()); - expirationAttributesFactoryBean.setAction(ExpirationActionType.DESTROY); + expirationAttributesFactoryBean.setAction(ExpirationAction.DESTROY); expirationAttributesFactoryBean.setTimeout(8192); expirationAttributesFactoryBean.afterPropertiesSet(); diff --git a/src/test/resources/org/springframework/data/gemfire/config/RegionEvictionAttributesNamespaceTest-context.xml b/src/test/resources/org/springframework/data/gemfire/config/RegionEvictionAttributesNamespaceTest-context.xml index e84b8bdc..e38ed1e3 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/RegionEvictionAttributesNamespaceTest-context.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/RegionEvictionAttributesNamespaceTest-context.xml @@ -12,6 +12,7 @@ "> + Overflow_To_Disk heap_percentage @@ -46,7 +47,7 @@ - +