diff --git a/src/main/java/org/springframework/data/gemfire/GemfireUtils.java b/src/main/java/org/springframework/data/gemfire/GemfireUtils.java index 661fdfa2..20c9097a 100644 --- a/src/main/java/org/springframework/data/gemfire/GemfireUtils.java +++ b/src/main/java/org/springframework/data/gemfire/GemfireUtils.java @@ -25,8 +25,11 @@ import org.w3c.dom.Element; import com.gemstone.gemfire.cache.CacheFactory; import com.gemstone.gemfire.cache.Region; +import com.gemstone.gemfire.cache.client.ClientCache; import com.gemstone.gemfire.cache.client.ClientCacheFactory; +import com.gemstone.gemfire.distributed.DistributedSystem; import com.gemstone.gemfire.internal.GemFireVersion; +import com.gemstone.gemfire.management.internal.cli.util.spring.StringUtils; /** * GemfireUtils is an abstract utility class encapsulating common functionality to access features and capabilities @@ -98,6 +101,16 @@ public abstract class GemfireUtils extends DistributedSystemUtils { return isClassAvailable(GATEWAY_TYPE_NAME); } + public static boolean isDurable(ClientCache clientCache) { + DistributedSystem distributedSystem = clientCache.getDistributedSystem(); + + // NOTE technically the following code snippet would be more useful/valuable but is not "testable"! + //((InternalDistributedSystem) distributedSystem).getConfig().getDurableClientId(); + + return (isConnected(distributedSystem) && StringUtils.hasText(distributedSystem.getProperties() + .getProperty(DURABLE_CLIENT_ID_PROPERTY_NAME, null))); + } + public static boolean closeCache() { try { CacheFactory.getAnyInstance().close(); diff --git a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java index b31024b4..a910041a 100644 --- a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java @@ -57,7 +57,7 @@ import com.gemstone.gemfire.pdx.PdxSerializer; public class ClientCacheFactoryBean extends CacheFactoryBean implements ApplicationListener { protected Boolean keepAlive = false; - protected Boolean readyForEvents = false; + protected Boolean readyForEvents; protected Integer durableClientTimeout; @@ -104,7 +104,7 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat gemfireProperties = distributedSystemProperties; } - DistributedSystemUtils.configureDurableClient(gemfireProperties, durableClientId, durableClientTimeout); + GemfireUtils.configureDurableClient(gemfireProperties, durableClientId, durableClientTimeout); return gemfireProperties; } @@ -389,7 +389,14 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat /* (non-Javadoc) */ public boolean isReadyForEvents() { - return Boolean.TRUE.equals(getReadyForEvents()); + Boolean readyForEvents = getReadyForEvents(); + + if (readyForEvents != null) { + return Boolean.TRUE.equals(readyForEvents); + } + else { + return GemfireUtils.isDurable((ClientCache) fetchCache()); + } } @Override diff --git a/src/main/java/org/springframework/data/gemfire/util/DistributedSystemUtils.java b/src/main/java/org/springframework/data/gemfire/util/DistributedSystemUtils.java index 9eada786..208b924d 100644 --- a/src/main/java/org/springframework/data/gemfire/util/DistributedSystemUtils.java +++ b/src/main/java/org/springframework/data/gemfire/util/DistributedSystemUtils.java @@ -20,6 +20,7 @@ import java.util.Properties; import org.springframework.util.Assert; +import com.gemstone.gemfire.cache.client.ClientCache; import com.gemstone.gemfire.cache.server.CacheServer; import com.gemstone.gemfire.distributed.DistributedSystem; import com.gemstone.gemfire.distributed.internal.DistributionConfig; @@ -57,11 +58,6 @@ public abstract class DistributedSystemUtils { return gemfireProperties; } - @SuppressWarnings("unchecked") - public static T getDistributedSystem() { - return (T) InternalDistributedSystem.getAnyInstance(); - } - public static boolean isConnected(DistributedSystem distributedSystem) { return (distributedSystem != null && distributedSystem.isConnected()); } @@ -70,4 +66,9 @@ public abstract class DistributedSystemUtils { return !isConnected(distributedSystem); } + @SuppressWarnings("unchecked") + public static T getDistributedSystem() { + return (T) InternalDistributedSystem.getAnyInstance(); + } + } diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.8.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.8.xsd index 26729b45..f479f4f0 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.8.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.8.xsd @@ -384,7 +384,7 @@ The name of the pool used by this client. ]]> - + - + 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 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 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 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 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 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 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 fetchCache() { + return (T) mockClientCache; + } + }; + + assertThat(clientCacheFactoryBean.getReadyForEvents(), is(nullValue())); + assertThat(clientCacheFactoryBean.isReadyForEvents(), is(false)); + + verify(mockClientCache, times(1)).getDistributedSystem(); } @Test diff --git a/src/test/java/org/springframework/data/gemfire/client/ClientRegionWithCacheLoaderWriterTest.java b/src/test/java/org/springframework/data/gemfire/client/ClientRegionWithCacheLoaderWriterTest.java index 259d6ca1..e99e366a 100644 --- a/src/test/java/org/springframework/data/gemfire/client/ClientRegionWithCacheLoaderWriterTest.java +++ b/src/test/java/org/springframework/data/gemfire/client/ClientRegionWithCacheLoaderWriterTest.java @@ -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 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); diff --git a/src/test/java/org/springframework/data/gemfire/config/ClientRegionNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/ClientRegionNamespaceTest.java index 8d780d6a..fb797e5a 100644 --- a/src/test/java/org/springframework/data/gemfire/config/ClientRegionNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/ClientRegionNamespaceTest.java @@ -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")); diff --git a/src/test/java/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest.java index 69d982b9..0621cce6 100644 --- a/src/test/java/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest.java @@ -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 { diff --git a/src/test/java/org/springframework/data/gemfire/test/MockClientCacheFactoryBean.java b/src/test/java/org/springframework/data/gemfire/test/MockClientCacheFactoryBean.java index 221fd9ed..cef3f4d6 100644 --- a/src/test/java/org/springframework/data/gemfire/test/MockClientCacheFactoryBean.java +++ b/src/test/java/org/springframework/data/gemfire/test/MockClientCacheFactoryBean.java @@ -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; + } + } diff --git a/src/test/java/org/springframework/data/gemfire/test/MockClientRegionFactory.java b/src/test/java/org/springframework/data/gemfire/test/MockClientRegionFactory.java new file mode 100644 index 00000000..86eb1a7b --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/test/MockClientRegionFactory.java @@ -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 extends MockRegionFactory { + + public MockClientRegionFactory(StubCache cache) { + super(cache); + } + + @SuppressWarnings({ "deprecation", "unchecked" }) + public ClientRegionFactory mockClientRegionFactory() { + return mockClientRegionFactory(ClientRegionShortcut.PROXY); + } + + @SuppressWarnings({ "deprecation", "unchecked" }) + public ClientRegionFactory mockClientRegionFactory(ClientRegionShortcut clientRegionShortcut) { + attributesFactory = new com.gemstone.gemfire.cache.AttributesFactory(); + attributesFactory.setDataPolicy(resolveDataPolicy(clientRegionShortcut)); + + final ClientRegionFactory mockClientRegionFactory = mock(ClientRegionFactory.class, "MockClientRegionFactory"); + + when(mockClientRegionFactory.create(anyString())).thenAnswer(new Answer() { + @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() { + @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() { + @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() { + @Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable { + Compressor compressor = invocation.getArgumentAt(0, Compressor.class); + attributesFactory.setCompressor(compressor); + return mockClientRegionFactory; + } + } + ); + + doAnswer(new Answer() { + @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() { + @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() { + @Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable { + CustomExpiry customEntryIdleTimeout = invocation.getArgumentAt(0, CustomExpiry.class); + attributesFactory.setCustomEntryIdleTimeout(customEntryIdleTimeout); + return mockClientRegionFactory; + } + } + ); + + when(mockClientRegionFactory.setCustomEntryTimeToLive(any(CustomExpiry.class))).thenAnswer( + new Answer() { + @Override public ClientRegionFactory answer(final InvocationOnMock invocation) throws Throwable { + CustomExpiry customEntryTimeToLive = invocation.getArgumentAt(0, CustomExpiry.class); + attributesFactory.setCustomEntryTimeToLive(customEntryTimeToLive); + return mockClientRegionFactory; + } + } + ); + + when(mockClientRegionFactory.setDiskStoreName(anyString())).thenAnswer( + new Answer() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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(){ + @Override public ClientRegionFactory answer(InvocationOnMock invocation) throws Throwable { + CacheListener cacheListener = (CacheListener) invocation.getArguments()[0]; + attributesFactory.addCacheListener(cacheListener); + return mockClientRegionFactory; + } + }); + + return mockClientRegionFactory; + } + + ClientRegionFactory createClientRegionFactory() { + return mockClientRegionFactory(); + } + + ClientRegionFactory 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; + } + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/test/MockRegionFactory.java b/src/test/java/org/springframework/data/gemfire/test/MockRegionFactory.java index 30fad0db..39e7e03f 100644 --- a/src/test/java/org/springframework/data/gemfire/test/MockRegionFactory.java +++ b/src/test/java/org/springframework/data/gemfire/test/MockRegionFactory.java @@ -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 { +public class MockRegionFactory { - 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 attributesFactory; + protected com.gemstone.gemfire.cache.AttributesFactory 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 { } @SuppressWarnings({ "deprecation", "rawtypes", "unchecked" }) - public RegionFactory createMockRegionFactory(RegionAttributes attributes) { + public RegionFactory createMockRegionFactory(RegionAttributes attributes) { attributesFactory = (attributes != null ? new com.gemstone.gemfire.cache.AttributesFactory(attributes) : new com.gemstone.gemfire.cache.AttributesFactory()); - // 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 { @SuppressWarnings({ "rawtypes", "unchecked" }) public Region mockRegion(String name) { - regionAttributes = attributesFactory.create(); + RegionAttributes regionAttributes = attributesFactory.create(); - RegionService mockRegionService = mockRegionService(); Region region = mock(Region.class); when(region.getAttributes()).thenReturn(regionAttributes); @@ -447,7 +441,7 @@ public class MockRegionFactory { 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() { @Override @@ -471,7 +465,8 @@ public class MockRegionFactory { 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 { return region; } - public static QueryService mockQueryService() { - return mockQueryService; - } - - public static RegionService mockRegionService() { - when(mockRegionService.getQueryService()).thenReturn(mockQueryService()); - return mockRegionService; - } - } diff --git a/src/test/java/org/springframework/data/gemfire/test/StubCache.java b/src/test/java/org/springframework/data/gemfire/test/StubCache.java index 213bbbe9..52840bc4 100644 --- a/src/test/java/org/springframework/data/gemfire/test/StubCache.java +++ b/src/test/java/org/springframework/data/gemfire/test/StubCache.java @@ -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 queryServiceReference = new AtomicReference(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 ClientRegionFactory createClientRegionFactory(ClientRegionShortcut shortcut) { + return new MockClientRegionFactory(this).createClientRegionFactory(shortcut); + } + + @Override + public ClientRegionFactory createClientRegionFactory(String regionAttributesId) { + return new MockClientRegionFactory(this).createClientRegionFactory(); + } + + @Override + public Set 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(); + } + } diff --git a/src/test/resources/org/springframework/data/gemfire/client/client-cache-ready-for-events.xml b/src/test/resources/org/springframework/data/gemfire/client/client-cache-ready-for-events.xml index 5ee14337..0c97851c 100644 --- a/src/test/resources/org/springframework/data/gemfire/client/client-cache-ready-for-events.xml +++ b/src/test/resources/org/springframework/data/gemfire/client/client-cache-ready-for-events.xml @@ -1,17 +1,25 @@ + + ClientReadyForEventsTest + 0 + warning + + - + diff --git a/src/test/resources/org/springframework/data/gemfire/client/clientcache-with-region-using-cache-loader-writer.xml b/src/test/resources/org/springframework/data/gemfire/client/clientcache-with-region-using-cache-loader-writer.xml index b38c8df1..038adc2e 100644 --- a/src/test/resources/org/springframework/data/gemfire/client/clientcache-with-region-using-cache-loader-writer.xml +++ b/src/test/resources/org/springframework/data/gemfire/client/clientcache-with-region-using-cache-loader-writer.xml @@ -1,13 +1,21 @@ - + + ClientCacheWithRegionUsingCacheLoaderWriterTest + 0 + warning + + + diff --git a/src/test/resources/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest-context.xml b/src/test/resources/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest-context.xml index 454a5dcf..5de3eef3 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest-context.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/TemplateClientRegionNamespaceTest-context.xml @@ -2,17 +2,25 @@ + + TemplateClientRegionNamespaceTest + 0 + warning + + - +