### Question:
SampleCommand implements CliCommand { @Override public int run(@Nonnull ProcessStream process) throws CmdLineException { for (int i = 0; i < number; i++) { PrintStream customStdOut = getOutStream(i); try { process = new ProcessStream( process.in(), customStdOut == null ? process.out() : customStdOut, process.err() ); handler.run(process); } finally { if (customStdOut != null) customStdOut.close(); } } return 0; }  @Option(name = "--number", aliases = {"-n"}, usage = "Number of samples to take") void setNumber(int number); @Option(name = "--interval", aliases = {"-i"}, usage = "minimal wait time before invoking second sample") void setInterval(String interval); @Override @Nonnull String getName(); @Override @Nonnull String getDescription(); @Override int run(@Nonnull ProcessStream process); @Option(name = "--out", aliases = {"-o"}, usage = "Output files to write the result")
    public String output; public long interval; }### Answer:
@Test public void once() throws Exception { run("sample", "-n" , "1", "threaddump", "--in", "process:" + Util.currentPid()); assertThat(err.toString(), equalTo("")); assertThat(exitValue, equalTo(0)); new ThreadDumpFactory().fromStream(new ByteArrayInputStream(out.toByteArray())); }
@Test public void outToFiles() throws Exception { File destination = tmp.newFolder(); run( "sample", "--number" , "3", "--out", destination.getAbsolutePath() + "/jstack", "threaddump", "--in", "process:" + Util.currentPid() ); assertThat(err.toString(), equalTo("")); assertThat(out.toString(), equalTo("")); assertThat(exitValue, equalTo(0)); String[] outs = destination.list(); assertThat(outs, arrayWithSize(3)); for (String j : outs) { assertThat(j, startsWith("jstack")); new ThreadDumpFactory().fromFile(new File(destination, j)); } }
@Test public void brokenCommand() throws Exception { run("sample", "-n" , "1", "Whoa?"); assertThat(err.toString(), containsString("Command \"Whoa?\" not found")); assertThat(out.toString(), equalTo("")); assertThat(exitValue, not(equalTo(0))); }
@Test public void brokenNumber() throws Exception { run("sample", "-n", "0", "help"); assertThat(err.toString(), containsString("Number of invocations must be positive. 0 given.")); assertThat(out.toString(), equalTo("")); assertThat(exitValue, not(equalTo(0))); }
@Test public void mixedArgsShouldReportProblemsForCorrectCommand() throws Exception { run("sample", "-n" , "1", "threaddump", "-o", "asdf"); assertThat(out.toString(), equalTo("")); assertThat(err.toString(), not(containsString("Run dumpling command periodically"))); assertThat(err.toString(), containsString("Print runtime as string")); }