Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @author Janne Valkealahti
* @author Mahmoud Ben Hassine
* @author David Pilar
*/
public class ShellTestClient {

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 {

Expand All @@ -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();
Expand Down