refactored Cachefactory bean to work with Gemfire 6 and some enhancements to region data policy validation

This commit is contained in:
David Turanski
2012-07-25 13:10:08 -04:00
parent 6b8a45acf8
commit a067ddbf48
15 changed files with 558 additions and 62 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2010-2012 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.DataPolicy;
/**
* @author David Turanski
*
*/
public class DataPolicyConverterTest {
DataPolicyConverter converter = new DataPolicyConverter();
@Test
public void test() {
assertEquals(DataPolicy.EMPTY, converter.convert("empty"));
assertNull(converter.convert("invalid"));
assertNull(converter.convert(null));
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2010-2012 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 org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
/**
* @author David Turanski
*
*/
public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig defaultConfig() {
return new RegionFactoryBeanConfig(new LocalRegionFactoryBean(), "default") {
@Override
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.DEFAULT, region.getAttributes().getDataPolicy());
}
@Override
public void configureRegionFactoryBean() {
// TODO Auto-generated method stub
}
};
}
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig emptyConfig() {
return new RegionFactoryBeanConfig(new LocalRegionFactoryBean(), "empty") {
@Override
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.EMPTY, region.getAttributes().getDataPolicy());
}
@Override
public void configureRegionFactoryBean() {
regionFactoryBean.setDataPolicy("empty");
}
};
}
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig invalidConfig() {
return new RegionFactoryBeanConfig(new LocalRegionFactoryBean(), "local-replicate") {
@Override
public void verify() {
assertNotNull(this.exception);
}
@Override
public void configureRegionFactoryBean() {
regionFactoryBean.setDataPolicy("replicate");
}
};
}
@Override
protected void createRegionFactoryBeanConfigs() {
addRFBConfig(defaultConfig());
addRFBConfig(emptyConfig());
addRFBConfig(invalidConfig());
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2010-2012 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 org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
/**
* @author David Turanski
*
*/
public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig defaultConfig() {
return new RegionFactoryBeanConfig(new RegionFactoryBean(), "default") {
@Override
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.DEFAULT, region.getAttributes().getDataPolicy());
}
@Override
public void configureRegionFactoryBean() {
// TODO Auto-generated method stub
}
};
}
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig persistent() {
return new RegionFactoryBeanConfig(new RegionFactoryBean(), "persistent") {
@Override
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.PERSISTENT_REPLICATE, region.getAttributes().getDataPolicy());
}
@Override
public void configureRegionFactoryBean() {
regionFactoryBean.setPersistent(true);
}
};
}
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig invalidPersistence() {
return new RegionFactoryBeanConfig(new RegionFactoryBean(), "invalid-persistence") {
@Override
public void configureRegionFactoryBean() {
regionFactoryBean.setDataPolicy("persistent_replicate");
regionFactoryBean.setPersistent(false);
}
@Override
public void verify() {
assertNotNull(this.exception);
assertEquals("Data policy persistent_replicate is invalid when persistent is false",
exception.getMessage());
}
};
}
@Override
protected void createRegionFactoryBeanConfigs() {
addRFBConfig(defaultConfig());
addRFBConfig(persistent());
addRFBConfig(invalidPersistence());
}
}

View File

@@ -65,7 +65,7 @@ public class LocalRegionNamespaceTest {
private void testPublishingLocal() throws Exception {
assertTrue(context.containsBean("pub"));
RegionFactoryBean fb = context.getBean("&pub", RegionFactoryBean.class);
assertEquals("NORMAL", TestUtils.readField("dataPolicyName", fb));
assertEquals("NORMAL", TestUtils.readField("dataPolicy", fb));
assertEquals(Scope.LOCAL, TestUtils.readField("scope", fb));
assertEquals("publisher", TestUtils.readField("name", fb));
RegionAttributes attrs = TestUtils.readField("attributes", fb);

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2010-2012 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.support;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.RegionFactoryBean;
import com.gemstone.gemfire.cache.GemFireCache;
/**
* @author David Turanski
*
*/
public abstract class AbstractRegionFactoryBeanTest {
protected GemFireCache cache;
Map<String, RegionFactoryBeanConfig> regionFactoryBeanConfigs = new HashMap<String, RegionFactoryBeanConfig>();
@Before
public void setUp() throws Exception {
CacheFactoryBean cfb = new CacheFactoryBean();
cfb.setBeanName("gemfire-cache");
cfb.setUseBeanFactoryLocator(false);
cfb.afterPropertiesSet();
cache = cfb.getObject();
}
@Test
public void testAll() throws Exception {
createRegionFactoryBeanConfigs();
for (RegionFactoryBeanConfig rfbc : regionFactoryBeanConfigs.values()) {
rfbc.test();
}
}
protected void addRFBConfig(RegionFactoryBeanConfig rfbc) {
if (regionFactoryBeanConfigs.containsKey(rfbc.regionName)) {
throw new RuntimeException("duplicate region name " + rfbc.regionName);
}
regionFactoryBeanConfigs.put(rfbc.regionName, rfbc);
}
@After
public void tearDown() {
cache.close();
}
public abstract class RegionFactoryBeanConfig {
@SuppressWarnings("rawtypes")
public final RegionFactoryBean regionFactoryBean;
public final String regionName;
public Exception exception;
@SuppressWarnings("rawtypes")
public RegionFactoryBeanConfig(RegionFactoryBean rfb, String regionName) {
this.regionFactoryBean = rfb;
this.regionName = regionName;
rfb.setBeanName(regionName);
rfb.setCache(cache);
}
public void test() {
configureRegionFactoryBean();
try {
regionFactoryBean.afterPropertiesSet();
}
catch (Exception e) {
this.exception = e;
}
verify();
}
public abstract void configureRegionFactoryBean();
public abstract void verify();
}
protected abstract void createRegionFactoryBeanConfigs();
}