private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @After public void cleanUpStreams() { System.setOut(null); System.setErr(null); }
@After public void tearDown() { System.setOut(this.out); }
public void stop() { if (System.out != originalOutputPrintStream) { System.setOut(originalOutputPrintStream); } if (System.err != originalErrorPrintStream) { System.setErr(originalErrorPrintStream); } }
public void record() { os = new PrintStream(output); err = new PrintStream(error); System.setOut(os); System.setErr(err); }
private static void replaceStdOutAndStdErr() { stdOut = System.out; stdErr = System.err; buffer = new ByteArrayOutputStream(); PrintStream capture = new PrintStream(buffer); System.setOut(capture); System.setErr(capture); }
@After public void tearDown() { System.setOut(out); }
@Before public void setUp() { System.setOut(new PrintStream(arrayOutputStream)); System.setErr(new PrintStream(arrayErrorStream)); }
protected static void resetStreamsAndSendOutput() { System.setOut(ORIGINAL_STDOUT); System.setErr(ORIGINAL_STDERR); System.setIn(ORIGINAL_STDIN); LOG.info("Sending stdout content through logger: \n\n{}\n\n", outContent.toString()); LOG.info("Sending stderr content through logger: \n\n{}\n\n", errContent.toString()); }
@After public void tearDown() { if (System.out != originalSystemOut) { System.out.close(); } if (System.err != originalSystemErr) { System.err.close(); } System.setOut(originalSystemOut); System.setErr(originalSystemErr); }
@Before public void setUp() { System.setOut(new PrintStream(arrayOutputStream)); System.setErr(new PrintStream(arrayErrorStream)); }
private void doFizzBuzz(final int n, final String s) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); System.setOut(new PrintStream(bos)); this.fb.fizzBuzz(n); System.out.flush(); String platformDependentExpectedResult = s.replaceAll("\\n", System.getProperty("line.separator")); assertEquals(platformDependentExpectedResult, baos.toString()); }
@Test public void testCommandUsageForGoodbye() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("bye", "--help"); assertThat(baos.toString()) .contains("A command saying goodbye.") // Summary .contains("-n <value>"); // Option }
@Test public void testCommandUsageForHello() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hello", "--help"); assertThat(baos.toString()) .contains("Usage: ") .contains("A command saying hello.") // Summary .contains("A simple command to wish you a good day.") // Description .contains("-n,--name <name>") // Option .contains("your name"); // Option description }
@Test public void testMissingOption() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hello"); assertThat(baos.toString()) .contains("The option", "name", "is required"); }
@Test public void testInvalidValue() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hidden", "-n", "vert.x", "-c", "hello"); assertThat(baos.toString()) .contains("The value 'hello' is not accepted by 'count'"); }
@Test public void testMissingValue() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hello", "-n"); assertThat(baos.toString()) .contains("The option 'name' requires a value"); }
@Test public void testCommandNotFound() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("missing"); assertThat(baos.toString()).contains("The command", "missing", "See", "--help"); baos.reset(); itf.execute("missing", "--help"); assertThat(baos.toString()).contains("The command", "missing", "See", "--help"); }
@Test public void testUsage() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("--help"); assertThat(baos.toString()).contains("hello").contains("bye") .doesNotContain("hidden").contains("A command saying hello"); }
@Test public void testHiddenCommandUsage() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hidden", "-h"); assertThat(baos.toString()) .contains("-n <value>") .doesNotContain("-c ") .doesNotContain("count"); }