SGF-438 - Make <gfe:client-cache>, 'ready-for-events' true the default for durable clients.

(cherry picked from commit 0514a6b5e84f43cd6717097c52141ce5c698bf99)

Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2015-10-15 18:26:32 -07:00
parent 1bb8e7b7a9
commit 09d44bb99b
16 changed files with 579 additions and 79 deletions

View File

@@ -39,7 +39,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer;
import org.springframework.data.gemfire.test.MockRegionFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -87,7 +86,7 @@ public class GemfireTemplateTest {
@Before
@SuppressWarnings("rawtypes")
public void setUp() throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
QueryService queryService = MockRegionFactory.mockQueryService();
QueryService queryService = simple.getRegionService().getQueryService();
Query singleQuery = mock(Query.class);
when(singleQuery.execute(any(Object[].class))).thenReturn(0);

View File

@@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -63,11 +64,8 @@ import com.gemstone.gemfire.pdx.PdxSerializer;
* @see org.mockito.Mockito
* @see org.junit.Test
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
* @see com.gemstone.gemfire.cache.GemFireCache
* @see com.gemstone.gemfire.cache.client.ClientCache
* @see com.gemstone.gemfire.cache.client.ClientCacheFactory
* @see com.gemstone.gemfire.cache.client.Pool
* @see com.gemstone.gemfire.distributed.DistributedSystem
* @since 1.7.0
*/
public class ClientCacheFactoryBeanTest {
@@ -474,15 +472,15 @@ public class ClientCacheFactoryBeanTest {
final ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override public boolean isReadyForEvents() {
return true;
}
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
clientCacheFactoryBean.setReadyForEvents(true);
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
verify(mockClientCache, times(1)).readyForEvents();
@@ -496,15 +494,15 @@ public class ClientCacheFactoryBeanTest {
doThrow(new RuntimeException("test")).when(mockClientCache).readyForEvents();
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override public boolean isReadyForEvents() {
return false;
}
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
clientCacheFactoryBean.setReadyForEvents(false);
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(false));
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
verify(mockClientCache, never()).readyForEvents();
@@ -518,33 +516,32 @@ public class ClientCacheFactoryBeanTest {
doThrow(new IllegalStateException("non-durable client")).when(mockClientCache).readyForEvents();
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override public boolean isReadyForEvents() {
return true;
}
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
clientCacheFactoryBean.setReadyForEvents(true);
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
verify(mockClientCache, times(1)).readyForEvents();
}
@Test
@SuppressWarnings("unchecked")
public void onApplicationEventHandlesCacheClosedException() {
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override public boolean isReadyForEvents() {
return true;
}
@Override protected <T extends GemFireCache> T fetchCache() {
throw new CacheClosedException("test");
}
};
clientCacheFactoryBean.setReadyForEvents(true);
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
}
@@ -614,15 +611,108 @@ public class ClientCacheFactoryBeanTest {
public void setAndGetReadyForEvents() {
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean();
assertFalse(clientCacheFactoryBean.getReadyForEvents());
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(nullValue()));
clientCacheFactoryBean.setReadyForEvents(true);
assertTrue(clientCacheFactoryBean.getReadyForEvents());
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(true));
clientCacheFactoryBean.setReadyForEvents(null);
assertNull(clientCacheFactoryBean.getReadyForEvents());
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(nullValue()));
clientCacheFactoryBean.setReadyForEvents(false);
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(false));
}
protected ClientCache mockClientCache(String durableClientId) {
ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
DistributedSystem mockDistributedSystem = mock(DistributedSystem.class, "MockDistributedSystem");
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty(DistributedSystemUtils.DURABLE_CLIENT_ID_PROPERTY_NAME, durableClientId);
gemfireProperties.setProperty(DistributedSystemUtils.DURABLE_CLIENT_TIMEOUT_PROPERTY_NAME, "300");
when(mockClientCache.getDistributedSystem()).thenReturn(mockDistributedSystem);
when(mockDistributedSystem.isConnected()).thenReturn(true);
when(mockDistributedSystem.getProperties()).thenReturn(gemfireProperties);
return mockClientCache;
}
@Test
@SuppressWarnings("unchecked")
public void isReadyForEventsIsTrueWhenClientCacheFactoryBeanReadyForEventsIsTrue() {
final ClientCache mockClientCache = mockClientCache("");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
clientCacheFactoryBean.setReadyForEvents(true);
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(true));
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
verify(mockClientCache, never()).getDistributedSystem();
}
@Test
@SuppressWarnings("unchecked")
public void isReadyForEventsIsFalseWhenClientCacheFactoryBeanReadyForEventsIsFalse() {
final ClientCache mockClientCache = mockClientCache("TestDurableClientId");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
clientCacheFactoryBean.setReadyForEvents(false);
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(false));
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(false));
verify(mockClientCache, never()).getDistributedSystem();
}
@Test
@SuppressWarnings("unchecked")
public void isReadyForEventsIsTrueWhenDurableClientIdSet() {
final ClientCache mockClientCache = mockClientCache("TestDurableClientId");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(nullValue()));
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
verify(mockClientCache, times(1)).getDistributedSystem();
}
@Test
@SuppressWarnings("unchecked")
public void isReadyForEventsIsFalseWhenDurableClientIdIsNotSet() {
final ClientCache mockClientCache = mockClientCache(" ");
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
@Override protected <T extends GemFireCache> T fetchCache() {
return (T) mockClientCache;
}
};
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(nullValue()));
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(false));
verify(mockClientCache, times(1)).getDistributedSystem();
}
@Test

View File

@@ -59,8 +59,7 @@ import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
* @since 1.3.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "clientcache-with-region-using-cache-loader-writer.xml",
initializers = GemfireTestApplicationContextInitializer.class)
@ContextConfiguration(locations = "clientcache-with-region-using-cache-loader-writer.xml")
@SuppressWarnings("unused")
public class ClientRegionWithCacheLoaderWriterTest {
@@ -72,12 +71,6 @@ public class ClientRegionWithCacheLoaderWriterTest {
@Resource(name = "localAppDataRegion")
private Region<Integer, Integer> localAppData;
@BeforeClass
public static void startCacheServer() throws Exception {
ForkUtil.startCacheServer(SpringCacheServerProcess.class.getName() + " "
+ "/org/springframework/data/gemfire/client/servercache-with-region-for-client.xml");
}
@Test
public void testCacheLoaderWriter() {
assertNotNull(localAppData);

View File

@@ -175,7 +175,6 @@ public class ClientRegionNamespaceTest {
assertEquals(DataPolicy.PERSISTENT_REPLICATE, persistentRegionAttributes.getDataPolicy());
assertEquals("diskStore", persistentRegionAttributes.getDiskStoreName());
assertEquals(10, persistentRegionAttributes.getDiskDirSizes()[0]);
assertEquals("gemfire-pool", persistentRegionAttributes.getPoolName());
}
@@ -230,12 +229,13 @@ public class ClientRegionNamespaceTest {
assertEquals(DataPolicy.EMPTY, compressed.getAttributes().getDataPolicy());
assertEquals("gemfire-pool", compressed.getAttributes().getPoolName());
assertTrue(String.format("Expected 'TestCompressor'; but was '%1$s'!",
ObjectUtils.nullSafeClassName(compressed.getAttributes().getCompressor())),
compressed.getAttributes().getCompressor() instanceof TestCompressor);
ObjectUtils.nullSafeClassName(compressed.getAttributes().getCompressor())),
compressed.getAttributes().getCompressor() instanceof TestCompressor);
assertEquals("STD", compressed.getAttributes().getCompressor().toString());
}
@Test
@SuppressWarnings("unchecked")
public void testClientRegionWithAttributes() {
assertTrue(context.containsBean("client-with-attributes"));

View File

@@ -63,7 +63,7 @@ import com.gemstone.gemfire.cache.util.ObjectSizer;
* @since 1.5.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = GemfireTestApplicationContextInitializer.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class TemplateClientRegionNamespaceTest {

View File

@@ -16,6 +16,7 @@ import java.util.Properties;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import com.gemstone.gemfire.cache.GemFireCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
/**
@@ -70,4 +71,10 @@ public class MockClientCacheFactoryBean extends ClientCacheFactoryBean {
return new ClientCacheFactory(gemfireProperties);
}
@Override
protected GemFireCache fetchCache() {
((StubCache) cache).setProperties(getProperties());
return cache;
}
}

View File

@@ -0,0 +1,329 @@
/*
* 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;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyFloat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.data.gemfire.config.GemfireConstants;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.CustomExpiry;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.EvictionAttributes;
import com.gemstone.gemfire.cache.ExpirationAttributes;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.compression.Compressor;
import com.gemstone.gemfire.management.internal.cli.util.spring.StringUtils;
/**
* The MockClientRegionFactory class...
*
* @author John Blum
* @since 1.8.0
*/
@SuppressWarnings("unused")
public class MockClientRegionFactory<K, V> extends MockRegionFactory<K, V> {
public MockClientRegionFactory(StubCache cache) {
super(cache);
}
@SuppressWarnings({ "deprecation", "unchecked" })
public ClientRegionFactory<K, V> mockClientRegionFactory() {
return mockClientRegionFactory(ClientRegionShortcut.PROXY);
}
@SuppressWarnings({ "deprecation", "unchecked" })
public ClientRegionFactory<K, V> mockClientRegionFactory(ClientRegionShortcut clientRegionShortcut) {
attributesFactory = new com.gemstone.gemfire.cache.AttributesFactory<K,V>();
attributesFactory.setDataPolicy(resolveDataPolicy(clientRegionShortcut));
final ClientRegionFactory<K, V> mockClientRegionFactory = mock(ClientRegionFactory.class, "MockClientRegionFactory");
when(mockClientRegionFactory.create(anyString())).thenAnswer(new Answer<Region>() {
@Override public Region answer(InvocationOnMock invocation) throws Throwable {
String name = (String) invocation.getArguments()[0];
Region region = mockRegion(name);
when(region.getAttributesMutator()).thenReturn(mockAttributesMutator);
cache.allRegions().put(name, region);
return region;
}
});
when(mockClientRegionFactory.createSubregion(any(Region.class), anyString())).thenAnswer(new Answer<Region>() {
@Override public Region answer(InvocationOnMock invocation) throws Throwable {
Region parent = (Region) invocation.getArguments()[0];
String name = (String) invocation.getArguments()[1];
String parentRegionName = parent.getFullPath();
assert parentRegionName != null : "Parent Region name was null!";
String subRegionName = (parentRegionName.startsWith("/") ? parentRegionName+"/"+name
: "/"+parentRegionName+"/"+ name);
Region subRegion = mockRegion(subRegionName);
cache.allRegions().put(subRegionName, subRegion);
cache.allRegions().put(name, subRegion);
return subRegion;
}
});
when(mockClientRegionFactory.setCloningEnabled(anyBoolean())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
attributesFactory.setCloningEnabled(Boolean.TRUE.equals(invocation.getArgumentAt(0, Boolean.class)));
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setCompressor(any(Compressor.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
Compressor compressor = invocation.getArgumentAt(0, Compressor.class);
attributesFactory.setCompressor(compressor);
return mockClientRegionFactory;
}
}
);
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
attributesFactory.setConcurrencyChecksEnabled(Boolean.TRUE.equals(
invocation.getArgumentAt(0, Boolean.class)));
return null;
}
}).when(mockClientRegionFactory).setConcurrencyChecksEnabled(anyBoolean());
when(mockClientRegionFactory.setConcurrencyLevel(anyInt())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
int concurrencyLevel = invocation.getArgumentAt(0, Integer.TYPE);
attributesFactory.setConcurrencyLevel(concurrencyLevel);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setCustomEntryIdleTimeout(any(CustomExpiry.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
CustomExpiry<K, V> customEntryIdleTimeout = invocation.getArgumentAt(0, CustomExpiry.class);
attributesFactory.setCustomEntryIdleTimeout(customEntryIdleTimeout);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setCustomEntryTimeToLive(any(CustomExpiry.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
CustomExpiry<K, V> customEntryTimeToLive = invocation.getArgumentAt(0, CustomExpiry.class);
attributesFactory.setCustomEntryTimeToLive(customEntryTimeToLive);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setDiskStoreName(anyString())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
String diskStoreName = invocation.getArgumentAt(0, String.class);
attributesFactory.setDiskStoreName(diskStoreName);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setDiskSynchronous(anyBoolean())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
attributesFactory.setDiskSynchronous(Boolean.TRUE.equals(
invocation.getArgumentAt(0, Boolean.class)));
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setEntryIdleTimeout(any(ExpirationAttributes.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
ExpirationAttributes entryIdleTimeout = invocation.getArgumentAt(0, ExpirationAttributes.class);
attributesFactory.setEntryIdleTimeout(entryIdleTimeout);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setEntryTimeToLive(any(ExpirationAttributes.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
ExpirationAttributes entryTimeToLive = invocation.getArgumentAt(0, ExpirationAttributes.class);
attributesFactory.setEntryTimeToLive(entryTimeToLive);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setEvictionAttributes(any(EvictionAttributes.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
EvictionAttributes evictionAttributes = invocation.getArgumentAt(0, EvictionAttributes.class);
attributesFactory.setEvictionAttributes(evictionAttributes);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setInitialCapacity(anyInt())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
int initialCapacity = invocation.getArgumentAt(0, Integer.TYPE);
attributesFactory.setInitialCapacity(initialCapacity);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setKeyConstraint(any(Class.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
Class keyConstraint = invocation.getArgumentAt(0, Class.class);
attributesFactory.setKeyConstraint(keyConstraint);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setLoadFactor(anyFloat())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
float loadFactor = invocation.getArgumentAt(0, Float.TYPE);
attributesFactory.setLoadFactor(loadFactor);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setPoolName(anyString())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
String poolName = invocation.getArgumentAt(0, String.class);
poolName = (StringUtils.hasText(poolName) ? poolName : GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME);
attributesFactory.setPoolName(poolName);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setRegionIdleTimeout(any(ExpirationAttributes.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
ExpirationAttributes regionIdleTimeout = invocation.getArgumentAt(0, ExpirationAttributes.class);
attributesFactory.setRegionIdleTimeout(regionIdleTimeout);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setRegionTimeToLive(any(ExpirationAttributes.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
ExpirationAttributes regionTimeToLive = invocation.getArgumentAt(0, ExpirationAttributes.class);
attributesFactory.setRegionTimeToLive(regionTimeToLive);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setStatisticsEnabled(anyBoolean())).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
attributesFactory.setStatisticsEnabled(Boolean.TRUE.equals(
invocation.getArgumentAt(0, Boolean.class)));
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.setValueConstraint(any(Class.class))).thenAnswer(
new Answer<ClientRegionFactory>() {
@Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable {
Class valueConstraint = invocation.getArgumentAt(0, Class.class);
attributesFactory.setValueConstraint(valueConstraint);
return mockClientRegionFactory;
}
}
);
when(mockClientRegionFactory.addCacheListener(any(CacheListener.class))).thenAnswer(new Answer<ClientRegionFactory>(){
@Override public ClientRegionFactory answer(InvocationOnMock invocation) throws Throwable {
CacheListener cacheListener = (CacheListener) invocation.getArguments()[0];
attributesFactory.addCacheListener(cacheListener);
return mockClientRegionFactory;
}
});
return mockClientRegionFactory;
}
ClientRegionFactory<K, V> createClientRegionFactory() {
return mockClientRegionFactory();
}
ClientRegionFactory<K, V> createClientRegionFactory(ClientRegionShortcut shortcut) {
return mockClientRegionFactory(shortcut);
}
DataPolicy resolveDataPolicy(ClientRegionShortcut clientRegionShortcut) {
clientRegionShortcut = (clientRegionShortcut != null ? clientRegionShortcut : ClientRegionShortcut.LOCAL);
switch (clientRegionShortcut) {
case CACHING_PROXY:
case CACHING_PROXY_HEAP_LRU:
case CACHING_PROXY_OVERFLOW:
case LOCAL:
case LOCAL_HEAP_LRU:
case LOCAL_OVERFLOW:
return DataPolicy.NORMAL;
case LOCAL_PERSISTENT:
case LOCAL_PERSISTENT_OVERFLOW:
return DataPolicy.PERSISTENT_REPLICATE;
case PROXY:
return DataPolicy.EMPTY;
default:
return DataPolicy.NORMAL;
}
}
}

View File

@@ -27,6 +27,7 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.util.ReflectionUtils;
import com.gemstone.gemfire.cache.AttributesMutator;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.CacheLoader;
import com.gemstone.gemfire.cache.CacheWriter;
@@ -39,26 +40,21 @@ import com.gemstone.gemfire.cache.PartitionAttributes;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.RegionService;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.cache.SubscriptionAttributes;
import com.gemstone.gemfire.cache.query.QueryService;
/**
* @author David Turanski
* @author John Blum
*/
@SuppressWarnings("deprecation")
public class MockRegionFactory<K,V> {
public class MockRegionFactory<K, V> {
private static QueryService mockQueryService = mock(QueryService.class);
private static RegionService mockRegionService = mock(RegionService.class);
protected static AttributesMutator mockAttributesMutator = mock(AttributesMutator.class);
private com.gemstone.gemfire.cache.AttributesFactory<K,V> attributesFactory;
protected com.gemstone.gemfire.cache.AttributesFactory<K,V> attributesFactory;
private RegionAttributes regionAttributes;
private final StubCache cache;
protected final StubCache cache;
public MockRegionFactory(StubCache cache) {
this.cache = cache;
@@ -69,13 +65,12 @@ public class MockRegionFactory<K,V> {
}
@SuppressWarnings({ "deprecation", "rawtypes", "unchecked" })
public RegionFactory<K, V> createMockRegionFactory(RegionAttributes<K,V> attributes) {
public RegionFactory<K, V> createMockRegionFactory(RegionAttributes<K, V> attributes) {
attributesFactory = (attributes != null ? new com.gemstone.gemfire.cache.AttributesFactory<K,V>(attributes)
: new com.gemstone.gemfire.cache.AttributesFactory<K,V>());
// Workaround for GemFire bug
// TODO ?!?!?!
if (attributes !=null) {
// Workaround for GemFire bug???
if (attributes != null) {
attributesFactory.setLockGrantor(attributes.isLockGrantor());
}
@@ -434,9 +429,8 @@ public class MockRegionFactory<K,V> {
@SuppressWarnings({ "rawtypes", "unchecked" })
public Region mockRegion(String name) {
regionAttributes = attributesFactory.create();
RegionAttributes<K, V> regionAttributes = attributesFactory.create();
RegionService mockRegionService = mockRegionService();
Region region = mock(Region.class);
when(region.getAttributes()).thenReturn(regionAttributes);
@@ -447,7 +441,7 @@ public class MockRegionFactory<K,V> {
when(region.getFullPath()).thenReturn(regionFullPath);
when(region.getName()).thenReturn(regionName);
when(region.getRegionService()).thenReturn(mockRegionService);
when(region.getRegionService()).thenReturn(cache);
when(region.getSubregion(anyString())).thenAnswer(new Answer<Region>() {
@Override
@@ -471,7 +465,8 @@ public class MockRegionFactory<K,V> {
Region parent = (Region) invocation.getMock();
String parentName = parent.getName();
String regionName = parentName.startsWith("/") ? parentName+"/"+name : "/"+parentName+"/"+ name;
String regionName = parentName
.startsWith("/") ? parentName + "/" + name : "/" + parentName + "/" + name;
Region subRegion = new MockRegionFactory(cache).createMockRegionFactory(attributes).create(regionName);
when(subRegion.getFullPath()).thenReturn(regionName);
@@ -485,13 +480,4 @@ public class MockRegionFactory<K,V> {
return region;
}
public static QueryService mockQueryService() {
return mockQueryService;
}
public static RegionService mockRegionService() {
when(mockRegionService.getQueryService()).thenReturn(mockQueryService());
return mockRegionService;
}
}

View File

@@ -5,7 +5,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -14,6 +16,7 @@ import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.Context;
import org.mockito.invocation.InvocationOnMock;
@@ -32,10 +35,15 @@ import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.RegionExistsException;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.RegionService;
import com.gemstone.gemfire.cache.RegionShortcut;
import com.gemstone.gemfire.cache.TimeoutException;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueueFactory;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.control.ResourceManager;
import com.gemstone.gemfire.cache.hdfs.HDFSStore;
import com.gemstone.gemfire.cache.hdfs.HDFSStoreFactory;
@@ -61,7 +69,9 @@ import com.gemstone.gemfire.pdx.PdxInstanceFactory;
import com.gemstone.gemfire.pdx.PdxSerializer;
@SuppressWarnings({ "deprecation", "unused" })
public class StubCache implements Cache {
public class StubCache implements Cache, ClientCache {
private static final AtomicReference<QueryService> queryServiceReference = new AtomicReference<QueryService>(null);
protected static final String NOT_IMPLEMENTED = "Not Implemented";
@@ -339,9 +349,11 @@ public class StubCache implements Cache {
@Override
public QueryService getQueryService() {
try {
return mockQueryService();
queryServiceReference.compareAndSet(null, mockQueryService());
return queryServiceReference.get();
}
catch (Exception ignore) {
catch (Exception e) {
e.printStackTrace();
return null;
}
}
@@ -841,4 +853,43 @@ public class StubCache implements Cache {
throw new UnsupportedOperationException("Not Implemented!");
}
public RegionService createAuthenticatedView(final Properties userSecurityProperties) {
return this;
}
@Override
public RegionService createAuthenticatedView(final Properties userSecurityProperties, final String poolName) {
return this;
}
@Override
public <K, V> ClientRegionFactory<K, V> createClientRegionFactory(ClientRegionShortcut shortcut) {
return new MockClientRegionFactory<K, V>(this).createClientRegionFactory(shortcut);
}
@Override
public <K, V> ClientRegionFactory<K, V> createClientRegionFactory(String regionAttributesId) {
return new MockClientRegionFactory<K, V>(this).createClientRegionFactory();
}
@Override
public Set<InetSocketAddress> getCurrentServers() {
return Collections.emptySet();
}
@Override
public Pool getDefaultPool() {
return null;
}
@Override
public QueryService getLocalQueryService() {
return getQueryService();
}
@Override
public QueryService getQueryService(final String poolName) {
return getQueryService();
}
}