SGF-381 - Enable RegionFactoryBean to respect the Data Policy specified on a nested or referenced RegionAttributes bean definition.

This commit is contained in:
John Blum
2015-03-09 23:35:16 -07:00
parent 396d69c901
commit 3063046e53
10 changed files with 486 additions and 224 deletions

View File

@@ -35,12 +35,11 @@ import com.gemstone.gemfire.cache.RegionShortcut;
* The PartitionedRegionFactoryBeanTest class is a test suite of test cases testing the component functionality
* and correct behavior of the PartitionedRegionFactoryBean class.
*
*
* @author David Turanski
* @author John Blum
* @see org.mockito.Mockito
* @see org.junit.Test
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
* @see org.springframework.data.gemfire.LocalRegionFactoryBean
* @since 1.3.x
*/
@SuppressWarnings("unchecked")
@@ -83,8 +82,8 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Override
protected void createRegionFactoryBeanConfigs() {
addRFBConfig(defaultConfig());
addRFBConfig(invalidConfig());
add(defaultConfig());
add(invalidConfig());
}
protected RegionFactory<?, ?> createMockRegionFactory() {

View File

@@ -13,6 +13,7 @@
* 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;
@@ -36,6 +37,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Test;
import org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest;
import org.springframework.data.gemfire.test.support.ArrayUtils;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CustomExpiry;
@@ -82,13 +84,13 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
private RegionFactoryBeanConfig defaultConfig() {
return new RegionFactoryBeanConfig(new RegionFactoryBean(), "default") {
@Override
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.DEFAULT, region.getAttributes().getDataPolicy());
public void configureRegionFactoryBean() {
}
@Override
public void configureRegionFactoryBean() {
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.DEFAULT, region.getAttributes().getDataPolicy());
}
};
}
@@ -97,14 +99,14 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
private RegionFactoryBeanConfig persistentConfig() {
return new RegionFactoryBeanConfig(new RegionFactoryBean(), "persistent") {
@Override
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.PERSISTENT_REPLICATE, region.getAttributes().getDataPolicy());
public void configureRegionFactoryBean() {
regionFactoryBean.setPersistent(true);
}
@Override
public void configureRegionFactoryBean() {
regionFactoryBean.setPersistent(true);
public void verify() {
Region region = regionFactoryBean.getRegion();
assertEquals(DataPolicy.PERSISTENT_REPLICATE, region.getAttributes().getDataPolicy());
}
};
}
@@ -129,14 +131,15 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Override
protected void createRegionFactoryBeanConfigs() {
addRFBConfig(defaultConfig());
addRFBConfig(persistentConfig());
addRFBConfig(invalidPersistentConfig());
add(defaultConfig());
add(persistentConfig());
add(invalidPersistentConfig());
}
protected PartitionAttributes createPartitionAttributes(final String colocatedWith, final int localMaxMemory,
final long recoveryDelay, final int redundantCopies, final long startupRecoveryDelay,
final long totalMaxMemory, final int totalNumberOfBuckets) throws Exception {
final long recoveryDelay, final int redundantCopies, final long startupRecoveryDelay,
final long totalMaxMemory, final int totalNumberOfBuckets) throws Exception {
PartitionAttributesFactoryBean partitionAttributesFactoryBean = new PartitionAttributesFactoryBean();
partitionAttributesFactoryBean.setColocatedWith(colocatedWith);
@@ -151,6 +154,12 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
return partitionAttributesFactoryBean.getObject();
}
protected RegionAttributes createMockRegionAttributes(final DataPolicy... dataPolicies) {
RegionAttributes mockRegionAttributes = mock(RegionAttributes.class);
when(mockRegionAttributes.getDataPolicy()).thenReturn(ArrayUtils.getFirst(DataPolicy.DEFAULT, dataPolicies));
return mockRegionAttributes;
}
protected RegionFactory<?, ?> createMockRegionFactory() {
return mock(RegionFactory.class);
}
@@ -189,7 +198,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
}
@Test(expected = IllegalArgumentException.class)
public void testAssertPersistentDataPolicyWithNotPersistentAttribute() {
public void testAssertPersistentDataPolicyWithNonPersistentAttribute() {
try {
RegionFactoryBean<?, ?> factoryBean = new RegionFactoryBean<Object, Object>();
factoryBean.setPersistent(false);
@@ -202,21 +211,6 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
}
}
@Test
public void testIsPersistentUnspecified() {
RegionFactoryBean<?, ?> factoryBean = new RegionFactoryBean<Object, Object>();
assertTrue(factoryBean.isPersistentUnspecified());
factoryBean.setPersistent(false);
assertFalse(factoryBean.isPersistentUnspecified());
factoryBean.setPersistent(true);
assertFalse(factoryBean.isPersistentUnspecified());
}
@Test
public void testIsPersistent() {
RegionFactoryBean<?, ?> factoryBean = new RegionFactoryBean<Object, Object>();
@@ -232,6 +226,25 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
assertTrue(factoryBean.isPersistent());
}
@Test
public void testIsPersistentUnspecified() {
RegionFactoryBean<?, ?> factoryBean = new RegionFactoryBean<Object, Object>();
assertTrue(factoryBean.isPersistentUnspecified());
factoryBean.setPersistent(false);
assertFalse(factoryBean.isPersistentUnspecified());
factoryBean.setPersistent(true);
assertFalse(factoryBean.isPersistentUnspecified());
factoryBean.setPersistent(null);
assertTrue(factoryBean.isPersistentUnspecified());
}
@Test
public void testIsNotPersistent() {
RegionFactoryBean<?, ?> factoryBean = new RegionFactoryBean<Object, Object>();
@@ -250,6 +263,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Test
public void testCreateRegionFactoryWithShortcut() {
Cache mockCache = mock(Cache.class);
RegionAttributes mockRegionAttributes = mock(RegionAttributes.class);
final RegionFactory mockRegionFactory = createMockRegionFactory();
@@ -284,31 +298,27 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
assertSame(mockRegionFactory, factoryBean.createRegionFactory(mockCache));
assertTrue(setDataPolicyCalled.get());
verify(mockCache).createRegionFactory(eq(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW));
verify(mockCache, times(1)).createRegionFactory(eq(RegionShortcut.PARTITION_REDUNDANT_PERSISTENT_OVERFLOW));
}
@Test
public void testCreateRegionFactoryWithAttributes() {
Cache mockCache = mock(Cache.class);
RegionAttributes mockRegionAttributes = mock(RegionAttributes.class);
final RegionFactory mockRegionFactory = createMockRegionFactory();
when(mockCache.createRegionFactory(eq(mockRegionAttributes))).thenReturn(mockRegionFactory);
RegionFactoryBean factoryBean = new RegionFactoryBean() {
@Override
protected RegionFactory mergeRegionAttributes(RegionFactory regionFactory, RegionAttributes regionAttributes) {
return mockRegionFactory;
}
};
RegionFactoryBean factoryBean = new RegionFactoryBean();
factoryBean.setAttributes(mockRegionAttributes);
factoryBean.setShortcut(null);
assertSame(mockRegionFactory, factoryBean.createRegionFactory(mockCache));
verify(mockCache).createRegionFactory(eq(mockRegionAttributes));
verify(mockCache, times(1)).createRegionFactory(eq(mockRegionAttributes));
}
@Test
@@ -319,12 +329,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
when(mockCache.createRegionFactory()).thenReturn(mockRegionFactory);
RegionFactoryBean factoryBean = new RegionFactoryBean() {
@Override
protected RegionFactory mergeRegionAttributes(RegionFactory regionFactory, RegionAttributes regionAttributes) {
return mockRegionFactory;
}
};
RegionFactoryBean factoryBean = new RegionFactoryBean();
factoryBean.setAttributes(null);
factoryBean.setShortcut(null);
@@ -373,7 +378,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
when(mockRegionAttributes.getSubscriptionAttributes()).thenReturn(testSubscriptionAttributes);
RegionFactoryBean factoryBean = new RegionFactoryBean() {
@Override boolean hasUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) {
@Override boolean isUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) {
return true;
}
@@ -479,7 +484,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
when(mockRegionAttributes.getSubscriptionAttributes()).thenReturn(null);
RegionFactoryBean factoryBean = new RegionFactoryBean() {
@Override boolean hasUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) {
@Override boolean isUserSpecifiedEvictionAttributes(final RegionAttributes regionAttributes) {
return false;
}
@@ -712,9 +717,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, " ");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy ' ' is invalid.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy ' ' is invalid.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -731,9 +736,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy '' is invalid.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy '' is invalid.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -750,9 +755,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "CSV");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'CSV' is invalid.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'CSV' is invalid.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -784,9 +789,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "EMPTY");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'EMPTY' is invalid when persistent is true.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'EMPTY' is invalid when persistent is true.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -817,11 +822,10 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
try {
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_PARTITION");
fail("Setting the 'persistent' attribute to FALSE and 'Data Policy' to PERSISTENT_PARTITION should have thrown an IllegalArgumentException!");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -838,11 +842,12 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
try {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "PARTITION");
fail("Setting the 'persistent' attribute to TRUE and 'Data Policy' to PARTITION should have thrown an IllegalArgumentException!");
fail(
"Setting the 'persistent' attribute to TRUE and 'Data Policy' to PARTITION should have thrown an IllegalArgumentException!");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PARTITION' is invalid when persistent is true.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'PARTITION' is invalid when persistent is true.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -869,9 +874,84 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
@Test
public void testResolveDataPolicyWhenPersistentUnspecifiedAndRegionAttributesPreloadedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setAttributes(createMockRegionAttributes(DataPolicy.PRELOADED));
factoryBean.setDataPolicy((DataPolicy) null);
factoryBean.resolveDataPolicy(mockRegionFactory, null, (String) null);
verify(mockRegionFactory, times(1)).setDataPolicy(eq(DataPolicy.PRELOADED));
assertEquals(DataPolicy.PRELOADED, factoryBean.getDataPolicy());
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndRegionAttributesPartitionDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setAttributes(createMockRegionAttributes(DataPolicy.PARTITION));
factoryBean.setDataPolicy((DataPolicy) null);
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, (String) null);
verify(mockRegionFactory, times(1)).setDataPolicy(eq(DataPolicy.PARTITION));
assertEquals(DataPolicy.PARTITION, factoryBean.getDataPolicy());
}
@Test
public void testResolveDataPolicyWhenPersistentAndRegionAttributesPersistentPartitionDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setAttributes(createMockRegionAttributes(DataPolicy.PERSISTENT_PARTITION));
factoryBean.setDataPolicy((DataPolicy) null);
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, (String) null);
verify(mockRegionFactory, times(1)).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
assertEquals(DataPolicy.PERSISTENT_PARTITION, factoryBean.getDataPolicy());
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenNotPersistentAndRegionAttributesPersistentPartitionDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setAttributes(createMockRegionAttributes(DataPolicy.PERSISTENT_PARTITION));
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, (String) null);
}
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenPersistentAndRegionAttributesPartitionDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setAttributes(createMockRegionAttributes(DataPolicy.PARTITION));
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, (String) null);
}
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'PARTITION' is invalid when persistent is true.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}
@Test
public void testResolveDataPolicyWhenPersistentUnspecifiedAndUnspecifiedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setAttributes(createMockRegionAttributes());
factoryBean.setPersistent(null);
factoryBean.resolveDataPolicy(mockRegionFactory, null, (DataPolicy) null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT));
@@ -880,6 +960,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Test
public void testResolveDataPolicyWhenNotPersistentAndUnspecifiedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setAttributes(createMockRegionAttributes());
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, (DataPolicy) null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT));
@@ -888,6 +969,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Test
public void testResolveDataPolicyWhenPersistentAndUnspecifiedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setAttributes(createMockRegionAttributes());
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, (DataPolicy) null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
@@ -916,9 +998,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
factoryBean.resolveDataPolicy(mockRegionFactory, false, DataPolicy.PERSISTENT_REPLICATE);
fail("Setting the 'persistent' attribute to FALSE and 'Data Policy' to PERSISTENT_REPLICATE should have thrown an IllegalArgumentException!");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PERSISTENT_REPLICATE' is invalid when persistent is false.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'PERSISTENT_REPLICATE' is invalid when persistent is false.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
@@ -937,9 +1019,9 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
factoryBean.resolveDataPolicy(mockRegionFactory, true, "REPLICATE");
fail("Setting the 'persistent' attribute to TRUE and 'Data Policy' to REPLICATE should have thrown an IllegalArgumentException!");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'REPLICATE' is invalid when persistent is true.", e.getMessage());
throw e;
catch (IllegalArgumentException expected) {
assertEquals("Data Policy 'REPLICATE' is invalid when persistent is true.", expected.getMessage());
throw expected;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);

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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
/**
* The RawRegionBeanDefinitionTest class is a test suite of test cases testing the contract and functionality
* of the SDG RegionFactoryBean class, and specifically the specification of the GemFire Region DataPolicy,
* when used as raw bean definition in Spring XML configuration meta-data.
*
* @author John Blum
* @see org.springframework.data.gemfire.PartitionAttributesFactoryBean
* @see org.springframework.data.gemfire.RegionAttributesFactoryBean
* @see org.springframework.data.gemfire.RegionFactoryBean
* @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.DataPolicy
* @see com.gemstone.gemfire.cache.Region
* @since 1.6.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = GemfireTestApplicationContextInitializer.class)
@SuppressWarnings("unused")
public class RegionDefinitionUsingBeansNamespaceTest {
@Resource(name = "Example")
private Region<?, ?> example;
@Test
public void testExampleRegionBeanDefinitionConfiguration() {
assertNotNull("The 'Example' Region was not properly configured and initialized!", example);
assertEquals("Example", example.getName());
assertEquals("/Example", example.getFullPath());
assertNotNull(example.getAttributes());
assertEquals(DataPolicy.PERSISTENT_PARTITION, example.getAttributes().getDataPolicy());
assertTrue(example.getAttributes().getStatisticsEnabled());
assertNotNull(example.getAttributes().getPartitionAttributes());
assertEquals(1, example.getAttributes().getPartitionAttributes().getRedundantCopies());
assertEquals(0, example.getAttributes().getPartitionAttributes().getRecoveryDelay());
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.support;
import java.io.File;
@@ -31,33 +32,18 @@ import com.gemstone.gemfire.cache.GemFireCache;
/**
* @author David Turanski
*
* @author John Blum
*/
public abstract class AbstractRegionFactoryBeanTest {
protected GemFireCache cache;
Map<String, RegionFactoryBeanConfig> regionFactoryBeanConfigs = new HashMap<String, RegionFactoryBeanConfig>();
private GemFireCache cache;
@Before
public void setUp() throws Exception {
cache = new StubCache();
}
@Test
public void testAll() throws Exception {
createRegionFactoryBeanConfigs();
for (RegionFactoryBeanConfig rfbc : regionFactoryBeanConfigs.values()) {
rfbc.test();
}
}
private Map<String, RegionFactoryBeanConfig> regionFactoryBeanConfigs = new HashMap<String, RegionFactoryBeanConfig>();
@AfterClass
public static void cleanUp() {
for (String name : new File(".").list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
@Override public boolean accept(File dir, String name) {
return name.startsWith("BACKUP");
}
})) {
@@ -65,49 +51,69 @@ public abstract class AbstractRegionFactoryBeanTest {
}
}
protected void addRFBConfig(RegionFactoryBeanConfig rfbc) {
if (regionFactoryBeanConfigs.containsKey(rfbc.regionName)) {
throw new RuntimeException("duplicate region name " + rfbc.regionName);
}
regionFactoryBeanConfigs.put(rfbc.regionName, rfbc);
@Before
public void setUp() throws Exception {
cache = new StubCache();
}
@After
public void tearDown() {
cache.close();
cache = null;
}
@Test
public void testAll() throws Exception {
createRegionFactoryBeanConfigs();
for (RegionFactoryBeanConfig regionFactoryBeanConfig : regionFactoryBeanConfigs.values()) {
regionFactoryBeanConfig.test();
}
}
protected void add(RegionFactoryBeanConfig regionFactoryBeanConfig) {
if (regionFactoryBeanConfigs.containsKey(regionFactoryBeanConfig.regionName)) {
throw new RuntimeException("duplicate region name " + regionFactoryBeanConfig.regionName);
}
regionFactoryBeanConfigs.put(regionFactoryBeanConfig.regionName, regionFactoryBeanConfig);
}
protected abstract void createRegionFactoryBeanConfigs();
public abstract class RegionFactoryBeanConfig {
public Exception exception;
@SuppressWarnings("rawtypes")
public final RegionFactoryBean regionFactoryBean;
public final String regionName;
public Exception exception;
@SuppressWarnings("rawtypes")
public RegionFactoryBeanConfig(RegionFactoryBean rfb, String regionName) {
this.regionFactoryBean = rfb;
public RegionFactoryBeanConfig(RegionFactoryBean regionFactoryBean, String regionName) {
this.regionFactoryBean = regionFactoryBean;
this.regionName = regionName;
rfb.setBeanName(regionName);
rfb.setCache(cache);
regionFactoryBean.setBeanName(regionName);
regionFactoryBean.setCache(cache);
}
public abstract void configureRegionFactoryBean();
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();
}

View File

@@ -21,9 +21,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.lang.reflect.Field;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.util.ReflectionUtils;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.CacheLoader;
@@ -79,6 +81,10 @@ public class MockRegionFactory<K,V> {
final RegionFactory<K, V> regionFactory = mock(RegionFactory.class);
Field attrsFactory = ReflectionUtils.findField(RegionFactory.class, "attrsFactory");
ReflectionUtils.makeAccessible(attrsFactory);
ReflectionUtils.setField(attrsFactory, regionFactory, attributesFactory);
when(regionFactory.create(anyString())).thenAnswer(new Answer<Region>() {
@Override public Region answer(InvocationOnMock invocation) throws Throwable {
String name = (String) invocation.getArguments()[0];

View File

@@ -0,0 +1,44 @@
/*
* 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.test.support;
/**
* ArrayUtils is a utility class for working with Java arrays.
*
* @author John Blum
* @since 1.6.0
*/
@SuppressWarnings("unused")
public class ArrayUtils {
public static <T> T getFirst(T... array) {
return getFirst(null, array);
}
public static <T> T getFirst(T defaultValue, T... array) {
return (isEmpty(array) ? defaultValue : array[0]);
}
public static boolean isEmpty(final Object... array) {
return (array == null || array.length == 0);
}
public static int length(final Object... array) {
return (array != null ? array.length : 0);
}
}