Add useful features to CliTester
This commit is contained in:
parent
9f13d291ec
commit
dac470a8fc
|
|
@ -30,12 +30,12 @@ import static org.junit.Assert.assertThat;
|
||||||
public class ClassLoaderIntegrationTests {
|
public class ClassLoaderIntegrationTests {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public CliTester cli = new CliTester();
|
public CliTester cli = new CliTester("src/test/resources/");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void runWithIsolatedClassLoader() throws Exception {
|
public void runWithIsolatedClassLoader() throws Exception {
|
||||||
// CLI classes or dependencies should not be exposed to the app
|
// 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());
|
SpringCli.class.getName());
|
||||||
assertThat(output, containsString("HasClasses-false-true-false"));
|
assertThat(output, containsString("HasClasses-false-true-false"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.Assume;
|
||||||
import org.junit.rules.TestRule;
|
import org.junit.rules.TestRule;
|
||||||
import org.junit.runner.Description;
|
import org.junit.runner.Description;
|
||||||
import org.junit.runners.model.Statement;
|
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.
|
* {@link TestRule} that can be used to invoke CLI commands.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
public class CliTester implements TestRule {
|
public class CliTester implements TestRule {
|
||||||
|
|
||||||
|
|
@ -49,17 +51,24 @@ public class CliTester implements TestRule {
|
||||||
|
|
||||||
private List<AbstractCommand> commands = new ArrayList<AbstractCommand>();
|
private List<AbstractCommand> commands = new ArrayList<AbstractCommand>();
|
||||||
|
|
||||||
|
private String prefix;
|
||||||
|
|
||||||
|
public CliTester(String prefix) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
public void setTimeout(long timeout) {
|
public void setTimeout(long timeout) {
|
||||||
this.timeout = 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(
|
Future<RunCommand> future = Executors.newSingleThreadExecutor().submit(
|
||||||
new Callable<RunCommand>() {
|
new Callable<RunCommand>() {
|
||||||
@Override
|
@Override
|
||||||
public RunCommand call() throws Exception {
|
public RunCommand call() throws Exception {
|
||||||
RunCommand command = new RunCommand();
|
RunCommand command = new RunCommand();
|
||||||
command.run(args);
|
command.run(sources);
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -67,13 +76,14 @@ public class CliTester implements TestRule {
|
||||||
return getOutput();
|
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(
|
Future<TestCommand> future = Executors.newSingleThreadExecutor().submit(
|
||||||
new Callable<TestCommand>() {
|
new Callable<TestCommand>() {
|
||||||
@Override
|
@Override
|
||||||
public TestCommand call() throws Exception {
|
public TestCommand call() throws Exception {
|
||||||
TestCommand command = new TestCommand();
|
TestCommand command = new TestCommand();
|
||||||
command.run(args);
|
command.run(sources);
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -81,13 +91,33 @@ public class CliTester implements TestRule {
|
||||||
return getOutput();
|
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() {
|
public String getOutput() {
|
||||||
return this.outputCapture.toString();
|
return this.outputCapture.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement apply(final Statement base, Description description) {
|
public Statement apply(final Statement base, final Description description) {
|
||||||
return this.outputCapture.apply(new RunLauncherStatement(base), 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() {
|
public String getHttpOutput() {
|
||||||
|
|
|
||||||
|
|
@ -23,20 +23,18 @@ import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests to exercise reproduce raised issues.
|
* Integration tests to exercise and reproduce specific issues.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
public class ReproIntegrationTests {
|
public class ReproIntegrationTests {
|
||||||
|
|
||||||
private static final String SRC = "src/test/resources/repro-samples";
|
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public CliTester cli = new CliTester();
|
public CliTester cli = new CliTester("src/test/resources/repro-samples/");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void grabAntBuilder() throws Exception {
|
public void grabAntBuilder() throws Exception {
|
||||||
this.cli.run(SRC + "/grab-ant-builder.groovy");
|
this.cli.run("grab-ant-builder.groovy");
|
||||||
assertThat(this.cli.getHttpOutput(),
|
assertThat(this.cli.getHttpOutput(),
|
||||||
containsString("{\"message\":\"Hello World\"}"));
|
containsString("{\"message\":\"Hello World\"}"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ import static org.junit.Assert.assertTrue;
|
||||||
public class SampleIntegrationTests {
|
public class SampleIntegrationTests {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public CliTester cli = new CliTester();
|
public CliTester cli = new CliTester("samples/");
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void cleanGrapes() throws Exception {
|
public static void cleanGrapes() throws Exception {
|
||||||
|
|
@ -47,26 +47,26 @@ public class SampleIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void appSample() throws Exception {
|
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"));
|
assertTrue("Wrong output: " + output, output.contains("Hello World"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void templateSample() throws Exception {
|
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!"));
|
assertTrue("Wrong output: " + output, output.contains("Hello World!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void jobSample() throws Exception {
|
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,
|
assertTrue("Wrong output: " + output,
|
||||||
output.contains("completed with the following parameters"));
|
output.contains("completed with the following parameters"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void reactorSample() throws Exception {
|
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;
|
int count = 0;
|
||||||
while (!output.contains("Hello Phil") && count++ < 5) {
|
while (!output.contains("Hello Phil") && count++ < 5) {
|
||||||
Thread.sleep(200);
|
Thread.sleep(200);
|
||||||
|
|
@ -77,8 +77,7 @@ public class SampleIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void jobWebSample() throws Exception {
|
public void jobWebSample() throws Exception {
|
||||||
String output = this.cli.run("samples/job.groovy", "samples/web.groovy",
|
String output = this.cli.run("job.groovy", "web.groovy", "foo=bar");
|
||||||
"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 = this.cli.getHttpOutput();
|
String result = this.cli.getHttpOutput();
|
||||||
|
|
@ -87,13 +86,13 @@ public class SampleIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void webSample() throws Exception {
|
public void webSample() throws Exception {
|
||||||
this.cli.run("samples/web.groovy");
|
this.cli.run("web.groovy");
|
||||||
assertEquals("World!", this.cli.getHttpOutput());
|
assertEquals("World!", this.cli.getHttpOutput());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void uiSample() throws Exception {
|
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();
|
String result = this.cli.getHttpOutput();
|
||||||
assertTrue("Wrong output: " + result, result.contains("Hello World"));
|
assertTrue("Wrong output: " + result, result.contains("Hello World"));
|
||||||
result = this.cli.getHttpOutput("http://localhost:8080/css/bootstrap.min.css");
|
result = this.cli.getHttpOutput("http://localhost:8080/css/bootstrap.min.css");
|
||||||
|
|
@ -102,37 +101,37 @@ public class SampleIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void actuatorSample() throws Exception {
|
public void actuatorSample() throws Exception {
|
||||||
this.cli.run("samples/actuator.groovy");
|
this.cli.run("actuator.groovy");
|
||||||
assertEquals("{\"message\":\"Hello World!\"}", this.cli.getHttpOutput());
|
assertEquals("{\"message\":\"Hello World!\"}", this.cli.getHttpOutput());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void httpSample() throws Exception {
|
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"));
|
assertTrue("Wrong output: " + output, output.contains("Hello World"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void integrationSample() throws Exception {
|
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"));
|
assertTrue("Wrong output: " + output, output.contains("Hello, World"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void xmlSample() throws Exception {
|
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"));
|
assertTrue("Wrong output: " + output, output.contains("Hello World"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void txSample() throws Exception {
|
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="));
|
assertTrue("Wrong output: " + output, output.contains("Foo count="));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void jmsSample() throws Exception {
|
public void jmsSample() throws Exception {
|
||||||
String output = this.cli.run("samples/jms.groovy");
|
String output = this.cli.run("jms.groovy");
|
||||||
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.deleteDirectory(new File("activemq-data"));// cleanup ActiveMQ cruft
|
FileUtils.deleteDirectory(new File("activemq-data"));// cleanup ActiveMQ cruft
|
||||||
|
|
@ -142,14 +141,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 {
|
||||||
String output = this.cli.run("samples/rabbit.groovy");
|
String output = this.cli.run("rabbit.groovy");
|
||||||
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 {
|
||||||
this.cli.run("samples/device.groovy");
|
this.cli.run("device.groovy");
|
||||||
assertEquals("Hello Normal Device!", this.cli.getHttpOutput());
|
assertEquals("Hello Normal Device!", this.cli.getHttpOutput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ public class TestCommandIntegrationTests {
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public CliTester cli = new CliTester();
|
public CliTester cli = new CliTester("test-samples/");
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void cleanGrapes() throws Exception {
|
public static void cleanGrapes() throws Exception {
|
||||||
|
|
@ -60,13 +60,13 @@ public class TestCommandIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void noTests() throws Throwable {
|
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"));
|
assertThat(output, containsString("No tests found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void empty() throws Exception {
|
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"));
|
assertThat(output, containsString("No tests found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,39 +74,37 @@ public class TestCommandIntegrationTests {
|
||||||
public void noFile() throws Exception {
|
public void noFile() throws Exception {
|
||||||
TestCommand command = new TestCommand();
|
TestCommand command = new TestCommand();
|
||||||
this.thrown.expect(RuntimeException.class);
|
this.thrown.expect(RuntimeException.class);
|
||||||
this.thrown.expectMessage("Can't find test-samples/nothing.groovy");
|
this.thrown.expectMessage("Can't find nothing.groovy");
|
||||||
command.run("test-samples/nothing.groovy");
|
command.run("nothing.groovy");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void appAndTestsInOneFile() throws Exception {
|
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)"));
|
assertThat(output, containsString("OK (1 test)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void appInOneFileTestsInAnotherFile() throws Exception {
|
public void appInOneFileTestsInAnotherFile() throws Exception {
|
||||||
String output = this.cli.test("test-samples/book.groovy",
|
String output = this.cli.test("book.groovy", "test.groovy");
|
||||||
"test-samples/test.groovy");
|
|
||||||
assertThat(output, containsString("OK (1 test)"));
|
assertThat(output, containsString("OK (1 test)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void spockTester() throws Exception {
|
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)"));
|
assertThat(output, containsString("OK (1 test)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void spockAndJunitTester() throws Exception {
|
public void spockAndJunitTester() throws Exception {
|
||||||
String output = this.cli.test("test-samples/spock.groovy",
|
String output = this.cli.test("spock.groovy", "book_and_tests.groovy");
|
||||||
"test-samples/book_and_tests.groovy");
|
|
||||||
assertThat(output, containsString("OK (2 tests)"));
|
assertThat(output, containsString("OK (2 tests)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyFailures() throws Exception {
|
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"));
|
assertThat(output, containsString("Tests run: 5, Failures: 3"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue