[bs-62] Add assertions to CLI integration tests

[Fixes #48658503]
pull/1/merge
Dave Syer 12 years ago
parent bcf86b320e
commit 61fa55b524

@ -15,15 +15,23 @@
*/ */
package org.springframework.bootstrap.cli; package org.springframework.bootstrap.cli;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.Executors; 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.apache.ivy.util.FileUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
* @author Dave Syer * @author Dave Syer
* *
@ -32,6 +40,25 @@ public class SampleIntegrationTests {
private RunCommand command; private RunCommand command;
private PrintStream savedOutput;
private ByteArrayOutputStream output;
@Before
public void init() {
this.savedOutput = System.out;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
}
@After
public void clear() {
System.setOut(this.savedOutput);
}
private String getOutput() {
return this.output.toString();
}
private void start(final String sample) throws Exception { private void start(final String sample) throws Exception {
Future<RunCommand> future = Executors.newSingleThreadExecutor().submit( Future<RunCommand> future = Executors.newSingleThreadExecutor().submit(
new Callable<RunCommand>() { new Callable<RunCommand>() {
@ -61,26 +88,39 @@ public class SampleIntegrationTests {
@Test @Test
public void appSample() throws Exception { public void appSample() throws Exception {
start("samples/app.groovy"); start("samples/app.groovy");
String output = getOutput();
assertTrue("Wrong output: " + output, output.contains("Hello World"));
} }
@Test @Test
public void jobSample() throws Exception { public void jobSample() throws Exception {
start("samples/job.groovy"); start("samples/job.groovy");
String output = getOutput();
assertTrue("Wrong output: " + output,
output.contains("completed with the following parameters"));
} }
@Test @Test
public void webSample() throws Exception { public void webSample() throws Exception {
start("samples/web.groovy"); start("samples/web.groovy");
String result = FileUtil.readEntirely(new URL("http://localhost:8080")
.openStream());
assertEquals("World!", result);
} }
@Test @Test
public void serviceSample() throws Exception { public void serviceSample() throws Exception {
start("samples/service.groovy"); start("samples/service.groovy");
String result = FileUtil.readEntirely(new URL("http://localhost:8080")
.openStream());
assertEquals("{\"message\":\"Hello World!\"}", result);
} }
@Test @Test
public void integrationSample() throws Exception { public void integrationSample() throws Exception {
start("samples/integration.groovy"); start("samples/integration.groovy");
String output = getOutput();
assertTrue("Wrong output: " + output, output.contains("Hello, World"));
} }
} }

@ -4,28 +4,22 @@ import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class SimpleBootstrapApplicationTests { public class SimpleBootstrapApplicationTests {
private static PrintStream savedOutput; private PrintStream savedOutput;
private static ByteArrayOutputStream output; private ByteArrayOutputStream output;
private String profiles; private String profiles;
@BeforeClass
public static void init() {
savedOutput = System.out;
output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
}
@Before @Before
public void before() { public void init() {
this.savedOutput = System.out;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
this.profiles = System.getProperty("spring.profiles.active"); this.profiles = System.getProperty("spring.profiles.active");
} }
@ -36,15 +30,11 @@ public class SimpleBootstrapApplicationTests {
} else { } else {
System.clearProperty("spring.profiles.active"); System.clearProperty("spring.profiles.active");
} }
System.setOut(this.savedOutput);
} }
@AfterClass private String getOutput() {
public static void clear() { return this.output.toString();
System.setOut(savedOutput);
}
private static String getOutput() {
return output.toString();
} }
@Test @Test

Loading…
Cancel
Save