From cf4df833332fca4ec440060600eec04f34bea709 Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 12 Apr 2019 17:08:34 -0700 Subject: [PATCH] Add additional integration tests testing the auto-configuration of GemfireTemplate beans for Regions with existing named and Region-based GemfireTemplates. Resolves gh-31. --- .../RegionTemplateAutoConfiguration.java | 16 +++ ...NameAutoConfigurationIntegrationTests.java | 96 ++++++++++++++++ ...gionAutoConfigurationIntegrationTests.java | 105 ++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByNameAutoConfigurationIntegrationTests.java create mode 100644 spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByRegionAutoConfigurationIntegrationTests.java diff --git a/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java index f316f702..8f63c58f 100644 --- a/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java +++ b/spring-geode-autoconfigure/src/main/java/org/springframework/geode/boot/autoconfigure/RegionTemplateAutoConfiguration.java @@ -15,6 +15,7 @@ */ package org.springframework.geode.boot.autoconfigure; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -37,6 +38,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.data.gemfire.GemfireTemplate; +import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -90,6 +92,8 @@ public class RegionTemplateAutoConfiguration { Optional.ofNullable(applicationContext) .filter(it -> bean instanceof Region) + .filter(it -> !it.containsBean(regionTemplateName)) + .filter(it -> isGemfireTemplateWithRegionNotPresent(it, (Region) bean)) .map(ConfigurableApplicationContext::getBeanFactory) .filter(SingletonBeanRegistry.class::isInstance) .map(SingletonBeanRegistry.class::cast) @@ -116,8 +120,20 @@ public class RegionTemplateAutoConfiguration { .ifPresent(rootRegions -> rootRegions.stream() .filter(Objects::nonNull) .filter(region -> !regionTemplateNames.contains(toRegionTemplateName(region.getName()))) + .filter(region -> !applicationContext.containsBean(toRegionTemplateName(region.getName()))) + .filter(region -> isGemfireTemplateWithRegionNotPresent(applicationContext, region)) .forEach(region -> beanFactory.registerSingleton(toRegionTemplateName(region.getName()), new GemfireTemplate(region)))); } } + + private boolean isGemfireTemplateWithRegionNotPresent(ApplicationContext applicationContext, Region region) { + + Map gemfireTemplateBeans = + applicationContext.getBeansOfType(GemfireTemplate.class, false, false); + + return CollectionUtils.nullSafeMap(gemfireTemplateBeans).values().stream() + .map(GemfireTemplate::getRegion) + .noneMatch(templateRegion -> templateRegion.equals(region)); + } } diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByNameAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByNameAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..505d9402 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByNameAutoConfigurationIntegrationTests.java @@ -0,0 +1,96 @@ +/* + * Copyright 2019 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 + * + * https://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.geode.boot.autoconfigure.template; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.annotation.Resource; + +import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.client.ClientRegionShortcut; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.DependsOn; +import org.springframework.data.gemfire.GemfireTemplate; +import org.springframework.data.gemfire.client.ClientRegionFactoryBean; +import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; +import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration; +import org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration tests for {@link RegionTemplateAutoConfiguration} using explicitly declared {@link Region} + * bean definition and existing, "named" {@link GemfireTemplate} in a Spring {@link ApplicationContext}. + * + * @author John Blum + * @see org.junit.Test + * @see org.apache.geode.cache.Region + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.data.gemfire.GemfireTemplate + * @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects + * @see org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.0.0 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +@SuppressWarnings("unused") +public class ExistingRegionTemplateByNameAutoConfigurationIntegrationTests { + + @Autowired + private GemfireTemplate exampleTemplate; + + @Resource(name = "Example") + private Region example; + + @Test + public void exampleRegionTemplateIsPresent() { + + assertThat(this.exampleTemplate).isNotNull(); + assertThat(this.exampleTemplate.getRegion()).isEqualTo(this.example); + } + + @EnableGemFireMockObjects + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class) + static class TestConfiguration { + + @Bean("Example") + public ClientRegionFactoryBean exampleRegion(GemFireCache gemfireCache) { + + ClientRegionFactoryBean exampleRegion = new ClientRegionFactoryBean<>(); + + exampleRegion.setCache(gemfireCache); + exampleRegion.setClose(false); + exampleRegion.setShortcut(ClientRegionShortcut.LOCAL); + + return exampleRegion; + } + + @Bean + @DependsOn("Example") + public GemfireTemplate exampleTemplate(GemFireCache gemfireCache) { + return new GemfireTemplate(gemfireCache.getRegion("/Example")); + } + } +} diff --git a/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByRegionAutoConfigurationIntegrationTests.java b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByRegionAutoConfigurationIntegrationTests.java new file mode 100644 index 00000000..84e6aed6 --- /dev/null +++ b/spring-geode-autoconfigure/src/test/java/org/springframework/geode/boot/autoconfigure/template/ExistingRegionTemplateByRegionAutoConfigurationIntegrationTests.java @@ -0,0 +1,105 @@ +/* + * Copyright 2019 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 + * + * https://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.geode.boot.autoconfigure.template; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.annotation.Resource; + +import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.client.ClientRegionShortcut; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.DependsOn; +import org.springframework.data.gemfire.GemfireTemplate; +import org.springframework.data.gemfire.client.ClientRegionFactoryBean; +import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; +import org.springframework.geode.boot.autoconfigure.ContinuousQueryAutoConfiguration; +import org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration tests for {@link RegionTemplateAutoConfiguration} using explicitly declared {@link Region} + * bean definition and existing {@link GemfireTemplate} {@link Region} in a Spring {@link ApplicationContext}. + * + * @author John Blum + * @see org.junit.Test + * @see org.apache.geode.cache.Region + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.test.context.SpringBootTest + * @see org.springframework.data.gemfire.GemfireTemplate + * @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects + * @see org.springframework.geode.boot.autoconfigure.RegionTemplateAutoConfiguration + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.0.0 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +@SuppressWarnings("unused") +public class ExistingRegionTemplateByRegionAutoConfigurationIntegrationTests { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private GemfireTemplate testTemplate; + + @Resource(name = "Example") + private Region example; + + @Test + public void exampleRegionTemplateIsNotPresent() { + + assertThat(this.applicationContext.containsBean("exampleTemplate")).isFalse(); + } + + @Test + public void testRegionTemplateIsPresent() { + + assertThat(this.testTemplate).isNotNull(); + assertThat(this.testTemplate.getRegion()).isEqualTo(this.example); + } + + @EnableGemFireMockObjects + @SpringBootApplication(exclude = ContinuousQueryAutoConfiguration.class) + static class TestConfiguration { + + @Bean("Example") + public ClientRegionFactoryBean exampleRegion(GemFireCache gemfireCache) { + + ClientRegionFactoryBean clientRegion = new ClientRegionFactoryBean<>(); + + clientRegion.setCache(gemfireCache); + clientRegion.setClose(false); + clientRegion.setShortcut(ClientRegionShortcut.LOCAL); + + return clientRegion; + } + + @Bean("TestTemplate") + @DependsOn("Example") + GemfireTemplate testTemplate(GemFireCache gemfireCache) { + return new GemfireTemplate(gemfireCache.getRegion("/Example")); + } + } +}