@@ -104,22 +104,34 @@ public void promptsSilentlyForUsernameAndPasswordIfNoneGivenIfOutputRedirected()
104104 }
105105
106106 doThrow (authException ).doNothing ().when (shell ).connect (connectionConfig );
107+ doReturn ("" ).doReturn ("secret" ).when (connectionConfig ).password ();
107108
108109 String inputString = "bob\n secret\n " ;
109110 InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
110111
111112 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
112- PrintStream ps = new PrintStream ( baos );
113-
114- Main main = new Main ( inputStream , ps );
115- main .connectMaybeInteractively ( shell , connectionConfig , true , false );
113+ PrintStream ps = new PrintStream (baos );
116114
117- String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
115+ // Redirect stdin and stdout
116+ InputStream stdIn = System .in ;
117+ PrintStream stdOut = System .out ;
118+ System .setIn (inputStream );
119+ System .setOut (ps );
118120
119- assertEquals ( "" , out );
120- verify ( connectionConfig ).setUsername ( "bob" );
121- verify ( connectionConfig ).setPassword ( "secret" );
122- verify ( shell , times ( 2 ) ).connect ( connectionConfig );
121+ try {
122+ Main main = new Main ();
123+ main .connectMaybeInteractively (shell , connectionConfig , true , false );
124+
125+ String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
126+
127+ assertEquals ("" , out );
128+ verify (connectionConfig ).setUsername ("bob" );
129+ verify (connectionConfig ).setPassword ("secret" );
130+ verify (shell , times (2 )).connect (connectionConfig );
131+ } finally {
132+ System .setIn (stdIn );
133+ System .setOut (stdOut );
134+ }
123135 }
124136
125137 @ Test
@@ -174,19 +186,30 @@ public void promptsSilentlyForUserIfPassExistsIfOutputRedirected() throws Except
174186 doReturn ("secret" ).when (connectionConfig ).password ();
175187
176188 String inputString = "bob\n " ;
177- InputStream inputStream = new ByteArrayInputStream ( inputString .getBytes () );
189+ InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
178190
179191 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
180- PrintStream ps = new PrintStream ( baos );
192+ PrintStream ps = new PrintStream (baos );
181193
182- Main main = new Main ( inputStream , ps );
183- main .connectMaybeInteractively ( shell , connectionConfig , true , false );
194+ // Redirect stdin and stdout
195+ InputStream stdIn = System .in ;
196+ PrintStream stdOut = System .out ;
197+ System .setIn (inputStream );
198+ System .setOut (ps );
184199
185- String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
200+ try {
201+ Main main = new Main ();
202+ main .connectMaybeInteractively (shell , connectionConfig , true , false );
203+
204+ String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
186205
187- assertEquals ( out , "" );
188- verify ( connectionConfig ).setUsername ( "bob" );
189- verify ( shell , times ( 2 ) ).connect ( connectionConfig );
206+ assertEquals (out , "" );
207+ verify (connectionConfig ).setUsername ("bob" );
208+ verify (shell , times (2 )).connect (connectionConfig );
209+ } finally {
210+ System .setIn (stdIn );
211+ System .setOut (stdOut );
212+ }
190213 }
191214
192215 @ Test
@@ -216,23 +239,34 @@ public void promptsSilentlyForPassIfUserExistsIfOutputRedirected() throws Except
216239 return ;
217240 }
218241
219- doThrow (authException ).doNothing ().when (shell ).connect (connectionConfig );
242+ doReturn ("bob" ).when (connectionConfig ).username ();
243+ doReturn ("" ).doReturn ("" ).doReturn ("secret" ).when (connectionConfig ).password ();
220244
221- String inputString = "bob \n secret \n " ;
245+ String inputString = "secret \n " ;
222246 InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
223247
224248 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
225- PrintStream ps = new PrintStream ( baos );
249+ PrintStream ps = new PrintStream (baos );
226250
227- Main main = new Main ( inputStream , ps );
228- main .connectMaybeInteractively ( shell , connectionConfig , true , false );
251+ // Redirect stdin and stdout
252+ InputStream stdIn = System .in ;
253+ PrintStream stdOut = System .out ;
254+ System .setIn (inputStream );
255+ System .setOut (ps );
229256
230- String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
257+ try {
258+ Main main = new Main ();
259+ main .connectMaybeInteractively (shell , connectionConfig , true , false );
260+
261+ String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
231262
232- assertEquals ( "" , out );
233- verify ( connectionConfig ).setUsername ( "bob" );
234- verify ( connectionConfig ).setPassword ( "secret" );
235- verify ( shell , times ( 2 ) ).connect ( connectionConfig );
263+ assertEquals (out , "" );
264+ verify (connectionConfig ).setPassword ("secret" );
265+ verify (shell , times (1 )).connect (connectionConfig );
266+ } finally {
267+ System .setIn (stdIn );
268+ System .setOut (stdOut );
269+ }
236270 }
237271
238272 @ Test
@@ -312,17 +346,28 @@ public void doesNotRepromptIfUserIsNotProvidedIfOutputRedirected() throws Except
312346 InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
313347
314348 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
315- PrintStream ps = new PrintStream ( baos );
316- Main main = new Main ( inputStream , ps );
317- main .connectMaybeInteractively ( shell , connectionConfig , true , false );
318-
319- String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
349+ PrintStream ps = new PrintStream (baos );
320350
321- assertEquals ( "" , out );
322- verify ( connectionConfig ).setUsername ( "" );
323- verify ( connectionConfig ).setPassword ( "secret" );
324- verify ( shell , times ( 2 ) ).connect ( connectionConfig );
351+ // Redirect stdin and stdout
352+ InputStream stdIn = System .in ;
353+ PrintStream stdOut = System .out ;
354+ System .setIn (inputStream );
355+ System .setOut (ps );
325356
357+ try {
358+ Main main = new Main ();
359+ main .connectMaybeInteractively (shell , connectionConfig , true , false );
360+
361+ String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
362+
363+ assertEquals ("" , out );
364+ verify (connectionConfig ).setUsername ("" );
365+ verify (connectionConfig ).setPassword ("secret" );
366+ verify (shell , times (2 )).connect (connectionConfig );
367+ } finally {
368+ System .setIn (stdIn );
369+ System .setOut (stdOut );
370+ }
326371 }
327372
328373 @ Test
0 commit comments