1717import org .springframework .shell .core .command .ExitStatus ;
1818import org .springframework .test .context .junit .jupiter .SpringExtension ;
1919
20+ import java .io .PrintWriter ;
21+
2022@ ExtendWith (SpringExtension .class )
2123class ShellTestClientTests {
2224
@@ -36,6 +38,42 @@ void testUnknownCommandExecution(@Autowired ShellTestClient shellTestClient) {
3638 .isInstanceOf (CommandNotFoundException .class );
3739 }
3840
41+ @ Test
42+ void testCommandExecutionWithReadingInput (@ Autowired ShellTestClient client ) throws Exception {
43+ // when
44+ ShellScreen screen = client .sendCommand ("hello" , ShellInputProvider .builder ().input ("hi" ).build ());
45+
46+ // then
47+ ShellAssertions .assertThat (screen ).containsText ("You said: hi" );
48+ }
49+
50+ @ Test
51+ void testCommandExecutionWithReadingPassword (@ Autowired ShellTestClient client ) throws Exception {
52+ // when
53+ ShellScreen screen = client .sendCommand ("password" , ShellInputProvider .builder ().password ("secret123" ).build ());
54+
55+ // then
56+ ShellAssertions .assertThat (screen ).containsText ("Your password is: secret123" );
57+ }
58+
59+ @ Test
60+ void testCommandExecutionWithComplexInputs (@ Autowired ShellTestClient client ) throws Exception {
61+ // given
62+ ShellInputProvider inputProvider = ShellInputProvider .builder ()
63+ .input ("One" , "Two" )
64+ .password ("secret1" , "secret2" )
65+ .build ();
66+
67+ // when
68+ ShellScreen screen = client .sendCommand ("complex" , inputProvider );
69+
70+ // then
71+ ShellAssertions .assertThat (screen ).containsText ("First input is: One" );
72+ ShellAssertions .assertThat (screen ).containsText ("First password is: secret1" );
73+ ShellAssertions .assertThat (screen ).containsText ("Second input is: Two" );
74+ ShellAssertions .assertThat (screen ).containsText ("Second password is: secret2" );
75+ }
76+
3977 @ Configuration
4078 static class TestCommands {
4179
@@ -50,6 +88,52 @@ public ExitStatus doExecute(CommandContext commandContext) {
5088 };
5189 }
5290
91+ @ Bean
92+ public Command hello () {
93+ return new AbstractCommand ("hello" , "A hello command" ) {
94+ @ Override
95+ public ExitStatus doExecute (CommandContext commandContext ) throws Exception {
96+ String message = commandContext .inputReader ().readInput ();
97+ commandContext .outputWriter ().println ("You said: " + message );
98+ return ExitStatus .OK ;
99+ }
100+ };
101+ }
102+
103+ @ Bean
104+ public Command password () {
105+ return new AbstractCommand ("password" , "A password command" ) {
106+ @ Override
107+ public ExitStatus doExecute (CommandContext commandContext ) throws Exception {
108+ char [] chars = commandContext .inputReader ().readPassword ();
109+ commandContext .outputWriter ().println ("Your password is: " + new String (chars ));
110+ return ExitStatus .OK ;
111+ }
112+ };
113+ }
114+
115+ @ Bean
116+ public Command complexCommand () {
117+ return new AbstractCommand ("complex" , "A complex command" ) {
118+ @ Override
119+ public ExitStatus doExecute (CommandContext commandContext ) throws Exception {
120+ String message = commandContext .inputReader ().readInput ();
121+ commandContext .outputWriter ().println ("First input is: " + message );
122+
123+ char [] chars = commandContext .inputReader ().readPassword ();
124+ commandContext .outputWriter ().println ("First password is: " + new String (chars ));
125+
126+ message = commandContext .inputReader ().readInput ();
127+ commandContext .outputWriter ().println ("Second input is: " + message );
128+
129+ chars = commandContext .inputReader ().readPassword ();
130+ commandContext .outputWriter ().println ("Second password is: " + new String (chars ));
131+
132+ return ExitStatus .OK ;
133+ }
134+ };
135+ }
136+
53137 @ Bean
54138 public CommandRegistry commandRegistry () {
55139 return new CommandRegistry ();
0 commit comments