CommandShell extends Configured implements Tool { @Override public int run(String[] args) throws Exception { int exitCode = 0; try { exitCode = init(args); if (exitCode != 0 || subcommand == null) { printShellUsage(); return exitCode; } if (subcommand.validate()) { subcommand.execute(); } else { printShellUsage(); exitCode = 1; } } catch (Exception e) { printShellUsage(); printException(e); return 1; } return exitCode; }  abstract String getCommandUsage(); void setSubCommand(SubCommand cmd); void setOut(PrintStream p); PrintStream getOut(); void setErr(PrintStream p); PrintStream getErr(); @Override int run(String[] args);  }
@Test public void testCommandShellExample() throws Exception { Example ex = new Example(); ex.setConf(new Configuration()); int rc = 0; outContent.reset(); String[] args1 = {"hello"}; rc = ex.run(args1); assertEquals(outMsg("test exit code - normal hello"), 0, rc); assertTrue(outMsg("test normal hello message"), outContent.toString().contains(Example.HELLO_MSG)); outContent.reset(); String[] args2 = {"hello", "x"}; rc = ex.run(args2); assertEquals(outMsg("test exit code - bad hello"), 1, rc); assertTrue(outMsg("test bad hello message"), outContent.toString().contains(Example.Hello.HELLO_USAGE)); outContent.reset(); String[] args3 = {"goodbye"}; rc = ex.run(args3); assertEquals(outMsg("test exit code - normal goodbye"), 0, rc); assertTrue(outMsg("test normal goodbye message"), outContent.toString().contains(Example.GOODBYE_MSG)); }