SGF-390 - Improve unit test coverage for the PoolFactoryBean class.

This commit is contained in:
John Blum
2015-04-01 14:25:00 -07:00
parent 06d1df02ce
commit d44464dee5
12 changed files with 610 additions and 131 deletions

View File

@@ -34,7 +34,6 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -387,19 +386,6 @@ public class CacheFactoryBeanTest {
assertTrue(new CacheFactoryBean().isSingleton());
}
@Test
public void testNullSafeCollectionWithNonNullCollection() {
assertNotNull(new CacheFactoryBean().nullSafeCollection(Collections.emptyList()));
}
@Test
public void testNullSafeCollectionWithNullCollection() {
Collection<?> collection = new CacheFactoryBean().nullSafeCollection(null);
assertNotNull(collection);
assertTrue(collection.isEmpty());
}
@Test
public void testDestroy() throws Exception {
final Cache mockCache = mock(Cache.class, "GemFireCache");

View File

@@ -156,7 +156,7 @@ public class RegionFactoryBeanTest extends AbstractRegionFactoryBeanTest {
protected RegionAttributes createMockRegionAttributes(final DataPolicy... dataPolicies) {
RegionAttributes mockRegionAttributes = mock(RegionAttributes.class);
when(mockRegionAttributes.getDataPolicy()).thenReturn(ArrayUtils.getFirst(DataPolicy.DEFAULT, dataPolicies));
when(mockRegionAttributes.getDataPolicy()).thenReturn(ArrayUtils.getFirst(dataPolicies, DataPolicy.DEFAULT));
return mockRegionAttributes;
}

View File

@@ -0,0 +1,246 @@
/*
* 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.client;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Properties;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.util.ReflectionUtils;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.client.PoolFactory;
/**
* The PoolFactoryBeanTest class is a test suite of test cases testing the contract and functionality
* of the PoolFactoryBean class.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.client.PoolFactoryBean
* @see com.gemstone.gemfire.cache.client.Pool
* @see com.gemstone.gemfire.cache.client.PoolFactory
* @since 1.7.0
*/
public class PoolFactoryBeanTest {
@Test
public void testGetObjectType() {
assertEquals(Pool.class, new PoolFactoryBean().getObjectType());
}
@Test
public void testIsSingleton() {
assertTrue(new PoolFactoryBean().isSingleton());
}
@Test
public void testAfterPropertiesSet() throws Exception {
BeanFactory mockBeanFactory = mock(BeanFactory.class, "MockSpringBeanFactory");
final PoolFactory mockPoolFactory = mock(PoolFactory.class, "MockGemFirePoolFactory");
Pool mockPool = mock(Pool.class, "GemFirePool");
final Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", "testAfterPropertiesSet");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean();
clientCacheFactoryBean.setProperties(gemfireProperties);
when(mockBeanFactory.getBean(eq(ClientCacheFactoryBean.class))).thenReturn(clientCacheFactoryBean);
when(mockPoolFactory.create(eq("GemFirePool"))).thenReturn(mockPool);
PoolFactoryBean poolFactoryBean = new PoolFactoryBean() {
@Override protected PoolFactory createPoolFactory() {
return mockPoolFactory;
}
@Override void doDistributedSystemConnect(Properties properties) {
assertSame(gemfireProperties, properties);
}
};
poolFactoryBean.setBeanFactory(mockBeanFactory);
poolFactoryBean.setBeanName("GemFirePool");
poolFactoryBean.setName(null);
poolFactoryBean.setFreeConnectionTimeout(60000);
poolFactoryBean.setIdleTimeout(120000l);
poolFactoryBean.setKeepAlive(false);
poolFactoryBean.setLoadConditioningInterval(15000);
poolFactoryBean.setLocators(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(), 54321)));
poolFactoryBean.setMaxConnections(50);
poolFactoryBean.setMinConnections(5);
poolFactoryBean.setMultiUserAuthentication(false);
poolFactoryBean.setPingInterval(5000l);
poolFactoryBean.setPrSingleHopEnabled(true);
poolFactoryBean.setRetryAttempts(10);
poolFactoryBean.setServerGroup("TestServerGroup");
poolFactoryBean.setServers(Collections.singletonList(new InetSocketAddress(InetAddress.getLocalHost(), 12345)));
poolFactoryBean.setSocketBufferSize(32768);
poolFactoryBean.setStatisticInterval(1000);
poolFactoryBean.setSubscriptionAckInterval(500);
poolFactoryBean.setSubscriptionMessageTrackingTimeout(20000);
poolFactoryBean.setSubscriptionRedundancy(2);
poolFactoryBean.setThreadLocalConnections(false);
poolFactoryBean.afterPropertiesSet();
assertSame(mockPool, poolFactoryBean.getObject());
verify(mockPoolFactory, times(1)).setFreeConnectionTimeout(eq(60000));
verify(mockPoolFactory, times(1)).setIdleTimeout(eq(120000l));
verify(mockPoolFactory, times(1)).setLoadConditioningInterval(eq(15000));
verify(mockPoolFactory, times(1)).setMaxConnections(eq(50));
verify(mockPoolFactory, times(1)).setMinConnections(eq(5));
verify(mockPoolFactory, times(1)).setMultiuserAuthentication(eq(false));
verify(mockPoolFactory, times(1)).setPingInterval(eq(5000l));
verify(mockPoolFactory, times(1)).setPRSingleHopEnabled(eq(true));
verify(mockPoolFactory, times(1)).setRetryAttempts(eq(10));
verify(mockPoolFactory, times(1)).setServerGroup(eq("TestServerGroup"));
verify(mockPoolFactory, times(1)).setSocketBufferSize(eq(32768));
verify(mockPoolFactory, times(1)).setStatisticInterval(eq(1000));
verify(mockPoolFactory, times(1)).setSubscriptionAckInterval(eq(500));
verify(mockPoolFactory, times(1)).setSubscriptionMessageTrackingTimeout(eq(20000));
verify(mockPoolFactory, times(1)).setSubscriptionRedundancy(eq(2));
verify(mockPoolFactory, times(1)).setThreadLocalConnections(eq(false));
verify(mockPoolFactory, times(1)).addLocator(InetAddress.getLocalHost().getHostName(), 54321);
verify(mockPoolFactory, times(1)).addServer(InetAddress.getLocalHost().getHostName(), 12345);
verify(mockPoolFactory, times(1)).create(eq("GemFirePool"));
}
@Test(expected = IllegalArgumentException.class)
public void testAfterPropertiesSetWithNoLocatorsServersSpecified() throws Exception {
try {
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setName("GemFirePool");
poolFactoryBean.setLocators(null);
poolFactoryBean.setServers(Collections.<InetSocketAddress>emptyList());
poolFactoryBean.afterPropertiesSet();
}
catch (IllegalArgumentException expected) {
assertEquals("at least one GemFire Locator or Server is required", expected.getMessage());
throw expected;
}
}
@Test(expected = IllegalArgumentException.class)
public void testAfterPropertiesSetWithUnspecifiedName() throws Exception {
try {
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setBeanName(null);
poolFactoryBean.setName(null);
poolFactoryBean.afterPropertiesSet();
}
catch (IllegalArgumentException expected) {
assertEquals("Pool 'name' is required", expected.getMessage());
throw expected;
}
}
@Test
public void testResolveGemfireProperties() {
BeanFactory mockBeanFactory = mock(BeanFactory.class, "MockSpringBeanFactory");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean();
when(mockBeanFactory.getBean(eq(ClientCacheFactoryBean.class))).thenReturn(clientCacheFactoryBean);
Properties expectedGemfireProperties = new Properties();
expectedGemfireProperties.setProperty("name", "testResolveGemfireProperties");
clientCacheFactoryBean.setProperties(expectedGemfireProperties);
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setBeanFactory(mockBeanFactory);
Properties resolvedGemfireProperties = poolFactoryBean.resolveGemfireProperties();
assertSame(expectedGemfireProperties, resolvedGemfireProperties);
}
@Test
public void testResolveUnresolvableGemfireProperties() {
BeanFactory mockBeanFactory = mock(BeanFactory.class, "MockSpringBeanFactory");
when(mockBeanFactory.getBean(eq(ClientCacheFactoryBean.class))).thenThrow(
new NoSuchBeanDefinitionException("TEST"));
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setBeanFactory(mockBeanFactory);
assertNull(poolFactoryBean.resolveGemfireProperties());
}
@Test
public void testDestroy() throws Exception {
Pool mockPool = mock(Pool.class, "MockGemFirePool");
when(mockPool.isDestroyed()).thenReturn(false);
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setPool(mockPool);
poolFactoryBean.destroy();
assertNull(TestUtils.readField("pool", poolFactoryBean));
verify(mockPool, times(1)).releaseThreadLocalConnection();
verify(mockPool, times(1)).destroy(eq(false));
}
@Test
public void testDestroyNonSpringBasedPool() throws Exception {
Pool mockPool = mock(Pool.class, "MockGemFirePool");
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
ReflectionUtils.setField(PoolFactoryBean.class.getDeclaredField("springBasedPool"), poolFactoryBean, false);
poolFactoryBean.setPool(mockPool);
poolFactoryBean.destroy();
verify(mockPool, never()).isDestroyed();
verify(mockPool, never()).releaseThreadLocalConnection();
verify(mockPool, never()).destroy(any(Boolean.class));
}
@Test
public void testDestroyWithUninitializedPool() throws Exception {
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setPool(null);
poolFactoryBean.destroy();
}
}

View File

@@ -20,25 +20,19 @@ package org.springframework.data.gemfire.test.support;
* ArrayUtils is a utility class for working with Java arrays.
*
* @author John Blum
* @see org.springframework.data.gemfire.util.ArrayUtils
* @since 1.6.0
*/
@SuppressWarnings("unused")
public class ArrayUtils {
// TODO replace with org.springframework.data.gemfire.util.ArrayUtils
public class ArrayUtils extends org.springframework.data.gemfire.util.ArrayUtils {
public static <T> T getFirst(T... array) {
return getFirst(null, array);
return getFirst(array, null);
}
public static <T> T getFirst(T defaultValue, T... array) {
public static <T> T getFirst(T[] array, T defaultValue) {
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);
}
}

View File

@@ -27,10 +27,12 @@ import java.util.List;
* @author John Blum
* @see java.util.Collection
* @see java.util.Collections
* @see org.springframework.data.gemfire.util.CollectionUtils
* @since 1.5.0
*/
@SuppressWarnings("unused")
public abstract class CollectionUtils {
// TODO replace with org.springframework.data.gemfire.util.CollectionUtils
public abstract class CollectionUtils extends org.springframework.data.gemfire.util.CollectionUtils {
public static <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
return new Iterable<T>() {

View File

@@ -0,0 +1,129 @@
/*
* 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.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.junit.Test;
/**
* The ArrayUtilsTest class is a test suite of test cases testing the contract and functionality
* of the ArrayUtils class.
*
* @author John Blum
* @see java.util.Arrays
* @see org.junit.Test
* @see org.springframework.data.gemfire.util.ArrayUtils
* @since 1.7.0
*/
public class ArrayUtilsTest {
@Test
public void testInsertBeginning() {
Object[] originalArray = { "testing", "tested" };
Object[] newArray = ArrayUtils.insert(originalArray, 0, "test");
assertNotSame(originalArray, newArray);
assertFalse(Arrays.equals(originalArray, newArray));
assertEquals("test", newArray[0]);
assertEquals("testing", newArray[1]);
assertEquals("tested", newArray[2]);
}
@Test
public void testInsertMiddle() {
Object[] originalArray = { "test", "tested" };
Object[] newArray = ArrayUtils.insert(originalArray, 1, "testing");
assertNotSame(originalArray, newArray);
assertFalse(Arrays.equals(originalArray, newArray));
assertEquals("test", newArray[0]);
assertEquals("testing", newArray[1]);
assertEquals("tested", newArray[2]);
}
@Test
public void testInsertEnd() {
Object[] originalArray = { "test", "testing" };
Object[] newArray = ArrayUtils.insert(originalArray, 2, "tested");
assertNotSame(originalArray, newArray);
assertFalse(Arrays.equals(originalArray, newArray));
assertEquals("test", newArray[0]);
assertEquals("testing", newArray[1]);
assertEquals("tested", newArray[2]);
}
@Test
public void testIsEmpty() {
assertFalse(ArrayUtils.isEmpty("test", "testing", "tested"));
assertFalse(ArrayUtils.isEmpty("test"));
assertFalse(ArrayUtils.isEmpty(""));
assertFalse(ArrayUtils.isEmpty(null, null, null));
assertTrue(ArrayUtils.isEmpty());
assertTrue(ArrayUtils.isEmpty((Object[]) null));
}
@Test
public void testLength() {
assertEquals(3, ArrayUtils.length("test", "testing", "tested"));
assertEquals(1, ArrayUtils.length("test"));
assertEquals(1, ArrayUtils.length(""));
assertEquals(3, ArrayUtils.length(null, null, null));
assertEquals(0, ArrayUtils.length());
assertEquals(0, ArrayUtils.length((Object[]) null));
}
@Test
public void testRemoveBeginning() {
Object[] originalArray = { "test", "testing", "tested" };
Object[] newArray = ArrayUtils.remove(originalArray, 0);
assertNotSame(originalArray, newArray);
assertFalse(Arrays.equals(originalArray, newArray));
assertEquals("testing", newArray[0]);
assertEquals("tested", newArray[1]);
}
@Test
public void testRemoveMiddle() {
Object[] originalArray = { "test", "testing", "tested" };
Object[] newArray = ArrayUtils.remove(originalArray, 1);
assertNotSame(originalArray, newArray);
assertFalse(Arrays.equals(originalArray, newArray));
assertEquals("test", newArray[0]);
assertEquals("tested", newArray[1]);
}
@Test
public void testRemoveEnd() {
Object[] originalArray = { "test", "testing", "tested" };
Object[] newArray = ArrayUtils.remove(originalArray, 2);
assertNotSame(originalArray, newArray);
assertFalse(Arrays.equals(originalArray, newArray));
assertEquals("test", newArray[0]);
assertEquals("testing", newArray[1]);
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.util;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
/**
* The CollectionUtilsTest class is a test suite of test cases testing the contract and functionality
* of the CollectionUtils class.
*
* @author John Blum
* @see java.util.Collection
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.util.CollectionUtils
* @since 1.7.0
*/
public class CollectionUtilsTest {
@Test
public void testNullSafeCollectionWithNonNullCollection() {
List<?> mockList = mock(List.class);
assertSame(mockList, CollectionUtils.nullSafeCollection(mockList));
}
@Test
public void testNullSafeCollectionWithNullCollection() {
Collection collection = CollectionUtils.nullSafeCollection(null);
assertNotNull(collection);
assertTrue(collection.isEmpty());
}
}