diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegions.java b/src/main/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegions.java index 78454de0..7c509d1e 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegions.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegions.java @@ -91,6 +91,8 @@ public @interface EnableEntityDefinedRegions { * * Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names. * + * Use the {@literal spring.data.gemfire.entities.base-packages} property in {@literal application.properties}. + * * @return a {@link String} array specifying the packages to search for application persistent entities. * @see #value() */ diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java index 0a06a241..9eb2c3da 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/EntityDefinedRegionsConfiguration.java @@ -188,10 +188,11 @@ public class EntityDefinedRegionsConfiguration extends AbstractAnnotationConfigS Set resolvedBasePackages = new HashSet<>(); - Collections.addAll(resolvedBasePackages, nullSafeArray(defaultIfEmpty( - enableEntityDefinedRegionAttributes.getStringArray("basePackages"), - enableEntityDefinedRegionAttributes.getStringArray("value")), - String.class)); + Collections.addAll(resolvedBasePackages, resolveProperty(entitiesProperty("base-packages"), + String[].class, nullSafeArray(defaultIfEmpty( + enableEntityDefinedRegionAttributes.getStringArray("basePackages"), + enableEntityDefinedRegionAttributes.getStringArray("value")), + String.class))); stream(nullSafeArray(enableEntityDefinedRegionAttributes.getClassArray( "basePackageClasses"), Class.class)) diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java b/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java index bf7c6094..a6b063bb 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java @@ -722,6 +722,11 @@ public abstract class AbstractAnnotationConfigSupport return String.format("%1$s%2$s.%3$s", propertyName("disk.store."), name, propertyNameSuffix); } + /* (non-Javadoc) */ + protected String entitiesProperty(String propertyNameSuffix) { + return String.format("%1$s%2$s", propertyName("entities."), propertyNameSuffix); + } + /* (non-Javadoc) */ protected String locatorProperty(String propertyNameSuffix) { return String.format("%1$s%2$s", propertyName("locator."), propertyNameSuffix); diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegionsIntegrationTests.java b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegionsIntegrationTests.java new file mode 100644 index 00000000..034567ce --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/EnableEntityDefinedRegionsIntegrationTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2017 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 + * + * http://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.data.gemfire.config.annotation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; + +import org.apache.geode.cache.DataPolicy; +import org.apache.geode.cache.Region; +import org.junit.After; +import org.junit.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.data.gemfire.GemfireUtils; +import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects; +import org.springframework.data.gemfire.test.model.Person; +import org.springframework.mock.env.MockPropertySource; + +/** + * Integration tests for {@link EnableEntityDefinedRegions} and {@link EntityDefinedRegionsConfiguration}. + * + * @author John Blum + * @see org.junit.Test + * @see org.apache.geode.cache.Region + * @see org.springframework.context.ConfigurableApplicationContext + * @see org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions + * @see org.springframework.data.gemfire.config.annotation.EntityDefinedRegionsConfiguration + * @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects + * @since 2.0.2 + */ +public class EnableEntityDefinedRegionsIntegrationTests { + + private ConfigurableApplicationContext applicationContext; + + @After + public void tearDown() { + Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close); + } + + private ConfigurableApplicationContext newApplicationContext(PropertySource testPropertySource, + Class... annotatedClasses) { + + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + + MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources(); + + propertySources.addFirst(testPropertySource); + + applicationContext.register(annotatedClasses); + applicationContext.registerShutdownHook(); + applicationContext.refresh(); + + return applicationContext; + } + + @Test + @SuppressWarnings("unchecked") + public void entityBasePackagesAreIdentifiedByProperty() { + + MockPropertySource testPropertySource = new MockPropertySource() + .withProperty("spring.data.gemfire.entities.base-packages", Person.class.getPackage().getName()); + + this.applicationContext = newApplicationContext(testPropertySource, TestConfiguration.class); + + assertThat(this.applicationContext).isNotNull(); + + Region people = this.applicationContext.getBean("People", Region.class); + + assertThat(people).isNotNull(); + assertThat(people.getName()).isEqualTo("People"); + assertThat(people.getFullPath()).isEqualTo(GemfireUtils.toRegionPath("People")); + assertThat(people.getAttributes()).isNotNull(); + assertThat(people.getAttributes().getDataPolicy()).isEqualTo(DataPolicy.EMPTY); + } + + @ClientCacheApplication + @EnableGemFireMockObjects + @EnableEntityDefinedRegions + static class TestConfiguration { + } +}