Shell implements CommandRegistry { public void run(InputProvider inputProvider) throws IOException { Object result = null; while (!(result instanceof ExitRequest)) { Input input; try { input = inputProvider.readInput(); } catch (Exception e) { if (e instanceof ExitRequest) { break; } resultHandler.handleResult(e); continue; } if (input == null) { break; } result = evaluate(input); if (result != NO_INPUT && !(result instanceof ExitRequest)) { resultHandler.handleResult(result); } } } Shell(ResultHandler resultHandler); @Autowired(required = false) void setValidatorFactory(ValidatorFactory validatorFactory); @Override Map<String, MethodTarget> listCommands(); @PostConstruct void gatherMethodTargets(); @Autowired void setParameterResolvers(List<ParameterResolver> resolvers); void run(InputProvider inputProvider); Object evaluate(Input input); List<CompletionProposal> complete(CompletionContext context); static final Object NO_INPUT; }
@Test public void commandMatch() throws IOException { when(parameterResolver.supports(any())).thenReturn(true); when(inputProvider.readInput()).thenReturn(() -> "hello world how are you doing ?", null); valueResult = new ValueResult(null, "test"); when(parameterResolver.resolve(any(), any())).thenReturn(valueResult); doThrow(new Exit()).when(resultHandler).handleResult(any()); shell.methodTargets = Collections.singletonMap("hello world", MethodTarget.of("helloWorld", this, new Command.Help("Say hello"))); try { shell.run(inputProvider); fail("Exit expected"); } catch (Exit expected) { } Assert.assertTrue(invoked); }
@Test public void commandNotFound() throws IOException { when(inputProvider.readInput()).thenReturn(() -> "hello world how are you doing ?", null); doThrow(new Exit()).when(resultHandler).handleResult(isA(CommandNotFound.class)); shell.methodTargets = Collections.singletonMap("bonjour", MethodTarget.of("helloWorld", this, new Command.Help("Say hello"))); try { shell.run(inputProvider); fail("Exit expected"); } catch (Exit expected) { } }
@Test public void commandNotFoundPrefix() throws IOException { when(inputProvider.readInput()).thenReturn(() -> "helloworld how are you doing ?", null); doThrow(new Exit()).when(resultHandler).handleResult(isA(CommandNotFound.class)); shell.methodTargets = Collections.singletonMap("hello", MethodTarget.of("helloWorld", this, new Command.Help("Say hello"))); try { shell.run(inputProvider); fail("Exit expected"); } catch (Exit expected) { } }
@Test public void noCommand() throws IOException { when(parameterResolver.supports(any())).thenReturn(true); when(inputProvider.readInput()).thenReturn(() -> "", () -> "hello world how are you doing ?", null); valueResult = new ValueResult(null, "test"); when(parameterResolver.resolve(any(), any())).thenReturn(valueResult); doThrow(new Exit()).when(resultHandler).handleResult(any()); shell.methodTargets = Collections.singletonMap("hello world", MethodTarget.of("helloWorld", this, new Command.Help("Say hello"))); try { shell.run(inputProvider); fail("Exit expected"); } catch (Exit expected) { } Assert.assertTrue(invoked); }
@Test public void commandThrowingAnException() throws IOException { when(parameterResolver.supports(any())).thenReturn(true); when(inputProvider.readInput()).thenReturn(() -> "fail", null); doThrow(new Exit()).when(resultHandler).handleResult(isA(SomeException.class)); shell.methodTargets = Collections.singletonMap("fail", MethodTarget.of("failing", this, new Command.Help("Will throw an exception"))); try { shell.run(inputProvider); fail("Exit expected"); } catch (Exit expected) { } Assert.assertTrue(invoked); }
@Test public void comments() throws IOException { when(parameterResolver.supports(any())).thenReturn(true); when(inputProvider.readInput()).thenReturn(() -> " shell.run(inputProvider); }