Skip to content

Commit

Permalink
Merge branch '1.5.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Dec 2, 2016
2 parents 39c0f22 + b546fd1 commit 3c873b0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
*/
public interface ApplicationLauncher {

LaunchedApplication launchApplication(JavaLauncher javaLauncher) throws Exception;
LaunchedApplication launchApplication(JvmLauncher javaLauncher) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class DevToolsIntegrationTests {
private final ApplicationLauncher applicationLauncher;

@Rule
public JavaLauncher javaLauncher = new JavaLauncher();
public JvmLauncher javaLauncher = new JvmLauncher();

@Parameters(name = "{0}")
public static Object[] parameters() {
Expand Down Expand Up @@ -106,7 +106,6 @@ public void removeARequestMappingFromAnExistingController() throws Exception {
controller("com.example.ControllerOne").build();
assertThat(template.getForEntity("http://localhost:" + awaitServerPort() + "/one",
String.class).getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);

}

@Test
Expand Down Expand Up @@ -138,11 +137,14 @@ public void deleteAController() throws Exception {
}

private int awaitServerPort() throws Exception {
long end = System.currentTimeMillis() + 20000;
long end = System.currentTimeMillis() + 30000;
while (this.serverPortFile.length() == 0) {
if (System.currentTimeMillis() > end) {
throw new IllegalStateException(
"server.port file was not written within 20 seconds");
throw new IllegalStateException(String.format(
"server.port file was not written within 30 seconds. "
+ "Application output:%n%s",
FileCopyUtils.copyToString(new FileReader(
this.launchedApplication.getStandardOut()))));
}
Thread.sleep(100);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import org.junit.runners.model.Statement;

/**
* @author awilkinson
* JUnit {@link TestRule} that launched a JVM and redirects its output to a test
* method-specific location.
*
* @author Andy Wilkinson
*/
public class JavaLauncher implements TestRule {
class JvmLauncher implements TestRule {

private File outputDirectory;

Expand All @@ -41,13 +44,36 @@ public Statement apply(Statement base, Description description) {
return base;
}

Process launch(String name, String classpath, String... args) throws IOException {
LaunchedJvm launch(String name, String classpath, String... args) throws IOException {
List<String> command = new ArrayList<String>(Arrays
.asList(System.getProperty("java.home") + "/bin/java", "-cp", classpath));
command.addAll(Arrays.asList(args));
return new ProcessBuilder(command.toArray(new String[command.size()]))
File standardOut = new File(this.outputDirectory, name + ".out");
Process process = new ProcessBuilder(command.toArray(new String[command.size()]))
.redirectError(new File(this.outputDirectory, name + ".err"))
.redirectOutput(new File(this.outputDirectory, name + ".out")).start();
.redirectOutput(standardOut).start();
return new LaunchedJvm(process, standardOut);
}

static class LaunchedJvm {

private final Process process;

private final File standardOut;

LaunchedJvm(Process process, File standardOut) {
this.process = process;
this.standardOut = standardOut;
}

Process getProcess() {
return this.process;
}

File getStandardOut() {
return this.standardOut;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class LaunchedApplication {

private final File classesDirectory;

private final File standardOut;

private final Process[] processes;

LaunchedApplication(File classesDirectory, Process... processes) {
LaunchedApplication(File classesDirectory, File standardOut, Process... processes) {
this.classesDirectory = classesDirectory;
this.standardOut = standardOut;
this.processes = processes;
}

Expand All @@ -40,6 +43,10 @@ void stop() {
}
}

File getStandardOut() {
return this.standardOut;
}

File getClassesDirectory() {
return this.classesDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.devtools.tests.JvmLauncher.LaunchedJvm;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.StringUtils;

Expand All @@ -31,11 +32,12 @@
public class LocalApplicationLauncher implements ApplicationLauncher {

@Override
public LaunchedApplication launchApplication(JavaLauncher javaLauncher)
public LaunchedApplication launchApplication(JvmLauncher jvmLauncher)
throws Exception {
Process process = javaLauncher.launch("local", createApplicationClassPath(),
LaunchedJvm jvm = jvmLauncher.launch("local", createApplicationClassPath(),
"com.example.DevToolsTestApplication", "--server.port=0");
return new LaunchedApplication(new File("target/app"), process);
return new LaunchedApplication(new File("target/app"), jvm.getStandardOut(),
jvm.getProcess());
}

protected String createApplicationClassPath() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;

import org.springframework.boot.devtools.RemoteSpringApplication;
import org.springframework.boot.devtools.tests.JvmLauncher.LaunchedJvm;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;
Expand All @@ -34,18 +35,19 @@
abstract class RemoteApplicationLauncher implements ApplicationLauncher {

@Override
public LaunchedApplication launchApplication(JavaLauncher javaLauncher)
public LaunchedApplication launchApplication(JvmLauncher javaLauncher)
throws Exception {
int port = SocketUtils.findAvailableTcpPort();
Process application = javaLauncher.launch("app", createApplicationClassPath(),
"com.example.DevToolsTestApplication", "--server.port=" + port,
"--spring.devtools.remote.secret=secret");
Process remoteSpringApplication = javaLauncher.launch("remote-spring-application",
createRemoteSpringApplicationClassPath(),
LaunchedJvm applicationJvm = javaLauncher.launch("app",
createApplicationClassPath(), "com.example.DevToolsTestApplication",
"--server.port=" + port, "--spring.devtools.remote.secret=secret");
LaunchedJvm remoteSpringApplicationJvm = javaLauncher.launch(
"remote-spring-application", createRemoteSpringApplicationClassPath(),
RemoteSpringApplication.class.getName(),
"--spring.devtools.remote.secret=secret", "http://localhost:" + port);
return new LaunchedApplication(new File("target/remote"), application,
remoteSpringApplication);
return new LaunchedApplication(new File("target/remote"),
applicationJvm.getStandardOut(), applicationJvm.getProcess(),
remoteSpringApplicationJvm.getProcess());
}

protected abstract String createApplicationClassPath() throws Exception;
Expand Down

0 comments on commit 3c873b0

Please sign in to comment.