diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java new file mode 100644 index 0000000000..5b9a0a5321 --- /dev/null +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -0,0 +1,104 @@ +/* + * Copyright 2012-2013 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.boot.cli; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.springframework.boot.OutputCapture; +import org.springframework.boot.cli.command.CleanCommand; +import org.springframework.boot.cli.command.RunCommand; + +/** + * {@link TestRule} that can be used to invoke CLI commands. + * + * @author Phillip Webb + */ +public class CliTester implements TestRule { + + private OutputCapture outputCapture = new OutputCapture(); + + private long timeout = TimeUnit.MINUTES.toMillis(6); + + private List commands = new ArrayList(); + + public void setTimeout(long timeout) { + this.timeout = timeout; + } + + public String run(final String... args) throws Exception { + Future future = Executors.newSingleThreadExecutor().submit( + new Callable() { + @Override + public RunCommand call() throws Exception { + RunCommand command = new RunCommand(); + command.run(args); + return command; + } + }); + this.commands.add(future.get(this.timeout, TimeUnit.MILLISECONDS)); + return getOutput(); + } + + public String getOutput() { + return this.outputCapture.toString(); + } + + @Override + public Statement apply(final Statement base, Description description) { + return this.outputCapture.apply(new RunLauncherStatement(base), description); + } + + private final class RunLauncherStatement extends Statement { + + private final Statement base; + + private RunLauncherStatement(Statement base) { + this.base = base; + } + + @Override + public void evaluate() throws Throwable { + System.setProperty("disableSpringSnapshotRepos", "false"); + try { + new CleanCommand().run("org.springframework"); + try { + this.base.evaluate(); + } + finally { + for (RunCommand command : CliTester.this.commands) { + if (command != null) { + command.stop(); + } + } + System.clearProperty("disableSpringSnapshotRepos"); + } + } + catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + } + +} diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java index a16b80638a..58c16d9634 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java @@ -17,20 +17,11 @@ package org.springframework.boot.cli; import java.io.File; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import org.junit.After; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import org.springframework.boot.OutputCapture; -import org.springframework.boot.cli.command.CleanCommand; -import org.springframework.boot.cli.command.RunCommand; import org.springframework.boot.cli.util.FileUtils; import org.springframework.boot.cli.util.IoUtils; @@ -43,6 +34,7 @@ import static org.junit.Assert.assertTrue; * @author Dave Syer * @author Greg Turnquist * @author Roy Clarkson + * @author Phillip Webb */ public class SampleIntegrationTests { @@ -52,59 +44,23 @@ public class SampleIntegrationTests { } @Rule - public OutputCapture outputCapture = new OutputCapture(); - - private RunCommand command; - - private void start(final String... sample) throws Exception { - Future future = Executors.newSingleThreadExecutor().submit( - new Callable() { - @Override - public RunCommand call() throws Exception { - RunCommand command = new RunCommand(); - command.run(sample); - return command; - } - }); - this.command = future.get(6, TimeUnit.MINUTES); - } - - @Before - public void setup() throws Exception { - System.setProperty("disableSpringSnapshotRepos", "false"); - new CleanCommand().run("org.springframework"); - } - - @After - public void teardown() { - System.clearProperty("disableSpringSnapshotRepos"); - } - - @After - public void stop() { - if (this.command != null) { - this.command.stop(); - } - } + public CliTester cli = new CliTester(); @Test public void appSample() throws Exception { - start("samples/app.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/app.groovy"); assertTrue("Wrong output: " + output, output.contains("Hello World")); } @Test public void templateSample() throws Exception { - start("samples/template.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/template.groovy"); assertTrue("Wrong output: " + output, output.contains("Hello World!")); } @Test public void jobSample() throws Exception { - start("samples/job.groovy", "foo=bar"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/job.groovy", "foo=bar"); System.out.println(output); assertTrue("Wrong output: " + output, output.contains("completed with the following parameters")); @@ -112,20 +68,19 @@ public class SampleIntegrationTests { @Test public void reactorSample() throws Exception { - start("samples/reactor.groovy", "Phil"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/reactor.groovy", "Phil"); int count = 0; while (!output.contains("Hello Phil") && count++ < 5) { Thread.sleep(200); - output = this.outputCapture.getOutputAndRelease(); + output = this.cli.getOutput(); } assertTrue("Wrong output: " + output, output.contains("Hello Phil")); } @Test public void jobWebSample() throws Exception { - start("samples/job.groovy", "samples/web.groovy", "foo=bar"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/job.groovy", "samples/web.groovy", + "foo=bar"); assertTrue("Wrong output: " + output, output.contains("completed with the following parameters")); String result = IoUtils.readEntirely("http://localhost:8080"); @@ -134,14 +89,14 @@ public class SampleIntegrationTests { @Test public void webSample() throws Exception { - start("samples/web.groovy"); + this.cli.run("samples/web.groovy"); String result = IoUtils.readEntirely("http://localhost:8080"); assertEquals("World!", result); } @Test public void uiSample() throws Exception { - start("samples/ui.groovy", "--classpath=.:src/test/resources"); + this.cli.run("samples/ui.groovy", "--classpath=.:src/test/resources"); String result = IoUtils.readEntirely("http://localhost:8080"); assertTrue("Wrong output: " + result, result.contains("Hello World")); result = IoUtils.readEntirely("http://localhost:8080/css/bootstrap.min.css"); @@ -150,15 +105,14 @@ public class SampleIntegrationTests { @Test public void actuatorSample() throws Exception { - start("samples/actuator.groovy"); + this.cli.run("samples/actuator.groovy"); String result = IoUtils.readEntirely("http://localhost:8080"); assertEquals("{\"message\":\"Hello World!\"}", result); } @Test public void httpSample() throws Exception { - start("samples/http.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/http.groovy"); assertTrue("Wrong output: " + output, output.contains("Hello World")); } @@ -167,29 +121,25 @@ public class SampleIntegrationTests { // Depends on 1.0.0.M1 of spring-integration-dsl-groovy-core System.clearProperty("disableSpringSnapshotRepos"); - start("samples/integration.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/integration.groovy"); assertTrue("Wrong output: " + output, output.contains("Hello, World")); } @Test public void xmlSample() throws Exception { - start("samples/runner.xml", "samples/runner.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/runner.xml", "samples/runner.groovy"); assertTrue("Wrong output: " + output, output.contains("Hello World")); } @Test public void txSample() throws Exception { - start("samples/tx.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/tx.groovy"); assertTrue("Wrong output: " + output, output.contains("Foo count=")); } @Test public void jmsSample() throws Exception { - start("samples/jms.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/jms.groovy"); assertTrue("Wrong output: " + output, output.contains("Received Greetings from Spring Boot via ActiveMQ")); FileUtils.recursiveDelete(new File("activemq-data")); // cleanup ActiveMQ cruft @@ -199,15 +149,14 @@ public class SampleIntegrationTests { @Ignore // this test requires RabbitMQ to be run, so disable it be default public void rabbitSample() throws Exception { - start("samples/rabbit.groovy"); - String output = this.outputCapture.getOutputAndRelease(); + String output = this.cli.run("samples/rabbit.groovy"); assertTrue("Wrong output: " + output, output.contains("Received Greetings from Spring Boot via RabbitMQ")); } @Test public void deviceSample() throws Exception { - start("samples/device.groovy"); + this.cli.run("samples/device.groovy"); String result = IoUtils.readEntirely("http://localhost:8080"); assertEquals("Hello Normal Device!", result); } diff --git a/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java b/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java index 3eb01cd2bc..72b3bc6108 100644 --- a/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java +++ b/spring-boot/src/test/java/org/springframework/boot/OutputCapture.java @@ -72,15 +72,6 @@ public class OutputCapture implements TestRule { this.copy = null; } - public String getOutputAndRelease() { - try { - return toString(); - } - finally { - releaseOutput(); - } - } - @Override public String toString() { return this.copy.toString();