FsShell extends Configured implements Tool { @Override public int run(String argv[]) throws Exception { init(); Tracer tracer = new Tracer.Builder("FsShell"). conf(TraceUtils.wrapHadoopConf(SHELL_HTRACE_PREFIX, getConf())). build(); 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) { if (e.getMessage() == null) { displayError(cmd, "Null exception message"); e.printStackTrace(System.err); } else { 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 { FsShell shell = new FsShell(new Configuration()); try (GenericTestUtils.SystemErrCapturer capture = new GenericTestUtils.SystemErrCapturer()) { ToolRunner.run(shell, new String[]{"dfs -mkdirs"}); Assert.assertThat("FSShell dfs command did not print the error " + "message when invalid command is passed", capture.getOutput(), StringContains.containsString( "-mkdirs: Unknown command")); Assert.assertThat("FSShell dfs command did not print help " + "message when invalid command is passed", capture.getOutput(), StringContains.containsString( "Usage: hadoop fs [generic options]")); } }
@Test public void testExceptionNullMessage() throws Exception { final String cmdName = "-cmdExNullMsg"; final Command cmd = Mockito.mock(Command.class); Mockito.when(cmd.run(Mockito.anyVararg())).thenThrow( new IllegalArgumentException()); Mockito.when(cmd.getUsage()).thenReturn(cmdName); final CommandFactory cmdFactory = Mockito.mock(CommandFactory.class); final String[] names = {cmdName}; Mockito.when(cmdFactory.getNames()).thenReturn(names); Mockito.when(cmdFactory.getInstance(cmdName)).thenReturn(cmd); FsShell shell = new FsShell(new Configuration()); shell.commandFactory = cmdFactory; try (GenericTestUtils.SystemErrCapturer capture = new GenericTestUtils.SystemErrCapturer()) { ToolRunner.run(shell, new String[]{cmdName}); Assert.assertThat(capture.getOutput(), StringContains.containsString(cmdName + ": Null exception message")); } }