From cbbcab3e6227f4dae55544d49fac80a3d6c9a332 Mon Sep 17 00:00:00 2001 From: eidottermihi Date: Tue, 3 Feb 2015 18:29:39 +0100 Subject: [PATCH] Removed @Profile for JGitEnvironmentRepository (so it's the default if no active profile is set). More tests for Subversion Support (based on Git tests). --- .../server/ConfigServerConfiguration.java | 1 - .../config/server/ConfigServerTestUtils.java | 3 +- .../server/SVNKitEnvironmentRepository.java | 13 +- ...EnvironmentRepositoryIntegrationTests.java | 120 ++++++++++++++++++ ...ubversionConfigServerIntegrationTests.java | 44 +++++++ .../resources/configserver-subversion.yml | 10 ++ 6 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepositoryIntegrationTests.java create mode 100644 spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SubversionConfigServerIntegrationTests.java create mode 100644 spring-cloud-config-server/src/test/resources/configserver-subversion.yml diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java index 9ed29824..522ad1a2 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java @@ -42,7 +42,6 @@ public class ConfigServerConfiguration { } @Configuration - @Profile({ "!native", "!subversion" }) protected static class GitRepositoryConfiguration { @Autowired diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerTestUtils.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerTestUtils.java index cedbfab1..9b5e5228 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerTestUtils.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerTestUtils.java @@ -17,6 +17,7 @@ package org.springframework.cloud.config.server; import org.eclipse.jgit.util.FileUtils; import org.springframework.util.FileSystemUtils; +import org.springframework.util.StringUtils; import java.io.File; import java.io.IOException; @@ -73,7 +74,7 @@ public class ConfigServerTestUtils { } local.mkdirs(); FileSystemUtils.copyRecursively(sourceDirFile, local); - return "file:///" + local.getAbsolutePath(); + return StringUtils.cleanPath("file:///" + local.getAbsolutePath()); } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepository.java index f5e1dc69..65d1ca9d 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepository.java @@ -24,6 +24,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.config.Environment; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.internal.wc.DefaultSVNAuthenticationManager; @@ -91,7 +92,17 @@ public class SVNKitEnvironmentRepository extends AbstractSCMEnvironmentRepositor public void afterPropertiesSet() throws Exception { Assert .state(getUri() != null, - "You need to configure a uri for the subversion repository (e.g. 'http://example.com/svn/'"); + "You need to configure a uri for the subversion repository (e.g. 'http://example.com/svn/')"); + resolveRelativeFileUri(); + } + + private void resolveRelativeFileUri() { + if (getUri().startsWith("file:///./")) { + String path = getUri().substring(8); + String absolutePath = new File(path).getAbsolutePath(); + setUri("file:///" + StringUtils.cleanPath(absolutePath)); + } + } public SVNKitEnvironmentRepository(ConfigurableEnvironment environment) { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepositoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepositoryIntegrationTests.java new file mode 100644 index 00000000..01067f86 --- /dev/null +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SVNKitEnvironmentRepositoryIntegrationTests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2013-2014 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; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.config.Environment; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.util.FileSystemUtils; +import org.springframework.util.StreamUtils; +import org.tmatesoft.svn.core.SVNException; +import org.tmatesoft.svn.core.SVNURL; +import org.tmatesoft.svn.core.wc2.SvnCheckout; +import org.tmatesoft.svn.core.wc2.SvnCommit; +import org.tmatesoft.svn.core.wc2.SvnOperationFactory; +import org.tmatesoft.svn.core.wc2.SvnTarget; + +import static org.junit.Assert.assertEquals; + +/** + * @author Michael Prankl + * + */ +public class SVNKitEnvironmentRepositoryIntegrationTests { + + private ConfigurableApplicationContext context; + + private File workingDir; + + @Before + public void init() { + workingDir = new File("target/repos/svn-config-repo-update"); + if (workingDir.exists()) { + FileSystemUtils.deleteRecursively(workingDir); + } + } + + @After + public void close() { + if (context != null) { + context.close(); + } + } + + @Test + public void vanilla() throws Exception { + String uri = ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo", + "target/config"); + context = new SpringApplicationBuilder(TestConfiguration.class).web(false) + .profiles("subversion").run("--spring.cloud.config.server.svn.uri=" + uri); + EnvironmentRepository repository = context.getBean(EnvironmentRepository.class); + repository.findOne("bar", "staging", "trunk"); + Environment environment = repository.findOne("bar", "staging", "trunk"); + assertEquals(2, environment.getPropertySources().size()); + } + + @Test + public void update() throws Exception { + String uri = ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo", + "target/config"); + context = new SpringApplicationBuilder(TestConfiguration.class).web(false) + .profiles("subversion").run("--spring.cloud.config.server.svn.uri=" + uri); + EnvironmentRepository repository = context.getBean(EnvironmentRepository.class); + repository.findOne("bar", "staging", "trunk"); + Environment environment = repository.findOne("bar", "staging", "trunk"); + assertEquals("bar", environment.getPropertySources().get(0).getSource().get("foo")); + updateRepoForUpdate(uri); + environment = repository.findOne("bar", "staging", "trunk"); + assertEquals("foo", environment.getPropertySources().get(0).getSource().get("foo")); + } + + private void updateRepoForUpdate(String uri) throws SVNException, FileNotFoundException, + IOException { + SvnOperationFactory svnFactory = new SvnOperationFactory(); + final SvnCheckout checkout = svnFactory.createCheckout(); + checkout.setSource(SvnTarget.fromURL(SVNURL.parseURIEncoded(uri))); + checkout.setSingleTarget(SvnTarget.fromFile(workingDir)); + checkout.run(); + + // update bar.properties + File barProps = new File(workingDir, "trunk/bar.properties"); + StreamUtils.copy("foo: foo", Charset.defaultCharset(), new FileOutputStream(barProps)); + // commit to repo + SvnCommit svnCommit = svnFactory.createCommit(); + svnCommit.setCommitMessage("update bar.properties"); + svnCommit.setSingleTarget(SvnTarget.fromFile(barProps)); + svnCommit.run(); + } + + @Configuration + @Import({ PropertyPlaceholderAutoConfiguration.class, ConfigServerConfiguration.class }) + protected static class TestConfiguration { + } + +} 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 new file mode 100644 index 00000000..494d0ea8 --- /dev/null +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/SubversionConfigServerIntegrationTests.java @@ -0,0 +1,44 @@ +package org.springframework.cloud.config.server; + +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.cloud.config.Environment; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +@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 +@ActiveProfiles("subversion") +public class SubversionConfigServerIntegrationTests { + + @Value("${local.server.port}") + private int port; + + @BeforeClass + public static void init() throws Exception { + ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo", + "target/repos/svn-config-repo"); + } + + @Test + public void contextLoads() { + Environment environment = new TestRestTemplate().getForObject("http://localhost:" + + port + "/foo/development/", Environment.class); + assertFalse(environment.getPropertySources().isEmpty()); + assertEquals("overrides", environment.getPropertySources().get(0).getName()); + assertEquals("{spring.cloud.config.enabled=true}", environment + .getPropertySources().get(0).getSource().toString()); + } + +} diff --git a/spring-cloud-config-server/src/test/resources/configserver-subversion.yml b/spring-cloud-config-server/src/test/resources/configserver-subversion.yml new file mode 100644 index 00000000..babc3d9b --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/configserver-subversion.yml @@ -0,0 +1,10 @@ +spring: + cloud: + config: + server: + defaultLabel: trunk + overrides: + spring: + cloud: + config: + enabled: true \ No newline at end of file