DATAGEODE-117 - Move configuration of PDX to a PdxConfiguration class imported by the EnablePdx annotation.
This commit is contained in:
@@ -18,37 +18,14 @@
|
||||
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.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
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.config.annotation.AbstractCacheConfiguration.DEFAULT_MCAST_PORT;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.geode.pdx.PdxSerializer;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.data.gemfire.CacheFactoryBean;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.MappingPdxSerializer;
|
||||
import org.springframework.util.MethodInvoker;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractCacheConfiguration}.
|
||||
@@ -66,183 +43,26 @@ import org.springframework.util.MethodInvoker;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AbstractCacheConfigurationUnitTests {
|
||||
|
||||
@Mock
|
||||
private BeanFactory mockBeanFactory;
|
||||
|
||||
private TestCacheConfiguration cacheConfiguration;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
cacheConfiguration = new TestCacheConfiguration();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T invokeMethod(Object obj, String methodName) throws Exception {
|
||||
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
|
||||
methodInvoker.setTargetObject(obj);
|
||||
methodInvoker.setTargetMethod(methodName);
|
||||
methodInvoker.prepare();
|
||||
|
||||
return (T) methodInvoker.invoke();
|
||||
}
|
||||
@Spy
|
||||
private AbstractCacheConfiguration cacheConfiguration;
|
||||
|
||||
@Test
|
||||
public void configurePdxWhenEnablePdxIsConfigured() {
|
||||
public void gemfirePropertiesContainsEssentialProperties() {
|
||||
|
||||
AnnotationMetadata mockAnnotationMetadata = mock(AnnotationMetadata.class);
|
||||
PdxSerializer mockPdxSerializer = mock(PdxSerializer.class);
|
||||
this.cacheConfiguration.setName("TestName");
|
||||
this.cacheConfiguration.setMcastPort(-1);
|
||||
this.cacheConfiguration.setLogLevel("DEBUG");
|
||||
this.cacheConfiguration.setLocators("skullbox[11235]");
|
||||
this.cacheConfiguration.setStartLocator("boombox[12480]");
|
||||
|
||||
Map<String, Object> annotationAttributes = new HashMap<>(5);
|
||||
Properties gemfireProperties = this.cacheConfiguration.gemfireProperties();
|
||||
|
||||
annotationAttributes.put("diskStoreName", "BlockDiskStore");
|
||||
annotationAttributes.put("ignoreUnreadFields", Boolean.FALSE);
|
||||
annotationAttributes.put("persistent", Boolean.TRUE);
|
||||
annotationAttributes.put("readSerialized", Boolean.TRUE);
|
||||
annotationAttributes.put("serializerBeanName", "MockPdxSerializer");
|
||||
|
||||
when(mockAnnotationMetadata.hasAnnotation(eq(EnablePdx.class.getName()))).thenReturn(true);
|
||||
when(mockAnnotationMetadata.getAnnotationAttributes(eq(EnablePdx.class.getName()))).thenReturn(annotationAttributes);
|
||||
when(mockBeanFactory.containsBean(eq("MockPdxSerializer"))).thenReturn(true);
|
||||
when(mockBeanFactory.getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class))).thenReturn(mockPdxSerializer);
|
||||
|
||||
cacheConfiguration.setBeanFactory(mockBeanFactory);
|
||||
cacheConfiguration.configurePdx(mockAnnotationMetadata);
|
||||
|
||||
assertThat(cacheConfiguration.getPdxDiskStoreName()).isEqualTo("BlockDiskStore");
|
||||
assertThat(cacheConfiguration.getPdxIgnoreUnreadFields()).isFalse();
|
||||
assertThat(cacheConfiguration.getPdxPersistent()).isTrue();
|
||||
assertThat(cacheConfiguration.getPdxReadSerialized()).isTrue();
|
||||
assertThat(cacheConfiguration.getPdxSerializer()).isEqualTo(mockPdxSerializer);
|
||||
|
||||
verify(mockAnnotationMetadata, times(1)).hasAnnotation(eq(EnablePdx.class.getName()));
|
||||
verify(mockAnnotationMetadata, times(1)).getAnnotationAttributes(eq(EnablePdx.class.getName()));
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("MockPdxSerializer"));
|
||||
verify(mockBeanFactory, times(1)).getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurePdxWhenEnablePdxIsNotConfigured() {
|
||||
|
||||
AnnotationMetadata mockAnnotationMetadata = mock(AnnotationMetadata.class);
|
||||
|
||||
when(mockAnnotationMetadata.hasAnnotation(anyString())).thenReturn(false);
|
||||
|
||||
cacheConfiguration.configurePdx(mockAnnotationMetadata);
|
||||
|
||||
assertThat(cacheConfiguration.getPdxDiskStoreName()).isNull();
|
||||
assertThat(cacheConfiguration.getPdxIgnoreUnreadFields()).isNull();
|
||||
assertThat(cacheConfiguration.getPdxPersistent()).isNull();
|
||||
assertThat(cacheConfiguration.getPdxReadSerialized()).isNull();
|
||||
assertThat(cacheConfiguration.getPdxSerializer()).isNull();
|
||||
|
||||
verify(mockAnnotationMetadata, times(1)).hasAnnotation(eq(EnablePdx.class.getName()));
|
||||
verifyNoMoreInteractions(mockAnnotationMetadata);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePdxSerializerUsesPdxSerializerBean() {
|
||||
|
||||
PdxSerializer mockPdxSerializer = mock(PdxSerializer.class);
|
||||
|
||||
when(mockBeanFactory.containsBean(anyString())).thenReturn(true);
|
||||
when(mockBeanFactory.getBean(anyString(), eq(PdxSerializer.class))).thenReturn(mockPdxSerializer);
|
||||
|
||||
cacheConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
PdxSerializer actualPdxSerializer = cacheConfiguration.resolvePdxSerializer("MockPdxSerializer");
|
||||
|
||||
assertThat(actualPdxSerializer).isEqualTo(mockPdxSerializer);
|
||||
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("MockPdxSerializer"));
|
||||
verify(mockBeanFactory, times(1)).getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePdxSerializerUsesConfiguredPdxSerializer() {
|
||||
|
||||
PdxSerializer mockPdxSerializer = mock(PdxSerializer.class);
|
||||
|
||||
when(mockBeanFactory.containsBean(anyString())).thenReturn(false);
|
||||
|
||||
cacheConfiguration.setBeanFactory(mockBeanFactory);
|
||||
cacheConfiguration.setPdxSerializer(mockPdxSerializer);
|
||||
|
||||
PdxSerializer actualPdxSerializer = cacheConfiguration.resolvePdxSerializer("TestPdxSerializer");
|
||||
|
||||
assertThat(actualPdxSerializer).isEqualTo(mockPdxSerializer);
|
||||
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("TestPdxSerializer"));
|
||||
verify(mockBeanFactory, never()).getBean(anyString(), eq(PdxSerializer.class));
|
||||
verifyZeroInteractions(mockPdxSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePdxSerializerCallsNewMappingPdxSerializer() {
|
||||
|
||||
AbstractCacheConfiguration cacheConfigurationSpy = spy(this.cacheConfiguration);
|
||||
MappingPdxSerializer mockPdxSerializer = mock(MappingPdxSerializer.class);
|
||||
|
||||
when(mockBeanFactory.containsBean(anyString())).thenReturn(false);
|
||||
doReturn(mockPdxSerializer).when(cacheConfigurationSpy).newPdxSerializer(any(BeanFactory.class));
|
||||
|
||||
cacheConfigurationSpy.setBeanFactory(mockBeanFactory);
|
||||
|
||||
PdxSerializer actualPdxSerializer = cacheConfigurationSpy.resolvePdxSerializer("TestPdxSerializer");
|
||||
|
||||
assertThat(actualPdxSerializer).isEqualTo(mockPdxSerializer);
|
||||
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("TestPdxSerializer"));
|
||||
verify(mockBeanFactory, never()).getBean(anyString(), eq(PdxSerializer.class));
|
||||
verify(cacheConfigurationSpy, times(1)).newPdxSerializer(eq(mockBeanFactory));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newPdxSerializerUsesConfiguredConversionServiceAndMappingContext() throws Exception {
|
||||
|
||||
ConfigurableBeanFactory mockBeanFactory = mock(ConfigurableBeanFactory.class);
|
||||
ConversionService mockConversionService = mock(ConversionService.class);
|
||||
GemfireMappingContext mockMappingContext = mock(GemfireMappingContext.class);
|
||||
|
||||
when(mockBeanFactory.getConversionService()).thenReturn(mockConversionService);
|
||||
|
||||
cacheConfiguration.setBeanFactory(mockBeanFactory);
|
||||
cacheConfiguration.setMappingContext(mockMappingContext);
|
||||
|
||||
MappingPdxSerializer pdxSerializer = cacheConfiguration.newPdxSerializer();
|
||||
|
||||
assertThat(pdxSerializer).isNotNull();
|
||||
assertThat((Object) invokeMethod(pdxSerializer, "getConversionService")).isEqualTo(mockConversionService);
|
||||
assertThat((Object) invokeMethod(pdxSerializer, "getMappingContext")).isEqualTo(mockMappingContext);
|
||||
|
||||
verify(mockBeanFactory, times(2)).getConversionService();
|
||||
verifyZeroInteractions(mockConversionService);
|
||||
verifyZeroInteractions(mockMappingContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newPdxSerializerDefaultsConversionServiceAndMappingContextWhenNotConfigured() throws Exception {
|
||||
|
||||
cacheConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
MappingPdxSerializer pdxSerializer = cacheConfiguration.newPdxSerializer();
|
||||
|
||||
assertThat(pdxSerializer).isNotNull();
|
||||
assertThat((Object) invokeMethod(pdxSerializer, "getConversionService")).isInstanceOf(ConversionService.class);
|
||||
assertThat((Object) invokeMethod(pdxSerializer, "getMappingContext")).isInstanceOf(GemfireMappingContext.class);
|
||||
}
|
||||
|
||||
protected static class TestCacheConfiguration extends AbstractCacheConfiguration {
|
||||
|
||||
@Override
|
||||
protected Class<? extends Annotation> getAnnotationType() {
|
||||
throw new UnsupportedOperationException("Not Implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T extends CacheFactoryBean> T newCacheFactoryBean() {
|
||||
throw new UnsupportedOperationException("Not Implemented");
|
||||
}
|
||||
assertThat(gemfireProperties).isNotNull();
|
||||
assertThat(gemfireProperties).hasSize(5);
|
||||
assertThat(gemfireProperties.getProperty("name")).isEqualTo("TestName");
|
||||
assertThat(gemfireProperties.getProperty("mcast-port")).isEqualTo(String.valueOf(DEFAULT_MCAST_PORT));
|
||||
assertThat(gemfireProperties.getProperty("log-level")).isEqualTo("DEBUG");
|
||||
assertThat(gemfireProperties.getProperty("locators")).isEqualTo("skullbox[11235]");
|
||||
assertThat(gemfireProperties.getProperty("start-locator")).isEqualTo("boombox[12480]");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,21 @@
|
||||
package org.springframework.data.gemfire.config.annotation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.geode.cache.GemFireCache;
|
||||
import org.apache.geode.pdx.PdxSerializer;
|
||||
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.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.data.gemfire.CacheFactoryBean;
|
||||
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
|
||||
import org.springframework.data.gemfire.util.ArrayUtils;
|
||||
import org.springframework.mock.env.MockPropertySource;
|
||||
@@ -267,9 +271,10 @@ public class EnableGemFirePropertiesIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameAndGroupGemFirePropertiesAnnotationConfiguration() {
|
||||
public void nameAndGroupsAnnotationBasedGemFirePropertiesConfiguration() {
|
||||
|
||||
this.applicationContext = newApplicationContext(TestNameAndGroupGemFirePropertiesAnnotationConfiguration.class);
|
||||
this.applicationContext =
|
||||
newApplicationContext(TestNameAndGroupsAnnotationBasedGemFirePropertiesConfiguration.class);
|
||||
|
||||
assertThat(this.applicationContext).isNotNull();
|
||||
assertThat(this.applicationContext.containsBean("gemfireCache")).isTrue();
|
||||
@@ -319,6 +324,37 @@ public class EnableGemFirePropertiesIntegrationTests {
|
||||
assertThat(gemfireProperties.getProperty("off-heap-memory-size")).isEqualTo("1024g");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pdxGemFirePropertiesConfiguration() {
|
||||
|
||||
PropertySource testPropertySource = new MockPropertySource("TestPropertySource")
|
||||
.withProperty("spring.data.gemfire.pdx.disk-store-name", "TestDiskStore")
|
||||
.withProperty("spring.data.gemfire.pdx.ignore-unread-fields", "true")
|
||||
.withProperty("spring.data.gemfire.pdx.persistent", "true")
|
||||
.withProperty("spring.data.gemfire.pdx.read-serialized", "true")
|
||||
.withProperty("spring.data.gemfire.pdx.serializer-bean-name", "mockPdxSerializer");
|
||||
|
||||
this.applicationContext =
|
||||
newApplicationContext(testPropertySource, TestPdxGemFirePropertiesConfiguration.class);
|
||||
|
||||
assertThat(this.applicationContext).isNotNull();
|
||||
assertThat(this.applicationContext.containsBean("gemfireCache")).isTrue();
|
||||
assertThat(this.applicationContext.containsBean("mockPdxSerializer")).isTrue();
|
||||
|
||||
CacheFactoryBean gemfireCache = this.applicationContext.getBean("&gemfireCache", CacheFactoryBean.class);
|
||||
|
||||
assertThat(gemfireCache).isNotNull();
|
||||
assertThat(gemfireCache.getPdxDiskStoreName()).isEqualTo("TestDiskStore");
|
||||
assertThat(gemfireCache.getPdxIgnoreUnreadFields()).isTrue();
|
||||
assertThat(gemfireCache.getPdxPersistent()).isTrue();
|
||||
assertThat(gemfireCache.getPdxReadSerialized()).isTrue();
|
||||
|
||||
PdxSerializer mockPdxSerializer = this.applicationContext.getBean("mockPdxSerializer", PdxSerializer.class);
|
||||
|
||||
assertThat(mockPdxSerializer).isNotNull();
|
||||
assertThat(gemfireCache.getPdxSerializer()).isEqualTo(mockPdxSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void redisServerGemFirePropertiesConfiguration() {
|
||||
|
||||
@@ -515,7 +551,7 @@ public class EnableGemFirePropertiesIntegrationTests {
|
||||
@EnableGemFireMockObjects
|
||||
@PeerCacheApplication
|
||||
@EnableGemFireProperties(name = "TestName", groups = { "TestGroupOne", "TestGroupTwo" })
|
||||
static class TestNameAndGroupGemFirePropertiesAnnotationConfiguration { }
|
||||
static class TestNameAndGroupsAnnotationBasedGemFirePropertiesConfiguration { }
|
||||
|
||||
@EnableGemFireMockObjects
|
||||
@PeerCacheApplication
|
||||
@@ -523,6 +559,18 @@ public class EnableGemFirePropertiesIntegrationTests {
|
||||
@EnableOffHeap(memorySize = "64g")
|
||||
static class TestOffHeapGemFirePropertiesConfiguration { }
|
||||
|
||||
@EnableGemFireMockObjects
|
||||
@PeerCacheApplication
|
||||
@EnablePdx
|
||||
@SuppressWarnings("unused")
|
||||
static class TestPdxGemFirePropertiesConfiguration {
|
||||
|
||||
@Bean
|
||||
PdxSerializer mockPdxSerializer() {
|
||||
return mock(PdxSerializer.class);
|
||||
}
|
||||
}
|
||||
|
||||
@EnableGemFireMockObjects
|
||||
@PeerCacheApplication
|
||||
@EnableGemFireProperties
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright 2018 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 java.util.Optional;
|
||||
|
||||
import org.apache.geode.cache.GemFireCache;
|
||||
import org.apache.geode.cache.client.ClientRegionShortcut;
|
||||
import org.apache.geode.pdx.PdxSerializer;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.gemfire.CacheFactoryBean;
|
||||
import org.springframework.data.gemfire.DiskStoreFactoryBean;
|
||||
import org.springframework.data.gemfire.LocalRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.mapping.MappingPdxSerializer;
|
||||
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
|
||||
|
||||
/**
|
||||
* The EnablePdxConfigurationIntegrationTests class...
|
||||
*
|
||||
* @author John Blum
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class EnablePdxConfigurationIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
|
||||
}
|
||||
|
||||
private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
|
||||
return new AnnotationConfigApplicationContext(annotatedClasses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void regionBeanDefinitionDependsOnPdxDiskStoreBean() {
|
||||
|
||||
this.applicationContext = newApplicationContext(TestEnablePdxWithDiskStoreConfiguration.class);
|
||||
|
||||
assertThat(this.applicationContext).isNotNull();
|
||||
assertThat(this.applicationContext.containsBean("gemfireCache")).isTrue();
|
||||
assertThat(this.applicationContext.containsBean("MockPdxSerializer")).isTrue();
|
||||
assertThat(this.applicationContext.containsBean("TestDiskStore")).isTrue();
|
||||
assertThat(this.applicationContext.containsBean("TestRegion")).isTrue();
|
||||
|
||||
CacheFactoryBean gemfireCache = this.applicationContext.getBean("&gemfireCache", CacheFactoryBean.class);
|
||||
|
||||
assertThat(gemfireCache).isNotNull();
|
||||
assertThat(gemfireCache.getPdxSerializer())
|
||||
.isEqualTo(this.applicationContext.getBean("MockPdxSerializer", PdxSerializer.class));
|
||||
|
||||
BeanDefinition testDiskStoreBeanDefinition =
|
||||
this.applicationContext.getBeanFactory().getBeanDefinition("TestDiskStore");
|
||||
|
||||
assertThat(testDiskStoreBeanDefinition).isNotNull();
|
||||
assertThat(testDiskStoreBeanDefinition.getDependsOn()).isNullOrEmpty();
|
||||
|
||||
BeanDefinition testRegionBeanDefinition =
|
||||
this.applicationContext.getBeanFactory().getBeanDefinition("TestRegion");
|
||||
|
||||
assertThat(testRegionBeanDefinition).isNotNull();
|
||||
assertThat(testRegionBeanDefinition.getDependsOn()).containsExactly("TestDiskStore");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void regionBeanDefinitionHasNoDependencies() {
|
||||
|
||||
this.applicationContext = newApplicationContext(TestEnablePdxConfigurationWithNoDiskStoreConfiguration.class);
|
||||
|
||||
assertThat(this.applicationContext).isNotNull();
|
||||
assertThat(this.applicationContext.containsBean("gemfireCache")).isTrue();
|
||||
assertThat(this.applicationContext.containsBean("TestDiskStore")).isTrue();
|
||||
assertThat(this.applicationContext.containsBean("TestRegion")).isTrue();
|
||||
|
||||
CacheFactoryBean gemfireCache = this.applicationContext.getBean("&gemfireCache", CacheFactoryBean.class);
|
||||
|
||||
assertThat(gemfireCache).isNotNull();
|
||||
assertThat(gemfireCache.getPdxSerializer()).isInstanceOf(MappingPdxSerializer.class);
|
||||
|
||||
BeanDefinition testDiskStoreBeanDefinition =
|
||||
this.applicationContext.getBeanFactory().getBeanDefinition("TestDiskStore");
|
||||
|
||||
assertThat(testDiskStoreBeanDefinition).isNotNull();
|
||||
assertThat(testDiskStoreBeanDefinition.getDependsOn()).isNullOrEmpty();
|
||||
|
||||
BeanDefinition testRegionBeanDefinition =
|
||||
this.applicationContext.getBeanFactory().getBeanDefinition("TestRegion");
|
||||
|
||||
assertThat(testRegionBeanDefinition).isNotNull();
|
||||
assertThat(testRegionBeanDefinition.getDependsOn()).isNullOrEmpty();
|
||||
|
||||
}
|
||||
|
||||
@EnableGemFireMockObjects
|
||||
@PeerCacheApplication
|
||||
@EnablePdx(diskStoreName = "TestDiskStore", serializerBeanName = "MockPdxSerializer")
|
||||
@SuppressWarnings("unused")
|
||||
static class TestEnablePdxWithDiskStoreConfiguration {
|
||||
|
||||
@Bean("TestDiskStore")
|
||||
DiskStoreFactoryBean testPdxDiskStore(GemFireCache gemfireCache) {
|
||||
|
||||
DiskStoreFactoryBean testDiskStore = new DiskStoreFactoryBean();
|
||||
|
||||
testDiskStore.setCache(gemfireCache);
|
||||
|
||||
return testDiskStore;
|
||||
}
|
||||
|
||||
@Bean("TestRegion")
|
||||
LocalRegionFactoryBean<Object, Object> testRegion(GemFireCache gemfireCache) {
|
||||
|
||||
LocalRegionFactoryBean<Object, Object> testRegion = new LocalRegionFactoryBean<>();
|
||||
|
||||
testRegion.setCache(gemfireCache);
|
||||
testRegion.setClose(false);
|
||||
testRegion.setPersistent(false);
|
||||
|
||||
return testRegion;
|
||||
}
|
||||
|
||||
@Bean("MockPdxSerializer")
|
||||
PdxSerializer mockPdxSerializer() {
|
||||
return mock(PdxSerializer.class);
|
||||
}
|
||||
}
|
||||
|
||||
@EnableGemFireMockObjects
|
||||
@ClientCacheApplication
|
||||
@EnablePdx
|
||||
@SuppressWarnings("unused")
|
||||
static class TestEnablePdxConfigurationWithNoDiskStoreConfiguration {
|
||||
|
||||
@Bean("TestDiskStore")
|
||||
DiskStoreFactoryBean testPdxDiskStore(GemFireCache gemfireCache) {
|
||||
|
||||
DiskStoreFactoryBean testDiskStore = new DiskStoreFactoryBean();
|
||||
|
||||
testDiskStore.setCache(gemfireCache);
|
||||
|
||||
return testDiskStore;
|
||||
}
|
||||
|
||||
@Bean("TestRegion")
|
||||
public ClientRegionFactoryBean<Object, Object> testRegion(GemFireCache gemfireCache) {
|
||||
|
||||
ClientRegionFactoryBean<Object, Object> testRegion = new ClientRegionFactoryBean<>();
|
||||
|
||||
testRegion.setCache(gemfireCache);
|
||||
testRegion.setClose(false);
|
||||
testRegion.setShortcut(ClientRegionShortcut.LOCAL);
|
||||
|
||||
return testRegion;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
* Copyright 2018 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.atLeastOnce;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
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 java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.geode.pdx.PdxSerializer;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.data.gemfire.CacheFactoryBean;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.MappingPdxSerializer;
|
||||
import org.springframework.util.MethodInvoker;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EnablePdx} and {@link PdxConfiguration}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.junit.Test
|
||||
* @see org.mockito.Mock
|
||||
* @see org.mockito.Spy
|
||||
* @see org.apache.geode.pdx.PdxSerializer
|
||||
* @see org.mockito.junit.MockitoJUnitRunner
|
||||
* @see org.springframework.beans.factory.BeanFactory
|
||||
* @see org.springframework.data.gemfire.CacheFactoryBean
|
||||
* @see org.springframework.data.gemfire.config.annotation.EnablePdx
|
||||
* @see org.springframework.data.gemfire.config.annotation.PdxConfiguration
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EnablePdxConfigurationUnitTests {
|
||||
|
||||
@Spy
|
||||
private PdxConfiguration pdxConfiguration;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T invokeMethod(Object obj, String methodName) throws Exception {
|
||||
|
||||
MethodInvoker methodInvoker = new MethodInvoker();
|
||||
|
||||
methodInvoker.setTargetObject(obj);
|
||||
methodInvoker.setTargetMethod(methodName);
|
||||
methodInvoker.prepare();
|
||||
|
||||
return (T) methodInvoker.invoke();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setImportMetadataWhenEnablePdxIsConfigured() {
|
||||
|
||||
AnnotationMetadata mockAnnotationMetadata = mock(AnnotationMetadata.class);
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
Map<String, Object> annotationAttributes = new HashMap<>(5);
|
||||
|
||||
annotationAttributes.put("diskStoreName", "MockDiskStore");
|
||||
annotationAttributes.put("ignoreUnreadFields", Boolean.TRUE);
|
||||
annotationAttributes.put("persistent", Boolean.TRUE);
|
||||
annotationAttributes.put("readSerialized", Boolean.TRUE);
|
||||
annotationAttributes.put("serializerBeanName", "MockPdxSerializer");
|
||||
|
||||
when(mockAnnotationMetadata.hasAnnotation(eq(EnablePdx.class.getName()))).thenReturn(true);
|
||||
when(mockAnnotationMetadata.getAnnotationAttributes(eq(EnablePdx.class.getName())))
|
||||
.thenReturn(annotationAttributes);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
this.pdxConfiguration.setImportMetadata(mockAnnotationMetadata);
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.getDiskStoreName().orElse(null)).isEqualTo("MockDiskStore");
|
||||
assertThat(this.pdxConfiguration.isIgnoreUnreadFields()).isTrue();
|
||||
assertThat(this.pdxConfiguration.isPersistent()).isTrue();
|
||||
assertThat(this.pdxConfiguration.isReadSerialized()).isTrue();
|
||||
assertThat(this.pdxConfiguration.getSerializerBeanName().orElse(null)).isEqualTo("MockPdxSerializer");
|
||||
|
||||
verify(mockAnnotationMetadata, times(1)).hasAnnotation(eq(EnablePdx.class.getName()));
|
||||
verify(mockAnnotationMetadata, times(1))
|
||||
.getAnnotationAttributes(eq(EnablePdx.class.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setImportMetadataWhenEnablePdxIsNotConfigured() {
|
||||
|
||||
AnnotationMetadata mockAnnotationMetadata = mock(AnnotationMetadata.class);
|
||||
|
||||
when(mockAnnotationMetadata.hasAnnotation(anyString())).thenReturn(false);
|
||||
|
||||
this.pdxConfiguration.setImportMetadata(mockAnnotationMetadata);
|
||||
|
||||
assertThat(this.pdxConfiguration.getDiskStoreName().isPresent()).isFalse();
|
||||
assertThat(this.pdxConfiguration.isIgnoreUnreadFields()).isFalse();
|
||||
assertThat(this.pdxConfiguration.isPersistent()).isFalse();
|
||||
assertThat(this.pdxConfiguration.isReadSerialized()).isFalse();
|
||||
assertThat(this.pdxConfiguration.getSerializerBeanName().isPresent()).isFalse();
|
||||
|
||||
verify(mockAnnotationMetadata, times(1)).hasAnnotation(eq(EnablePdx.class.getName()));
|
||||
verifyNoMoreInteractions(mockAnnotationMetadata);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configuresPdxForCacheFactoryBean() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
PdxSerializer mockPdxSerializer = mock(PdxSerializer.class);
|
||||
|
||||
when(mockBeanFactory.containsBean(eq("MockPdxSerializer"))).thenReturn(true);
|
||||
when(mockBeanFactory.getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class)))
|
||||
.thenReturn(mockPdxSerializer);
|
||||
|
||||
doReturn(Optional.of("MockPdxDiskStore")).when(this.pdxConfiguration).getDiskStoreName();
|
||||
doReturn(true).when(this.pdxConfiguration).isIgnoreUnreadFields();
|
||||
doReturn(true).when(this.pdxConfiguration).isPersistent();
|
||||
doReturn(true).when(this.pdxConfiguration).isReadSerialized();
|
||||
doReturn(Optional.of("MockPdxSerializer")).when(this.pdxConfiguration).getSerializerBeanName();
|
||||
|
||||
CacheFactoryBean cacheFactoryBean = new CacheFactoryBean();
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
this.pdxConfiguration.configurePdx(cacheFactoryBean);
|
||||
|
||||
assertThat(cacheFactoryBean.getPdxDiskStoreName()).isEqualTo("MockPdxDiskStore");
|
||||
assertThat(cacheFactoryBean.getPdxIgnoreUnreadFields()).isTrue();
|
||||
assertThat(cacheFactoryBean.getPdxPersistent()).isTrue();
|
||||
assertThat(cacheFactoryBean.getPdxReadSerialized()).isTrue();
|
||||
assertThat(cacheFactoryBean.getPdxSerializer()).isEqualTo(mockPdxSerializer);
|
||||
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("MockPdxSerializer"));
|
||||
verify(mockBeanFactory, times(1))
|
||||
.getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class));
|
||||
|
||||
verify(this.pdxConfiguration, times(1)).getDiskStoreName();
|
||||
verify(this.pdxConfiguration, times(1)).isIgnoreUnreadFields();
|
||||
verify(this.pdxConfiguration, times(1)).isPersistent();
|
||||
verify(this.pdxConfiguration, times(1)).isReadSerialized();
|
||||
verify(this.pdxConfiguration, times(1)).getSerializerBeanName();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveConversionServiceWithNonConfigurableBeanFactoryReturnsNull() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.resolveConversionService().isPresent()).isFalse();
|
||||
|
||||
verifyZeroInteractions(mockBeanFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveConversionServiceReturnsConfiguredConversionService() {
|
||||
|
||||
ConfigurableBeanFactory mockBeanFactory = mock(ConfigurableBeanFactory.class);
|
||||
|
||||
ConversionService mockConversionService = mock(ConversionService.class);
|
||||
|
||||
when(mockBeanFactory.getConversionService()).thenReturn(mockConversionService);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.resolveConversionService().orElse(null))
|
||||
.isEqualTo(mockConversionService);
|
||||
|
||||
verify(mockBeanFactory, atLeastOnce()).getConversionService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void resolveMappingContextWhenBeanFactoryDoesNotContainMappingContextBeanReturnsNull() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
when(mockBeanFactory.getBean(any(Class.class))).thenThrow(new NoSuchBeanDefinitionException("test"));
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.resolveMappingContext().isPresent()).isFalse();
|
||||
|
||||
verify(mockBeanFactory, times(1)).getBean(eq(GemfireMappingContext.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMappingContextReturnsConfiguredMappingContext() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
GemfireMappingContext mockMappingContext = mock(GemfireMappingContext.class);
|
||||
|
||||
when(mockBeanFactory.getBean(eq(GemfireMappingContext.class))).thenReturn(mockMappingContext);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.resolveMappingContext().orElse(null)).isEqualTo(mockMappingContext);
|
||||
|
||||
verify(mockBeanFactory, times(1)).getBean(eq(GemfireMappingContext.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePdxSerializerWhenBeanFactoryDoesNotContainPdxSerializerBeanReturnsMappingPdxSerializer() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
when(mockBeanFactory.containsBean(anyString())).thenReturn(false);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
this.pdxConfiguration.setSerializerBeanName("MockPdxSerializer");
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.getSerializerBeanName().orElse(null)).isEqualTo("MockPdxSerializer");
|
||||
assertThat(this.pdxConfiguration.resolvePdxSerializer()).isInstanceOf(MappingPdxSerializer.class);
|
||||
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("MockPdxSerializer"));
|
||||
verify(mockBeanFactory, never()).getBean(anyString(), any(PdxSerializer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePdxSerializerReturnsConfiguredPdxSerializer() {
|
||||
|
||||
BeanFactory mockBeanFactory = mock(BeanFactory.class);
|
||||
|
||||
PdxSerializer mockPdxSerializer = mock(PdxSerializer.class);
|
||||
|
||||
when(mockBeanFactory.containsBean(eq("MockPdxSerializer"))).thenReturn(true);
|
||||
when(mockBeanFactory.getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class)))
|
||||
.thenReturn(mockPdxSerializer);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
this.pdxConfiguration.setSerializerBeanName("MockPdxSerializer");
|
||||
|
||||
assertThat(this.pdxConfiguration.getBeanFactory()).isEqualTo(mockBeanFactory);
|
||||
assertThat(this.pdxConfiguration.getSerializerBeanName().orElse(null)).isEqualTo("MockPdxSerializer");
|
||||
assertThat(this.pdxConfiguration.resolvePdxSerializer()).isEqualTo(mockPdxSerializer);
|
||||
|
||||
verify(mockBeanFactory, times(1)).containsBean(eq("MockPdxSerializer"));
|
||||
verify(mockBeanFactory, times(1))
|
||||
.getBean(eq("MockPdxSerializer"), eq(PdxSerializer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newPdxSerializerReturnsMappingPdxSerializerConfiguredWithResolvedConversionServiceAndMappingContext()
|
||||
throws Exception {
|
||||
|
||||
ConfigurableBeanFactory mockBeanFactory = mock(ConfigurableBeanFactory.class);
|
||||
|
||||
ConversionService mockConversionService = mock(ConversionService.class);
|
||||
|
||||
GemfireMappingContext mockMappingContext = mock(GemfireMappingContext.class);
|
||||
|
||||
when(mockBeanFactory.getBean(eq(GemfireMappingContext.class))).thenReturn(mockMappingContext);
|
||||
when(mockBeanFactory.getConversionService()).thenReturn(mockConversionService);
|
||||
|
||||
this.pdxConfiguration.setBeanFactory(mockBeanFactory);
|
||||
|
||||
PdxSerializer pdxSerializer = this.pdxConfiguration.newPdxSerializer();
|
||||
|
||||
assertThat(pdxSerializer).isInstanceOf(MappingPdxSerializer.class);
|
||||
|
||||
assertThat(this.<Object>invokeMethod(pdxSerializer, "getConversionService"))
|
||||
.isEqualTo(mockConversionService);
|
||||
|
||||
assertThat(this.<Object>invokeMethod(pdxSerializer, "getMappingContext"))
|
||||
.isEqualTo(mockMappingContext);
|
||||
|
||||
verify(mockBeanFactory, times(1)).getBean(eq(GemfireMappingContext.class));
|
||||
verify(mockBeanFactory, atLeastOnce()).getConversionService();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user