DATAGEODE-324 - Add ability to resolve cache instance from BeanFactoryCacheResolver qualified by bean name.

This commit is contained in:
John Blum
2020-04-02 14:20:29 -07:00
parent c9cb4fbea0
commit 2e1ed277d2
2 changed files with 113 additions and 2 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.gemfire.support;
import java.util.Optional;
import org.apache.geode.cache.GemFireCache;
import org.springframework.beans.BeansException;
@@ -23,6 +25,7 @@ import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.data.gemfire.CacheResolver;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Cacheable {@link CacheResolver} implementation capable of resolving a {@link GemFireCache} instance
@@ -40,6 +43,8 @@ public class BeanFactoryCacheResolver extends AbstractCachingCacheResolver<GemFi
private BeanFactory beanFactory;
private String cacheBeanName;
/**
* Constructs a new instance of {@link BeanFactoryCacheResolver} initialized with the given, required
* Spring {@link BeanFactory}.
@@ -79,6 +84,28 @@ public class BeanFactoryCacheResolver extends AbstractCachingCacheResolver<GemFi
return this.beanFactory;
}
/**
* Sets (configures) the {@link String bean name} used to further qualify the resolution of
* the {@link GemFireCache} object reference in a Spring context.
*
* @param cacheBeanName {@link String name} of the {@link GemFireCache} bean in the Spring context.
*/
public void setCacheBeanName(String cacheBeanName) {
this.cacheBeanName = cacheBeanName;
}
/**
* Returns the optionally configured {@link String bean name} used to further qualify the resolution of
* the {@link GemFireCache} object reference in a Spring context.
*
* @return the configured {@link String name} of the {@link GemFireCache} bean in the Spring context.
*/
public Optional<String> getCacheBeanName() {
return Optional.ofNullable(this.cacheBeanName)
.filter(StringUtils::hasText);
}
/**
* Uses the configured Spring {@link BeanFactory} to resolve a reference to
* the single {@link GemFireCache} instance.
@@ -90,6 +117,9 @@ public class BeanFactoryCacheResolver extends AbstractCachingCacheResolver<GemFi
*/
@Override
protected GemFireCache doResolve() {
return getBeanFactory().getBean(GemFireCache.class);
return getCacheBeanName()
.map(cacheBeanName -> getBeanFactory().getBean(cacheBeanName, GemFireCache.class))
.orElse(getBeanFactory().getBean(GemFireCache.class));
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.data.gemfire.support;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -63,9 +64,9 @@ public class BeanFactoryCacheResolverUnitTests {
assertThat(cacheResolver).isNotNull();
assertThat(cacheResolver.getBeanFactory()).isSameAs(this.mockBeanFactory);
assertThat(cacheResolver.getCacheBeanName().orElse(null)).isNull();
}
@SuppressWarnings("all")
@Test(expected = IllegalArgumentException.class)
public void constructWithNullBeanFactoryThrowsIllegalArgumentException() {
@@ -81,6 +82,67 @@ public class BeanFactoryCacheResolverUnitTests {
}
}
@Test
public void setAndGetBeanFactory() {
BeanFactory mockBeanFactoryTwo = mock(BeanFactory.class);
BeanFactoryCacheResolver cacheResolver = new BeanFactoryCacheResolver(this.mockBeanFactory);
assertThat(cacheResolver.getBeanFactory()).isSameAs(this.mockBeanFactory);
cacheResolver.setBeanFactory(mockBeanFactoryTwo);
assertThat(cacheResolver.getBeanFactory()).isSameAs(mockBeanFactoryTwo);
}
@Test(expected = IllegalArgumentException.class)
public void setBeanFactoryToNullThrowsIllegalArgumentException() {
BeanFactoryCacheResolver cacheResolver = new BeanFactoryCacheResolver(this.mockBeanFactory);
try {
assertThat(cacheResolver.getBeanFactory()).isEqualTo(this.mockBeanFactory);
cacheResolver.setBeanFactory(null);
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("BeanFactory must not be null");
assertThat(expected).hasNoCause();
throw expected;
}
finally {
assertThat(cacheResolver.getBeanFactory()).isEqualTo(this.mockBeanFactory);
}
}
@Test
public void setAndGetCacheBeanName() {
BeanFactoryCacheResolver cacheResolver = new BeanFactoryCacheResolver(this.mockBeanFactory);
assertThat(cacheResolver.getCacheBeanName().orElse(null)).isNull();
cacheResolver.setCacheBeanName("TestCacheBeanName");
assertThat(cacheResolver.getCacheBeanName().orElse(null)).isEqualTo("TestCacheBeanName");
cacheResolver.setCacheBeanName(" ");
assertThat(cacheResolver.getCacheBeanName().orElse(null)).isNull();
cacheResolver.setCacheBeanName("");
assertThat(cacheResolver.getCacheBeanName().orElse(null)).isNull();
cacheResolver.setCacheBeanName(null);
assertThat(cacheResolver.getCacheBeanName().orElse(null)).isNull();
}
@Test
public void doResolveResolvesGemFireCache() {
@@ -95,4 +157,23 @@ public class BeanFactoryCacheResolverUnitTests {
verify(cacheResolver, times(1)).doResolve();
verifyNoInteractions(this.mockCache);
}
@Test
public void doResolveQualifiedGemFireCache() {
when(this.mockBeanFactory.getBean(eq("QualifiedCache"), eq(GemFireCache.class)))
.thenReturn(this.mockCache);
BeanFactoryCacheResolver cacheResolver = new BeanFactoryCacheResolver(this.mockBeanFactory);
assertThat(cacheResolver.doResolve()).isNull();
cacheResolver.setCacheBeanName("NonExistingCache");
assertThat(cacheResolver.doResolve()).isNull();
cacheResolver.setCacheBeanName("QualifiedCache");
assertThat(cacheResolver.doResolve()).isEqualTo(this.mockCache);
}
}