Polish contribution

Closes gh-3499
This commit is contained in:
Stephane Nicoll
2016-02-15 20:25:12 +01:00
parent 76f1ca4188
commit da3b49e024
15 changed files with 320 additions and 162 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.couchbase;
import java.util.Arrays;
import java.util.List;
import javax.validation.Validator;
@@ -24,10 +23,14 @@ import javax.validation.Validator;
import com.couchbase.client.java.CouchbaseBucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
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.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.config.CouchbaseBucketFactoryBean;
@@ -38,10 +41,12 @@ import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbase
* Auto-Configuration} for Couchbase.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @since 1.4.0
*/
@Configuration
@ConditionalOnClass({ CouchbaseBucket.class, CouchbaseBucketFactoryBean.class })
@ConditionalOnClass({CouchbaseBucket.class, CouchbaseBucketFactoryBean.class})
@Conditional(CouchbaseAutoConfiguration.CouchbaseCondition.class)
@EnableConfigurationProperties(CouchbaseProperties.class)
public class CouchbaseAutoConfiguration {
@@ -52,24 +57,44 @@ public class CouchbaseAutoConfiguration {
}
@Configuration
static class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@ConditionalOnMissingBean(AbstractCouchbaseConfiguration.class)
public static class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Autowired
private CouchbaseProperties properties;
@Override
protected List<String> getBootstrapHosts() {
return Arrays.asList(this.properties.getHosts());
return this.properties.getBootstrapHosts();
}
@Override
protected String getBucketName() {
return this.properties.getBucketName();
return this.properties.getBucket().getName();
}
@Override
protected String getBucketPassword() {
return this.properties.getBucketPassword();
return this.properties.getBucket().getPassword();
}
}
/**
* Determine if Couchbase should be configured. This happens if either the user-configuration
* defines a couchbase configuration or if at least the bucket name is specified.
*/
static class CouchbaseCondition extends AnyNestedCondition {
CouchbaseCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(prefix = "spring.data.couchbase.bucket", name = "name")
static class BucketNameProperty {
}
@ConditionalOnBean(AbstractCouchbaseConfiguration.class)
static class CouchbaseConfiguration {
}
}

View File

@@ -16,54 +16,68 @@
package org.springframework.boot.autoconfigure.couchbase;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for Couchbase.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "spring.data.couchbase")
public class CouchbaseProperties {
/**
* Couchabase server hosts.
* Couchbase nodes (host or IP address) to bootstrap from.
*/
private String[] hosts;
private List<String> bootstrapHosts = new ArrayList<String>(Collections.singletonList("localhost"));
/**
* Couchbase bucket name.
*/
private String bucketName;
private final Bucket bucket = new Bucket();
/**
* Couchbase bucket password.
*/
private String bucketPassword;
public String[] getHosts() {
return this.hosts;
public List<String> getBootstrapHosts() {
return this.bootstrapHosts;
}
public void setHosts(String[] hosts) {
this.hosts = hosts;
public void setBootstrapHosts(List<String> bootstrapHosts) {
this.bootstrapHosts = bootstrapHosts;
}
public String getBucketName() {
return this.bucketName;
public Bucket getBucket() {
return this.bucket;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
static class Bucket {
public String getBucketPassword() {
return this.bucketPassword;
}
/**
* Name of the bucket to connect to.
*/
private String name;
public void setBucketPassword(String bucketPassword) {
this.bucketPassword = bucketPassword;
/**
* Password of the bucket.
*/
private String password = "";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
}

View File

@@ -16,15 +16,27 @@
package org.springframework.boot.autoconfigure.couchbase;
import javax.validation.Validator;
import com.couchbase.client.java.Bucket;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CouchbaseAutoConfiguration}
@@ -46,17 +58,55 @@ public class CouchbaseAutoConfigurationTests {
}
@Test
public void validateProperties() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.couchbase.hosts=localhost",
"spring.data.couchbase.bucket-name=test",
"spring.data.couchbase.bucket-password=test");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
public void bucketNameIsRequired() {
load(null);
assertThat(this.context.getBeansOfType(CouchbaseTemplate.class)).isEmpty();
assertThat(this.context.getBeansOfType(Bucket.class)).isEmpty();
assertThat(this.context.getBeansOfType(ValidatingCouchbaseEventListener.class)).isEmpty();
}
@Test
public void bucketNameIsNotRequiredIfCustomConfigurationIsSpecified() throws Exception {
load(CouchbaseTestConfiguration.class);
assertThat(this.context.getBeansOfType(AbstractCouchbaseConfiguration.class)).hasSize(1);
CouchbaseTestConfiguration configuration = this.context.getBean(CouchbaseTestConfiguration.class);
assertThat(this.context.getBean(CouchbaseTemplate.class)).isSameAs(configuration.couchbaseTemplate());
assertThat(this.context.getBean(Bucket.class)).isSameAs(configuration.couchbaseClient());
assertThat(this.context.getBeansOfType(ValidatingCouchbaseEventListener.class)).isEmpty();
}
@Test
public void validatorIsPresent() {
load(ValidatorConfiguration.class);
ValidatingCouchbaseEventListener listener = this.context
.getBean(ValidatingCouchbaseEventListener.class);
assertThat(new DirectFieldAccessor(listener).getPropertyValue("validator"))
.isEqualTo(this.context.getBean(Validator.class));
}
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);
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("Connection refused");
this.context.refresh();
context.refresh();
this.context = context;
}
@Configuration
@Import(CouchbaseTestConfiguration.class)
static class ValidatorConfiguration {
@Bean
public Validator myValidator() {
return mock(Validator.class);
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2012-2016 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.boot.autoconfigure.couchbase;
import java.util.Collections;
import java.util.List;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseBucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.cluster.ClusterInfo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import static org.mockito.Mockito.mock;
/**
* Test configuration for couchbase that mocks access.
*
* @author Stephane Nicoll
*/
@Configuration
public class CouchbaseTestConfiguration extends AbstractCouchbaseConfiguration {
@Override
protected List<String> getBootstrapHosts() {
return Collections.singletonList("localhost");
}
@Override
protected String getBucketName() {
return "my-bucket";
}
@Override
protected String getBucketPassword() {
return "my-password";
}
@Override
public Cluster couchbaseCluster() throws Exception {
return mock(CouchbaseCluster.class);
}
@Bean
public ClusterInfo couchbaseClusterInfo() {
return mock(ClusterInfo.class);
}
@Override
public Bucket couchbaseClient() throws Exception {
return mock(CouchbaseBucket.class);
}
}

View File

@@ -16,35 +16,21 @@
package org.springframework.boot.autoconfigure.data.couchbase;
import java.util.Collections;
import java.util.List;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseBucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.cluster.ClusterInfo;
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.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.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.WriteResultChecking;
import org.springframework.data.couchbase.core.query.Consistency;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* @author Eddú Meléndez
@@ -61,7 +47,6 @@ public class CouchbaseRepositoriesAutoConfigurationTests {
@Test
public void testDefaultRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
addCouchbaseProperties(this.context);
this.context.register(TestConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
@@ -72,79 +57,16 @@ public class CouchbaseRepositoriesAutoConfigurationTests {
@Test
public void testNoRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
addCouchbaseProperties(this.context);
this.context.register(EmptyConfiguration.class, TestConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(Bucket.class)).isNotNull();
}
@Test
public void templateExists() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.couchbase.hosts=localhost",
"spring.data.couchbase.bucket-name=test",
"spring.data.couchbase.bucket-password=test");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(CouchbaseTemplate.class).length).isEqualTo(1);
}
private void addCouchbaseProperties(AnnotationConfigApplicationContext context) {
EnvironmentTestUtils.addEnvironment(context,
"spring.data.couchbase.hosts=localhost",
"spring.data.couchbase.bucket-name=test",
"spring.data.couchbase.bucket-password=test");
}
@Configuration
@TestAutoConfigurationPackage(City.class)
@Import(CouchbaseRepositoriesRegistrar.class)
static class TestConfiguration extends AbstractCouchbaseConfiguration {
@Override
protected List<String> getBootstrapHosts() {
return Collections.singletonList("192.1.2.3");
}
@Override
protected String getBucketName() {
return "someBucket";
}
@Override
protected String getBucketPassword() {
return "someBucketPassword";
}
@Override
public Cluster couchbaseCluster() throws Exception {
return mock(CouchbaseCluster.class);
}
@Bean
public ClusterInfo couchbaseClusterInfo() {
return mock(ClusterInfo.class);
}
@Override
public Bucket couchbaseClient() throws Exception {
return mock(CouchbaseBucket.class);
}
@Override
public CouchbaseTemplate couchbaseTemplate() throws Exception {
CouchbaseTemplate template = super.couchbaseTemplate();
template.setWriteResultChecking(WriteResultChecking.LOG);
return template;
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.READ_YOUR_OWN_WRITES;
}
@Import({ CouchbaseRepositoriesRegistrar.class, CouchbaseTestConfiguration.class })
static class TestConfiguration {
}