DATACMNS-360 - Scanning for repository interfaces now considers active profiles.

The configuration entry points (RepositoryBeanDefinitionParser and RepositoryBeanDefinitionRegistrar) now hand the Environment they're running in forward into the infrastructure scanning for repositories. Thus it will correctly find or not find repository interfaces based on which profiles are activated or not.
This commit is contained in:
Oliver Gierke
2013-08-25 18:29:52 +02:00
parent eb4b801dc9
commit 5ccb0bc9c2
8 changed files with 140 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 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.
@@ -22,6 +22,8 @@ import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
@@ -34,12 +36,14 @@ import org.springframework.core.type.StandardAnnotationMetadata;
public class AnnotationRepositoryConfigurationSourceUnitTests {
RepositoryConfigurationSource source;
Environment environment;
@Before
public void setUp() {
AnnotationMetadata annotationMetadata = new StandardAnnotationMetadata(SampleConfiguration.class, true);
source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class);
environment = new StandardEnvironment();
source = new AnnotationRepositoryConfigurationSource(annotationMetadata, EnableRepositories.class, environment);
}
@Test
@@ -62,7 +66,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfiguration.class);
RepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class);
EnableRepositories.class, environment);
Iterable<String> packages = source.getBasePackages();
assertThat(packages, hasItem(DefaultConfiguration.class.getPackage().getName()));
@@ -73,7 +77,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfigurationWithBasePackage.class);
RepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class);
EnableRepositories.class, environment);
Iterable<String> packages = source.getBasePackages();
assertThat(packages, hasItem("foo"));

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2013 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.repository.config;
import org.springframework.context.annotation.Profile;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSourceUnitTests.Person;
/**
* @author Oliver Gierke
*/
@Profile("profile")
interface ProfileRepository extends Repository<Person, Long> {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 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.
@@ -26,6 +26,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
@@ -38,8 +39,7 @@ import org.springframework.data.repository.core.support.RepositoryFactoryBeanSup
@RunWith(MockitoJUnitRunner.class)
public class RepositoryBeanDefinitionRegistrarSupportUnitTests {
@Mock
BeanDefinitionRegistry registry;
@Mock BeanDefinitionRegistry registry;
@Test
public void registersBeanDefinitionForFoundBean() {
@@ -48,7 +48,41 @@ public class RepositoryBeanDefinitionRegistrarSupportUnitTests {
DummyRegistrar registrar = new DummyRegistrar();
registrar.registerBeanDefinitions(metadata, registry);
verify(registry, times(1)).registerBeanDefinition(eq("myRepository"), any(BeanDefinition.class));
assertBeanDefinitionRegisteredFor("myRepository");
assertNoBeanDefinitionRegisteredFor("profileRepository");
}
/**
* @see DATACMNS-360
*/
@Test
public void registeredProfileRepositoriesIfProfileActivated() {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(SampleConfiguration.class, true);
StandardEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("profile");
DummyRegistrar registrar = new DummyRegistrar();
registrar.setEnvironment(environment);
registrar.registerBeanDefinitions(metadata, registry);
assertBeanDefinitionRegisteredFor("myRepository", "profileRepository");
}
private void assertBeanDefinitionRegisteredFor(String... names) {
for (String name : names) {
verify(registry, times(1)).registerBeanDefinition(eq(name), any(BeanDefinition.class));
}
}
private void assertNoBeanDefinitionRegisteredFor(String... names) {
for (String name : names) {
verify(registry, times(0)).registerBeanDefinition(eq(name), any(BeanDefinition.class));
}
}
static class DummyRegistrar extends RepositoryBeanDefinitionRegistrarSupport {