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 ec06c4b3..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,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, S, ID extends Serializable> extends RepositoryFactoryBeanSupport implements ApplicationContextAware { + private ApplicationContext applicationContext; + private Iterable> regions; private MappingContext, GemfirePersistentProperty> context; @@ -58,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; } /** @@ -66,6 +70,7 @@ public class GemfireRepositoryFactoryBean, S, ID ext * @param context the context to set */ public void setGemfireMappingContext(MappingContext, GemfirePersistentProperty> context) { + setMappingContext(context); this.context = context; } @@ -87,10 +92,9 @@ 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))); + } + +}