SGF-455 - Simplified registration for shared GemfireMappingContext for repositories.
A <gemfire:repositories /> now registers a single bean definition for a GemfireMappingContext that gets wired to the GemfireRepositoryFactoryBean instances registered within the system unless an explicit mapping context is referenced. The GemfireRepositoryFactoryBean now checks a GemfireMappingContext being registered and rejects to start without one. The same applies to GemfireRepositoryFactory. Related tickets: SGF-448, SGF-449, SGF-450. (cherry picked from commit 34665d16ec543e85afcde174194ba5fbff44b841)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2015 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.
|
||||
@@ -16,19 +16,14 @@
|
||||
|
||||
package org.springframework.data.gemfire.repository.config;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
|
||||
@@ -49,6 +44,7 @@ import org.w3c.dom.Element;
|
||||
public class GemfireRepositoryConfigurationExtensionTest {
|
||||
|
||||
private GemfireRepositoryConfigurationExtension repositoryConfigurationExtension;
|
||||
private RuntimeBeanReference defaultMappingContextReference = new RuntimeBeanReference(GemfireRepositoryConfigurationExtension.DEFAULT_MAPPING_CONTEXT_BEAN_NAME);
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@@ -97,10 +93,10 @@ public class GemfireRepositoryConfigurationExtensionTest {
|
||||
|
||||
repositoryConfigurationExtension.postProcess(beanDefinitionBuilder, mockRepositoryConfigurationSource);
|
||||
|
||||
Object mappingContextRef = beanDefinitionBuilder.getRawBeanDefinition().getPropertyValues()
|
||||
PropertyValue mappingContextRef = beanDefinitionBuilder.getRawBeanDefinition().getPropertyValues()
|
||||
.getPropertyValue("gemfireMappingContext");
|
||||
|
||||
assertThat(mappingContextRef, is(nullValue()));
|
||||
assertThat(mappingContextRef.getValue(), is((Object) defaultMappingContextReference));
|
||||
|
||||
verify(mockRepositoryConfigurationSource, times(1)).getAttribute(eq("mappingContextRef"));
|
||||
}
|
||||
@@ -119,10 +115,10 @@ public class GemfireRepositoryConfigurationExtensionTest {
|
||||
|
||||
repositoryConfigurationExtension.postProcess(beanDefinitionBuilder, mockRepositoryConfigurationSource);
|
||||
|
||||
Object mappingContextRef = beanDefinitionBuilder.getRawBeanDefinition().getPropertyValues()
|
||||
PropertyValue mappingContextRef = beanDefinitionBuilder.getRawBeanDefinition().getPropertyValues()
|
||||
.getPropertyValue("gemfireMappingContext");
|
||||
|
||||
assertThat(mappingContextRef, is(nullValue()));
|
||||
assertThat(mappingContextRef.getValue(), is((Object) defaultMappingContextReference));
|
||||
|
||||
verify(mockRepositoryConfigurationSource, times(1)).getElement();
|
||||
verify(mockElement, times(1)).getAttribute(eq("mapping-context-ref"));
|
||||
|
||||
@@ -16,40 +16,22 @@
|
||||
|
||||
package org.springframework.data.gemfire.repository.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Matchers;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.gemfire.TestUtils;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.gemfire.repository.sample.PersonRepository;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
import com.gemstone.gemfire.cache.RegionAttributes;
|
||||
|
||||
/**
|
||||
* The GemfireRepositoryFactoryBeanTest class is test suite of test cases testing the contract and functionality
|
||||
@@ -62,196 +44,47 @@ import com.gemstone.gemfire.cache.Region;
|
||||
* @see org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean
|
||||
* @since 1.6.3
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class GemfireRepositoryFactoryBeanTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
private GemfireRepositoryFactoryBean repositoryFactoryBean;
|
||||
GemfireRepositoryFactoryBean repositoryFactoryBean;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
repositoryFactoryBean = new GemfireRepositoryFactoryBean();
|
||||
}
|
||||
|
||||
protected <T> List<T> toList(Iterable<T> iterable) {
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
if (iterable != null) {
|
||||
for (T element : iterable) {
|
||||
list.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
@Test
|
||||
public void rejectsMappingContextNotSet() {
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage("GemfireMappingContext");
|
||||
|
||||
repositoryFactoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setAndGetApplicationContextSuccessfully() {
|
||||
Map<String, Region> expectedRegions = new HashMap<String, Region>(2);
|
||||
|
||||
expectedRegions.put("regionOne", mock(Region.class));
|
||||
expectedRegions.put("regionTwo", mock(Region.class));
|
||||
|
||||
ApplicationContext mockApplicationContext = mock(ApplicationContext.class);
|
||||
|
||||
when(mockApplicationContext.getBeansOfType(eq(Region.class))).thenReturn(expectedRegions);
|
||||
|
||||
repositoryFactoryBean.setApplicationContext(mockApplicationContext);
|
||||
|
||||
Iterable<Region> actualRegions = repositoryFactoryBean.getRegions();
|
||||
|
||||
assertThat(actualRegions, is(notNullValue()));
|
||||
|
||||
List<Region> regions = toList(actualRegions);
|
||||
|
||||
assertThat(regions.size(), is(equalTo(2)));
|
||||
assertThat(regions.containsAll(expectedRegions.values()), is(true));
|
||||
|
||||
ApplicationContext actualApplicationContext = repositoryFactoryBean.getApplicationContext();
|
||||
|
||||
assertThat(actualApplicationContext, is(sameInstance(mockApplicationContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBeansOfType(eq(Region.class));
|
||||
public void initializesWithMappingContext() {
|
||||
|
||||
RegionAttributes<?, ?> attributes = mock(RegionAttributes.class);
|
||||
doReturn(Long.class).when(attributes).getKeyConstraint();
|
||||
|
||||
Region<?, ?> region = mock(Region.class);
|
||||
doReturn("simple").when(region).getName();
|
||||
doReturn(attributes).when(region).getAttributes();
|
||||
|
||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||
doReturn(Collections.singletonMap("simple", region)).when(applicationContext).getBeansOfType(Region.class);
|
||||
|
||||
repositoryFactoryBean.setApplicationContext(applicationContext);
|
||||
repositoryFactoryBean.setRepositoryInterface(PersonRepository.class);
|
||||
repositoryFactoryBean.setGemfireMappingContext(new GemfireMappingContext());
|
||||
repositoryFactoryBean.afterPropertiesSet();
|
||||
|
||||
assertThat(repositoryFactoryBean.getObject(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNullApplicationContextThrowsIllegalStateException() {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The Spring ApplicationContext was not properly initialized");
|
||||
repositoryFactoryBean.getApplicationContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setGemfireMappingContextAlsoSetsMappingContext() throws Exception {
|
||||
MappingContext mockMappingContext = mock(MappingContext.class);
|
||||
|
||||
repositoryFactoryBean.setGemfireMappingContext(mockMappingContext);
|
||||
|
||||
MappingContext actualMappingContext = repositoryFactoryBean.getGemfireMappingContext();
|
||||
|
||||
assertThat(actualMappingContext, is(sameInstance(mockMappingContext)));
|
||||
|
||||
actualMappingContext = TestUtils.readField("mappingContext", repositoryFactoryBean);
|
||||
|
||||
assertThat(actualMappingContext, is(sameInstance(mockMappingContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void resolveConfiguredMappingContext() {
|
||||
final MappingContext mockMappingContext = mock(MappingContext.class);
|
||||
|
||||
repositoryFactoryBean = new GemfireRepositoryFactoryBean() {
|
||||
@Override protected MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> getGemfireMappingContext() {
|
||||
return mockMappingContext;
|
||||
}
|
||||
|
||||
@Override public void setGemfireMappingContext(MappingContext mappingContext) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"setGemfireMappingContext with (%1$s) should not have been called", mappingContext));
|
||||
}
|
||||
};
|
||||
|
||||
repositoryFactoryBean.resolveMappingContext();
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(sameInstance(mockMappingContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMappingContextFromApplicationContext() throws Exception {
|
||||
final ApplicationContext mockApplicationContext = mock(ApplicationContext.class);
|
||||
|
||||
GemfireMappingContext mockMappingContext = mock(GemfireMappingContext.class);
|
||||
|
||||
when(mockApplicationContext.getBean(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME), eq(
|
||||
GemfireMappingContext.class))).thenReturn(mockMappingContext);
|
||||
|
||||
repositoryFactoryBean = new GemfireRepositoryFactoryBean() {
|
||||
@Override protected ApplicationContext getApplicationContext() {
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(nullValue()));
|
||||
|
||||
repositoryFactoryBean.resolveMappingContext();
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(sameInstance(
|
||||
(MappingContext) mockMappingContext)));
|
||||
|
||||
MappingContext actualMappingContext = TestUtils.readField("mappingContext", repositoryFactoryBean);
|
||||
|
||||
assertThat(actualMappingContext, is(sameInstance((MappingContext) mockMappingContext)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBean(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME),
|
||||
eq(GemfireMappingContext.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveNewMappingContext() throws Exception {
|
||||
final ApplicationContext mockApplicationContext = mock(ApplicationContext.class);
|
||||
|
||||
when(mockApplicationContext.getBean(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME), eq(
|
||||
GemfireMappingContext.class))).thenThrow(new NoSuchBeanDefinitionException(
|
||||
GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME));
|
||||
|
||||
repositoryFactoryBean = new GemfireRepositoryFactoryBean() {
|
||||
@Override protected ApplicationContext getApplicationContext() {
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(nullValue()));
|
||||
|
||||
repositoryFactoryBean.resolveMappingContext();
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(instanceOf(GemfireMappingContext.class)));
|
||||
|
||||
MappingContext actualMappingContext = TestUtils.readField("mappingContext", repositoryFactoryBean);
|
||||
|
||||
assertThat(actualMappingContext, is(instanceOf(GemfireMappingContext.class)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBean(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME),
|
||||
eq(GemfireMappingContext.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveNewMappingContextAndRegistersSingleton() throws Exception {
|
||||
final ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class);
|
||||
|
||||
ConfigurableListableBeanFactory mockBeanFactory = mock(ConfigurableListableBeanFactory.class);
|
||||
|
||||
when(mockApplicationContext.getBean(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME), eq(
|
||||
GemfireMappingContext.class))).thenThrow(new NoSuchBeanDefinitionException(
|
||||
GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME));
|
||||
|
||||
when(mockApplicationContext.getBeanFactory()).thenReturn(mockBeanFactory);
|
||||
|
||||
repositoryFactoryBean = new GemfireRepositoryFactoryBean() {
|
||||
@Override protected ApplicationContext getApplicationContext() {
|
||||
return mockApplicationContext;
|
||||
}
|
||||
};
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(nullValue()));
|
||||
|
||||
repositoryFactoryBean.resolveMappingContext();
|
||||
|
||||
assertThat(repositoryFactoryBean.getGemfireMappingContext(), is(instanceOf(GemfireMappingContext.class)));
|
||||
|
||||
MappingContext actualMappingContext = TestUtils.readField("mappingContext", repositoryFactoryBean);
|
||||
|
||||
assertThat(actualMappingContext, is(instanceOf(GemfireMappingContext.class)));
|
||||
|
||||
verify(mockApplicationContext, times(1)).getBean(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME),
|
||||
eq(GemfireMappingContext.class));
|
||||
verify(mockApplicationContext, times(1)).getBeanFactory();
|
||||
verify(mockBeanFactory, times(1)).registerSingleton(eq(GemfireRepositoryFactoryBean.DEFAULT_MAPPING_CONTEXT_BEAN_NAME),
|
||||
Matchers.isA(GemfireMappingContext.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2015 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.
|
||||
@@ -15,15 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.gemfire.repository.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.Regions;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.gemfire.repository.sample.PersonRepository;
|
||||
@@ -39,13 +39,13 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
@ContextConfiguration("../config/repo-context.xml")
|
||||
public class GemfireRepositoryFactoryIntegrationTests extends AbstractGemfireRepositoryFactoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
@Autowired ApplicationContext context;
|
||||
@Autowired GemfireMappingContext mappingContext;
|
||||
|
||||
@Override
|
||||
protected PersonRepository getRepository(Regions regions) {
|
||||
|
||||
GemfireRepositoryFactory factory = new GemfireRepositoryFactory(regions, null);
|
||||
GemfireRepositoryFactory factory = new GemfireRepositoryFactory(regions, mappingContext);
|
||||
return factory.getRepository(PersonRepository.class);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class GemfireRepositoryFactoryIntegrationTests extends AbstractGemfireRep
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void throwsExceptionIfReferencedRegionIsNotConfigured() {
|
||||
|
||||
GemfireRepositoryFactory factory = new GemfireRepositoryFactory((Iterable) Collections.emptySet(), null);
|
||||
GemfireRepositoryFactory factory = new GemfireRepositoryFactory((Iterable) Collections.emptySet(), mappingContext);
|
||||
factory.getRepository(PersonRepository.class);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user