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 be9e4fbd..0820e8d6 100644 --- a/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java +++ b/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java @@ -1,9 +1,5 @@ package sample; -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -19,22 +15,27 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) -@IntegrationTest("server.port:0") +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +@RunWith ( SpringJUnit4ClassRunner.class ) +@SpringApplicationConfiguration ( classes = Application.class ) +@IntegrationTest ( "server.port:0" ) @WebAppConfiguration public class ApplicationTests { private static int configPort = 0; - @Value("${local.server.port}") + @Value ( "${local.server.port}" ) private int port; private static ConfigurableApplicationContext server; @BeforeClass public static void startConfigServer() throws IOException { - String repo = ConfigServerTestUtils.prepareLocalRepo(); + String baseDir = ConfigServerTestUtils.getBaseDirectory("spring-cloud-config-sample"); + String repo = ConfigServerTestUtils.prepareLocalRepo(baseDir, "target/repos", "config-repo", "target/config"); server = SpringApplication.run( org.springframework.cloud.config.server.ConfigServerApplication.class, "--server.port=" + configPort, "--spring.config.name=server", @@ -43,11 +44,11 @@ public class ApplicationTests { .getEmbeddedServletContainer().getPort(); System.setProperty("config.port", "" + configPort); } - + @AfterClass public static void close() { System.clearProperty("config.port"); - if (server!=null) { + if (server != null) { server.close(); } } @@ -55,7 +56,7 @@ public class ApplicationTests { @Test public void contextLoads() { String foo = new TestRestTemplate().getForObject("http://localhost:" + port - + "/env/info.foo", String.class); + + "/env/info.foo", String.class); assertEquals("bar", foo); } 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 421fac69..40dab447 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 @@ -15,28 +15,28 @@ */ package org.springframework.cloud.config.server; -import java.io.File; -import java.io.IOException; - import org.eclipse.jgit.util.FileUtils; import org.springframework.util.FileSystemUtils; +import java.io.File; +import java.io.IOException; + /** * @author Dave Syer - * */ public class ConfigServerTestUtils { public static String prepareLocalRepo() throws IOException { - return prepareLocalRepo("target/repos", "config-repo", "target/config"); + return prepareLocalRepo("./", "target/repos", "config-repo", "target/config"); } public static String prepareLocalRepo(String repoPath) throws IOException { - return prepareLocalRepo("target/repos", repoPath, "target/config"); + return prepareLocalRepo("./", "target/repos", repoPath, "target/config"); } - public static String prepareLocalRepo(String buildDir, String repoPath, - String checkoutDir) throws IOException { + public static String prepareLocalRepo(String baseDir, String buildDir, String repoPath, + String checkoutDir) throws IOException { + buildDir = baseDir + buildDir; new File(buildDir).mkdirs(); if (!repoPath.startsWith("/")) { repoPath = "/" + repoPath; @@ -44,7 +44,7 @@ public class ConfigServerTestUtils { if (!repoPath.endsWith("/")) { repoPath = repoPath + "/"; } - File source = new File("src/test/resources" + repoPath); + File source = new File(baseDir + "src/test/resources" + repoPath); FileSystemUtils.copyRecursively(source, new File(buildDir + repoPath)); File dotGit = new File(buildDir + repoPath + ".git"); File git = new File(buildDir + repoPath + "git"); @@ -64,6 +64,11 @@ public class ConfigServerTestUtils { return "file:" + buildDir + repoPath; } + public static String getBaseDirectory(String potentialRoot) { + return new File(potentialRoot).exists() ? potentialRoot + "/" : "./"; + } + + public static String copyLocalRepo(String path) throws IOException { File dest = new File("target/repos/" + path); FileSystemUtils.deleteRecursively(dest);