SGF-364 - Move EvictionAttributesFactoryBean and SubscriptionAttributesFactoryBean along with supporting enum types from the ..data.gemfire.config package to ..data.gemfire.

This commit is contained in:
John Blum
2015-01-07 18:40:11 -08:00
parent 7bab63703e
commit 8532441fac
18 changed files with 840 additions and 134 deletions

View File

@@ -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<EvictionAttributes>, InitializingBean {
public class EvictionAttributesFactoryBean implements FactoryBean<EvictionAttributes>, 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;

View File

@@ -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;
}
}

View File

@@ -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<String, EvictionType> {
/* 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));
}
}

View File

@@ -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<SubscriptionAttributes>, 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<Subscripti
return true;
}
public InterestPolicy getPolicy() {
return policy;
}
public SubscriptionType getType() {
return type;
}
public void setPolicy(InterestPolicy policy) {
this.policy = policy;
}
public void setType(SubscriptionType type) {
this.type = type;
}
public SubscriptionType getType() {
return (type != null ? type : SubscriptionType.DEFAULT);
}
public void setPolicy(InterestPolicy policy) {
setType(SubscriptionType.valueOf(policy));
}
public InterestPolicy getPolicy() {
return getType().getInterestPolicy();
}
}

View File

@@ -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.InterestPolicy;
/**
* Simple enumeration for the various GemFire Subscription types.
*
* @author Lyndon Adams
* @author John Blum
* @since 1.3.0
*/
// TODO consider renaming this enum to InterestPolicyType
@SuppressWarnings("unused")
public enum SubscriptionType {
ALL(InterestPolicy.ALL),
DEFAULT(InterestPolicy.DEFAULT),
CACHE_CONTENT(InterestPolicy.CACHE_CONTENT);
private final InterestPolicy interestPolicy;
/**
* Constructs an instance of the SubscriptionType enum initialized with the corresponding GemFire
* InterestPolicy instance.
*
* @param interestPolicy a GemFire InterestPolicy corresponding to this SubscriptionType.
*/
SubscriptionType(final InterestPolicy interestPolicy) {
this.interestPolicy = interestPolicy;
}
/**
* Determines the value of the given GemFire InterestPolicy as a SubscriptionType enumerated value.
*
* @param interestPolicy the GemFire InterestPolicy to evaluate.
* @return a SubscriptionType enumerated value for the given GemFire InterestPolicy or null
* if the GemFire InterestPolicy does not correspond to a SubscriptionType enumerated value.
* @see #getInterestPolicy()
* @see com.gemstone.gemfire.cache.InterestPolicy
*/
public static SubscriptionType valueOf(final InterestPolicy interestPolicy) {
for (SubscriptionType subscriptionType : values()) {
if (subscriptionType.getInterestPolicy().equals(interestPolicy)) {
return subscriptionType;
}
}
return null;
}
/**
* Determines the value of the given String as a SubscriptionType enumerated value. The String value's case
* is ignored.
*
* @param value a String value specifying the desired SubscriptionType enumerated value.
* @return a SubscriptionType enumerated value for the given String or null if the String value
* does not evaluate to a SubscriptionType enumerated value.
* @see java.lang.Enum#name()
*/
public static SubscriptionType valueOfIgnoreCase(final String value) {
for (SubscriptionType subscriptionType : values()) {
if (subscriptionType.name().equalsIgnoreCase(value)) {
return subscriptionType;
}
}
return null;
}
/**
* Returns the GemFire InterestPolicy corresponding to this SubscriptionType enumerated value.
*
* @return the GemFire InterestPolicy corresponding to this SubscriptionType enumerated value.
* @see com.gemstone.gemfire.cache.InterestPolicy
*/
public InterestPolicy getInterestPolicy() {
return interestPolicy;
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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 SubscriptionTypeConverter class is a Spring Converter implementation and Java PropertyEditor handling
* the conversion between Strings and SubscriptionType enumerated values.
*
* @author John Blum
* @see java.beans.PropertyEditorSupport
* @see org.springframework.core.convert.converter.Converter
* @see org.springframework.data.gemfire.SubscriptionType
* @since 1.6.0
*/
@SuppressWarnings("unused")
public class SubscriptionTypeConverter extends PropertyEditorSupport implements Converter<String, SubscriptionType> {
/* 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));
}
}

View File

@@ -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
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -26,9 +26,10 @@ import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Parser for &lt;transaction-manager&gt; definitions.
* Parser for &lt;transaction-manager&gt; 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;
}
}

View File

@@ -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
*/

View File

@@ -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;
}
}
}

View File

@@ -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));
}
}

View File

@@ -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()));
}
}

View File

@@ -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;
}
}
}

View File

@@ -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"));
}
}

View File

@@ -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());
}
}