diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/AbstractCouchbaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/AbstractCouchbaseAutoConfigurationTests.java index 69d00e5002..7fcf5e0f04 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/AbstractCouchbaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/AbstractCouchbaseAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationIntegrationTests.java index bb0e6a0926..ac04d9b622 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -24,6 +24,9 @@ import com.couchbase.client.java.env.CouchbaseEnvironment; import org.junit.Rule; import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -35,28 +38,38 @@ import static org.mockito.Mockito.mock; * * @author Stephane Nicoll */ -public class CouchbaseAutoConfigurationIntegrationTests - extends AbstractCouchbaseAutoConfigurationTests { +public class CouchbaseAutoConfigurationIntegrationTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class, + CouchbaseAutoConfiguration.class)); @Rule public final CouchbaseTestServer couchbase = new CouchbaseTestServer(); @Test public void defaultConfiguration() { - load(null, "spring.couchbase.bootstrapHosts=localhost"); - assertThat(this.context.getBeansOfType(Cluster.class)).hasSize(1); - assertThat(this.context.getBeansOfType(ClusterInfo.class)).hasSize(1); - assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); - assertThat(this.context.getBeansOfType(Bucket.class)).hasSize(1); + this.contextRunner + .withPropertyValues("spring.couchbase.bootstrapHosts=localhost") + .run((context) -> { + assertThat(context).hasSingleBean(Cluster.class) + .hasSingleBean(ClusterInfo.class) + .hasSingleBean(CouchbaseEnvironment.class) + .hasSingleBean(Bucket.class); + }); } @Test public void customConfiguration() { - load(CustomConfiguration.class, "spring.couchbase.bootstrapHosts=localhost"); - assertThat(this.context.getBeansOfType(Cluster.class)).hasSize(2); - assertThat(this.context.getBeansOfType(ClusterInfo.class)).hasSize(1); - assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); - assertThat(this.context.getBeansOfType(Bucket.class)).hasSize(2); + this.contextRunner + .withUserConfiguration(CustomConfiguration.class) + .withPropertyValues("spring.couchbase.bootstrapHosts=localhost") + .run((context) -> { + assertThat(context.getBeansOfType(Cluster.class)).hasSize(2); + assertThat(context.getBeansOfType(ClusterInfo.class)).hasSize(1); + assertThat(context.getBeansOfType(CouchbaseEnvironment.class)).hasSize(1); + assertThat(context.getBeansOfType(Bucket.class)).hasSize(2); + }); } @Configuration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java index f1b3d57bc6..5926211a81 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.couchbase; +import java.util.function.Consumer; + import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.CouchbaseBucket; @@ -24,8 +26,12 @@ import com.couchbase.client.java.env.CouchbaseEnvironment; import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration.CouchbaseConfiguration; import org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -38,102 +44,122 @@ import static org.mockito.Mockito.mock; * @author EddĂș MelĂ©ndez * @author Stephane Nicoll */ -public class CouchbaseAutoConfigurationTests - extends AbstractCouchbaseAutoConfigurationTests { +public class CouchbaseAutoConfigurationTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class, + CouchbaseAutoConfiguration.class)); @Test public void bootstrapHostsIsRequired() { - load(null); - assertNoCouchbaseBeans(); + this.contextRunner.run((context) -> { + assertNoCouchbaseBeans(context); + }); } @Test public void bootstrapHostsNotRequiredIfCouchbaseConfigurerIsSet() { - load(CouchbaseTestConfigurer.class); - assertThat(this.context.getBeansOfType(CouchbaseTestConfigurer.class)).hasSize(1); - // No beans are going to be created - assertNoCouchbaseBeans(); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .run((context) -> { + assertThat(context).hasSingleBean(CouchbaseTestConfigurer.class); + // No beans are going to be created + assertNoCouchbaseBeans(context); + }); } @Test public void bootstrapHostsIgnoredIfCouchbaseConfigurerIsSet() { - load(CouchbaseTestConfigurer.class, "spring.couchbase.bootstrapHosts=localhost"); - assertThat(this.context.getBeansOfType(CouchbaseTestConfigurer.class)).hasSize(1); - assertNoCouchbaseBeans(); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .withPropertyValues("spring.couchbase.bootstrapHosts=localhost") + .run((context) -> { + assertThat(context).hasSingleBean(CouchbaseTestConfigurer.class); + assertNoCouchbaseBeans(context); + }); } - private void assertNoCouchbaseBeans() { + private void assertNoCouchbaseBeans(AssertableApplicationContext context) { // No beans are going to be created - assertThat(this.context.getBeansOfType(CouchbaseEnvironment.class)).isEmpty(); - assertThat(this.context.getBeansOfType(ClusterInfo.class)).isEmpty(); - assertThat(this.context.getBeansOfType(Cluster.class)).isEmpty(); - assertThat(this.context.getBeansOfType(Bucket.class)).isEmpty(); + assertThat(context).doesNotHaveBean(CouchbaseEnvironment.class) + .doesNotHaveBean(ClusterInfo.class) + .doesNotHaveBean(Cluster.class) + .doesNotHaveBean(Bucket.class); } @Test - public void customizeEnvEndpoints() throws Exception { - DefaultCouchbaseEnvironment env = customizeEnv( + public void customizeEnvEndpoints() { + testCouchbaseEnv(env -> { + assertThat(env.kvEndpoints()).isEqualTo(4); + assertThat(env.queryEndpoints()).isEqualTo(5); + assertThat(env.viewEndpoints()).isEqualTo(6); + }, "spring.couchbase.env.endpoints.keyValue=4", "spring.couchbase.env.endpoints.query=5", "spring.couchbase.env.endpoints.view=6"); - assertThat(env.kvEndpoints()).isEqualTo(4); - assertThat(env.queryEndpoints()).isEqualTo(5); - assertThat(env.viewEndpoints()).isEqualTo(6); } @Test - public void customizeEnvTimeouts() throws Exception { - DefaultCouchbaseEnvironment env = customizeEnv( + public void customizeEnvTimeouts() { + testCouchbaseEnv(env -> { + assertThat(env.connectTimeout()).isEqualTo(100); + assertThat(env.kvTimeout()).isEqualTo(200); + assertThat(env.queryTimeout()).isEqualTo(300); + assertThat(env.socketConnectTimeout()).isEqualTo(400); + assertThat(env.viewTimeout()).isEqualTo(500); + }, "spring.couchbase.env.timeouts.connect=100", "spring.couchbase.env.timeouts.keyValue=200", "spring.couchbase.env.timeouts.query=300", "spring.couchbase.env.timeouts.socket-connect=400", "spring.couchbase.env.timeouts.view=500"); - assertThat(env.connectTimeout()).isEqualTo(100); - assertThat(env.kvTimeout()).isEqualTo(200); - assertThat(env.queryTimeout()).isEqualTo(300); - assertThat(env.socketConnectTimeout()).isEqualTo(400); - assertThat(env.viewTimeout()).isEqualTo(500); } @Test - public void enableSslNoEnabledFlag() throws Exception { - DefaultCouchbaseEnvironment env = customizeEnv( + public void enableSslNoEnabledFlag() { + testCouchbaseEnv(env -> { + assertThat(env.sslEnabled()).isTrue(); + assertThat(env.sslKeystoreFile()).isEqualTo("foo"); + assertThat(env.sslKeystorePassword()).isEqualTo("secret"); + }, "spring.couchbase.env.ssl.keyStore=foo", "spring.couchbase.env.ssl.keyStorePassword=secret"); - assertThat(env.sslEnabled()).isTrue(); - assertThat(env.sslKeystoreFile()).isEqualTo("foo"); - assertThat(env.sslKeystorePassword()).isEqualTo("secret"); } @Test - public void disableSslEvenWithKeyStore() throws Exception { - DefaultCouchbaseEnvironment env = customizeEnv( + public void disableSslEvenWithKeyStore() { + testCouchbaseEnv(env -> { + assertThat(env.sslEnabled()).isFalse(); + assertThat(env.sslKeystoreFile()).isNull(); + assertThat(env.sslKeystorePassword()).isNull(); + }, "spring.couchbase.env.ssl.enabled=false", "spring.couchbase.env.ssl.keyStore=foo", "spring.couchbase.env.ssl.keyStorePassword=secret"); - assertThat(env.sslEnabled()).isFalse(); - assertThat(env.sslKeystoreFile()).isNull(); - assertThat(env.sslKeystorePassword()).isNull(); } - @Test - public void customizeEnvWithCustomCouchbaseConfiguration() { - load(CustomCouchbaseConfiguration.class, - "spring.couchbase.bootstrap-hosts=localhost", - "spring.couchbase.env.timeouts.connect=100"); - assertThat(this.context.getBeansOfType(CouchbaseConfiguration.class)).hasSize(1); - DefaultCouchbaseEnvironment env = this.context - .getBean(DefaultCouchbaseEnvironment.class); - assertThat(env.socketConnectTimeout()).isEqualTo(5000); - assertThat(env.connectTimeout()).isEqualTo(2000); + private void testCouchbaseEnv(Consumer environmentConsumer, + String... environment) { + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .withPropertyValues(environment) + .run((context) -> { + CouchbaseProperties properties = context.getBean(CouchbaseProperties.class); + DefaultCouchbaseEnvironment env = new CouchbaseConfiguration(properties) + .couchbaseEnvironment(); + environmentConsumer.accept(env); + }); } - private DefaultCouchbaseEnvironment customizeEnv(String... environment) - throws Exception { - load(CouchbaseTestConfigurer.class, environment); - CouchbaseProperties properties = this.context.getBean(CouchbaseProperties.class); - return new CouchbaseConfiguration(properties).couchbaseEnvironment(); + @Test + public void customizeEnvWithCustomCouchbaseConfiguration() { + this.contextRunner.withUserConfiguration(CustomCouchbaseConfiguration.class) + .withPropertyValues( + "spring.couchbase.bootstrap-hosts=localhost", + "spring.couchbase.env.timeouts.connect=100") + .run((context) -> { + assertThat(context).hasSingleBean(CouchbaseConfiguration.class); + DefaultCouchbaseEnvironment env = context.getBean(DefaultCouchbaseEnvironment.class); + assertThat(env.socketConnectTimeout()).isEqualTo(5000); + assertThat(env.connectTimeout()).isEqualTo(2000); + }); } @Configuration