SGF-289 - Enumeration restrictions (xsd:enumeration) should be avoided in the XML schema.

Removed the XSD enumeration restriction on the 'index-update-type' attribute of the 'baseRegionType' element in the Spring GemFire XML Schema (XSD).
This commit is contained in:
John Blum
2014-11-21 19:48:11 -08:00
parent 74755d0654
commit 4a1a3bf64d
11 changed files with 395 additions and 25 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
@@ -908,6 +909,15 @@ public class CacheFactoryBean implements BeanClassLoaderAware, BeanFactoryAware,
return lazyInitialize;
}
/* (non-Javadoc) */
private void initBeanFactory() {
if (getBeanFactory() instanceof ConfigurableBeanFactory) {
((ConfigurableBeanFactory) getBeanFactory()).registerCustomEditor(IndexMaintenanceType.class,
IndexMaintenanceTypeConverter.class);
}
}
/* (non-Javadoc) */
protected void postProcessPropertiesBeforeInitialization(Properties gemfireProperties) {
if (GemfireUtils.isGemfireVersion8OrAbove()) {
gemfireProperties.setProperty("disable-auto-reconnect", String.valueOf(
@@ -917,11 +927,13 @@ public class CacheFactoryBean implements BeanClassLoaderAware, BeanFactoryAware,
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
initBeanFactory();
postProcessPropertiesBeforeInitialization(getProperties());
if (!isLazyInitialize()) {

View File

@@ -21,8 +21,12 @@ import org.springframework.core.convert.converter.Converter;
import com.gemstone.gemfire.cache.DataPolicy;
/**
* The DataPolicyConverter class converts String values into GemFire DataPolicy enumerated values.
*
* @author David Turanski
*
* @author John Blum
* @see org.springframework.core.convert.converter.Converter
* @see com.gemstone.gemfire.cache.DataPolicy
*/
public class DataPolicyConverter implements Converter<String, DataPolicy> {

View File

@@ -0,0 +1,81 @@
/*
* 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.AttributesFactory;
import com.gemstone.gemfire.cache.RegionFactory;
/**
* The IndexMaintenanceType enum is a enumerated type of GemFire Index maintenance update options.
*
* @author John Blum
* @see com.gemstone.gemfire.cache.AttributesFactory#setIndexMaintenanceSynchronous(boolean)
* @see com.gemstone.gemfire.cache.RegionAttributes#getIndexMaintenanceSynchronous()
* @see com.gemstone.gemfire.cache.RegionFactory#setIndexMaintenanceSynchronous(boolean)
* @since 1.6.0
*/
@SuppressWarnings("unused")
public enum IndexMaintenanceType {
SYNCHRONOUS,
ASYNCHRONOUS;
/**
* Determines the appropriate IndexMaintenanceType given a String value. This method is null-safe
* and case-insensitive.
*
* @param value the String value indicating the type of Index maintenance (update).
* @return a IndexMaintenanceType enumerated value based on the given String value, or null
* if the String representation does not match a IndexMaintenanceType.
* @see java.lang.Enum#name()
* @see java.lang.String#equalsIgnoreCase(String)
*/
public static IndexMaintenanceType valueOfIgnoreCase(final String value) {
for (IndexMaintenanceType indexMaintenanceType : values()) {
if (indexMaintenanceType.name().equalsIgnoreCase(value)) {
return indexMaintenanceType;
}
}
return null;
}
/**
* Sets the GemFire AttributesFactory's 'indexMaintenanceSynchronous' property appropriately based on
* this IndexMaintenanceType.
*
* @param attributesFactory the AttributesFactory instance on which to set the indexMaintenanceProperty.
* @throws java.lang.NullPointerException if the AttributesFactory reference is null.
* @see #setIndexMaintenance(com.gemstone.gemfire.cache.RegionFactory)
*/
@SuppressWarnings("deprecation")
public void setIndexMaintenance(final AttributesFactory attributesFactory) {
attributesFactory.setIndexMaintenanceSynchronous(equals(SYNCHRONOUS));
}
/**
* Sets the GemFire RegionFactory's 'indexMaintenanceSynchronous' property appropriately based on
* this IndexMaintenanceType.
*
* @param regionFactory the RegionFactory instance on which to set the indexMaintenanceProperty.
* @throws java.lang.NullPointerException if the RegionFactory reference is null.
* @see #setIndexMaintenance(com.gemstone.gemfire.cache.AttributesFactory)
*/
public void setIndexMaintenance(final RegionFactory regionFactory) {
regionFactory.setIndexMaintenanceSynchronous(equals(SYNCHRONOUS));
}
}

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;
/**
* The IndexMaintenanceTypeConverter class...
*
* @author John Blum
* @see java.beans.PropertyEditorSupport
* @see org.springframework.core.convert.converter.Converter
* @since 1.5.0
*/
@SuppressWarnings("unused")
public class IndexMaintenanceTypeConverter extends PropertyEditorSupport implements Converter<String, IndexMaintenanceType> {
/* (non-Javadoc) */
private IndexMaintenanceType assertConverted(final String source, final IndexMaintenanceType indexMaintenanceType) {
if (indexMaintenanceType == null) {
throw new IllegalArgumentException(String.format("Source (%1$s) is not a valid IndexMaintenanceType!",
source));
}
return indexMaintenanceType;
}
/**
* Sets the IndexMaintenanceType 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 IndexMaintenanceType.
*
* @param text the String value to express (convert) as a IndexMaintenanceType.
* @throws java.lang.IllegalArgumentException if the String value does not represent a valid IndexMaintenanceType.
* @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 IndexMaintenanceType.
*
* @param source the String value to convert into a IndexMaintenanceType.
* @return a IndexMaintenanceType for the given String value.
* @throws java.lang.IllegalArgumentException if the String value does not represent a valid IndexMaintenanceType.
* @see org.springframework.data.gemfire.IndexMaintenanceType#valueOfIgnoreCase(String)
*/
@Override
public IndexMaintenanceType convert(final String source) {
return assertConverted(source, IndexMaintenanceType.valueOfIgnoreCase(source));
}
}

View File

@@ -19,11 +19,11 @@ package org.springframework.data.gemfire;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.RegionAttributes;
/**
* Spring-friendly bean for creating {@link RegionAttributes}. Eliminates the need of using
* a XML 'factory-method' tag.
* Spring-friendly bean for creating {@link RegionAttributes}. Eliminates the need of using a XML 'factory-method' tag.
*
* @author Costin Leau
* @author John Blum
@@ -32,9 +32,9 @@ import com.gemstone.gemfire.cache.RegionAttributes;
* @see com.gemstone.gemfire.cache.AttributesFactory
* @see com.gemstone.gemfire.cache.RegionAttributes
*/
@SuppressWarnings("deprecation")
public class RegionAttributesFactoryBean extends com.gemstone.gemfire.cache.AttributesFactory
implements FactoryBean<RegionAttributes>, InitializingBean {
@SuppressWarnings({ "deprecation", "unused" })
public class RegionAttributesFactoryBean extends AttributesFactory implements FactoryBean<RegionAttributes>,
InitializingBean {
private RegionAttributes attributes;
@@ -53,6 +53,10 @@ public class RegionAttributesFactoryBean extends com.gemstone.gemfire.cache.Attr
return true;
}
public void setIndexUpdateType(final IndexMaintenanceType indexUpdateType) {
indexUpdateType.setIndexMaintenance(this);
}
@Override
public void afterPropertiesSet() throws Exception {
attributes = super.create();

View File

@@ -307,6 +307,7 @@ abstract class ParsingUtils {
setPropertyValue(element, regionAttributesBuilder, "enable-async-conflation");
setPropertyValue(element, regionAttributesBuilder, "enable-subscription-conflation");
setPropertyValue(element, regionAttributesBuilder, "ignore-jta", "ignoreJTA");
setPropertyValue(element, regionAttributesBuilder, "index-update-type");
setPropertyValue(element, regionAttributesBuilder, "initial-capacity");
setPropertyValue(element, regionAttributesBuilder, "is-lock-grantor", "lockGrantor");
setPropertyValue(element, regionAttributesBuilder, "key-constraint");
@@ -315,18 +316,11 @@ abstract class ParsingUtils {
setPropertyValue(element, regionAttributesBuilder, "publisher");
setPropertyValue(element, regionAttributesBuilder, "value-constraint");
String indexUpdateType = element.getAttribute("index-update-type");
if (StringUtils.hasText(indexUpdateType)) {
regionAttributesBuilder.addPropertyValue("indexMaintenanceSynchronous",
"synchronous".equals(indexUpdateType));
}
String concurrencyChecksEnabled = element.getAttribute("concurrency-checks-enabled");
if (StringUtils.hasText(concurrencyChecksEnabled)) {
if (!GemfireUtils.isGemfireVersion7OrAbove()) {
log.warn("Setting 'concurrency-checks-enabled' is only available in Gemfire 7.0 or above");
log.warn("Setting 'concurrency-checks-enabled' is only available in Gemfire 7.0 or above!");
}
else {
ParsingUtils.setPropertyValue(element, regionAttributesBuilder, "concurrency-checks-enabled");

View File

@@ -896,19 +896,13 @@ Specifies if WAN Gateway hub id if enable-gateway is true. (Deprecated since Gem
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="index-update-type" use="optional">
<xsd:attribute name="index-update-type" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Specifies whether Region indexes are maintained synchronously with Region modifications, or asynchronously
in a background thread. GemFire default is synchronous.
]]></xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="asynchronous"/>
<xsd:enumeration value="synchronous"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>

View File

@@ -0,0 +1,81 @@
/*
* 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 IndexMaintenanceTypeConverterTest class is a test suite of test case testing the contract and functionality
* of the IndexMaintenanceTypeConverter.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.IndexMaintenanceTypeConverter
* @since 1.6.0
*/
public class IndexMaintenanceTypeConverterTest {
private final IndexMaintenanceTypeConverter converter = new IndexMaintenanceTypeConverter();
@After
public void tearDown() {
converter.setValue(null);
}
@Test
public void testConvert() {
assertEquals(IndexMaintenanceType.SYNCHRONOUS, converter.convert("Synchronous"));
assertEquals(IndexMaintenanceType.ASYNCHRONOUS, converter.convert("asynchronous"));
}
@Test(expected = IllegalArgumentException.class)
public void testConvertThrowsIllegalArgumentExceptionForInvalidStringValue() {
try {
converter.convert("sync");
}
catch (IllegalArgumentException expected) {
assertEquals("Source (sync) is not a valid IndexMaintenanceType!", expected.getMessage());
throw expected;
}
}
@Test
public void testSetAsText() {
converter.setAsText("aSynchronous");
assertEquals(IndexMaintenanceType.ASYNCHRONOUS, converter.getValue());
converter.setAsText("synchronous");
assertEquals(IndexMaintenanceType.SYNCHRONOUS, converter.getValue());
}
@Test(expected = IllegalArgumentException.class)
public void testSetAsTextThrowsIllegalArgumentException() {
try {
assertNull(converter.getValue());
converter.setAsText("async");
}
catch (IllegalArgumentException expected) {
assertEquals("Source (async) is not a valid IndexMaintenanceType!", expected.getMessage());
throw expected;
}
}
}

View File

@@ -0,0 +1,103 @@
/*
* 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 static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.RegionFactory;
/**
* The IndexMaintenanceTypeTest class is a test suite of test cases testing the contract and functionality of the
* IndexMaintenanceType enum type.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.IndexMaintenanceType
* @since 1.6.0
*/
public class IndexMaintenanceTypeTest {
@Test
public void testValueOfIgnoreCase() {
assertEquals(IndexMaintenanceType.SYNCHRONOUS, IndexMaintenanceType.valueOfIgnoreCase("SYNCHRONOUS"));
assertEquals(IndexMaintenanceType.SYNCHRONOUS, IndexMaintenanceType.valueOfIgnoreCase("Synchronous"));
assertEquals(IndexMaintenanceType.SYNCHRONOUS, IndexMaintenanceType.valueOfIgnoreCase("synchronous"));
assertEquals(IndexMaintenanceType.SYNCHRONOUS, IndexMaintenanceType.valueOfIgnoreCase("SynCHrOnOus"));
assertEquals(IndexMaintenanceType.ASYNCHRONOUS, IndexMaintenanceType.valueOfIgnoreCase("ASYNChronous"));
}
@Test
public void testValueOfIgnoreCaseIsNull() {
assertNull(IndexMaintenanceType.valueOfIgnoreCase("synchronicity"));
assertNull(IndexMaintenanceType.valueOfIgnoreCase("SYNC"));
assertNull(IndexMaintenanceType.valueOfIgnoreCase("ASYNC"));
assertNull(IndexMaintenanceType.valueOfIgnoreCase("CONCURRENT"));
assertNull(IndexMaintenanceType.valueOfIgnoreCase("parallel"));
assertNull(IndexMaintenanceType.valueOfIgnoreCase(" "));
assertNull(IndexMaintenanceType.valueOfIgnoreCase(""));
assertNull(IndexMaintenanceType.valueOfIgnoreCase(null));
}
@Test
@SuppressWarnings("deprecation")
public void testAttributesFactorySetIndexMaintenanceAsynchronous() {
AttributesFactory mockAttributesFactory = mock(AttributesFactory.class,
"testAttributesFactorySetIndexMaintenanceAsynchronous");
IndexMaintenanceType.ASYNCHRONOUS.setIndexMaintenance(mockAttributesFactory);
verify(mockAttributesFactory).setIndexMaintenanceSynchronous(eq(false));
}
@Test
@SuppressWarnings("deprecation")
public void testAttributesFactorySetIndexMaintenanceSynchronous() {
AttributesFactory mockAttributesFactory = mock(AttributesFactory.class,
"testAttributesFactorySetIndexMaintenanceAsynchronous");
IndexMaintenanceType.SYNCHRONOUS.setIndexMaintenance(mockAttributesFactory);
verify(mockAttributesFactory).setIndexMaintenanceSynchronous(eq(true));
}
@Test
public void testRegionFactorySetIndexMaintenanceAsynchronous() {
RegionFactory mockRegionFactory = mock(RegionFactory.class, "testRegionFactorySetIndexMaintenanceAsynchronous");
IndexMaintenanceType.ASYNCHRONOUS.setIndexMaintenance(mockRegionFactory);
verify(mockRegionFactory).setIndexMaintenanceSynchronous(eq(false));
}
@Test
public void testRegionFactorySetIndexMaintenanceSynchronous() {
RegionFactory mockRegionFactory = mock(RegionFactory.class, "testRegionFactorySetIndexMaintenanceSynchronous");
IndexMaintenanceType.SYNCHRONOUS.setIndexMaintenance(mockRegionFactory);
verify(mockRegionFactory).setIndexMaintenanceSynchronous(eq(true));
}
}

View File

@@ -147,6 +147,21 @@ public class ReplicatedRegionNamespaceTest {
assertEquals(String.class, regionAttributes.getValueConstraint());
}
@Test
public void testReplicatedWithSynchronousIndexUpdates() {
assertTrue(context.containsBean("replicated-with-synchronous-index-updates"));
Region region = context.getBean("replicated-with-synchronous-index-updates", Region.class);
assertNotNull(String.format("The '%1$s' Region was not properly configured and initialized!",
"replicated-with-synchronous-index-updates"), region);
RegionAttributes regionAttributes = region.getAttributes();
assertNotNull(regionAttributes);
assertTrue(regionAttributes.getIndexMaintenanceSynchronous());
}
@Test
@SuppressWarnings("rawtypes")
public void testRegionLookup() throws Exception {

View File

@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
" default-lazy-init="true">
@@ -31,6 +33,10 @@
<gfe:cache-writer ref="c-writer"/>
</gfe:replicated-region>
<bean id="c-listener" class="org.springframework.data.gemfire.SimpleCacheListener"/>
<bean id="c-loader" class="org.springframework.data.gemfire.SimpleCacheLoader"/>
<bean id="c-writer" class="org.springframework.data.gemfire.SimpleCacheWriter"/>
<gfe:replicated-region id="replicated-with-attributes"
cloning-enabled="false"
concurrency-level="10"
@@ -47,13 +53,18 @@
scope="global"
value-constraint="java.lang.String"/>
<util:properties id="regionConfigurationSettings">
<prop key="gemfire.index-update-type">synchronous</prop>
</util:properties>
<context:property-placeholder properties-ref="regionConfigurationSettings"/>
<gfe:replicated-region id="replicated-with-synchronous-index-updates" index-update-type="${gemfire.index-update-type}"/>
<gfe:replicated-region id="Compressed" persistent="false">
<gfe:compressor ref="testCompressor"/>
</gfe:replicated-region>
<bean id="c-listener" class="org.springframework.data.gemfire.SimpleCacheListener"/>
<bean id="c-loader" class="org.springframework.data.gemfire.SimpleCacheLoader"/>
<bean id="c-writer" class="org.springframework.data.gemfire.SimpleCacheWriter"/>
<bean id="testCompressor" class="org.springframework.data.gemfire.config.ReplicatedRegionNamespaceTest$TestCompressor" p:name="XYZ"/>
<gfe:lookup-region id="lookup" name="existing"/>