diff --git a/spring-shell-test/src/main/java/org/springframework/shell/test/ShellTestClient.java b/spring-shell-test/src/main/java/org/springframework/shell/test/ShellTestClient.java index e3161d06f..51ee7b92f 100644 --- a/spring-shell-test/src/main/java/org/springframework/shell/test/ShellTestClient.java +++ b/spring-shell-test/src/main/java/org/springframework/shell/test/ShellTestClient.java @@ -32,6 +32,7 @@ * * @author Janne Valkealahti * @author Mahmoud Ben Hassine + * @author David Pilar */ public class ShellTestClient { @@ -56,14 +57,20 @@ public ShellTestClient(CommandParser commandParser, CommandRegistry commandRegis /** * Execute a command and write its result to the shell screen. * @param input the raw input command + * @param inputResponses the responses to the command input * @return the shell screen after command execution * @throws Exception if an error occurred during command execution */ - public ShellScreen sendCommand(String input) throws Exception { + public ShellScreen sendCommand(String input, String... inputResponses) throws Exception { StringWriter stringWriter = new StringWriter(); ParsedInput parsedInput = this.commandParser.parse(input); PrintWriter outputWriter = new PrintWriter(stringWriter); InputReader inputReader = new InputReader() { + @Override + public String readInput(String prompt) { + outputWriter.print(prompt); + return String.join(" ", inputResponses); + } }; CommandContext commandContext = new CommandContext(parsedInput, this.commandRegistry, outputWriter, inputReader); diff --git a/spring-shell-test/src/test/java/org/springframework/shell/test/ShellTestClientTests.java b/spring-shell-test/src/test/java/org/springframework/shell/test/ShellTestClientTests.java index 3f1ebbbef..3d44a3383 100644 --- a/spring-shell-test/src/test/java/org/springframework/shell/test/ShellTestClientTests.java +++ b/spring-shell-test/src/test/java/org/springframework/shell/test/ShellTestClientTests.java @@ -17,6 +17,8 @@ import org.springframework.shell.core.command.ExitStatus; import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.io.PrintWriter; + @ExtendWith(SpringExtension.class) class ShellTestClientTests { @@ -36,6 +38,18 @@ void testUnknownCommandExecution(@Autowired ShellTestClient shellTestClient) { .isInstanceOf(CommandNotFoundException.class); } + @Test + void testCommandExecutionWithInputReader(@Autowired ShellTestClient client) throws Exception { + ShellScreen screen = client.sendCommand("hello"); + ShellAssertions.assertThat(screen).containsText("You said: "); + + screen = client.sendCommand("hello", "hi"); + ShellAssertions.assertThat(screen).containsText("You said: hi"); + + screen = client.sendCommand("hello", "hi", "to", "you"); + ShellAssertions.assertThat(screen).containsText("You said: hi to you"); + } + @Configuration static class TestCommands { @@ -50,6 +64,18 @@ public ExitStatus doExecute(CommandContext commandContext) { }; } + @Bean + public Command hello() { + return new AbstractCommand("hello", "A hello command") { + @Override + public ExitStatus doExecute(CommandContext commandContext) throws Exception { + String message = commandContext.inputReader().readInput(); + commandContext.outputWriter().println("You said: " + message); + return ExitStatus.OK; + } + }; + } + @Bean public CommandRegistry commandRegistry() { return new CommandRegistry();