DATAGEODE-33 - Add EnableCachingDefinedRegions annotation to configure Geode Regions based on Spring Caching annotations.
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright 2017 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.annotation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.apache.geode.cache.GemFireCache;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* The EnableCachingDefinedRegionsIntegrationTests class...
|
||||
*
|
||||
* @author John Blum
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
@SuppressWarnings("unused")
|
||||
public class EnableCachingDefinedRegionsIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
private CacheableEchoService echoService;
|
||||
|
||||
@Autowired
|
||||
private GemFireCache gemfireCache;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
//System.err.printf("@Cacheable Beans [%s]%n",
|
||||
// Arrays.toString(applicationContext.getBeanNamesForAnnotation(Cacheable.class)));
|
||||
|
||||
assertThat(this.gemfireCache).isNotNull();
|
||||
|
||||
//System.err.printf("Cache Regions [%s]%n", this.gemfireCache.rootRegions().stream()
|
||||
// .map(Region::getFullPath).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheRegionsExists() {
|
||||
assertThat(gemfireCache.getRegion("/Example")).isNotNull();
|
||||
assertThat(gemfireCache.getRegion("/Echo")).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void echoServiceOperationsAreSuccessful() {
|
||||
assertThat(echoService.isCacheMiss()).isFalse();
|
||||
assertThat(echoService.echo("one")).isEqualTo("one");
|
||||
assertThat(echoService.isCacheMiss()).isTrue();
|
||||
assertThat(echoService.echo("two")).isEqualTo("two");
|
||||
assertThat(echoService.isCacheMiss()).isTrue();
|
||||
assertThat(echoService.echo("one")).isEqualTo("one");
|
||||
assertThat(echoService.isCacheMiss()).isFalse();
|
||||
assertThat(echoService.echo("three")).isEqualTo("three");
|
||||
assertThat(echoService.isCacheMiss()).isTrue();
|
||||
assertThat(echoService.echo("two")).isEqualTo("two");
|
||||
assertThat(echoService.isCacheMiss()).isFalse();
|
||||
}
|
||||
|
||||
@PeerCacheApplication(name = "EnableCachingDefinedRegionsIntegrationTests", logLevel = "warning")
|
||||
@EnableCachingDefinedRegions
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
CacheableEchoService echoService() {
|
||||
return new CacheableEchoService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
TestService testService() {
|
||||
return new DefaultTestService();
|
||||
}
|
||||
}
|
||||
|
||||
@Service
|
||||
static class CacheableEchoService {
|
||||
|
||||
private final AtomicBoolean cacheMiss = new AtomicBoolean(false);
|
||||
|
||||
public boolean isCacheMiss() {
|
||||
return this.cacheMiss.compareAndSet(true, false);
|
||||
}
|
||||
|
||||
@Cacheable("Echo")
|
||||
public Object echo(String key) {
|
||||
this.cacheMiss.set(true);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
interface TestService {
|
||||
Object testMethod(String key);
|
||||
}
|
||||
|
||||
static class DefaultTestService implements TestService {
|
||||
|
||||
@CachePut("Example")
|
||||
public Object testMethod(String key) {
|
||||
return "test";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,699 @@
|
||||
/*
|
||||
* Copyright 2017 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.annotation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
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.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.asArray;
|
||||
import static org.springframework.data.gemfire.util.CollectionUtils.asSet;
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newRuntimeException;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
import org.springframework.data.gemfire.config.xml.GemfireConstants;
|
||||
import org.springframework.data.gemfire.test.support.MapBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EnableCachingDefinedRegions} and {@link CachingDefinedRegionsConfiguration}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.junit.Test
|
||||
* @see org.apache.geode.cache.Region
|
||||
* @see org.springframework.beans.factory.config.BeanDefinition
|
||||
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry
|
||||
* @see org.springframework.cache.annotation.Cacheable
|
||||
* @see org.springframework.cache.annotation.CacheEvict
|
||||
* @see org.springframework.cache.annotation.CachePut
|
||||
* @see org.springframework.cache.annotation.Caching
|
||||
* @see org.springframework.stereotype.Service
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class EnableCachingDefinedRegionsUnitTests {
|
||||
|
||||
// Subject Under Test (SUT)
|
||||
private CachingDefinedRegionsConfiguration configuration = new CachingDefinedRegionsConfiguration();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private BeanDefinition mockBeanDefinition(Class<?> beanClass) {
|
||||
|
||||
try {
|
||||
|
||||
AbstractBeanDefinition mockBeanDefinition = mock(AbstractBeanDefinition.class, beanClass.getSimpleName());
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn(beanClass.getName());
|
||||
when(mockBeanDefinition.resolveBeanClass(any(ClassLoader.class))).thenReturn((Class) beanClass);
|
||||
when(mockBeanDefinition.getRole()).thenReturn(BeanDefinition.ROLE_APPLICATION);
|
||||
|
||||
return mockBeanDefinition;
|
||||
}
|
||||
catch (ClassNotFoundException cause) {
|
||||
throw newRuntimeException(cause, "Mock for class [%s] failed", beanClass.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private BeanDefinitionRegistry mockBeanDefinitionRegistry(Map<String, BeanDefinition> registeredBeanDefinitions) {
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
|
||||
|
||||
when(mockBeanDefinitionRegistry.getBeanDefinitionNames())
|
||||
.thenReturn(registeredBeanDefinitions.keySet().toArray(new String[registeredBeanDefinitions.size()]));
|
||||
|
||||
when(mockBeanDefinitionRegistry.getBeanDefinition(anyString()))
|
||||
.thenAnswer(invocation -> registeredBeanDefinitions.get(invocation.<String>getArgument(0)));
|
||||
|
||||
when(mockBeanDefinitionRegistry.containsBeanDefinition(anyString()))
|
||||
.thenAnswer(invocation -> registeredBeanDefinitions.containsKey(invocation.<String>getArgument(0)));
|
||||
|
||||
doAnswer(invocation -> registeredBeanDefinitions.put(invocation.getArgument(0), invocation.getArgument(1)))
|
||||
.when(mockBeanDefinitionRegistry).registerBeanDefinition(anyString(), any(BeanDefinition.class));
|
||||
|
||||
return mockBeanDefinitionRegistry;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheableServiceOneRegistersRegionsOneAndTwo() {
|
||||
|
||||
Map<String, BeanDefinition> registeredBeanDefinitions = MapBuilder.<String, BeanDefinition>newMapBuilder()
|
||||
.put("cacheableServiceOne", mockBeanDefinition(CacheableServiceOne.class))
|
||||
.build();
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mockBeanDefinitionRegistry(registeredBeanDefinitions);
|
||||
|
||||
this.configuration.registerBeanDefinitions(mockBeanDefinitionRegistry);
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1)).getBeanDefinitionNames();
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceOne"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.containsBeanDefinition(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(2))
|
||||
.registerBeanDefinition(anyString(), any(BeanDefinition.class));
|
||||
|
||||
Arrays.asList("RegionOne", "RegionTwo").forEach(beanName -> {
|
||||
verify(mockBeanDefinitionRegistry, times(1)).containsBeanDefinition(eq(beanName));
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.registerBeanDefinition(eq(beanName), any(BeanDefinition.class));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheableServiceTwoRegistersRegionsTwoThreeFourFiveAndSix() {
|
||||
|
||||
Map<String, BeanDefinition> registeredBeanDefinitions = MapBuilder.<String, BeanDefinition>newMapBuilder()
|
||||
.put("cacheableServiceTwo", mockBeanDefinition(CacheableServiceTwo.class))
|
||||
.build();
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mockBeanDefinitionRegistry(registeredBeanDefinitions);
|
||||
|
||||
this.configuration.registerBeanDefinitions(mockBeanDefinitionRegistry);
|
||||
|
||||
Set<String> registeredRegionBeanNames =
|
||||
asSet("RegionTwo", "RegionThree", "RegionFour", "RegionFive", "RegionSix");
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1)).getBeanDefinitionNames();
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceTwo"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.containsBeanDefinition(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(registeredRegionBeanNames.size()))
|
||||
.registerBeanDefinition(anyString(), any(BeanDefinition.class));
|
||||
|
||||
registeredRegionBeanNames.forEach(beanName -> {
|
||||
verify(mockBeanDefinitionRegistry, times(1)).containsBeanDefinition(eq(beanName));
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.registerBeanDefinition(eq(beanName), any(BeanDefinition.class));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheableServiceThreeRegistersSeventeenRegionBeans() {
|
||||
|
||||
Map<String, BeanDefinition> registeredBeanDefinitions = MapBuilder.<String, BeanDefinition>newMapBuilder()
|
||||
.put("cacheableServiceThree", mockBeanDefinition(CacheableServiceThree.class))
|
||||
.build();
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mockBeanDefinitionRegistry(registeredBeanDefinitions);
|
||||
|
||||
this.configuration.registerBeanDefinitions(mockBeanDefinitionRegistry);
|
||||
|
||||
Set<String> registeredRegionBeanNames =
|
||||
asSet("RegionSix", "RegionSeven", "RegionEight", "RegionNine", "RegionTen", "RegionEleven",
|
||||
"RegionTwelve", "RegionThirteen", "RegionFourteen", "RegionFifteen", "RegionSixteen", "RegionSeventeen",
|
||||
"RegionEighteen", "RegionNineteen", "RegionTwenty", "RegionTwentyOne", "RegionTwentyFive");
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1)).getBeanDefinitionNames();
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceThree"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.containsBeanDefinition(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(registeredRegionBeanNames.size()))
|
||||
.registerBeanDefinition(anyString(), any(BeanDefinition.class));
|
||||
|
||||
registeredRegionBeanNames.forEach(beanName -> {
|
||||
verify(mockBeanDefinitionRegistry, times(1)).containsBeanDefinition(eq(beanName));
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.registerBeanDefinition(eq(beanName), any(BeanDefinition.class));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheableServiceFourRegistersNineteenRegionBeans() {
|
||||
|
||||
Map<String, BeanDefinition> registeredBeanDefinitions = MapBuilder.<String, BeanDefinition>newMapBuilder()
|
||||
.put("cacheableServiceFour", mockBeanDefinition(CacheableServiceFour.class))
|
||||
.build();
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mockBeanDefinitionRegistry(registeredBeanDefinitions);
|
||||
|
||||
this.configuration.registerBeanDefinitions(mockBeanDefinitionRegistry);
|
||||
|
||||
Set<String> registeredRegionBeanNames =
|
||||
asSet("RegionSix", "RegionSeven", "RegionEight", "RegionNine", "RegionTen", "RegionEleven",
|
||||
"RegionTwelve", "RegionThirteen", "RegionFourteen", "RegionFifteen", "RegionSixteen", "RegionSeventeen",
|
||||
"RegionEighteen", "RegionNineteen", "RegionTwenty", "RegionTwentyOne", "RegionTwentyTwo",
|
||||
"RegionTwentyThree", "RegionTwentyFour");
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1)).getBeanDefinitionNames();
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceFour"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.containsBeanDefinition(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, never())
|
||||
.registerBeanDefinition(eq("RegionTwentyFive"), any(BeanDefinition.class));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(registeredRegionBeanNames.size()))
|
||||
.registerBeanDefinition(anyString(), any(BeanDefinition.class));
|
||||
|
||||
registeredRegionBeanNames.forEach(beanName ->
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.registerBeanDefinition(eq(beanName), any(BeanDefinition.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheableServiceOneTwoThreeRegistersTwentyTwoRegionBeans() {
|
||||
|
||||
Map<String, BeanDefinition> registeredBeanDefinitions = MapBuilder.<String, BeanDefinition>newMapBuilder()
|
||||
.put("cacheableServiceOne", mockBeanDefinition(CacheableServiceOne.class))
|
||||
.put("cacheableServiceTwo", mockBeanDefinition(CacheableServiceTwo.class))
|
||||
.put("cacheableServiceThree", mockBeanDefinition(CacheableServiceThree.class))
|
||||
.build();
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mockBeanDefinitionRegistry(registeredBeanDefinitions);
|
||||
|
||||
this.configuration.registerBeanDefinitions(mockBeanDefinitionRegistry);
|
||||
|
||||
Set<String> registeredRegionBeanNames =
|
||||
asSet("RegionOne", "RegionTwo", "RegionThree", "RegionFour", "RegionFive", "RegionSix",
|
||||
"RegionSeven", "RegionEight", "RegionNine", "RegionTen", "RegionEleven", "RegionTwelve",
|
||||
"RegionThirteen", "RegionFourteen", "RegionFifteen", "RegionSixteen", "RegionSeventeen",
|
||||
"RegionEighteen", "RegionNineteen", "RegionTwenty", "RegionTwentyOne", "RegionTwentyFive");
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1)).getBeanDefinitionNames();
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceOne"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceTwo"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.getBeanDefinition(eq("cacheableServiceThree"));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(3))
|
||||
.containsBeanDefinition(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(registeredRegionBeanNames.size()))
|
||||
.registerBeanDefinition(anyString(), any(BeanDefinition.class));
|
||||
|
||||
registeredRegionBeanNames.forEach(beanName ->
|
||||
verify(mockBeanDefinitionRegistry, times(1))
|
||||
.registerBeanDefinition(eq(beanName), any(BeanDefinition.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void collectCacheNamesForCacheableAndCachePutOnCacheableServiceThreeRegistersRegionFifteenSixteenSeventeen() {
|
||||
|
||||
assertThat(this.configuration.collectCacheNames(CacheableServiceThree.class, Cacheable.class, CachePut.class))
|
||||
.containsExactly("RegionFifteen", "RegionSixteen", "RegionSeventeen");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void collectCacheNamesForCacheableOnCacheableServiceThreeRegistersRegionFifteen() {
|
||||
|
||||
assertThat(this.configuration.collectCacheNames(CacheableServiceThree.class, Cacheable.class))
|
||||
.containsExactly("RegionFifteen");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void collectCacheNamesForCachePutOnCacheableServiceThreeRegistersRegionSixteenSeventeen() {
|
||||
|
||||
assertThat(this.configuration.collectCacheNames(CacheableServiceThree.class, CachePut.class))
|
||||
.containsExactly("RegionSixteen", "RegionSeventeen");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void resolveBeanClassFromBeanClassName() throws ClassNotFoundException {
|
||||
|
||||
AbstractBeanDefinition mockBeanDefinition = mock(AbstractBeanDefinition.class);
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
|
||||
|
||||
when(mockBeanDefinition.resolveBeanClass(any(ClassLoader.class))).thenReturn((Class) CacheableServiceOne.class);
|
||||
|
||||
assertThat(this.configuration.resolveBeanClass(mockBeanDefinition, mockBeanDefinitionRegistry).orElse(null))
|
||||
.isEqualTo(CacheableServiceOne.class);
|
||||
|
||||
verify(mockBeanDefinition, times(1))
|
||||
.resolveBeanClass(eq(Thread.currentThread().getContextClassLoader()));
|
||||
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
|
||||
verifyZeroInteractions(mockBeanDefinitionRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void resolveBeanClassFromFactoryMethodReturnType() throws ClassNotFoundException {
|
||||
|
||||
AnnotatedBeanDefinition mockBeanDefinition = mock(AnnotatedBeanDefinition.class);
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
|
||||
|
||||
MethodMetadata mockMethodMetadata = mock(MethodMetadata.class);
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn(" ");
|
||||
when(mockBeanDefinition.getFactoryMethodName()).thenReturn("testFactoryMethod");
|
||||
when(mockBeanDefinition.getFactoryMethodMetadata()).thenReturn(mockMethodMetadata);
|
||||
when(mockMethodMetadata.getReturnTypeName()).thenReturn(CacheableServiceTwo.class.getName());
|
||||
|
||||
assertThat(this.configuration.resolveBeanClass(mockBeanDefinition, mockBeanDefinitionRegistry).orElse(null))
|
||||
.isEqualTo(CacheableServiceTwo.class);
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodMetadata();
|
||||
verify(mockMethodMetadata, times(1)).getReturnTypeName();
|
||||
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
verifyNoMoreInteractions(mockMethodMetadata);
|
||||
|
||||
verifyZeroInteractions(mockBeanDefinitionRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureBeanIsTrue() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mockBeanDefinition(CacheableServiceOne.class);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureBean(mockBeanDefinition)).isTrue();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getRole();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureBeanWithInfrastructureClassIsFalse() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mockBeanDefinition(SpringVersion.class);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureBean(mockBeanDefinition)).isFalse();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getRole();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureBeanWithInfrastructureRoleIsFalse() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mockBeanDefinition(CacheableServiceOne.class);
|
||||
|
||||
when(mockBeanDefinition.getRole()).thenReturn(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureBean(mockBeanDefinition)).isFalse();
|
||||
|
||||
verify(mockBeanDefinition, never()).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getRole();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureClassWithBeanDefinitionIsTrue() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mockBeanDefinition(CacheableServiceOne.class);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureClass(mockBeanDefinition)).isTrue();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureClassWithBeanDefinitionIsFalse() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mockBeanDefinition(SpringVersion.class);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureClass(mockBeanDefinition)).isFalse();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureClassIsTrue() {
|
||||
assertThat(this.configuration.isNotInfrastructureClass(CacheableServiceOne.class.getName())).isTrue();
|
||||
assertThat(this.configuration.isNotInfrastructureClass("org.example.app.MyClass")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureClassIsFalse() {
|
||||
assertThat(this.configuration.isNotInfrastructureClass("org.springframework.SomeType")).isFalse();
|
||||
assertThat(this.configuration.isNotInfrastructureClass("org.springframework.core.type.SomeType"))
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureRoleIsTrue() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
|
||||
|
||||
when(mockBeanDefinition.getRole()).thenReturn(BeanDefinition.ROLE_APPLICATION).thenReturn(Integer.MAX_VALUE);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureRole(mockBeanDefinition)).isTrue();
|
||||
assertThat(this.configuration.isNotInfrastructureRole(mockBeanDefinition)).isTrue();
|
||||
|
||||
verify(mockBeanDefinition, times(2)).getRole();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotInfrastructureRoleIsFalse() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
|
||||
|
||||
when(mockBeanDefinition.getRole()).thenReturn(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
.thenReturn(BeanDefinition.ROLE_SUPPORT);
|
||||
|
||||
assertThat(this.configuration.isNotInfrastructureRole(mockBeanDefinition)).isFalse();
|
||||
assertThat(this.configuration.isNotInfrastructureRole(mockBeanDefinition)).isFalse();
|
||||
|
||||
verify(mockBeanDefinition, times(2)).getRole();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUserLevelMethodWithNullMethodReturnsFalse() {
|
||||
assertThat(this.configuration.isUserLevelMethod(null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUserLevelMethodWithObjectMethodReturnsFalse() throws NoSuchMethodException {
|
||||
assertThat(this.configuration.isUserLevelMethod(Object.class.getMethod("equals", Object.class)))
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUserLevelMethodWithUserMethodReturnsTrue() throws NoSuchMethodException {
|
||||
assertThat(this.configuration.isUserLevelMethod(CacheableServiceOne.class.getMethod("cacheableMethodOne")))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveAnnotationFromClass() {
|
||||
|
||||
Annotation cacheable = this.configuration.resolveAnnotation(CacheableServiceFour.class, Cacheable.class);
|
||||
|
||||
assertThat(cacheable).isNotNull();
|
||||
assertThat(AnnotationUtils.getAnnotationAttributes(cacheable).get("cacheNames"))
|
||||
.isEqualTo(asArray("RegionFifteen"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveAnnotationFromMethod() throws NoSuchMethodException {
|
||||
|
||||
Method cacheableMethodFour = CacheableServiceFour.class.getMethod("cacheableMethodFour");
|
||||
|
||||
Annotation cachePut = this.configuration.resolveAnnotation(cacheableMethodFour, CachePut.class);
|
||||
|
||||
assertThat(cachePut).isNotNull();
|
||||
assertThat(AnnotationUtils.getAnnotationAttributes(cachePut).get("cacheNames"))
|
||||
.isEqualTo(asArray("RegionTwentyOne", "RegionTwentyTwo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveAnnotationIsUnresolvable() throws NoSuchMethodException {
|
||||
|
||||
Method cacheableMethodFive = CacheableServiceFour.class.getMethod("cacheableMethodFive");
|
||||
|
||||
assertThat(this.configuration.resolveAnnotation(cacheableMethodFive, Cacheable.class)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassIsUnresolvable() throws ClassNotFoundException {
|
||||
|
||||
AbstractBeanDefinition mockBeanDefinition = mock(AbstractBeanDefinition.class);
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn("non.existing.bean.Class");
|
||||
when(mockBeanDefinition.resolveBeanClass(any(ClassLoader.class))).thenThrow(new ClassNotFoundException("TEST"));
|
||||
|
||||
assertThat(this.configuration.resolveBeanClass(mockBeanDefinition, null).orElse(null)).isNull();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1))
|
||||
.resolveBeanClass(eq(Thread.currentThread().getContextClassLoader()));
|
||||
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassLoaderReturnsRegistryClassLoader() {
|
||||
|
||||
ClassLoader mockClassLoader = mock(ClassLoader.class);
|
||||
|
||||
DefaultListableBeanFactory mockBeanDefinitionRegistry = mock(DefaultListableBeanFactory.class);
|
||||
|
||||
when(mockBeanDefinitionRegistry.getBeanClassLoader()).thenReturn(mockClassLoader);
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassLoader(mockBeanDefinitionRegistry)).isEqualTo(mockClassLoader);
|
||||
|
||||
verify(mockBeanDefinitionRegistry, times(1)).getBeanClassLoader();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassLoaderReturnsThreadContextClassLoader() {
|
||||
|
||||
BeanDefinitionRegistry mockBeanDefinitionRegistry = mock(BeanDefinitionRegistry.class);
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassLoader(mockBeanDefinitionRegistry))
|
||||
.isEqualTo(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
verifyZeroInteractions(mockBeanDefinitionRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassNameReturnsBeanDefinitionBeanClassName() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mockBeanDefinition(CacheableServiceOne.class);
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassName(mockBeanDefinition).orElse(null))
|
||||
.isEqualTo(CacheableServiceOne.class.getName());
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassNameReturnsFactoryMethodReturnType() {
|
||||
|
||||
AnnotatedBeanDefinition mockBeanDefinition = mock(AnnotatedBeanDefinition.class);
|
||||
|
||||
MethodMetadata mockMethodMetadata = mock(MethodMetadata.class);
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn(null);
|
||||
when(mockBeanDefinition.getFactoryMethodName()).thenReturn("testFactoryMethod");
|
||||
when(mockBeanDefinition.getFactoryMethodMetadata()).thenReturn(mockMethodMetadata);
|
||||
when(mockMethodMetadata.getReturnTypeName()).thenReturn(CacheableServiceTwo.class.getName());
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassName(mockBeanDefinition).orElse(null))
|
||||
.isEqualTo(CacheableServiceTwo.class.getName());
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodMetadata();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassNameWithNoFactoryMethodMetadataReturnsEmpty() {
|
||||
|
||||
AnnotatedBeanDefinition mockBeanDefinition = mock(AnnotatedBeanDefinition.class);
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn(null);
|
||||
when(mockBeanDefinition.getFactoryMethodName()).thenReturn("testFactoryMethod");
|
||||
when(mockBeanDefinition.getFactoryMethodMetadata()).thenReturn(null);
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassName(mockBeanDefinition).orElse(null)).isNull();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodMetadata();
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassNameWithNonAnnotatedBeanDefinitionReturnsEmpty() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn(null);
|
||||
when(mockBeanDefinition.getFactoryMethodName()).thenReturn("testFactoryMethod");
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassName(mockBeanDefinition).orElse(null)).isNull();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodName();
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveBeanClassNameWithNoFactoryMethodNameReturnsEmpty() {
|
||||
|
||||
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
|
||||
|
||||
when(mockBeanDefinition.getBeanClassName()).thenReturn(null);
|
||||
when(mockBeanDefinition.getFactoryMethodName()).thenReturn(" ");
|
||||
|
||||
assertThat(this.configuration.resolveBeanClassName(mockBeanDefinition).orElse(null)).isNull();
|
||||
|
||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||
verify(mockBeanDefinition, times(1)).getFactoryMethodName();
|
||||
verifyNoMoreInteractions(mockBeanDefinition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeResolveTypeReturnsType() {
|
||||
assertThat(this.configuration.safeResolveType(() -> Object.class)).isEqualTo(Object.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeResolveTypeThrowingClassNotFoundExceptionReturnsNull() {
|
||||
assertThat(this.configuration.safeResolveType(() -> { throw new ClassNotFoundException("TEST"); })).isNull();
|
||||
}
|
||||
|
||||
@Service
|
||||
@Cacheable(cacheNames = { "RegionOne", "RegionTwo" })
|
||||
@SuppressWarnings("unused")
|
||||
static class CacheableServiceOne {
|
||||
|
||||
public void cacheableMethodOne() {}
|
||||
|
||||
}
|
||||
|
||||
@Service
|
||||
@Cacheable(cacheNames = { "RegionTwo" })
|
||||
@CachePut("RegionSix")
|
||||
@SuppressWarnings("unused")
|
||||
static class CacheableServiceTwo {
|
||||
|
||||
@Cacheable("RegionThree")
|
||||
public void cacheableMethodOne() {}
|
||||
|
||||
@CacheEvict(cacheNames = { "RegionThree", "RegionFour" })
|
||||
public void cacheableMethodTwo() {}
|
||||
|
||||
@CachePut(cacheNames = "RegionFive")
|
||||
public void cacheableMethodThree() {}
|
||||
|
||||
}
|
||||
|
||||
@Service
|
||||
@Caching(
|
||||
cacheable = { @Cacheable(cacheNames = { "RegionSix", "RegionSeven" }), @Cacheable("RegionEight") },
|
||||
evict = { @CacheEvict(cacheNames = { "RegionNine", "RegionTen"}), @CacheEvict("RegionEleven") },
|
||||
put = { @CachePut(cacheNames = { "RegionTwelve", "RegionThirteen" }), @CachePut("RegionFourteen") }
|
||||
)
|
||||
@Cacheable("RegionFifteen")
|
||||
@CachePut(cacheNames = { "RegionSixteen", "RegionSeventeen" })
|
||||
@SuppressWarnings("unused")
|
||||
static class CacheableServiceThree {
|
||||
|
||||
@Caching(
|
||||
cacheable = { @Cacheable(cacheNames = { "RegionSix", "RegionTwelve" }), @Cacheable("RegionEighteen") },
|
||||
put = { @CachePut({ "RegionTen", "RegionNineteen" }), @CachePut("RegionTwenty") }
|
||||
)
|
||||
public void cacheableMethodOne() {}
|
||||
|
||||
@CachePut("RegionTwentyOne")
|
||||
public void cacheableMethodTwo() {}
|
||||
|
||||
@Cacheable("RegionTwentyFive")
|
||||
public void cacheableMethodFour() {}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class CacheableServiceFour extends CacheableServiceThree {
|
||||
|
||||
@Cacheable({ "RegionEleven", "RegionTwentyTwo", "RegionTwentyThree", "RegionTwentyFour" })
|
||||
public void cacheableMethodThree() {}
|
||||
|
||||
@Override
|
||||
@CachePut({ "RegionTwentyOne", "RegionTwentyTwo" })
|
||||
public void cacheableMethodFour() {}
|
||||
|
||||
public void cacheableMethodFive() {}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,7 @@ import org.springframework.data.gemfire.mapping.annotation.ReplicateRegion;
|
||||
* @see org.springframework.data.gemfire.config.annotation.EntityDefinedRegionsConfiguration
|
||||
* @since 1.9.0
|
||||
*/
|
||||
public class EnableEntityDefinedRegionsConfigurationUnitTests {
|
||||
public class EnableEntityDefinedRegionsUnitTests {
|
||||
|
||||
private static final AtomicInteger MOCK_ID = new AtomicInteger(0);
|
||||
|
||||
@@ -77,9 +77,12 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
}
|
||||
|
||||
protected Region<?, ?> mockRegion(String regionFullPath) {
|
||||
|
||||
Region<?, ?> mockRegion = mock(Region.class);
|
||||
|
||||
when(mockRegion.getFullPath()).thenReturn(regionFullPath);
|
||||
when(mockRegion.getName()).thenReturn(toRegionName(regionFullPath));
|
||||
|
||||
return mockRegion;
|
||||
}
|
||||
|
||||
@@ -90,6 +93,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void setAndGetBeanFactory() {
|
||||
|
||||
autoRegionLookupBeanPostProcessor.setBeanFactory(mockBeanFactory);
|
||||
|
||||
assertThat(autoRegionLookupBeanPostProcessor.getBeanFactory()).isSameAs(mockBeanFactory);
|
||||
@@ -97,6 +101,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void setBeanFactoryToIncompatibleBeanFactoryType() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
@@ -108,7 +113,9 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void setBeanFactoryToNull() {
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage(String.format("BeanFactory [null] must be an instance of %s",
|
||||
@@ -119,15 +126,17 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void getBeanFactoryUninitialized() {
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage("BeanFactory was not properly initialized");
|
||||
exception.expectMessage("BeanFactory was not properly configured");
|
||||
|
||||
autoRegionLookupBeanPostProcessor.getBeanFactory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessBeforeInitializationReturnsBean() {
|
||||
|
||||
Object bean = new Object();
|
||||
|
||||
assertThat(autoRegionLookupBeanPostProcessor.postProcessBeforeInitialization(bean, "test")).isSameAs(bean);
|
||||
@@ -135,6 +144,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void postProcessAfterInitializationWithNonGemFireCacheBean() {
|
||||
|
||||
Object bean = new Object();
|
||||
|
||||
AutoRegionLookupBeanPostProcessor autoRegionLookupBeanPostProcessorSpy =
|
||||
@@ -147,8 +157,11 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void registerCacheRegionsAsBeansIsSuccessful() {
|
||||
Set<Region<?, ?>> expected = CollectionUtils.asSet(mockRegion("one"), mockRegion("two"), mockRegion("three"));
|
||||
final Set<Region<?, ?>> actual = new HashSet<Region<?, ?>>(expected.size());
|
||||
|
||||
Set<Region<?, ?>> expected = CollectionUtils.asSet(mockRegion("one"),
|
||||
mockRegion("two"), mockRegion("three"));
|
||||
|
||||
Set<Region<?, ?>> actual = new HashSet<>(expected.size());
|
||||
|
||||
AutoRegionLookupBeanPostProcessor autoRegionLookupBeanPostProcessor = new AutoRegionLookupBeanPostProcessor() {
|
||||
@Override void registerCacheRegionAsBean(Region<?, ?> region) {
|
||||
@@ -173,9 +186,10 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void registerCacheRegionAsBeanIsSuccessful() {
|
||||
|
||||
Region<?, ?> mockRegion = mockRegion("Example");
|
||||
|
||||
when(mockRegion.subregions(anyBoolean())).thenReturn(Collections.<Region<?, ?>>emptySet());
|
||||
when(mockRegion.subregions(anyBoolean())).thenReturn(Collections.emptySet());
|
||||
when(mockBeanFactory.containsBean(anyString())).thenReturn(false);
|
||||
|
||||
autoRegionLookupBeanPostProcessor.setBeanFactory(mockBeanFactory);
|
||||
@@ -190,11 +204,12 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void registerCacheRegionAsBeanRegistersSubRegionIgnoresRootRegion() {
|
||||
|
||||
Region<?, ?> mockRootRegion = mockRegion("Root");
|
||||
Region<?, ?> mockSubRegion = mockRegion("/Root/Sub");
|
||||
|
||||
when(mockRootRegion.subregions(anyBoolean())).thenReturn(CollectionUtils.<Region<?, ?>>asSet(mockSubRegion));
|
||||
when(mockSubRegion.subregions(anyBoolean())).thenReturn(Collections.<Region<?, ?>>emptySet());
|
||||
when(mockRootRegion.subregions(anyBoolean())).thenReturn(CollectionUtils.asSet(mockSubRegion));
|
||||
when(mockSubRegion.subregions(anyBoolean())).thenReturn(Collections.emptySet());
|
||||
when(mockBeanFactory.containsBean(eq("Root"))).thenReturn(true);
|
||||
when(mockBeanFactory.containsBean(eq("/Root/Sub"))).thenReturn(false);
|
||||
|
||||
@@ -215,6 +230,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void registerNullCacheRegionAsBeanDoesNothing() {
|
||||
|
||||
autoRegionLookupBeanPostProcessor.setBeanFactory(mockBeanFactory);
|
||||
autoRegionLookupBeanPostProcessor.registerCacheRegionAsBean(null);
|
||||
|
||||
@@ -223,6 +239,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void getBeanNameReturnsRegionFullPath() {
|
||||
|
||||
Region mockRegion = mockRegion("/Parent/Child");
|
||||
|
||||
assertThat(autoRegionLookupBeanPostProcessor.getBeanName(mockRegion)).isEqualTo("/Parent/Child");
|
||||
@@ -233,6 +250,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void getBeanNameReturnsRegionName() {
|
||||
|
||||
Region mockRegion = mockRegion("/Example");
|
||||
|
||||
assertThat(autoRegionLookupBeanPostProcessor.getBeanName(mockRegion)).isEqualTo("Example");
|
||||
@@ -243,7 +261,10 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void nullSafeSubRegionsWhenSubRegionsIsNotNull() {
|
||||
Set<Region<?, ?>> mockSubRegions = CollectionUtils.asSet(mockRegion("one"), mockRegion("two"));
|
||||
|
||||
Set<Region<?, ?>> mockSubRegions =
|
||||
CollectionUtils.asSet(mockRegion("one"), mockRegion("two"));
|
||||
|
||||
Region mockRegion = mockRegion("parent");
|
||||
|
||||
when(mockRegion.subregions(anyBoolean())).thenReturn(mockSubRegions);
|
||||
@@ -255,6 +276,7 @@ public class AutoRegionLookupBeanPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void nullSafeSubRegionsWhenSubRegionsIsNull() {
|
||||
|
||||
Region mockRegion = mockRegion("parent");
|
||||
|
||||
when(mockRegion.subregions(anyBoolean())).thenReturn(null);
|
||||
|
||||
@@ -34,8 +34,6 @@ import org.apache.geode.cache.Region;
|
||||
import org.apache.geode.cache.asyncqueue.AsyncEventQueue;
|
||||
import org.apache.geode.internal.cache.PartitionedRegion;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
@@ -68,49 +66,49 @@ public class PdxDiskStoreAwareBeanFactoryPostProcessorUnitTests {
|
||||
}
|
||||
|
||||
protected ConfigurableListableBeanFactory mockBeanFactory(final Map<String, BeanDefinition> beanDefinitions) {
|
||||
final ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class);
|
||||
|
||||
ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class);
|
||||
|
||||
when(mockBeanFactory.getBeanDefinitionNames()).thenReturn(toStringArray(beanDefinitions.keySet()));
|
||||
|
||||
when(mockBeanFactory.getBeanNamesForType(isA(Class.class))).then(new Answer<String[]>() {
|
||||
@Override
|
||||
public String[] answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] arguments = invocation.getArguments();
|
||||
when(mockBeanFactory.getBeanNamesForType(isA(Class.class))).then(invocation -> {
|
||||
|
||||
assertThat(arguments).isNotNull();
|
||||
assertThat(arguments.length).isEqualTo(1);
|
||||
assertThat(arguments[0]).isInstanceOf(Class.class);
|
||||
Object[] arguments = invocation.getArguments();
|
||||
|
||||
Class beanType = (Class) arguments[0];
|
||||
assertThat(arguments).isNotNull();
|
||||
assertThat(arguments.length).isEqualTo(1);
|
||||
assertThat(arguments[0]).isInstanceOf(Class.class);
|
||||
|
||||
List<String> beanNames = new ArrayList<String>(beanDefinitions.size());
|
||||
Class beanType = (Class) arguments[0];
|
||||
|
||||
for (Map.Entry<String, BeanDefinition> entry : beanDefinitions.entrySet()) {
|
||||
BeanDefinition beanDefinition = entry.getValue();
|
||||
List<String> beanNames = new ArrayList<>(beanDefinitions.size());
|
||||
|
||||
if (isBeanType(beanDefinition, beanType)) {
|
||||
beanNames.add(entry.getKey());
|
||||
}
|
||||
for (Map.Entry<String, BeanDefinition> entry : beanDefinitions.entrySet()) {
|
||||
BeanDefinition beanDefinition = entry.getValue();
|
||||
|
||||
if (isBeanType(beanDefinition, beanType)) {
|
||||
beanNames.add(entry.getKey());
|
||||
}
|
||||
|
||||
return toStringArray(beanNames);
|
||||
}
|
||||
|
||||
return toStringArray(beanNames);
|
||||
});
|
||||
|
||||
when(mockBeanFactory.getBeanDefinition(anyString())).then(new Answer<BeanDefinition>() {
|
||||
@Override
|
||||
public BeanDefinition answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] arguments = invocation.getArguments();
|
||||
assertThat(arguments).isNotNull();
|
||||
assertThat(arguments.length).isEqualTo(1);
|
||||
return beanDefinitions.get(String.valueOf(arguments[0]));
|
||||
}
|
||||
when(mockBeanFactory.getBeanDefinition(anyString())).then(invocation -> {
|
||||
|
||||
Object[] arguments = invocation.getArguments();
|
||||
|
||||
assertThat(arguments).isNotNull();
|
||||
assertThat(arguments.length).isEqualTo(1);
|
||||
|
||||
return beanDefinitions.get(String.valueOf(arguments[0]));
|
||||
});
|
||||
|
||||
return mockBeanFactory;
|
||||
}
|
||||
|
||||
protected static BeanDefinitionBuilder newBeanDefinitionBuilder(Object beanClassObject, String... dependencies) {
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
|
||||
|
||||
if (beanClassObject instanceof Class) {
|
||||
@@ -124,6 +122,7 @@ public class PdxDiskStoreAwareBeanFactoryPostProcessorUnitTests {
|
||||
}
|
||||
|
||||
protected static BeanDefinitionBuilder addDependsOn(BeanDefinitionBuilder builder, String... dependencies) {
|
||||
|
||||
for (String dependency : dependencies) {
|
||||
builder.addDependsOn(dependency);
|
||||
}
|
||||
@@ -181,6 +180,7 @@ public class PdxDiskStoreAwareBeanFactoryPostProcessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void initializedPdxDiskStoreAwareBeanFactoryPostProcessor() {
|
||||
|
||||
PdxDiskStoreAwareBeanFactoryPostProcessor postProcessor =
|
||||
new PdxDiskStoreAwareBeanFactoryPostProcessor("testPdxDiskStoreName");
|
||||
|
||||
@@ -191,6 +191,7 @@ public class PdxDiskStoreAwareBeanFactoryPostProcessorUnitTests {
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
public void postProcessBeanFactory() {
|
||||
|
||||
Map<String, BeanDefinition> beanDefinitions = new HashMap<String, BeanDefinition>(13);
|
||||
|
||||
beanDefinitions.put("someBean", defineBean("org.company.app.domain.SomeBean", "someOtherBean"));
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2017 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.support;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The {@link MapBuilder} class employs the Builder Software Design Pattern to build a {@link Map}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.util.Map
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class MapBuilder<KEY, VALUE> {
|
||||
|
||||
public static <KEY, VALUE> MapBuilder<KEY, VALUE> newMapBuilder() {
|
||||
return new MapBuilder<>();
|
||||
}
|
||||
|
||||
private final Map<KEY, VALUE> map = new HashMap<>();
|
||||
|
||||
public MapBuilder<KEY, VALUE> put(KEY key, VALUE value) {
|
||||
this.map.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MapBuilder<KEY, VALUE> remove(KEY key) {
|
||||
this.map.remove(key);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<KEY, VALUE> build() {
|
||||
return this.map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user