Add support to mock the GemFireCache.getName() property based on the GemFire distribution configuration properties when using the factories to create cache instances.

This commit is contained in:
John Blum
2019-07-25 14:09:17 -07:00
parent 317e074c84
commit 2272f3e95a
2 changed files with 97 additions and 0 deletions

View File

@@ -138,6 +138,7 @@ import org.apache.geode.cache.wan.GatewayTransportFilter;
import org.apache.geode.compression.Compressor;
import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.internal.DistributionConfig;
import org.apache.geode.internal.cache.PoolManagerImpl;
import org.apache.geode.pdx.PdxSerializer;
import org.apache.lucene.analysis.Analyzer;
@@ -627,6 +628,12 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
when(mockGemFireCache.getDistributedSystem()).thenReturn(mockDistributedSystem);
when(mockGemFireCache.getName()).thenAnswer(invocation -> Optional.ofNullable(gemfireProperties)
.map(AtomicReference::get)
.map(properties -> properties.getProperty(DistributionConfig.NAME_NAME))
.filter(StringUtils::hasText)
.orElse(null));
when(mockGemFireCache.getRegionAttributes(anyString()))
.thenAnswer(invocation -> regionAttributes.get(invocation.<String>getArgument(0)));

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2019 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
*
* https://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.tests;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Properties;
import org.junit.Test;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientCacheFactory;
import org.apache.geode.distributed.internal.DistributionConfig;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport;
/**
* Unit Tests for spying on {@link CacheFactory} and {@link ClientCacheFactory} objects as well as asserting
* the configuration of the mock cache instance created by the factories.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.CacheFactory
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.ClientCacheFactory
* @see org.apache.geode.distributed.internal.DistributionConfig
* @see org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport
* @since 0.0.6
*/
public class CacheAndClientCacheFactorySpiesConfiguresMockCacheNameUnitTests {
@Test
public void cacheFactorySpyConfiguresMockCacheName() {
Properties gemfireProperties = new Properties();
gemfireProperties.setProperty(DistributionConfig.NAME_NAME, "MockCacheName");
CacheFactory cacheFactory = GemFireMockObjectsSupport.spyOn(new CacheFactory(gemfireProperties));
assertThat(cacheFactory).isNotNull();
Cache mockCache = cacheFactory.create();
assertThat(mockCache).isNotNull();
assertThat(mockCache).isNotInstanceOf(GemFireCacheImpl.class);
assertThat(mockCache.getDistributedSystem()).isNotNull();
assertThat(mockCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(mockCache.getDistributedSystem().getProperties().getProperty(DistributionConfig.NAME_NAME))
.isEqualTo("MockCacheName");
assertThat(mockCache.getName()).isEqualTo("MockCacheName");
}
@Test
public void clientCacheFactorySpyConfigureMockClientCacheName() {
ClientCacheFactory clientCacheFactory = GemFireMockObjectsSupport.spyOn(new ClientCacheFactory()
.set(DistributionConfig.NAME_NAME, "MockClientCacheName"));
assertThat(clientCacheFactory).isNotNull();
ClientCache mockClientCache = clientCacheFactory.create();
assertThat(mockClientCache).isNotNull();
assertThat(mockClientCache).isNotInstanceOf(GemFireCacheImpl.class);
assertThat(mockClientCache.getDistributedSystem()).isNotNull();
assertThat(mockClientCache.getDistributedSystem().getProperties()).isNotNull();
assertThat(mockClientCache.getDistributedSystem().getProperties().getProperty(DistributionConfig.NAME_NAME))
.isEqualTo("MockClientCacheName");
assertThat(mockClientCache.getName()).isEqualTo("MockClientCacheName");
}
}