Merged PR #33 to fix JIRA issue SGF-203 concerning persistence.

This commit is contained in:
John Blum
2013-10-31 16:41:32 -07:00
23 changed files with 1535 additions and 402 deletions

View File

@@ -24,15 +24,47 @@ import com.gemstone.gemfire.cache.DataPolicy;
/**
* @author David Turanski
*
* @author John Blum
*/
public class DataPolicyConverterTest {
DataPolicyConverter converter = new DataPolicyConverter();
private final DataPolicyConverter converter = new DataPolicyConverter();
protected int getDataPolicyEnumerationSize() {
for (byte ordinal = 0; ordinal < Byte.MAX_VALUE; ordinal++) {
try {
DataPolicy.fromOrdinal(ordinal);
}
catch (Exception e) {
return ordinal;
}
}
throw new IndexOutOfBoundsException("The size of the Data Policy enumeration could not be determined"
+ " because the ordinal based on Byte.MAX_VALUE was exhausted!");
}
@Test
public void test() {
public void testPolicyToDataPolicy() {
// exclude DEFAULT
assertEquals(getDataPolicyEnumerationSize(), DataPolicyConverter.Policy.values().length - 1);
assertEquals(DataPolicy.EMPTY, DataPolicyConverter.Policy.EMPTY.toDataPolicy());
assertEquals(DataPolicy.NORMAL, DataPolicyConverter.Policy.NORMAL.toDataPolicy());
assertEquals(DataPolicy.PRELOADED, DataPolicyConverter.Policy.PRELOADED.toDataPolicy());
assertEquals(DataPolicy.PARTITION, DataPolicyConverter.Policy.PARTITION.toDataPolicy());
assertEquals(DataPolicy.PERSISTENT_PARTITION, DataPolicyConverter.Policy.PERSISTENT_PARTITION.toDataPolicy());
assertEquals(DataPolicy.REPLICATE, DataPolicyConverter.Policy.REPLICATE.toDataPolicy());
assertEquals(DataPolicy.PERSISTENT_REPLICATE, DataPolicyConverter.Policy.PERSISTENT_REPLICATE.toDataPolicy());
assertEquals(DataPolicy.DEFAULT, DataPolicyConverter.Policy.DEFAULT.toDataPolicy());
}
@Test
public void testConvert() {
assertEquals(DataPolicy.EMPTY, converter.convert("empty"));
assertEquals(DataPolicy.PARTITION, converter.convert("Partition"));
assertEquals(DataPolicy.PERSISTENT_REPLICATE, converter.convert("PERSISTENT_REPLICATE"));
assertNull(converter.convert("invalid"));
assertNull(converter.convert(null));
}
}

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.Assume.assumeTrue;
import org.junit.Test;
import com.gemstone.gemfire.internal.GemFireVersion;
/**
* The GemfireUtilsTest class is a test suite of test cases testing the contract and functionality of the GemfireUtils
* abstract utility class.
* <p/>
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.GemfireUtils
* @since 1.3.3
*/
public class GemfireUtilsTest {
// NOTE implementation is based on a GemFire internal class... com.gemstone.gemfire.internal.GemFireVersion.
protected int getGemFireVersion() {
try {
String gemfireVersion = GemFireVersion.getGemFireVersion();
StringBuilder buffer = new StringBuilder();
buffer.append(GemFireVersion.getMajorVersion(gemfireVersion));
buffer.append(GemFireVersion.getMinorVersion(gemfireVersion));
return Integer.decode(buffer.toString());
}
catch (NumberFormatException ignore) {
return -1;
}
}
@Test
public void testIsGemfireVersion65OrAbove() {
int gemfireVersion = getGemFireVersion();
assumeTrue(gemfireVersion > -1);
assertEquals(getGemFireVersion() >= 65, GemfireUtils.isGemfireVersion65OrAbove());
}
@Test
public void testIsGemfireVersion7OrAbove() {
int gemfireVersion = getGemFireVersion();
assumeTrue(gemfireVersion > -1);
assertEquals(getGemFireVersion() >= 70, GemfireUtils.isGemfireVersion7OrAbove());
}
}

View File

@@ -17,18 +17,34 @@ package org.springframework.data.gemfire;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionFactory;
/**
* The PartitionedRegionFactoryBeanTest class is a test suite of test cases testing the component functionality
* and correct behavior of the PartitionedRegionFactoryBean class.
* <p/>
* @author David Turanski
*
* @author John Blum
* @see org.mockito.Mockito
* @see org.junit.Test
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
* @since 1.3.x
*/
@SuppressWarnings("unchecked")
public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
private final LocalRegionFactoryBean factoryBean = new LocalRegionFactoryBean();
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig defaultConfig() {
return new RegionFactoryBeanConfig(new LocalRegionFactoryBean(), "default") {
@@ -42,24 +58,6 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@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");
}
};
}
@@ -83,7 +81,102 @@ public class LocalRegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Override
protected void createRegionFactoryBeanConfigs() {
addRFBConfig(defaultConfig());
addRFBConfig(emptyConfig());
addRFBConfig(invalidConfig());
}
protected RegionFactory<?, ?> createMockRegionFactory() {
return mock(RegionFactory.class);
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithInvalidDataPolicyName() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.resolveDataPolicy(mockRegionFactory, null, "INVALID_DATA_POLICY_NAME");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'INVALID_DATA_POLICY_NAME' is invalid.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED));
}
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithInvalidDataPolicyRegionType() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.resolveDataPolicy(mockRegionFactory, null, "PARTITION");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PARTITION' is not supported in Local Regions.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.NORMAL));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PRELOADED));
}
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL));
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndNormalDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "Normal");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL));
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndNormalDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "NORMAL");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL));
}
@Test
public void testResolveDataPolicyWhenPersistentAndNormalDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "NORMAL");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndPreloadedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "preloaded");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PRELOADED));
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndPreloadedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PreLoaded");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PRELOADED));
}
@Test
public void testResolveDataPolicyWhenPersistentAndPreloadedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "PRELOADED");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}

View File

@@ -0,0 +1,168 @@
/*
* 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.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.RegionFactory;
/**
* The PartitionedRegionFactoryBeanTest class is a test suite of test cases testing the component functionality
* and correct behavior of the PartitionedRegionFactoryBean class.
* <p/>
* @author John Blum
* @see org.mockito.Mockito
* @see org.junit.Test
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
* @since 1.3.3
*/
@SuppressWarnings("unchecked")
public class PartitionedRegionFactoryBeanTest {
private final PartitionedRegionFactoryBean factoryBean = new PartitionedRegionFactoryBean();
protected RegionFactory<?, ?> createMockRegionFactory() {
return mock(RegionFactory.class);
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithInvalidDataPolicyName() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.resolveDataPolicy(mockRegionFactory, null, "INVALID_DATA_POLICY_NAME");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'INVALID_DATA_POLICY_NAME' is invalid.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
}
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithInvalidDataPolicyRegionType() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.resolveDataPolicy(mockRegionFactory, null, "REPLICATE");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'REPLICATE' is not supported in Partitioned Regions.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE));
}
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION));
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "PARTITION");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION));
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndPersistentPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_PARTITION");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
@Test
public void testResolveDataPolicyWithPersistentFalseAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION));
}
@Test
public void testResolveDataPolicyWithPersistentTrueAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
@Test
public void testResolveDataPolicyWithPersistentFalseAndPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PARTITION");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PARTITION));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithPersistentFalseAndPersistentPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_PARTITION");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithPersistentTrueAndPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "PARTITION");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PARTITION' is invalid when persistent is true.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION));
}
}
@Test
public void testResolveDataPolicyWithPersistentTrueAndPersistentPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "PERSISTENT_PARTITION");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
}

View File

@@ -17,18 +17,27 @@ package org.springframework.data.gemfire;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.springframework.data.gemfire.support.AbstractRegionFactoryBeanTest;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionFactory;
/**
* @author David Turanski
*
* @author John Blum
*/
@SuppressWarnings("unchecked")
public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
private final RegionFactoryBean factoryBean = new RegionFactoryBean();
@SuppressWarnings("rawtypes")
private RegionFactoryBeanConfig defaultConfig() {
return new RegionFactoryBeanConfig(new RegionFactoryBean(), "default") {
@@ -77,8 +86,8 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
@Override
public void verify() {
assertNotNull(this.exception);
assertEquals("Data policy persistent_replicate is invalid when persistent is false",
exception.getMessage());
assertEquals("Data Policy 'PERSISTENT_REPLICATE' is invalid when persistent is false.",
exception.getMessage());
}
};
}
@@ -89,4 +98,118 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
addRFBConfig(persistent());
addRFBConfig(invalidPersistence());
}
protected RegionFactory<?, ?> createMockRegionFactory() {
return mock(RegionFactory.class);
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT));
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.DEFAULT));
}
@Test
public void testResolveDataPolicyWhenPersistentAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithInvalidDataPolicyName() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "CSV");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'CSV' is invalid.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndNormalDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "NORMAL");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.NORMAL));
}
@Test
public void testResolveDataPolicyWhenNotPersistentUnspecifiedAndPreloadedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PRELOADED");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PRELOADED));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenPersistentAndPersistentEmptyDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
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;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.DEFAULT));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.EMPTY));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndPersistentPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_PARTITION");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenNotPersistentAndPersistentPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_PARTITION");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PERSISTENT_PARTITION' is invalid when persistent is false.", e.getMessage());
throw e;
}
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
public void testResolveDataPolicyWhenPersistentAndPersistentPartitionedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "PERSISTENT_PARTITION");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_PARTITION));
}
}

View File

@@ -0,0 +1,204 @@
/*
* 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.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.RegionFactory;
/**
* The ReplicatedRegionFactoryBeanTest class is a test suite of test cases testing the component functionality
* and correct behavior of the ReplicatedRegionFactoryBean class.
* <p/>
* @author John Blum
* @see org.mockito.Mockito
* @see org.junit.Test
* @see org.springframework.data.gemfire.ReplicatedRegionFactoryBean
* @since 1.3.3
*/
@SuppressWarnings("unchecked")
public class ReplicatedRegionFactoryBeanTest {
private final ReplicatedRegionFactoryBean factoryBean = new ReplicatedRegionFactoryBean();
protected RegionFactory<?, ?> createMockRegionFactory() {
return mock(RegionFactory.class);
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE));
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE));
}
@Test
public void testResolveDataPolicyWhenPersistentAndDataPolicyUnspecified() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, null);
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
@Test
public void testResolveDataPolicyWithPersistentUnspecifiedAndEmptyDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "EMPTY");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.EMPTY));
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndEmptyDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "empty");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.EMPTY));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenPersistentAndEmptyDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
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;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.EMPTY));
}
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithPersistentUnspecifiedAndInvalidDataPolicyName() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.resolveDataPolicy(mockRegionFactory, null, "INVALID_DATA_POLICY_NAME");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'INVALID_DATA_POLICY_NAME' is invalid.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE));
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWithPersistentUnspecifiedAndInvalidDataPolicyRegionType() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.resolveDataPolicy(mockRegionFactory, null, "PARTITION");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PARTITION' is not supported in Replicated Regions.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.PARTITION));
}
}
@Test
public void testResolveDataPolicyWhenPersistentUnspecifiedAndReplicatedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "REPLICATE");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE));
}
@Test
public void testResolveDataPolicyWhenNotPersistentAndReplicatedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "REPLICATE");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.REPLICATE));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenPersistentAndReplicatedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "REPLICATE");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'REPLICATE' is invalid when persistent is true.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE));
}
}
@Test
public void testResolveDataPolicyWhenPersistentUnspecifiedAndPersistentReplicatedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.resolveDataPolicy(mockRegionFactory, null, "PERSISTENT_REPLICATE");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
@Test(expected = IllegalArgumentException.class)
public void testResolveDataPolicyWhenNotPersistentAndPersistentReplicatedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
try {
factoryBean.setPersistent(false);
factoryBean.resolveDataPolicy(mockRegionFactory, false, "PERSISTENT_REPLICATE");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PERSISTENT_REPLICATE' is invalid when persistent is false.", e.getMessage());
throw e;
}
finally {
verify(mockRegionFactory, never()).setDataPolicy(null);
verify(mockRegionFactory, never()).setDataPolicy(eq(DataPolicy.REPLICATE));
}
}
@Test
public void testResolveDataPolicyWhenPersistentAndPersistentReplicatedDataPolicy() {
RegionFactory mockRegionFactory = createMockRegionFactory();
factoryBean.setPersistent(true);
factoryBean.resolveDataPolicy(mockRegionFactory, true, "PERSISTENT_REPLICATE");
verify(mockRegionFactory).setDataPolicy(eq(DataPolicy.PERSISTENT_REPLICATE));
}
}

View File

@@ -15,15 +15,22 @@
*/
package org.springframework.data.gemfire.client;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.data.gemfire.TestUtils;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
@@ -36,34 +43,79 @@ import com.gemstone.gemfire.cache.client.Pool;
*/
public class ClientRegionFactoryBeanTest {
private ClientRegionFactoryBean<Object, Object> factoryBean;
@Before
public void setup() {
factoryBean = new ClientRegionFactoryBean<Object, Object>();
}
@After
public void tearDown() throws Exception {
factoryBean.destroy();
factoryBean = null;
}
@SuppressWarnings("unchecked")
@Test
public void testLookupFallbackFailingToUseProvidedShortcut() throws Exception {
ClientRegionFactoryBean<Object, Object> fb = new ClientRegionFactoryBean<Object, Object>();
factoryBean.setShortcut(ClientRegionShortcut.CACHING_PROXY);
fb.setShortcut(ClientRegionShortcut.CACHING_PROXY);
BeanFactory beanFactory = mock(BeanFactory.class);
Pool pool = mock(Pool.class);
Pool pool = Mockito.mock(Pool.class);
BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
Mockito.when(beanFactory.getBean(Pool.class)).thenReturn(pool);
fb.setBeanFactory(beanFactory);
when(beanFactory.getBean(Pool.class)).thenReturn(pool);
String regionName = "regionName";
ClientCache cache = Mockito.mock(ClientCache.class);
factoryBean.setBeanFactory(beanFactory);
@SuppressWarnings("unchecked")
ClientRegionFactory<Object, Object> factory = Mockito.mock(ClientRegionFactory.class);
Mockito.when(cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)).thenReturn(factory);
ClientCache cache = mock(ClientCache.class);
ClientRegionFactory<Object, Object> clientRegionFactory = mock(ClientRegionFactory.class);
Region<Object, Object> expectedRegion = mock(Region.class);
@SuppressWarnings("unchecked")
Region<Object, Object> region = Mockito.mock(Region.class);
Mockito.when(factory.create(regionName)).thenReturn(region);
when(cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)).thenReturn(clientRegionFactory);
when(clientRegionFactory.create("testRegion")).thenReturn(expectedRegion);
Region<Object, Object> result = fb.lookupFallback(cache, regionName);
Region<Object, Object> actualRegion = factoryBean.lookupFallback(cache, "testRegion");
assertSame(region, result);
assertSame(expectedRegion, actualRegion);
}
@Test
public void testSetDataPolicyName() throws Exception {
factoryBean.setDataPolicyName("NORMAL");
assertEquals(DataPolicy.NORMAL, TestUtils.readField("dataPolicy", factoryBean));
}
@Test(expected = IllegalArgumentException.class)
public void testSetDataPolicyNameWithInvalidName() throws Exception {
try {
factoryBean.setDataPolicyName("INVALID");
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'INVALID' is invalid.", e.getMessage());
throw e;
}
finally {
assertNull(TestUtils.readField("dataPolicy", factoryBean));
}
}
@Test
public void testIsPersistent() {
assertFalse(factoryBean.isPersistent());
factoryBean.setPersistent(false);
assertFalse(factoryBean.isPersistent());
factoryBean.setPersistent(true);
assertTrue(factoryBean.isPersistent());
}
@Test
public void testIsNotPersistent() {
assertFalse(factoryBean.isNotPersistent());
factoryBean.setPersistent(true);
assertFalse(factoryBean.isNotPersistent());
factoryBean.setPersistent(false);
assertTrue(factoryBean.isNotPersistent());
}
@Test
@@ -115,4 +167,176 @@ public class ClientRegionFactoryBeanTest {
assertFalse(factory.isDestroy()); // setting close to true should set destroy to false
}
@Test
public void testResolveClientRegionShortcut() throws Exception {
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertNull(TestUtils.readField("persistent", factoryBean));
assertNull(TestUtils.readField("shortcut", factoryBean));
assertEquals(ClientRegionShortcut.LOCAL, factoryBean.resolveClientRegionShortcut());
}
@Test
public void testResolveClientRegionShortcutWhenNotPersistent() throws Exception {
factoryBean.setPersistent(false);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertTrue(factoryBean.isNotPersistent());
assertNull(TestUtils.readField("shortcut", factoryBean));
assertEquals(ClientRegionShortcut.LOCAL, factoryBean.resolveClientRegionShortcut());
}
@Test
public void testResolveClientRegionShortcutWhenPersistent() throws Exception {
factoryBean.setPersistent(true);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertTrue(factoryBean.isPersistent());
assertNull(TestUtils.readField("shortcut", factoryBean));
assertEquals(ClientRegionShortcut.LOCAL_PERSISTENT, factoryBean.resolveClientRegionShortcut());
}
@Test
public void testResolveClientRegionShortcutUsingShortcut() throws Exception {
factoryBean.setShortcut(ClientRegionShortcut.CACHING_PROXY_OVERFLOW);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertNull(TestUtils.readField("persistent", factoryBean));
assertEquals(ClientRegionShortcut.CACHING_PROXY_OVERFLOW, factoryBean.resolveClientRegionShortcut());
}
@Test
public void testResolveClientRegionShortcutUsingShortcutWhenNotPersistent() throws Exception {
factoryBean.setPersistent(false);
factoryBean.setShortcut(ClientRegionShortcut.CACHING_PROXY_HEAP_LRU);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertTrue(factoryBean.isNotPersistent());
assertEquals(ClientRegionShortcut.CACHING_PROXY_HEAP_LRU, factoryBean.resolveClientRegionShortcut());
}
@Test(expected = IllegalArgumentException.class)
public void testResolveClientRegionShortcutUsingShortcutWhenPersistent() throws Exception {
try {
factoryBean.setPersistent(true);
factoryBean.setShortcut(ClientRegionShortcut.CACHING_PROXY);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertTrue(factoryBean.isPersistent());
factoryBean.resolveClientRegionShortcut();
}
catch (IllegalArgumentException e) {
assertEquals("Client Region Shortcut 'CACHING_PROXY' is invalid when persistent is true.", e.getMessage());
throw e;
}
}
@Test
public void testResolveClientRegionShortcutUsingPersistentShortcut() throws Exception {
factoryBean.setShortcut(ClientRegionShortcut.LOCAL_PERSISTENT);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertNull(TestUtils.readField("persistent", factoryBean));
assertEquals(ClientRegionShortcut.LOCAL_PERSISTENT, factoryBean.resolveClientRegionShortcut());
}
@Test(expected = IllegalArgumentException.class)
public void testResolveClientRegionShortcutUsingPersistentShortcutWhenNotPersistent() throws Exception {
try {
factoryBean.setPersistent(false);
factoryBean.setShortcut(ClientRegionShortcut.LOCAL_PERSISTENT);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertTrue(factoryBean.isNotPersistent());
factoryBean.resolveClientRegionShortcut();
}
catch (IllegalArgumentException e) {
assertEquals("Client Region Shortcut 'LOCAL_PERSISTENT' is invalid when persistent is false.", e.getMessage());
throw e;
}
}
@Test
public void testResolveClientRegionShortcutUsingPersistentShortcutWhenPersistent() throws Exception {
factoryBean.setPersistent(true);
factoryBean.setShortcut(ClientRegionShortcut.LOCAL_PERSISTENT_OVERFLOW);
assertNull(TestUtils.readField("dataPolicy", factoryBean));
assertTrue(factoryBean.isPersistent());
assertEquals(ClientRegionShortcut.LOCAL_PERSISTENT_OVERFLOW, factoryBean.resolveClientRegionShortcut());
}
@Test
public void testResolveClientRegionShortcutUsingEmptyDataPolicy() throws Exception {
factoryBean.setDataPolicy(DataPolicy.EMPTY);
assertNull(TestUtils.readField("persistent", factoryBean));
assertNull(TestUtils.readField("shortcut", factoryBean));
assertEquals(ClientRegionShortcut.PROXY, factoryBean.resolveClientRegionShortcut());
}
@Test
public void testResolveClientRegionShortcutUsingNormalDataPolicyWhenNotPersistent() throws Exception {
factoryBean.setDataPolicy(DataPolicy.NORMAL);
factoryBean.setPersistent(false);
assertTrue(factoryBean.isNotPersistent());
assertNull(TestUtils.readField("shortcut", factoryBean));
assertEquals(ClientRegionShortcut.CACHING_PROXY, factoryBean.resolveClientRegionShortcut());
}
@Test(expected = IllegalArgumentException.class)
public void testResolveClientRegionShortcutUsingNormalDataPolicyWhenPersistent() throws Exception {
try {
factoryBean.setDataPolicy(DataPolicy.NORMAL);
factoryBean.setPersistent(true);
assertTrue(factoryBean.isPersistent());
assertNull(TestUtils.readField("shortcut", factoryBean));
factoryBean.resolveClientRegionShortcut();
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'NORMAL' is invalid when persistent is true.", e.getMessage());
throw e;
}
}
@Test
public void testResolveClientRegionShortcutUsingPersistentReplicateDataPolicy() throws Exception {
factoryBean.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
assertNull(TestUtils.readField("persistent", factoryBean));
assertNull(TestUtils.readField("shortcut", factoryBean));
assertEquals(ClientRegionShortcut.LOCAL_PERSISTENT, factoryBean.resolveClientRegionShortcut());
}
@Test(expected = IllegalArgumentException.class)
public void testResolveClientRegionShortcutUsingPersistentReplicateDataPolicyWhenNotPersistent() throws Exception {
try {
factoryBean.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factoryBean.setPersistent(false);
assertTrue(factoryBean.isNotPersistent());
assertNull(TestUtils.readField("shortcut", factoryBean));
factoryBean.resolveClientRegionShortcut();
}
catch (IllegalArgumentException e) {
assertEquals("Data Policy 'PERSISTENT_REPLICATE' is invalid when persistent is false.", e.getMessage());
throw e;
}
}
@Test
public void testResolveClientRegionShortcutUsingPersistentReplicateDataPolicyWhenPersistent() throws Exception {
factoryBean.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factoryBean.setPersistent(true);
assertNull(TestUtils.readField("shortcut", factoryBean));
assertTrue(factoryBean.isPersistent());
assertEquals(ClientRegionShortcut.LOCAL_PERSISTENT, factoryBean.resolveClientRegionShortcut());
}
}

View File

@@ -129,7 +129,6 @@ public class ClientRegionNamespaceTest {
public void testOverflowToDisk() throws Exception {
assertTrue(context.containsBean("overflow"));
ClientRegionFactoryBean fb = context.getBean("&overflow", ClientRegionFactoryBean.class);
assertEquals(DataPolicy.NORMAL, TestUtils.readField("dataPolicy", fb));
RegionAttributes attrs = TestUtils.readField("attributes", fb);
EvictionAttributes evicAttr = attrs.getEvictionAttributes();
assertEquals(EvictionAction.OVERFLOW_TO_DISK, evicAttr.getAction());

View File

@@ -0,0 +1,47 @@
/*
* 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.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* The ClientRegionUsingDataPolicyAndShortcutTest class is a test suite of test case testing a client Region
* bean definition with both data-policy and shortcut specified.
* <p/>
* @author John Blum
* @see org.junit.Assert
* @since 1.3.3
*/
public class ClientRegionUsingDataPolicyAndShortcutTest {
@Test(expected = BeanDefinitionParsingException.class)
public void testClientRegionBeanDefinitionWithDataPolicyAndShortcut() {
try {
new ClassPathXmlApplicationContext("/org/springframework/data/gemfire/config/client-region-using-datapolicy-and-shortcut.xml");
}
catch (BeanDefinitionParsingException e) {
assertTrue(e.getMessage().contains("Only one of [data-policy, shortcut] may be specified with element"));
throw e;
}
}
}

View File

@@ -28,6 +28,7 @@ import java.util.List;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.data.gemfire.RecreatingContextTest;
import org.springframework.data.gemfire.RegionFactoryBean;
import org.springframework.data.gemfire.TestUtils;
@@ -71,7 +72,7 @@ public class GemfireV7GatewayNamespaceTest extends RecreatingContextTest {
@Before
@Override
public void createCtx() {
if (ParsingUtils.GEMFIRE_VERSION.startsWith("7")) {
if (GemfireUtils.GEMFIRE_VERSION.startsWith("7")) {
super.createCtx();
}
}