From ad53f09ed484619090350624d508dd5591f0e8d4 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 7 Mar 2016 13:19:22 +0100 Subject: [PATCH] Improve Couchbase repository support Previously, if Couchbase was available on the classpath but not configured, Spring Boot would attempt to scan the project for repositories anyway. This commit makes sure that it only happens if an infrastructure bean required is present. The tests have also been rewritten to better reflect what would happen in practice. Closes gh-5349 --- ...ouchbaseRepositoriesAutoConfiguration.java | 4 ++ ...aseRepositoriesAutoConfigurationTests.java | 70 +++++++++++++------ 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfiguration.java index f13a5d606c..e28c855212 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfiguration.java @@ -19,12 +19,14 @@ package org.springframework.boot.autoconfigure.data.couchbase; import com.couchbase.client.java.Bucket; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.couchbase.repository.CouchbaseRepository; +import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping; import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean; /** @@ -32,10 +34,12 @@ import org.springframework.data.couchbase.repository.support.CouchbaseRepository * Repositories. * * @author Eddú Meléndez + * @author Stephane Nicoll * @since 1.4.0 */ @Configuration @ConditionalOnClass({ Bucket.class, CouchbaseRepository.class }) +@ConditionalOnBean(RepositoryOperationsMapping.class) @ConditionalOnProperty(prefix = "spring.data.couchbase.repositories", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnMissingBean(CouchbaseRepositoryFactoryBean.class) @Import(CouchbaseRepositoriesRegistrar.class) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java index 26fa8f2586..fa21a2567c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java @@ -16,18 +16,17 @@ package org.springframework.boot.autoconfigure.data.couchbase; -import com.couchbase.client.java.Bucket; import org.junit.After; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; -import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties; +import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfiguration; import org.springframework.boot.autoconfigure.data.couchbase.city.City; import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository; import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; -import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -38,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link CouchbaseRepositoriesAutoConfiguration}. * * @author Eddú Meléndez + * @author Stephane Nicoll */ public class CouchbaseRepositoriesAutoConfigurationTests { @@ -45,39 +45,67 @@ public class CouchbaseRepositoriesAutoConfigurationTests { @After public void close() { - this.context.close(); + if (this.context != null) { + this.context.close(); + } } @Test - public void testDefaultRepositoryConfiguration() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(TestConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBean(CityRepository.class)).isNotNull(); - assertThat(this.context.getBean(Bucket.class)).isNotNull(); + public void couchbaseNotAvailable() throws Exception { + load(CouchbaseNotAvailableConfiguration.class); + assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); } @Test - public void testNoRepositoryConfiguration() throws Exception { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(EmptyConfiguration.class, TestConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBean(Bucket.class)).isNotNull(); + public void defaultRepository() throws Exception { + load(DefaultConfiguration.class); + assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(1); } + @Test + public void disableRepository() { + load(DefaultConfiguration.class, "spring.data.couchbase.repositories.enabled=false"); + assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); + } + + @Test + public void noRepositoryAvailable() throws Exception { + load(NoRepositoryConfiguration.class); + assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(context, environment); + if (config != null) { + context.register(config); + } + context.register(PropertyPlaceholderAutoConfiguration.class, + CouchbaseAutoConfiguration.class, + CouchbaseRepositoriesAutoConfiguration.class); + context.refresh(); + this.context = context; + } + + + @Configuration + @TestAutoConfigurationPackage(City.class) + static class CouchbaseNotAvailableConfiguration { + + } + + @Configuration @TestAutoConfigurationPackage(City.class) - @EnableConfigurationProperties(CouchbaseProperties.class) - @Import({ CouchbaseRepositoriesRegistrar.class, CouchbaseTestConfiguration.class }) - static class TestConfiguration { + @Import(CouchbaseTestConfiguration.class) + static class DefaultConfiguration { } @Configuration @TestAutoConfigurationPackage(EmptyDataPackage.class) - protected static class EmptyConfiguration { + @Import(CouchbaseTestConfiguration.class) + protected static class NoRepositoryConfiguration { }