Commit eff587d5 authored by Phillip Webb's avatar Phillip Webb

Create CliTester JUnit @Rule

Extract logic from SampleIntegrationTests into a reusable JUnit @Rule.
parent a9c9c383
/*
* 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<RunCommand> commands = new ArrayList<RunCommand>();
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public String run(final String... args) throws Exception {
Future<RunCommand> future = Executors.newSingleThreadExecutor().submit(
new Callable<RunCommand>() {
@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);
}
}
}
}
...@@ -17,20 +17,11 @@ ...@@ -17,20 +17,11 @@
package org.springframework.boot.cli; package org.springframework.boot.cli;
import java.io.File; 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.BeforeClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.FileUtils;
import org.springframework.boot.cli.util.IoUtils; import org.springframework.boot.cli.util.IoUtils;
...@@ -43,6 +34,7 @@ import static org.junit.Assert.assertTrue; ...@@ -43,6 +34,7 @@ import static org.junit.Assert.assertTrue;
* @author Dave Syer * @author Dave Syer
* @author Greg Turnquist * @author Greg Turnquist
* @author Roy Clarkson * @author Roy Clarkson
* @author Phillip Webb
*/ */
public class SampleIntegrationTests { public class SampleIntegrationTests {
...@@ -52,59 +44,23 @@ public class SampleIntegrationTests { ...@@ -52,59 +44,23 @@ public class SampleIntegrationTests {
} }
@Rule @Rule
public OutputCapture outputCapture = new OutputCapture(); public CliTester cli = new CliTester();
private RunCommand command;
private void start(final String... sample) throws Exception {
Future<RunCommand> future = Executors.newSingleThreadExecutor().submit(
new Callable<RunCommand>() {
@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();
}
}
@Test @Test
public void appSample() throws Exception { public void appSample() throws Exception {
start("samples/app.groovy"); String output = this.cli.run("samples/app.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World")); assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
@Test @Test
public void templateSample() throws Exception { public void templateSample() throws Exception {
start("samples/template.groovy"); String output = this.cli.run("samples/template.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World!")); assertTrue("Wrong output: " + output, output.contains("Hello World!"));
} }
@Test @Test
public void jobSample() throws Exception { public void jobSample() throws Exception {
start("samples/job.groovy", "foo=bar"); String output = this.cli.run("samples/job.groovy", "foo=bar");
String output = this.outputCapture.getOutputAndRelease();
System.out.println(output); System.out.println(output);
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters")); output.contains("completed with the following parameters"));
...@@ -112,20 +68,19 @@ public class SampleIntegrationTests { ...@@ -112,20 +68,19 @@ public class SampleIntegrationTests {
@Test @Test
public void reactorSample() throws Exception { public void reactorSample() throws Exception {
start("samples/reactor.groovy", "Phil"); String output = this.cli.run("samples/reactor.groovy", "Phil");
String output = this.outputCapture.getOutputAndRelease();
int count = 0; int count = 0;
while (!output.contains("Hello Phil") && count++ < 5) { while (!output.contains("Hello Phil") && count++ < 5) {
Thread.sleep(200); Thread.sleep(200);
output = this.outputCapture.getOutputAndRelease(); output = this.cli.getOutput();
} }
assertTrue("Wrong output: " + output, output.contains("Hello Phil")); assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
} }
@Test @Test
public void jobWebSample() throws Exception { public void jobWebSample() throws Exception {
start("samples/job.groovy", "samples/web.groovy", "foo=bar"); String output = this.cli.run("samples/job.groovy", "samples/web.groovy",
String output = this.outputCapture.getOutputAndRelease(); "foo=bar");
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters")); output.contains("completed with the following parameters"));
String result = IoUtils.readEntirely("http://localhost:8080"); String result = IoUtils.readEntirely("http://localhost:8080");
...@@ -134,14 +89,14 @@ public class SampleIntegrationTests { ...@@ -134,14 +89,14 @@ public class SampleIntegrationTests {
@Test @Test
public void webSample() throws Exception { public void webSample() throws Exception {
start("samples/web.groovy"); this.cli.run("samples/web.groovy");
String result = IoUtils.readEntirely("http://localhost:8080"); String result = IoUtils.readEntirely("http://localhost:8080");
assertEquals("World!", result); assertEquals("World!", result);
} }
@Test @Test
public void uiSample() throws Exception { 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"); String result = IoUtils.readEntirely("http://localhost:8080");
assertTrue("Wrong output: " + result, result.contains("Hello World")); assertTrue("Wrong output: " + result, result.contains("Hello World"));
result = IoUtils.readEntirely("http://localhost:8080/css/bootstrap.min.css"); result = IoUtils.readEntirely("http://localhost:8080/css/bootstrap.min.css");
...@@ -150,15 +105,14 @@ public class SampleIntegrationTests { ...@@ -150,15 +105,14 @@ public class SampleIntegrationTests {
@Test @Test
public void actuatorSample() throws Exception { public void actuatorSample() throws Exception {
start("samples/actuator.groovy"); this.cli.run("samples/actuator.groovy");
String result = IoUtils.readEntirely("http://localhost:8080"); String result = IoUtils.readEntirely("http://localhost:8080");
assertEquals("{\"message\":\"Hello World!\"}", result); assertEquals("{\"message\":\"Hello World!\"}", result);
} }
@Test @Test
public void httpSample() throws Exception { public void httpSample() throws Exception {
start("samples/http.groovy"); String output = this.cli.run("samples/http.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World")); assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
...@@ -167,29 +121,25 @@ public class SampleIntegrationTests { ...@@ -167,29 +121,25 @@ public class SampleIntegrationTests {
// Depends on 1.0.0.M1 of spring-integration-dsl-groovy-core // Depends on 1.0.0.M1 of spring-integration-dsl-groovy-core
System.clearProperty("disableSpringSnapshotRepos"); System.clearProperty("disableSpringSnapshotRepos");
start("samples/integration.groovy"); String output = this.cli.run("samples/integration.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello, World")); assertTrue("Wrong output: " + output, output.contains("Hello, World"));
} }
@Test @Test
public void xmlSample() throws Exception { public void xmlSample() throws Exception {
start("samples/runner.xml", "samples/runner.groovy"); String output = this.cli.run("samples/runner.xml", "samples/runner.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Hello World")); assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
@Test @Test
public void txSample() throws Exception { public void txSample() throws Exception {
start("samples/tx.groovy"); String output = this.cli.run("samples/tx.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, output.contains("Foo count=")); assertTrue("Wrong output: " + output, output.contains("Foo count="));
} }
@Test @Test
public void jmsSample() throws Exception { public void jmsSample() throws Exception {
start("samples/jms.groovy"); String output = this.cli.run("samples/jms.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("Received Greetings from Spring Boot via ActiveMQ")); output.contains("Received Greetings from Spring Boot via ActiveMQ"));
FileUtils.recursiveDelete(new File("activemq-data")); // cleanup ActiveMQ cruft FileUtils.recursiveDelete(new File("activemq-data")); // cleanup ActiveMQ cruft
...@@ -199,15 +149,14 @@ public class SampleIntegrationTests { ...@@ -199,15 +149,14 @@ public class SampleIntegrationTests {
@Ignore @Ignore
// this test requires RabbitMQ to be run, so disable it be default // this test requires RabbitMQ to be run, so disable it be default
public void rabbitSample() throws Exception { public void rabbitSample() throws Exception {
start("samples/rabbit.groovy"); String output = this.cli.run("samples/rabbit.groovy");
String output = this.outputCapture.getOutputAndRelease();
assertTrue("Wrong output: " + output, assertTrue("Wrong output: " + output,
output.contains("Received Greetings from Spring Boot via RabbitMQ")); output.contains("Received Greetings from Spring Boot via RabbitMQ"));
} }
@Test @Test
public void deviceSample() throws Exception { public void deviceSample() throws Exception {
start("samples/device.groovy"); this.cli.run("samples/device.groovy");
String result = IoUtils.readEntirely("http://localhost:8080"); String result = IoUtils.readEntirely("http://localhost:8080");
assertEquals("Hello Normal Device!", result); assertEquals("Hello Normal Device!", result);
} }
......
...@@ -72,15 +72,6 @@ public class OutputCapture implements TestRule { ...@@ -72,15 +72,6 @@ public class OutputCapture implements TestRule {
this.copy = null; this.copy = null;
} }
public String getOutputAndRelease() {
try {
return toString();
}
finally {
releaseOutput();
}
}
@Override @Override
public String toString() { public String toString() {
return this.copy.toString(); return this.copy.toString();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment