Use random ports for tests

Update remaining tests to use random ports.

Fixes gh-337
This commit is contained in:
Phillip Webb
2014-04-23 19:10:25 +01:00
parent af33cc2b97
commit 4119ef5cf4
10 changed files with 122 additions and 53 deletions

View File

@@ -39,6 +39,7 @@ import org.springframework.boot.cli.command.jar.JarCommand;
import org.springframework.boot.cli.command.run.RunCommand;
import org.springframework.boot.cli.command.test.TestCommand;
import org.springframework.boot.cli.util.OutputCapture;
import org.springframework.util.SocketUtils;
/**
* {@link TestRule} that can be used to invoke CLI commands.
@@ -57,6 +58,8 @@ public class CliTester implements TestRule {
private final String prefix;
private final int port = SocketUtils.findAvailableTcpPort();
public CliTester(String prefix) {
this.prefix = prefix;
}
@@ -96,11 +99,13 @@ public class CliTester implements TestRule {
@Override
public T call() throws Exception {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
System.setProperty("server.port", String.valueOf(CliTester.this.port));
try {
command.run(sources);
return command;
}
finally {
System.clearProperty("server.port");
Thread.currentThread().setContextClassLoader(loader);
}
}
@@ -148,12 +153,13 @@ public class CliTester implements TestRule {
}
public String getHttpOutput() {
return getHttpOutput("http://localhost:8080");
return getHttpOutput("/");
}
public String getHttpOutput(String uri) {
try {
InputStream stream = URI.create(uri).toURL().openStream();
InputStream stream = URI.create("http://localhost:" + this.port + uri)
.toURL().openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
StringBuilder result = new StringBuilder();

View File

@@ -99,7 +99,7 @@ public class SampleIntegrationTests {
this.cli.run("ui.groovy", "--classpath=.:src/test/resources");
String result = this.cli.getHttpOutput();
assertTrue("Wrong output: " + result, result.contains("Hello World"));
result = this.cli.getHttpOutput("http://localhost:8080/css/bootstrap.min.css");
result = this.cli.getHttpOutput("/css/bootstrap.min.css");
assertTrue("Wrong output: " + result, result.contains("container"));
}