HadoopREPL extends REPL { @Override protected void evaluate(final String input) throws ExitSignal { popHistory(); final Iterable<String> inputParts = ARG_SPLITTER.limit(2).split(input); if (Iterables.isEmpty(inputParts)) { } else { final String command = Iterables.get(inputParts, 0).toLowerCase(); if (commandMappings.containsKey(call(command))) { commandMappings.get(call(command)).execute( new CommandInvocation(command, Iterables.toArray(ARG_SPLITTER.split(Iterables.get(inputParts, 1, "")), String.class)), sessionState ); } else { sessionState.output("Unknown command \"%s\"", command); } pushHistory(input); } } HadoopREPL(final Configuration configuration);  HadoopREPL(final Configuration configuration, final SessionState sessionState);  HadoopREPL(final Configuration configuration, final SessionState sessionState, final Map<Call, Command> commandMappings); static void main(final String[] args);  }
@Test public void testEvaluate() throws Exception { final Configuration configuration = mock(Configuration.class); final SessionState sessionState = mock(SessionState.class); final HadoopREPL repl = new HadoopREPL(configuration, sessionState, ImmutableMap.<Call, Command>of( call("test"), new Command() { @Override public void execute(final CommandInvocation call, final SessionState ss) throws REPL.ExitSignal { assertEquals("test", call.command()); assertArrayEquals(new String[0], call.args()); assertEquals(sessionState, ss); } @Override public Usage usage(final SessionState sessionState) { return null; } } )); repl.evaluate("test"); assertEquals("test", Iterables.get(repl.history(), 0)); }
@Test public void testEvaluateHelpWithUsage() throws Exception { final Configuration configuration = new Configuration(); final SessionState sessionState = mock(SessionState.class); when(sessionState.configuration()).thenReturn(configuration); final HadoopREPL repl = new HadoopREPL(configuration, sessionState); doAnswer(new Answer() { @Override public Object answer(final InvocationOnMock invocationOnMock) throws Throwable { assertEquals("Displaying help for \"save\"", String.format(invocationOnMock.getArguments()[0].toString(), invocationOnMock.getArguments()[1])); return null; } }).when(sessionState).output(anyString()); doAnswer(new Answer() { @Override public Object answer(final InvocationOnMock invocationOnMock) throws Throwable { final Command.Usage usage = (Command.Usage) invocationOnMock.getArguments()[0]; assertEquals("save", usage.command); return null; } }).when(sessionState).outputUsage(any(Command.Usage.class)); repl.evaluate("help save"); assertEquals("help save", Iterables.get(repl.history(), 0)); }
@Test public void testEvaluateHelpWithNoSuchCommandUsage() throws Exception { final Configuration configuration = new Configuration(); final SessionState sessionState = mock(SessionState.class); when(sessionState.configuration()).thenReturn(configuration); final HadoopREPL repl = new HadoopREPL(configuration, sessionState); doAnswer(new Answer() { @Override public Object answer(final InvocationOnMock invocationOnMock) throws Throwable { assertEquals("Unknown command \"test\"", String.format(invocationOnMock.getArguments()[0].toString(), invocationOnMock.getArguments()[1])); return null; } }).when(sessionState).error(anyString()); repl.evaluate("help test"); assertEquals("help test", Iterables.get(repl.history(), 0)); }
@Test public void testEvaluateUnknownCommand() throws Exception { final Configuration configuration = mock(Configuration.class); final SessionState sessionState = mock(SessionState.class); doAnswer(new Answer() { @Override public Object answer(final InvocationOnMock invocationOnMock) throws Throwable { assertEquals("Unknown command \"test\"", String.format(invocationOnMock.getArguments()[0].toString(), invocationOnMock.getArguments()[1])); return null; } }).when(sessionState).output(anyString(), any(Object[].class)); final HadoopREPL repl = new HadoopREPL(configuration, sessionState, ImmutableMap.<Call, Command>of()); repl.evaluate("test"); assertEquals("test", Iterables.get(repl.history(), 0)); }