diff --git a/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java b/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java index 0cddadf5..90cc3eec 100644 --- a/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java +++ b/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java @@ -1,35 +1,33 @@ package sample; -import static org.junit.Assert.assertEquals; - import java.io.IOException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) +import static org.junit.Assert.assertEquals; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, // Normally spring.cloud.config.enabled:true is the default but since we have the config // server on the classpath we need to set it explicitly -@IntegrationTest({ "server.port:0", "spring.cloud.config.enabled:true" }) -@WebAppConfiguration + properties = { "spring.cloud.config.enabled:true" }, webEnvironment = RANDOM_PORT) public class ApplicationTests { private static int configPort = 0; - @Value("${local.server.port}") + @LocalServerPort private int port; private static ConfigurableApplicationContext server; diff --git a/spring-cloud-config-sample/src/test/java/sample/ServerNativeApplicationTests.java b/spring-cloud-config-sample/src/test/java/sample/ServerNativeApplicationTests.java index 29707056..34fb5828 100644 --- a/spring-cloud-config-sample/src/test/java/sample/ServerNativeApplicationTests.java +++ b/spring-cloud-config-sample/src/test/java/sample/ServerNativeApplicationTests.java @@ -1,7 +1,5 @@ package sample; -import static org.junit.Assert.assertFalse; - import java.io.IOException; import org.junit.AfterClass; @@ -9,21 +7,21 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) -@IntegrationTest({"server.port:0", "spring.application.name:bad"}) -@WebAppConfiguration +import static org.junit.Assert.assertFalse; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, + properties = "spring.application.name:bad", webEnvironment = RANDOM_PORT) public class ServerNativeApplicationTests { private static int configPort = 0; @@ -31,7 +29,7 @@ public class ServerNativeApplicationTests { @Autowired private ConfigurableEnvironment environment; - @Value("${local.server.port}") + @LocalServerPort private int port; private static ConfigurableApplicationContext server; diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnableConfigServer.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnableConfigServer.java index 9443e6b9..b092791d 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnableConfigServer.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnableConfigServer.java @@ -15,27 +15,24 @@ */ package org.springframework.cloud.config.server; +import org.springframework.cloud.config.server.config.ConfigServerConfiguration; +import org.springframework.context.annotation.Import; + import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.springframework.cloud.config.server.config.ConfigServerEncryptionConfiguration; -import org.springframework.cloud.config.server.config.ConfigServerMvcConfiguration; -import org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration; -import org.springframework.cloud.config.server.config.ResourceRepositoryConfiguration; -import org.springframework.context.annotation.Import; - /** * @author Dave Syer + * @author * */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented -@Import({ ResourceRepositoryConfiguration.class, EnvironmentRepositoryConfiguration.class, - ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class }) +@Import(ConfigServerConfiguration.class) public @interface EnableConfigServer { } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapConfiguration.java index f2bcb9b5..326cb108 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/bootstrap/ConfigServerBootstrapConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.cloud.config.server.bootstrap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.cloud.config.server.config.ConfigServerProperties; import org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration; @@ -41,6 +42,7 @@ import org.springframework.util.StringUtils; public class ConfigServerBootstrapConfiguration { @ConditionalOnProperty("spring.cloud.config.server.bootstrap") + @EnableConfigurationProperties(ConfigServerProperties.class) @Import(EnvironmentRepositoryConfiguration.class) protected static class LocalPropertySourceLocatorConfiguration { diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerAutoConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerAutoConfiguration.java new file mode 100644 index 00000000..3917c2a4 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerAutoConfiguration.java @@ -0,0 +1,35 @@ +/* + * Copyright 2013-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.cloud.config.server.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * @author Spencer Gibb + */ +@Configuration +@ConditionalOnBean(ConfigServerConfiguration.Marker.class) +@EnableConfigurationProperties(ConfigServerProperties.class) +@Import({ EnvironmentRepositoryConfiguration.class, ResourceRepositoryConfiguration.class, + ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class }) +public class ConfigServerAutoConfiguration { + +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerConfiguration.java new file mode 100644 index 00000000..49465fcd --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerConfiguration.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-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.cloud.config.server.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Spencer Gibb + */ +@Configuration +public class ConfigServerConfiguration { + class Marker {} + + @Bean + public Marker enableConfigServerMarker() { + return new Marker(); + } +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerMvcConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerMvcConfiguration.java index 8f7743e8..61f762e3 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerMvcConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerMvcConfiguration.java @@ -40,12 +40,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; @ConditionalOnWebApplication public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter { - @Autowired - private EnvironmentRepository repository; - - @Autowired - private ConfigServerProperties server; - @Autowired(required = false) private EnvironmentEncryptor environmentEncryptor; @@ -60,24 +54,24 @@ public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter { } @Bean - public EnvironmentController environmentController() { - EnvironmentController controller = new EnvironmentController(encrypted(), this.objectMapper); - controller.setStripDocumentFromYaml(this.server.isStripDocumentFromYaml()); + public EnvironmentController environmentController(EnvironmentRepository envRepository, ConfigServerProperties server) { + EnvironmentController controller = new EnvironmentController(encrypted(envRepository, server), this.objectMapper); + controller.setStripDocumentFromYaml(server.isStripDocumentFromYaml()); return controller; } @Bean @ConditionalOnBean(ResourceRepository.class) - public ResourceController resourceController(ResourceRepository repository) { + public ResourceController resourceController(ResourceRepository repository, EnvironmentRepository envRepository, ConfigServerProperties server) { ResourceController controller = new ResourceController(repository, - encrypted()); + encrypted(envRepository, server)); return controller; } - private EnvironmentRepository encrypted() { + private EnvironmentRepository encrypted(EnvironmentRepository envRepository, ConfigServerProperties server) { EnvironmentEncryptorEnvironmentRepository encrypted = new EnvironmentEncryptorEnvironmentRepository( - this.repository, this.environmentEncryptor); - encrypted.setOverrides(this.server.getOverrides()); + envRepository, this.environmentEncryptor); + encrypted.setOverrides(server.getOverrides()); return encrypted; } } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java index 7590f1a8..af263c13 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java @@ -40,7 +40,6 @@ import org.springframework.web.client.RestTemplate; */ @Configuration @ConditionalOnMissingBean(EnvironmentRepository.class) -@EnableConfigurationProperties(ConfigServerProperties.class) public class EnvironmentRepositoryConfiguration { @Bean @@ -130,4 +129,4 @@ public class EnvironmentRepositoryConfiguration { return new EnvironmentWatch.Default(); } } -} \ No newline at end of file +} diff --git a/spring-cloud-config-server/src/main/resources/META-INF/spring.factories b/spring-cloud-config-server/src/main/resources/META-INF/spring.factories index 2306bfd0..94a0bb01 100644 --- a/spring-cloud-config-server/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-config-server/src/main/resources/META-INF/spring.factories @@ -8,5 +8,6 @@ org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapApplicati # Autoconfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration,\ org.springframework.cloud.config.server.config.EncryptionAutoConfiguration,\ org.springframework.cloud.config.server.config.SingleEncryptorAutoConfiguration diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/BootstrapConfigServerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/BootstrapConfigServerIntegrationTests.java index 542b83db..5a215621 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/BootstrapConfigServerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/BootstrapConfigServerIntegrationTests.java @@ -1,32 +1,29 @@ package org.springframework.cloud.config.server; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - import java.io.IOException; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.cloud.config.environment.Environment; -import org.springframework.cloud.config.server.ConfigServerApplication; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = ConfigServerApplication.class) -@IntegrationTest({ "server.port:0", "spring.cloud.bootstrap.name:enable-bootstrap" }) -@WebAppConfiguration +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigServerApplication.class, properties = "spring.cloud.bootstrap.name:enable-bootstrap", + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") public class BootstrapConfigServerIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @Value("${info.foo}") diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOffIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOffIntegrationTests.java index 1b37e217..e6c0b6c8 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOffIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOffIntegrationTests.java @@ -1,10 +1,5 @@ package org.springframework.cloud.config.server; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.BDDMockito.given; -import static org.mockito.Matchers.anyString; - import java.io.IOException; import org.junit.BeforeClass; @@ -13,10 +8,9 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.ConfigClientOffIntegrationTests.TestConfiguration; @@ -29,17 +23,20 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.ByteArrayResource; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = TestConfiguration.class) -@IntegrationTest("server.port:0") -@WebAppConfiguration +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.anyString; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TestConfiguration.class, webEnvironment = RANDOM_PORT) @ActiveProfiles("test") public class ConfigClientOffIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @Autowired diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOnIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOnIntegrationTests.java index 9275c7b3..480370af 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOnIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ConfigClientOnIntegrationTests.java @@ -1,10 +1,5 @@ package org.springframework.cloud.config.server; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.BDDMockito.given; -import static org.mockito.Matchers.anyString; - import java.io.IOException; import org.junit.BeforeClass; @@ -13,10 +8,10 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.ConfigClientOnIntegrationTests.TestConfiguration; @@ -29,17 +24,20 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.ByteArrayResource; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = TestConfiguration.class) -@IntegrationTest({ "server.port:0", "spring.cloud.config.enabled:true" }) -@WebAppConfiguration +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.anyString; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TestConfiguration.class, properties = "spring.cloud.config.enabled:true", + webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") public class ConfigClientOnIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @Autowired diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeConfigServerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeConfigServerIntegrationTests.java index 528bd943..a80d21bf 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeConfigServerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeConfigServerIntegrationTests.java @@ -1,35 +1,33 @@ package org.springframework.cloud.config.server; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - import java.io.IOException; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.environment.Environment; -import org.springframework.cloud.config.server.ConfigServerApplication; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = ConfigServerApplication.class) -@IntegrationTest({ "server.port:0", "spring.config.name:configserver" }) -@WebAppConfiguration +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigServerApplication.class, + properties = { "spring.config.name:configserver" }, + webEnvironment = RANDOM_PORT) @ActiveProfiles({ "test", "native" }) public class NativeConfigServerIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @BeforeClass diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SubversionConfigServerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SubversionConfigServerIntegrationTests.java index f35ba2b1..2e557aee 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SubversionConfigServerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SubversionConfigServerIntegrationTests.java @@ -21,36 +21,35 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.environment.SvnKitEnvironmentRepository; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; /** * @author Michael Prankl * @author Dave Syer * @author Roy Clarkson */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = ConfigServerApplication.class) -@IntegrationTest({ "server.port:0", "spring.config.name:configserver", - "spring.cloud.config.server.svn.uri:file:///./target/repos/svn-config-repo" }) -@WebAppConfiguration +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigServerApplication.class, + properties = { "spring.config.name:configserver", + "spring.cloud.config.server.svn.uri:file:///./target/repos/svn-config-repo" }, + webEnvironment = RANDOM_PORT) @ActiveProfiles("subversion") public class SubversionConfigServerIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @Autowired diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/VanillaConfigServerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/VanillaConfigServerIntegrationTests.java index 244996a3..b88d25dd 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/VanillaConfigServerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/VanillaConfigServerIntegrationTests.java @@ -1,32 +1,37 @@ package org.springframework.cloud.config.server; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - import java.io.IOException; +import java.util.Arrays; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.cloud.config.environment.Environment; -import org.springframework.cloud.config.server.ConfigServerApplication; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = ConfigServerApplication.class) -@IntegrationTest({"server.port:0", "spring.config.name:configserver", "spring.cloud.config.server.git.uri:file:./target/repos/config-repo"}) -@WebAppConfiguration +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigServerApplication.class, properties = { + "spring.config.name:configserver", "spring.cloud.config.server.git.uri:file:./target/repos/config-repo"}, + webEnvironment = RANDOM_PORT) @ActiveProfiles("test") public class VanillaConfigServerIntegrationTests { - @Value("${local.server.port}") + @LocalServerPort private int port; @BeforeClass @@ -44,4 +49,21 @@ public class VanillaConfigServerIntegrationTests { .getPropertySources().get(0).getSource().toString()); } + @Test + public void resourseEndpointsWork() { + String text = new TestRestTemplate().getForObject("http://localhost:" + + port + "/foo/development/master/bar.properties", String.class); + + String expected = "foo: bar"; + assertEquals("invalid content", expected, text); + + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM)); + ResponseEntity response = new TestRestTemplate().exchange("http://localhost:" + + port + "/foo/development/raw/bar.properties", HttpMethod.GET, new HttpEntity<>(headers), byte[].class); + //FIXME: this is calling the text endpoint, not the binary one + // assertTrue("invalid content type", response.getHeaders().getContentType().isCompatibleWith(MediaType.APPLICATION_OCTET_STREAM)); + assertEquals(expected.length(), response.getBody().length); + } + } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/CustomEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/CustomEnvironmentRepositoryTests.java index a5b5b89f..2e1b8d04 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/CustomEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/CustomEnvironmentRepositoryTests.java @@ -41,7 +41,7 @@ import static org.junit.Assert.assertFalse; * */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = TestApplication.class, properties = { "server.port:0", +@SpringBootTest(classes = TestApplication.class, properties = { "spring.config.name:configserver" }, webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") @DirtiesContext diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java index 949867ae..6a9e373a 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerIntegrationTests.java @@ -29,7 +29,7 @@ import org.springframework.cloud.config.server.environment.EnvironmentController import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -42,7 +42,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; * @author Roy Clarkson * @author Ivan Corrales Solera */ -@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) @SpringBootTest(classes = ControllerConfiguration.class) public class EnvironmentControllerIntegrationTests { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryConcurrencyTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryConcurrencyTests.java index 3e0773be..42d84a85 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryConcurrencyTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryConcurrencyTests.java @@ -32,9 +32,10 @@ import org.junit.Before; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.server.config.ConfigServerProperties; import org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration; -import org.springframework.cloud.config.server.environment.EnvironmentRepository; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; @@ -93,6 +94,7 @@ public class JGitEnvironmentRepositoryConcurrencyTests { } @Configuration + @EnableConfigurationProperties(ConfigServerProperties.class) @Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class }) protected static class TestConfiguration { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java index ec6fac2a..233457c2 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepositoryIntegrationTests.java @@ -48,7 +48,9 @@ import org.junit.Before; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.server.config.ConfigServerProperties; import org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; @@ -413,6 +415,7 @@ public class JGitEnvironmentRepositoryIntegrationTests { } @Configuration + @EnableConfigurationProperties(ConfigServerProperties.class) @Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class }) protected static class TestConfiguration { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/MultipleJGitEnvironmentRepositoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/MultipleJGitEnvironmentRepositoryIntegrationTests.java index 4c5cecd8..3e20e33f 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/MultipleJGitEnvironmentRepositoryIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/MultipleJGitEnvironmentRepositoryIntegrationTests.java @@ -29,9 +29,10 @@ import org.junit.Before; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.server.config.ConfigServerProperties; import org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration; -import org.springframework.cloud.config.server.environment.EnvironmentRepository; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; @@ -163,6 +164,7 @@ public class MultipleJGitEnvironmentRepositoryIntegrationTests { } @Configuration + @EnableConfigurationProperties(ConfigServerProperties.class) @Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class }) protected static class TestConfiguration { } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/SVNKitEnvironmentRepositoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/SVNKitEnvironmentRepositoryIntegrationTests.java index 44807ce4..e148d6f2 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/SVNKitEnvironmentRepositoryIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/SVNKitEnvironmentRepositoryIntegrationTests.java @@ -30,7 +30,9 @@ import org.junit.Before; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.server.config.ConfigServerProperties; import org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration; import org.springframework.cloud.config.server.test.ConfigServerTestUtils; import org.springframework.context.ConfigurableApplicationContext; @@ -159,6 +161,7 @@ public class SVNKitEnvironmentRepositoryIntegrationTests { } @Configuration + @EnableConfigurationProperties(ConfigServerProperties.class) @Import({ PropertyPlaceholderAutoConfiguration.class, EnvironmentRepositoryConfiguration.class }) protected static class TestConfiguration { } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java index 6a69b5ee..73b339a4 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java @@ -21,7 +21,7 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.environment.EnvironmentRepository; import org.springframework.cloud.config.server.resource.ResourceControllerIntegrationTests.ControllerConfiguration; @@ -29,8 +29,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.ByteArrayResource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -42,9 +41,8 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; * @author Dave Syer * */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = ControllerConfiguration.class) -@WebAppConfiguration +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ControllerConfiguration.class) public class ResourceControllerIntegrationTests { @Autowired