From 14d42618a15f0cd26cb17e3e6ab33a48437f24d7 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 3 Dec 2015 16:19:56 -0800 Subject: [PATCH] SGF-449 - GemfireRepositoryFactoryBean.setGemfireMappingContext needs to call RepositoryFactoryBeanSupport.setMappingContext. (cherry picked from commit 37d41a1ed2166a5296e43d2778511fd3131e4510) Signed-off-by: John Blum --- .../support/GemfireRepositoryFactoryBean.java | 21 ++-- .../GemfireRepositoryFactoryBeanTest.java | 99 +++++++++++++++++++ 2 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/springframework/data/gemfire/repository/config/GemfireRepositoryFactoryBeanTest.java diff --git a/src/main/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryBean.java b/src/main/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryBean.java index 3fba3505..489b7deb 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryBean.java @@ -37,14 +37,18 @@ import com.gemstone.gemfire.cache.Region; * {@link FactoryBean} adapter for {@link GemfireRepositoryFactory}. * * @author Oliver Gierke + * @author John Blum */ -public class GemfireRepositoryFactoryBean, S, ID extends Serializable> extends - RepositoryFactoryBeanSupport implements ApplicationContextAware { +@SuppressWarnings("unused") +public class GemfireRepositoryFactoryBean, S, ID extends Serializable> + extends RepositoryFactoryBeanSupport implements ApplicationContextAware { - private MappingContext, GemfirePersistentProperty> context; + private ApplicationContext applicationContext; private Iterable> regions; + private MappingContext, GemfirePersistentProperty> context; + /* * (non-Javadoc) * @@ -57,6 +61,7 @@ public class GemfireRepositoryFactoryBean, S, ID ext public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Collection regions = applicationContext.getBeansOfType(Region.class).values(); this.regions = (Iterable) Collections.unmodifiableCollection(regions); + this.applicationContext = applicationContext; } /** @@ -64,8 +69,8 @@ public class GemfireRepositoryFactoryBean, S, ID ext * * @param context the context to set */ - public void setGemfireMappingContext( - MappingContext, GemfirePersistentProperty> context) { + public void setGemfireMappingContext(MappingContext, GemfirePersistentProperty> context) { + setMappingContext(context); this.context = context; } @@ -87,12 +92,12 @@ public class GemfireRepositoryFactoryBean, 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(); } + } diff --git a/src/test/java/org/springframework/data/gemfire/repository/config/GemfireRepositoryFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/repository/config/GemfireRepositoryFactoryBeanTest.java new file mode 100644 index 00000000..4ca0a914 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/config/GemfireRepositoryFactoryBeanTest.java @@ -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 List toList(Iterable iterable) { + List list = new ArrayList(); + + if (iterable != null) { + for (T element : iterable) { + list.add(element); + } + } + + return list; + } + + @Test + public void setApplicationContext() throws Exception { + Map expectedRegions = new HashMap(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 actualRegions = TestUtils.readField("regions", repositoryFactoryBean); + + assertThat(actualRegions, is(notNullValue())); + + List 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))); + } + +}