SGF-697 - Allow basePackages to be configured using a property when creating Entity-defined Regions.
This commit is contained in:
@@ -91,6 +91,8 @@ public @interface EnableEntityDefinedRegions {
|
|||||||
*
|
*
|
||||||
* Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
|
* 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.
|
* @return a {@link String} array specifying the packages to search for application persistent entities.
|
||||||
* @see #value()
|
* @see #value()
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -188,10 +188,11 @@ public class EntityDefinedRegionsConfiguration extends AbstractAnnotationConfigS
|
|||||||
|
|
||||||
Set<String> resolvedBasePackages = new HashSet<>();
|
Set<String> resolvedBasePackages = new HashSet<>();
|
||||||
|
|
||||||
Collections.addAll(resolvedBasePackages, nullSafeArray(defaultIfEmpty(
|
Collections.addAll(resolvedBasePackages, resolveProperty(entitiesProperty("base-packages"),
|
||||||
enableEntityDefinedRegionAttributes.getStringArray("basePackages"),
|
String[].class, nullSafeArray(defaultIfEmpty(
|
||||||
enableEntityDefinedRegionAttributes.getStringArray("value")),
|
enableEntityDefinedRegionAttributes.getStringArray("basePackages"),
|
||||||
String.class));
|
enableEntityDefinedRegionAttributes.getStringArray("value")),
|
||||||
|
String.class)));
|
||||||
|
|
||||||
stream(nullSafeArray(enableEntityDefinedRegionAttributes.getClassArray(
|
stream(nullSafeArray(enableEntityDefinedRegionAttributes.getClassArray(
|
||||||
"basePackageClasses"), Class.class))
|
"basePackageClasses"), Class.class))
|
||||||
|
|||||||
@@ -722,6 +722,11 @@ public abstract class AbstractAnnotationConfigSupport
|
|||||||
return String.format("%1$s%2$s.%3$s", propertyName("disk.store."), name, propertyNameSuffix);
|
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) */
|
/* (non-Javadoc) */
|
||||||
protected String locatorProperty(String propertyNameSuffix) {
|
protected String locatorProperty(String propertyNameSuffix) {
|
||||||
return String.format("%1$s%2$s", propertyName("locator."), propertyNameSuffix);
|
return String.format("%1$s%2$s", propertyName("locator."), propertyNameSuffix);
|
||||||
|
|||||||
@@ -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<Long, Person> 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 {
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user