FsShell extends Configured implements Tool { @Override public int run(String argv[]) throws Exception { init(); int exitCode = -1; if (argv.length < 1) { printUsage(System.err); } else { String cmd = argv[0]; Command instance = null; try { instance = commandFactory.getInstance(cmd); if (instance == null) { throw new UnknownCommandException(); } TraceScope scope = tracer.newScope(instance.getCommandName()); if (scope.getSpan() != null) { String args = StringUtils.join(" ", argv); if (args.length() > 2048) { args = args.substring(0, 2048); } scope.getSpan().addKVAnnotation("args", args); } try { exitCode = instance.run(Arrays.copyOfRange(argv, 1, argv.length)); } finally { scope.close(); } } catch (IllegalArgumentException e) { displayError(cmd, e.getLocalizedMessage()); printUsage(System.err); if (instance != null) { printInstanceUsage(System.err, instance); } } catch (Exception e) { LOG.debug("Error", e); displayError(cmd, "Fatal internal error"); e.printStackTrace(System.err); } } tracer.close(); return exitCode; } FsShell();  FsShell(Configuration conf); Path getCurrentTrashDir(); Path getCurrentTrashDir(Path path); @Override int run(String argv[]); void close(); static void main(String argv[]);  }
@Test public void testDFSWithInvalidCommmand() throws Throwable { Configuration conf = new Configuration(); FsShell shell = new FsShell(conf); String[] args = new String[1]; args[0] = "dfs -mkdirs"; final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); final PrintStream out = new PrintStream(bytes); final PrintStream oldErr = System.err; try { System.setErr(out); ToolRunner.run(shell, args); String errorValue=new String(bytes.toString()); Assert .assertTrue( "FSShell dfs command did not print the error " + "message when invalid command is passed", errorValue.contains("-mkdirs: Unknown command")); Assert .assertTrue( "FSShell dfs command did not print help " + "message when invalid command is passed", errorValue.contains("Usage: hadoop fs [generic options]")); } finally { IOUtils.closeStream(out); System.setErr(oldErr); } }