SGF-692 - Add Annotation support for Region Compression.

This commit is contained in:
John Blum
2017-11-21 18:09:50 -08:00
parent adf29c60ff
commit f5c7e8205a
12 changed files with 591 additions and 11 deletions

14
pom.xml
View File

@@ -16,6 +16,9 @@
<name>Spring Data GemFire</name>
<properties>
<dist.key>SGF</dist.key>
<java-module-name>spring.data.gemfire</java-module-name>
<source.level>1.8</source.level>
<antlr.version>2.7.7</antlr.version>
<apache-shiro.version>1.3.2</apache-shiro.version>
<assertj.version>3.6.2</assertj.version>
@@ -23,9 +26,9 @@
<gemfire.version>9.1.1</gemfire.version>
<google-code-findbugs.version>2.0.2</google-code-findbugs.version>
<multithreadedtc.version>1.01</multithreadedtc.version>
<snappy.version>0.4</snappy.version>
<springdata.commons>2.1.0.BUILD-SNAPSHOT</springdata.commons>
<spring-shell.version>1.2.0.RELEASE</spring-shell.version>
<java-module-name>spring.data.gemfire</java-module-name>
</properties>
<repositories>
@@ -149,7 +152,14 @@
<optional>true</optional>
</dependency>
<!-- 3rd Party Depenendencies -->
<dependency>
<groupId>org.iq80.snappy</groupId>
<artifactId>snappy</artifactId>
<version>${snappy.version}</version>
<optional>true</optional>
</dependency>
<!-- 3rd Party Dependencies -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>

View File

@@ -49,6 +49,7 @@ import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.asyncqueue.AsyncEventQueue;
import org.apache.geode.cache.wan.GatewaySender;
import org.apache.geode.compression.Compressor;
import org.apache.geode.internal.cache.UserSpecifiedRegionAttributes;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
@@ -125,6 +126,8 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
private Class<K> keyConstraint;
private Class<V> valueConstraint;
private Compressor compressor;
private DataPolicy dataPolicy;
private EvictionAttributes evictionAttributes;
@@ -310,6 +313,8 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
Optional.ofNullable(this.cacheWriter).ifPresent(regionFactory::setCacheWriter);
Optional.ofNullable(this.compressor).ifPresent(regionFactory::setCompressor);
resolveDataPolicy(regionFactory, this.persistent, this.dataPolicy);
Optional.ofNullable(this.diskStoreName)
@@ -767,6 +772,16 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
this.close = close;
}
/**
* Configures the {@link Compressor} used to compress the this {@link Region Region's} data.
*
* @param compressor {@link Compressor} used to compress the this {@link Region Region's} data.
* @see org.apache.geode.compression.Compressor
*/
public void setCompressor(Compressor compressor) {
this.compressor = compressor;
}
/**
* Indicates whether the Region referred to by this factory bean will be destroyed on shutdown (default false).
*

View File

@@ -41,6 +41,7 @@ import org.apache.geode.cache.client.ClientRegionFactory;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolManager;
import org.apache.geode.compression.Compressor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
@@ -95,6 +96,8 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
private ClientRegionShortcut shortcut;
private Compressor compressor;
private DataPolicy dataPolicy;
private EvictionAttributes evictionAttributes;
@@ -391,6 +394,8 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
stream(nullSafeArray(this.cacheListeners, CacheListener.class)).forEach(clientRegionFactory::addCacheListener);
Optional.ofNullable(this.compressor).ifPresent(clientRegionFactory::setCompressor);
Optional.ofNullable(this.diskStoreName).filter(StringUtils::hasText)
.ifPresent(clientRegionFactory::setDiskStoreName);
@@ -560,6 +565,16 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
this.destroy = (this.destroy && !close); // retain previous value iff close is false.
}
/**
* Configures the {@link Compressor} used to compress the this {@link Region Region's} data.
*
* @param compressor {@link Compressor} used to compress the this {@link Region Region's} data.
* @see org.apache.geode.compression.Compressor
*/
public void setCompressor(Compressor compressor) {
this.compressor = compressor;
}
/**
* Sets the Data Policy. Used only when a new Region is created.
*

View File

@@ -0,0 +1,184 @@
/*
* 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 java.util.Arrays.stream;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import static org.springframework.data.gemfire.util.CollectionUtils.asSet;
import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeIterable;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.apache.geode.cache.Region;
import org.apache.geode.compression.Compressor;
import org.apache.geode.compression.SnappyCompressor;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.gemfire.RegionLookupFactoryBean;
import org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.data.gemfire.util.SpringUtils;
import org.springframework.util.StringUtils;
/**
* The {@link CompressionConfiguration} class is a Spring {@link ImportAware} implementation capable of
* enabling Pivotal GemFire/Apache Geode cache {@link Region Regions} data compression.
*
* @author John Blum
* @see org.apache.geode.cache.Region
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor
* @see org.springframework.context.annotation.Bean
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.context.annotation.ImportAware
* @see org.springframework.data.gemfire.config.annotation.EnableCompression
* @see org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport
* @since 2.0.2
*/
@Configuration
@SuppressWarnings("unused")
public class CompressionConfiguration extends AbstractAnnotationConfigSupport implements ImportAware {
protected static final String SNAPPY_COMPRESSOR_BEAN_NAME = "SnappyCompressor";
private String compressorBeanName = SNAPPY_COMPRESSOR_BEAN_NAME;
private Set<String> regionNames = new HashSet<>();
/**
* Returns the {@link EnableCompression} {@link Annotation} {@link Class} type.
*
* @return the {@link EnableCompression} {@link Annotation} {@link Class} type.
* @see org.springframework.data.gemfire.config.annotation.EnableOffHeap
* @see java.lang.annotation.Annotation
*/
@Override
protected Class<? extends Annotation> getAnnotationType() {
return EnableCompression.class;
}
public void setCompressorBeanName(String compressorBeanName) {
this.compressorBeanName = compressorBeanName;
}
protected String resolveCompressorBeanName() {
return Optional.ofNullable(this.compressorBeanName).filter(StringUtils::hasText)
.orElse(SNAPPY_COMPRESSOR_BEAN_NAME);
}
public void setRegionNames(String[] regionNames) {
setRegionNames(asSet(nullSafeArray(regionNames, String.class)));
}
public void setRegionNames(Iterable<String> regionNames) {
this.regionNames = CollectionUtils.addAll(this.regionNames, nullSafeIterable(regionNames));
}
protected Set<String> resolveRegionNames() {
return Collections.unmodifiableSet(this.regionNames);
}
@Override
public void setImportMetadata(AnnotationMetadata importingClassMetadata) {
if (isAnnotationPresent(importingClassMetadata)) {
AnnotationAttributes enableCompressionAttributes = getAnnotationAttributes(importingClassMetadata);
setCompressorBeanName(resolveProperty(cacheCompressionProperty("compressor-bean-name"),
enableCompressionAttributes.getString("compressorBeanName")));
setRegionNames(resolveProperty(cacheCompressionProperty("region-names"),
String[].class, enableCompressionAttributes.getStringArray("regionNames")));
}
}
@Bean(SNAPPY_COMPRESSOR_BEAN_NAME)
Compressor snappyCompressor() {
return new SnappyCompressor();
}
@Bean
BeanFactoryPostProcessor regionCompressionBeanFactoryPostProcessor() {
String resolvedCompressorBeanName = resolveCompressorBeanName();
return beanFactory ->
stream(nullSafeArray(beanFactory.getBeanDefinitionNames(), String.class)).forEach(beanName ->
Optional.of(beanFactory.getBeanDefinition(beanName))
.filter(beanDefinition -> isTargetedRegionBean(beanName, beanDefinition, beanFactory))
.ifPresent(beanDefinition -> SpringUtils.setPropertyReference(
beanDefinition, "compressor", resolvedCompressorBeanName)));
}
private boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
ConfigurableListableBeanFactory beanFactory) {
return isNamedRegion(beanName, beanDefinition, beanFactory) && isRegionBean(beanDefinition, beanFactory);
}
private boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
return Optional.ofNullable(beanDefinition)
.flatMap(it -> resolveBeanClass(it, beanFactory.getBeanClassLoader()))
.filter(beanClass -> RegionLookupFactoryBean.class.isAssignableFrom(beanClass))
.isPresent();
}
private boolean isNamedRegion(String beanName, BeanDefinition beanDefinition,
ConfigurableListableBeanFactory beanFactory) {
Set<String> resolvedRegionNames = resolveRegionNames();
return CollectionUtils.isEmpty(resolvedRegionNames)
|| CollectionUtils.containsAny(resolvedRegionNames, resolveBeanNames(beanName, beanDefinition, beanFactory));
}
private Collection<String> resolveBeanNames(String beanName, BeanDefinition beanDefinition,
ConfigurableListableBeanFactory beanFactory) {
Collection<String> beanNames = new HashSet<>();
beanNames.add(beanName);
Collections.addAll(beanNames, beanFactory.getAliases(beanName));
PropertyValue regionName = beanDefinition.getPropertyValues().getPropertyValue("regionName");
if (regionName != null) {
Object regionNameValue = regionName.getValue();
if (regionNameValue != null) {
beanNames.add(regionNameValue.toString());
}
}
return beanNames;
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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.springframework.data.gemfire.config.annotation.CompressionConfiguration.SNAPPY_COMPRESSOR_BEAN_NAME;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.geode.cache.Region;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* The {@link EnableCompression} annotation marks a Spring {@link Configuration @Configuration} annotated application
* {@link Class} to configure and enable Pivotal GemFire/Apache Geode {@link Region} data compression.
*
* @author John Blum
* @see java.lang.annotation.Annotation
* @see org.springframework.context.annotation.Import
* @see org.springframework.data.gemfire.config.annotation.CompressionConfiguration
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Import(CompressionConfiguration.class)
public @interface EnableCompression {
/**
* Reference to the {@link String name} of a bean having type {@link org.apache.geode.compression.Compressor}
* registered in the Spring container to handle {@link Region} compression.
*
* Defaults to {@literal snappyCompressor}.
*
* Set the {@literal spring.data.gemfire.cache.compression.compressor-bean-name}
* in {@literal application.properties}.
*/
String compressorBeanName() default SNAPPY_COMPRESSOR_BEAN_NAME;
/**
* Identifies all the {@link Region Regions} by name in which the data compression will be enabled.
*
* Defaults to all {@link Region Regions}.
*
* Set the {@literal spring.data.gemfire.cache.compression.region-names} property
* in {@literal application.properties}.
*/
String[] regionNames() default {};
}

View File

@@ -32,7 +32,6 @@ import java.util.Set;
import org.apache.geode.cache.Region;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -48,11 +47,15 @@ import org.springframework.data.gemfire.util.PropertiesBuilder;
/**
* The {@link OffHeapConfiguration} class is a Spring {@link ImportBeanDefinitionRegistrar} capable of
* enabling Pivotal GemFire/Apache Geode cache {@link Region Regions} to use Off-Heap memory for data storage.
* enabling Pivotal GemFire/Apache Geode cache {@link Region Regions} to use Off-Heap Memory for data storage.
*
* @author John Blum
* @see java.util.Properties
* @see org.apache.geode.cache.Region
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar
* @see org.springframework.data.gemfire.config.annotation.EnableOffHeap
* @see org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport
* @see org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport
* @since 1.9.0
*/
@@ -121,13 +124,13 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
.addPropertyValue("offHeap", true)));
}
boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
private boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
ConfigurableListableBeanFactory beanFactory) {
return isNamedRegion(beanName, beanDefinition, beanFactory) && isRegionBean(beanDefinition, beanFactory);
}
boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
private boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
return Optional.ofNullable(beanDefinition)
.flatMap(it -> resolveBeanClass(it, beanFactory.getBeanClassLoader()))
@@ -135,13 +138,15 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
.isPresent();
}
boolean isNamedRegion(String beanName, BeanDefinition beanDefinition, BeanFactory beanFactory) {
private boolean isNamedRegion(String beanName, BeanDefinition beanDefinition,
ConfigurableListableBeanFactory beanFactory) {
return CollectionUtils.isEmpty(regionNames)
|| CollectionUtils.containsAny(regionNames, getBeanNames(beanName, beanDefinition, beanFactory));
|| CollectionUtils.containsAny(regionNames, resolveBeanNames(beanName, beanDefinition, beanFactory));
}
Collection<String> getBeanNames(String beanName, BeanDefinition beanDefinition, BeanFactory beanFactory) {
private Collection<String> resolveBeanNames(String beanName, BeanDefinition beanDefinition,
ConfigurableListableBeanFactory beanFactory) {
Collection<String> beanNames = new HashSet<>();

View File

@@ -33,6 +33,7 @@ import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.compression.Compressor;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.data.gemfire.GenericRegionFactoryBean;
@@ -61,6 +62,7 @@ import org.springframework.util.StringUtils;
* @see org.apache.geode.cache.Scope
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.ClientRegionShortcut
* @see org.apache.geode.compression.Compressor
* @see org.springframework.data.gemfire.GenericRegionFactoryBean
* @see org.springframework.data.gemfire.LocalRegionFactoryBean
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
@@ -84,6 +86,8 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
private ClientRegionShortcut clientRegionShortcut = ClientRegionShortcut.PROXY;
private Compressor compressor;
private DataPolicy dataPolicy = DataPolicy.DEFAULT;
private List<RegionConfigurer> regionConfigurers = Collections.emptyList();
@@ -129,6 +133,7 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
clientRegionFactory.setBeanFactory(getBeanFactory());
clientRegionFactory.setCache(gemfireCache);
clientRegionFactory.setClose(isClose());
clientRegionFactory.setCompressor(getCompressor());
clientRegionFactory.setDiskStoreName(getDiskStoreName());
clientRegionFactory.setKeyConstraint(getKeyConstraint());
clientRegionFactory.setLookupEnabled(getLookupEnabled());
@@ -178,6 +183,7 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
serverRegionFactory.setBeanFactory(getBeanFactory());
serverRegionFactory.setCache(gemfireCache);
serverRegionFactory.setClose(isClose());
serverRegionFactory.setCompressor(getCompressor());
serverRegionFactory.setDataPolicy(getDataPolicy());
serverRegionFactory.setDiskStoreName(getDiskStoreName());
serverRegionFactory.setKeyConstraint(getKeyConstraint());
@@ -254,6 +260,26 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
return Boolean.TRUE.equals(getClose());
}
/**
* Configures the {@link Compressor} used to compress the this {@link Region Region's} data.
*
* @param compressor {@link Compressor} used to compress the this {@link Region Region's} data.
* @see org.apache.geode.compression.Compressor
*/
public void setCompressor(Compressor compressor) {
this.compressor = compressor;
}
/**
* Returns the configured {@link Compressor} used to compress the this {@link Region Region's} data.
*
* @return the configured {@link Compressor} used to compress the this {@link Region Region's} data.
* @see org.apache.geode.compression.Compressor
*/
protected Compressor getCompressor() {
return this.compressor;
}
public void setDataPolicy(DataPolicy dataPolicy) {
this.dataPolicy = dataPolicy;
}
@@ -278,10 +304,21 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
return this.keyConstraint;
}
/**
* Configure the {@link Region} to manage data in Off-Heap Memory.
*
* @param offHeap boolean value indicating whether the {@link Region Region's} data
* will be managed in Off-Heap Memory.
*/
public void setOffHeap(Boolean offHeap) {
this.offHeap = offHeap;
}
/**
* Return the configuration setting for whether the {@link Region Region's} data will be managed in Off-Heap Memory.
*
* @return a boolean value indicating whether the {@link Region Region's} data will be managed in Off-Heap Memory.
*/
protected Boolean getOffHeap() {
return this.offHeap;
}

View File

@@ -29,6 +29,7 @@ import java.util.function.Supplier;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.util.StringUtils;
/**
@@ -53,6 +54,24 @@ public abstract class SpringUtils {
return bean;
}
/* (non-Javadoc) */
public static BeanDefinition setPropertyReference(BeanDefinition beanDefinition,
String propertyName, String beanName) {
beanDefinition.getPropertyValues().addPropertyValue(propertyName, new RuntimeBeanReference(beanName));
return beanDefinition;
}
/* (non-Javadoc) */
public static BeanDefinition setPropertyValue(BeanDefinition beanDefinition,
String propertyName, Object propertyValue) {
beanDefinition.getPropertyValues().addPropertyValue(propertyName, propertyValue);
return beanDefinition;
}
/* (non-Javadoc) */
public static String defaultIfEmpty(String value, String defaultValue) {
return (StringUtils.hasText(value) ? value : defaultValue);

View File

@@ -0,0 +1,178 @@
/*
* 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.Mockito.mock;
import static org.springframework.data.gemfire.config.annotation.CompressionConfiguration.SNAPPY_COMPRESSOR_BEAN_NAME;
import java.util.Arrays;
import java.util.Optional;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.compression.Compressor;
import org.apache.geode.compression.SnappyCompressor;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.GemfireUtils;
import org.springframework.data.gemfire.LocalRegionFactoryBean;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
import org.springframework.data.gemfire.test.model.Person;
/**
* The EnableCompressionConfigurationUnitTests class...
*
* @author John Blum
* @since 1.0.0
*/
public class EnableCompressionConfigurationUnitTests {
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
}
private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
return new AnnotationConfigApplicationContext(annotatedClasses);
}
private void assertRegionCompressor(Region<?, ?> region, String regionName, Compressor compressor) {
assertThat(region).isNotNull();
assertThat(region.getName()).isEqualTo(regionName);
assertThat(region.getFullPath()).isEqualTo(GemfireUtils.toRegionPath(regionName));
assertThat(region.getAttributes()).isNotNull();
assertThat(region.getAttributes().getCompressor()).isEqualTo(compressor);
}
@Test
public void enableCompressionForAllRegions() {
this.applicationContext = newApplicationContext(EnableCompressionForAllRegionsConfiguration.class);
assertThat(this.applicationContext).isNotNull();
Compressor compressor = this.applicationContext.getBean(Compressor.class);
assertThat(compressor).isInstanceOf(SnappyCompressor.class);
Arrays.asList("People", "ExampleLocalRegion", "ExamplePartitionRegion", "ExampleReplicateRegion")
.forEach(regionName -> {
assertThat(this.applicationContext.containsBean(regionName)).isTrue();
assertRegionCompressor(this.applicationContext.getBean(regionName, Region.class),
regionName, compressor);
});
}
@Test
public void enableCompressionForSelectRegions() {
this.applicationContext = newApplicationContext(EnableCompressionForSelectRegionsConfiguration.class);
assertThat(this.applicationContext).isNotNull();
Compressor compressor = this.applicationContext.getBean("MockCompressor", Compressor.class);
assertThat(compressor).isNotNull();
assertThat(this.applicationContext.containsBean(SNAPPY_COMPRESSOR_BEAN_NAME)).isTrue();
Arrays.asList("People", "ExampleClientRegion").forEach(regionName -> {
assertThat(this.applicationContext.containsBean(regionName)).isTrue();
assertRegionCompressor(this.applicationContext.getBean(regionName, Region.class),
regionName, "People".equals(regionName) ? compressor : null);
});
}
@PeerCacheApplication
@EnableGemFireMockObjects
@EnableEntityDefinedRegions(basePackageClasses = Person.class)
@EnableCompression
@SuppressWarnings("unused")
static class EnableCompressionForAllRegionsConfiguration {
@Bean("ExampleLocalRegion")
public LocalRegionFactoryBean<Object, Object> localRegion(GemFireCache gemfireCache) {
LocalRegionFactoryBean<Object, Object> localRegion = new LocalRegionFactoryBean<>();
localRegion.setCache(gemfireCache);
localRegion.setClose(false);
localRegion.setPersistent(false);
return localRegion;
}
@Bean("ExamplePartitionRegion")
public PartitionedRegionFactoryBean<Object, Object> partitionRegion(GemFireCache gemfireCache) {
PartitionedRegionFactoryBean<Object, Object> partitionRegion = new PartitionedRegionFactoryBean<>();
partitionRegion.setCache(gemfireCache);
partitionRegion.setClose(false);
partitionRegion.setPersistent(false);
return partitionRegion;
}
@Bean("ExampleReplicateRegion")
public ReplicatedRegionFactoryBean<Object, Object> replicateRegion(GemFireCache gemfireCache) {
ReplicatedRegionFactoryBean<Object, Object> replicateRegion = new ReplicatedRegionFactoryBean<>();
replicateRegion.setCache(gemfireCache);
replicateRegion.setClose(false);
replicateRegion.setPersistent(false);
return replicateRegion;
}
}
@ClientCacheApplication
@EnableGemFireMockObjects
@EnableEntityDefinedRegions(basePackageClasses = Person.class)
@EnableCompression(compressorBeanName = "MockCompressor", regionNames = "People")
@SuppressWarnings("unused")
static class EnableCompressionForSelectRegionsConfiguration {
@Bean("ExampleClientRegion")
public ClientRegionFactoryBean<Object, Object> clientRegion(GemFireCache gemfireCache) {
ClientRegionFactoryBean<Object, Object> clientRegion = new ClientRegionFactoryBean<>();
clientRegion.setCache(gemfireCache);
clientRegion.setClose(false);
clientRegion.setShortcut(ClientRegionShortcut.LOCAL);
return clientRegion;
}
@Bean("MockCompressor")
Compressor mockCompressor() {
return mock(Compressor.class);
}
}
}

View File

@@ -256,7 +256,7 @@ public class EnableGemFirePropertiesIntegrationTests {
public void offHeapGemFirePropertiesConfiguration() {
MockPropertySource testPropertySource = new MockPropertySource()
.withProperty("spring.data.gemfire.cache.off-heap-memory-size", "1024g");
.withProperty("spring.data.gemfire.cache.off-heap.memory-size", "1024g");
this.applicationContext = newApplicationContext(testPropertySource, TestOffHeapGemFirePropertiesConfiguration.class);

View File

@@ -370,7 +370,8 @@ public class AbstractAnnotationConfigSupportTests {
when(mockBeanDefinition.resolveBeanClass(any(ClassLoader.class)))
.thenThrow(new ClassNotFoundException("Class [non.existing.bean.Class] not found"));
assertThat(this.support.resolveBeanClass(mockBeanDefinition, null).orElse(null)).isNull();
assertThat(this.support.resolveBeanClass(mockBeanDefinition, (BeanDefinitionRegistry) null)
.orElse(null)).isNull();
verify(mockBeanDefinition, times(1)).getBeanClassName();
verify(mockBeanDefinition, never()).getFactoryMethodName();

View File

@@ -34,7 +34,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
/**
* Unit tests for {@link SpringUtils}.
@@ -56,6 +58,7 @@ public class SpringUtilsUnitTests {
@Test
public void addDependsOnToExistingDependencies() {
when(mockBeanDefinition.getDependsOn()).thenReturn(asArray("testBeanNameOne", "testBeanNameTwo"));
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanNameThree")).isSameAs(mockBeanDefinition);
@@ -67,6 +70,7 @@ public class SpringUtilsUnitTests {
@Test
public void addDependsOnToNonExistingDependencies() {
when(mockBeanDefinition.getDependsOn()).thenReturn(null);
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanName")).isSameAs(mockBeanDefinition);
@@ -77,6 +81,7 @@ public class SpringUtilsUnitTests {
@Test
public void addDependsOnWithMultipleDependenciesWithExistingDependencies() {
when(mockBeanDefinition.getDependsOn()).thenReturn(asArray("testBeanNameOne", "testBeanNameTwo"));
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanNameThree", "testBeanNameFour"))
@@ -87,6 +92,48 @@ public class SpringUtilsUnitTests {
.setDependsOn("testBeanNameOne", "testBeanNameTwo", "testBeanNameThree", "testBeanNameFour");
}
@Test
@SuppressWarnings("all")
public void setBeanDefinitionPropertyReference() {
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
when(mockBeanDefinition.getPropertyValues()).thenReturn(mutablePropertyValues);
assertThat(mutablePropertyValues.size()).isEqualTo(0);
SpringUtils.setPropertyReference(mockBeanDefinition, "testProperty", "testBean");
assertThat(mutablePropertyValues.size()).isEqualTo(1);
assertThat(mutablePropertyValues.getPropertyValue("testProperty")).isNotNull();
assertThat(mutablePropertyValues.getPropertyValue("testProperty").getValue())
.isInstanceOf(RuntimeBeanReference.class);
assertThat(((RuntimeBeanReference) mutablePropertyValues.getPropertyValue("testProperty").getValue()).getBeanName())
.isEqualTo("testBean");
}
@Test
@SuppressWarnings("all")
public void setBeanDefinitionPropertyValue() {
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
when(mockBeanDefinition.getPropertyValues()).thenReturn(mutablePropertyValues);
assertThat(mutablePropertyValues.size()).isEqualTo(0);
SpringUtils.setPropertyValue(mockBeanDefinition, "testProperty", "testValue");
assertThat(mutablePropertyValues.size()).isEqualTo(1);
assertThat(mutablePropertyValues.getPropertyValue("testProperty")).isNotNull();
assertThat(mutablePropertyValues.getPropertyValue("testProperty").getValue())
.isEqualTo("testValue");
}
@Test
public void defaultIfEmptyReturnsValue() {
assertThat(SpringUtils.defaultIfEmpty("test", "DEFAULT")).isEqualTo("test");