Add useful features to CliTester

This commit is contained in:
Dave Syer 2013-11-06 09:42:16 +00:00
parent 9f13d291ec
commit dac470a8fc
5 changed files with 67 additions and 42 deletions

View File

@ -30,12 +30,12 @@ import static org.junit.Assert.assertThat;
public class ClassLoaderIntegrationTests {
@Rule
public CliTester cli = new CliTester();
public CliTester cli = new CliTester("src/test/resources/");
@Test
public void runWithIsolatedClassLoader() throws Exception {
// CLI classes or dependencies should not be exposed to the app
String output = this.cli.run("src/test/resources/classloader-test-app.groovy",
String output = this.cli.run("classloader-test-app.groovy",
SpringCli.class.getName());
assertThat(output, containsString("HasClasses-false-true-false"));
}

View File

@ -27,6 +27,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Assume;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
@ -40,6 +41,7 @@ import org.springframework.boot.cli.command.TestCommand;
* {@link TestRule} that can be used to invoke CLI commands.
*
* @author Phillip Webb
* @author Dave Syer
*/
public class CliTester implements TestRule {
@ -49,17 +51,24 @@ public class CliTester implements TestRule {
private List<AbstractCommand> commands = new ArrayList<AbstractCommand>();
private String prefix;
public CliTester(String prefix) {
this.prefix = prefix;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public String run(final String... args) throws Exception {
public String run(String... args) throws Exception {
final String[] sources = getSources(args);
Future<RunCommand> future = Executors.newSingleThreadExecutor().submit(
new Callable<RunCommand>() {
@Override
public RunCommand call() throws Exception {
RunCommand command = new RunCommand();
command.run(args);
command.run(sources);
return command;
}
});
@ -67,13 +76,14 @@ public class CliTester implements TestRule {
return getOutput();
}
public String test(final String... args) throws Exception {
public String test(String... args) throws Exception {
final String[] sources = getSources(args);
Future<TestCommand> future = Executors.newSingleThreadExecutor().submit(
new Callable<TestCommand>() {
@Override
public TestCommand call() throws Exception {
TestCommand command = new TestCommand();
command.run(args);
command.run(sources);
return command;
}
});
@ -81,13 +91,33 @@ public class CliTester implements TestRule {
return getOutput();
}
protected String[] getSources(String... args) {
final String[] sources = new String[args.length];
for (int i = 0; i < args.length; i++) {
String arg = args[i];
sources[i] = this.prefix + arg;
}
return sources;
}
public String getOutput() {
return this.outputCapture.toString();
}
@Override
public Statement apply(final Statement base, Description description) {
return this.outputCapture.apply(new RunLauncherStatement(base), description);
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Assume.assumeTrue(
"Not running sample integration tests because integration profile not active",
System.getProperty("spring.profiles.active", "integration")
.contains("integration"));
CliTester.this.outputCapture.apply(new RunLauncherStatement(base),
description);
}
};
}
public String getHttpOutput() {

View File

@ -23,20 +23,18 @@ import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
/**
* Integration tests to exercise reproduce raised issues.
* Integration tests to exercise and reproduce specific issues.
*
* @author Phillip Webb
*/
public class ReproIntegrationTests {
private static final String SRC = "src/test/resources/repro-samples";
@Rule
public CliTester cli = new CliTester();
public CliTester cli = new CliTester("src/test/resources/repro-samples/");
@Test
public void grabAntBuilder() throws Exception {
this.cli.run(SRC + "/grab-ant-builder.groovy");
this.cli.run("grab-ant-builder.groovy");
assertThat(this.cli.getHttpOutput(),
containsString("{\"message\":\"Hello World\"}"));
}

View File

@ -38,7 +38,7 @@ import static org.junit.Assert.assertTrue;
public class SampleIntegrationTests {
@Rule
public CliTester cli = new CliTester();
public CliTester cli = new CliTester("samples/");
@BeforeClass
public static void cleanGrapes() throws Exception {
@ -47,26 +47,26 @@ public class SampleIntegrationTests {
@Test
public void appSample() throws Exception {
String output = this.cli.run("samples/app.groovy");
String output = this.cli.run("app.groovy");
assertTrue("Wrong output: " + output, output.contains("Hello World"));
}
@Test
public void templateSample() throws Exception {
String output = this.cli.run("samples/template.groovy");
String output = this.cli.run("template.groovy");
assertTrue("Wrong output: " + output, output.contains("Hello World!"));
}
@Test
public void jobSample() throws Exception {
String output = this.cli.run("samples/job.groovy", "foo=bar");
String output = this.cli.run("job.groovy", "foo=bar");
assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters"));
}
@Test
public void reactorSample() throws Exception {
String output = this.cli.run("samples/reactor.groovy", "Phil");
String output = this.cli.run("reactor.groovy", "Phil");
int count = 0;
while (!output.contains("Hello Phil") && count++ < 5) {
Thread.sleep(200);
@ -77,8 +77,7 @@ public class SampleIntegrationTests {
@Test
public void jobWebSample() throws Exception {
String output = this.cli.run("samples/job.groovy", "samples/web.groovy",
"foo=bar");
String output = this.cli.run("job.groovy", "web.groovy", "foo=bar");
assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters"));
String result = this.cli.getHttpOutput();
@ -87,13 +86,13 @@ public class SampleIntegrationTests {
@Test
public void webSample() throws Exception {
this.cli.run("samples/web.groovy");
this.cli.run("web.groovy");
assertEquals("World!", this.cli.getHttpOutput());
}
@Test
public void uiSample() throws Exception {
this.cli.run("samples/ui.groovy", "--classpath=.:src/test/resources");
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");
@ -102,37 +101,37 @@ public class SampleIntegrationTests {
@Test
public void actuatorSample() throws Exception {
this.cli.run("samples/actuator.groovy");
this.cli.run("actuator.groovy");
assertEquals("{\"message\":\"Hello World!\"}", this.cli.getHttpOutput());
}
@Test
public void httpSample() throws Exception {
String output = this.cli.run("samples/http.groovy");
String output = this.cli.run("http.groovy");
assertTrue("Wrong output: " + output, output.contains("Hello World"));
}
@Test
public void integrationSample() throws Exception {
String output = this.cli.run("samples/integration.groovy");
String output = this.cli.run("integration.groovy");
assertTrue("Wrong output: " + output, output.contains("Hello, World"));
}
@Test
public void xmlSample() throws Exception {
String output = this.cli.run("samples/runner.xml", "samples/runner.groovy");
String output = this.cli.run("runner.xml", "runner.groovy");
assertTrue("Wrong output: " + output, output.contains("Hello World"));
}
@Test
public void txSample() throws Exception {
String output = this.cli.run("samples/tx.groovy");
String output = this.cli.run("tx.groovy");
assertTrue("Wrong output: " + output, output.contains("Foo count="));
}
@Test
public void jmsSample() throws Exception {
String output = this.cli.run("samples/jms.groovy");
String output = this.cli.run("jms.groovy");
assertTrue("Wrong output: " + output,
output.contains("Received Greetings from Spring Boot via ActiveMQ"));
FileUtils.deleteDirectory(new File("activemq-data"));// cleanup ActiveMQ cruft
@ -142,14 +141,14 @@ public class SampleIntegrationTests {
@Ignore
// this test requires RabbitMQ to be run, so disable it be default
public void rabbitSample() throws Exception {
String output = this.cli.run("samples/rabbit.groovy");
String output = this.cli.run("rabbit.groovy");
assertTrue("Wrong output: " + output,
output.contains("Received Greetings from Spring Boot via RabbitMQ"));
}
@Test
public void deviceSample() throws Exception {
this.cli.run("samples/device.groovy");
this.cli.run("device.groovy");
assertEquals("Hello Normal Device!", this.cli.getHttpOutput());
}

View File

@ -40,7 +40,7 @@ public class TestCommandIntegrationTests {
public ExpectedException thrown = ExpectedException.none();
@Rule
public CliTester cli = new CliTester();
public CliTester cli = new CliTester("test-samples/");
@BeforeClass
public static void cleanGrapes() throws Exception {
@ -60,13 +60,13 @@ public class TestCommandIntegrationTests {
@Test
public void noTests() throws Throwable {
String output = this.cli.test("test-samples/book.groovy");
String output = this.cli.test("book.groovy");
assertThat(output, containsString("No tests found"));
}
@Test
public void empty() throws Exception {
String output = this.cli.test("test-samples/empty.groovy");
String output = this.cli.test("empty.groovy");
assertThat(output, containsString("No tests found"));
}
@ -74,39 +74,37 @@ public class TestCommandIntegrationTests {
public void noFile() throws Exception {
TestCommand command = new TestCommand();
this.thrown.expect(RuntimeException.class);
this.thrown.expectMessage("Can't find test-samples/nothing.groovy");
command.run("test-samples/nothing.groovy");
this.thrown.expectMessage("Can't find nothing.groovy");
command.run("nothing.groovy");
}
@Test
public void appAndTestsInOneFile() throws Exception {
String output = this.cli.test("test-samples/book_and_tests.groovy");
String output = this.cli.test("book_and_tests.groovy");
assertThat(output, containsString("OK (1 test)"));
}
@Test
public void appInOneFileTestsInAnotherFile() throws Exception {
String output = this.cli.test("test-samples/book.groovy",
"test-samples/test.groovy");
String output = this.cli.test("book.groovy", "test.groovy");
assertThat(output, containsString("OK (1 test)"));
}
@Test
public void spockTester() throws Exception {
String output = this.cli.test("test-samples/spock.groovy");
String output = this.cli.test("spock.groovy");
assertThat(output, containsString("OK (1 test)"));
}
@Test
public void spockAndJunitTester() throws Exception {
String output = this.cli.test("test-samples/spock.groovy",
"test-samples/book_and_tests.groovy");
String output = this.cli.test("spock.groovy", "book_and_tests.groovy");
assertThat(output, containsString("OK (2 tests)"));
}
@Test
public void verifyFailures() throws Exception {
String output = this.cli.test("test-samples/failures.groovy");
String output = this.cli.test("failures.groovy");
assertThat(output, containsString("Tests run: 5, Failures: 3"));
}
}