SGF-449 - GemfireRepositoryFactoryBean.setGemfireMappingContext needs to call RepositoryFactoryBeanSupport.setMappingContext.

(cherry picked from commit 37d41a1ed2)
Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2015-12-03 16:19:56 -08:00
parent 17d46b0204
commit fd79e22ab4
2 changed files with 106 additions and 3 deletions

View File

@@ -37,11 +37,14 @@ import com.gemstone.gemfire.cache.Region;
* {@link FactoryBean} adapter for {@link GemfireRepositoryFactory}.
*
* @author Oliver Gierke
* @author John Blum
*/
@SuppressWarnings("unused")
public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID> implements ApplicationContextAware {
private ApplicationContext applicationContext;
private Iterable<Region<?, ?>> regions;
private MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> context;
@@ -58,6 +61,7 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Collection<Region> regions = applicationContext.getBeansOfType(Region.class).values();
this.regions = (Iterable) Collections.unmodifiableCollection(regions);
this.applicationContext = applicationContext;
}
/**
@@ -66,6 +70,7 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
* @param context the context to set
*/
public void setGemfireMappingContext(MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> context) {
setMappingContext(context);
this.context = context;
}
@@ -87,10 +92,9 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
*/
@Override
public void afterPropertiesSet() {
if (this.context == null) {
this.context = new GemfireMappingContext();
setMappingContext(context);
// TODO register the GemfireMappingContext instance as a (Singleton) bean in the Spring context
setGemfireMappingContext(new GemfireMappingContext());
}
super.afterPropertiesSet();

View File

@@ -0,0 +1,99 @@
package org.springframework.data.gemfire.repository.config;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
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.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
import org.springframework.data.mapping.context.MappingContext;
import com.gemstone.gemfire.cache.Region;
/**
* The GemfireRepositoryFactoryBeanTest class is test suite of test cases testing the contract and functionality
* of the GemfireRepositoryFactoryBean class.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @since 1.6.3
*/
public class GemfireRepositoryFactoryBeanTest {
private 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 setApplicationContext() throws Exception {
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 = TestUtils.readField("regions", repositoryFactoryBean);
assertThat(actualRegions, is(notNullValue()));
List<Region> regions = toList(actualRegions);
assertThat(regions.size(), is(equalTo(2)));
assertThat(regions.containsAll(expectedRegions.values()), is(true));
ApplicationContext actualApplicationContext = TestUtils.readField("applicationContext", repositoryFactoryBean);
assertThat(actualApplicationContext, is(sameInstance(mockApplicationContext)));
}
@Test
@SuppressWarnings("unchecked")
public void setGemfireMappingContextAlsoSetsMappingContext() throws Exception {
MappingContext mockMappingContext = mock(MappingContext.class);
repositoryFactoryBean.setGemfireMappingContext(mockMappingContext);
MappingContext actualMappingContext = TestUtils.readField("context", repositoryFactoryBean);
assertThat(actualMappingContext, is(sameInstance(mockMappingContext)));
actualMappingContext = TestUtils.readField("mappingContext", repositoryFactoryBean);
assertThat(actualMappingContext, is(sameInstance(mockMappingContext)));
}
}