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
pull/5341/head
Stephane Nicoll 9 years ago
parent bbd7840785
commit ad53f09ed4

@ -19,12 +19,14 @@ package org.springframework.boot.autoconfigure.data.couchbase;
import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Bucket;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.data.couchbase.repository.CouchbaseRepository; import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean; import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean;
/** /**
@ -32,10 +34,12 @@ import org.springframework.data.couchbase.repository.support.CouchbaseRepository
* Repositories. * Repositories.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
* @since 1.4.0 * @since 1.4.0
*/ */
@Configuration @Configuration
@ConditionalOnClass({ Bucket.class, CouchbaseRepository.class }) @ConditionalOnClass({ Bucket.class, CouchbaseRepository.class })
@ConditionalOnBean(RepositoryOperationsMapping.class)
@ConditionalOnProperty(prefix = "spring.data.couchbase.repositories", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.data.couchbase.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnMissingBean(CouchbaseRepositoryFactoryBean.class) @ConditionalOnMissingBean(CouchbaseRepositoryFactoryBean.class)
@Import(CouchbaseRepositoriesRegistrar.class) @Import(CouchbaseRepositoriesRegistrar.class)

@ -16,18 +16,17 @@
package org.springframework.boot.autoconfigure.data.couchbase; package org.springframework.boot.autoconfigure.data.couchbase;
import com.couchbase.client.java.Bucket;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; 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.couchbase.CouchbaseTestConfiguration;
import org.springframework.boot.autoconfigure.data.couchbase.city.City; import org.springframework.boot.autoconfigure.data.couchbase.city.City;
import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository; import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository;
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; 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.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -38,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link CouchbaseRepositoriesAutoConfiguration}. * Tests for {@link CouchbaseRepositoriesAutoConfiguration}.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class CouchbaseRepositoriesAutoConfigurationTests { public class CouchbaseRepositoriesAutoConfigurationTests {
@ -45,39 +45,67 @@ public class CouchbaseRepositoriesAutoConfigurationTests {
@After @After
public void close() { public void close() {
this.context.close(); if (this.context != null) {
this.context.close();
}
} }
@Test @Test
public void testDefaultRepositoryConfiguration() throws Exception { public void couchbaseNotAvailable() throws Exception {
this.context = new AnnotationConfigApplicationContext(); load(CouchbaseNotAvailableConfiguration.class);
this.context.register(TestConfiguration.class, assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0);
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
assertThat(this.context.getBean(Bucket.class)).isNotNull();
} }
@Test @Test
public void testNoRepositoryConfiguration() throws Exception { public void defaultRepository() throws Exception {
this.context = new AnnotationConfigApplicationContext(); load(DefaultConfiguration.class);
this.context.register(EmptyConfiguration.class, TestConfiguration.class, assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(1);
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(Bucket.class)).isNotNull();
} }
@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 @Configuration
@TestAutoConfigurationPackage(City.class) @TestAutoConfigurationPackage(City.class)
@EnableConfigurationProperties(CouchbaseProperties.class) @Import(CouchbaseTestConfiguration.class)
@Import({ CouchbaseRepositoriesRegistrar.class, CouchbaseTestConfiguration.class }) static class DefaultConfiguration {
static class TestConfiguration {
} }
@Configuration @Configuration
@TestAutoConfigurationPackage(EmptyDataPackage.class) @TestAutoConfigurationPackage(EmptyDataPackage.class)
protected static class EmptyConfiguration { @Import(CouchbaseTestConfiguration.class)
protected static class NoRepositoryConfiguration {
} }

Loading…
Cancel
Save