55import org .junit .Test ;
66import org .junit .rules .ExpectedException ;
77import org .mockito .ArgumentCaptor ;
8- import org .neo4j .driver .exceptions .AuthenticationException ;
9- import org .neo4j .driver .exceptions .Neo4jException ;
10- import org .neo4j .shell .cli .CliArgs ;
11- import org .neo4j .shell .system .Utils ;
128
139import java .io .ByteArrayInputStream ;
1410import java .io .ByteArrayOutputStream ;
1511import java .io .InputStream ;
1612import java .io .PrintStream ;
1713import java .nio .charset .StandardCharsets ;
1814
15+ import org .neo4j .driver .exceptions .AuthenticationException ;
16+ import org .neo4j .driver .exceptions .Neo4jException ;
17+ import org .neo4j .shell .cli .CliArgs ;
18+ import org .neo4j .shell .system .Utils ;
19+
1920import static org .junit .Assert .assertEquals ;
2021import static org .junit .Assert .assertTrue ;
2122import static org .junit .Assert .fail ;
@@ -108,28 +109,17 @@ public void promptsSilentlyForUsernameAndPasswordIfNoneGivenIfOutputRedirected()
108109 InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
109110
110111 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
111- PrintStream ps = new PrintStream (baos );
112+ PrintStream ps = new PrintStream ( baos );
112113
113- // Redirect stdin and stdout
114- InputStream stdIn = System .in ;
115- PrintStream stdOut = System .out ;
116- System .setIn (inputStream );
117- System .setOut (ps );
114+ Main main = new Main ( inputStream , ps );
115+ main .connectMaybeInteractively ( shell , connectionConfig , true , false );
118116
119- try {
120- Main main = new Main ();
121- main .connectMaybeInteractively (shell , connectionConfig , true , false );
122-
123- String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
124-
125- assertEquals ("" , out );
126- verify (connectionConfig ).setUsername ("bob" );
127- verify (connectionConfig ).setPassword ("secret" );
128- verify (shell , times (2 )).connect (connectionConfig );
129- } finally {
130- System .setIn (stdIn );
131- System .setOut (stdOut );
132- }
117+ String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
118+
119+ assertEquals ( "" , out );
120+ verify ( connectionConfig ).setUsername ( "bob" );
121+ verify ( connectionConfig ).setPassword ( "secret" );
122+ verify ( shell , times ( 2 ) ).connect ( connectionConfig );
133123 }
134124
135125 @ Test
@@ -184,30 +174,19 @@ public void promptsSilentlyForUserIfPassExistsIfOutputRedirected() throws Except
184174 doReturn ("secret" ).when (connectionConfig ).password ();
185175
186176 String inputString = "bob\n " ;
187- InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
177+ InputStream inputStream = new ByteArrayInputStream ( inputString .getBytes () );
188178
189179 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
190- PrintStream ps = new PrintStream (baos );
191-
192- // Redirect stdin and stdout
193- InputStream stdIn = System .in ;
194- PrintStream stdOut = System .out ;
195- System .setIn (inputStream );
196- System .setOut (ps );
180+ PrintStream ps = new PrintStream ( baos );
197181
198- try {
199- Main main = new Main ();
200- main .connectMaybeInteractively (shell , connectionConfig , true , false );
182+ Main main = new Main ( inputStream , ps );
183+ main .connectMaybeInteractively ( shell , connectionConfig , true , false );
201184
202- String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
185+ String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
203186
204- assertEquals (out , "" );
205- verify (connectionConfig ).setUsername ("bob" );
206- verify (shell , times (2 )).connect (connectionConfig );
207- } finally {
208- System .setIn (stdIn );
209- System .setOut (stdOut );
210- }
187+ assertEquals ( out , "" );
188+ verify ( connectionConfig ).setUsername ( "bob" );
189+ verify ( shell , times ( 2 ) ).connect ( connectionConfig );
211190 }
212191
213192 @ Test
@@ -232,42 +211,28 @@ public void promptsForPassBeforeConnectIfUserExistsIfInteractive() throws Except
232211
233212 @ Test
234213 public void promptsSilentlyForPassIfUserExistsIfOutputRedirected () throws Exception {
235- <<<<<<< HEAD
236- doThrow (authException ).doNothing ().when (shell ).connect (connectionConfig );
237- =======
238214 if (Utils .isWindows ()) {
239215 // Disable this test on Windows due to problem with redirection
240216 return ;
241217 }
242218
243- >>>>>>> upstream /1.1
244- doReturn ("bob" ).when (connectionConfig ).username ();
219+ doThrow (authException ).doNothing ().when (shell ).connect (connectionConfig );
245220
246- String inputString = "secret \n " ;
221+ String inputString = "bob \n secret \n " ;
247222 InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
248223
249224 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
250- PrintStream ps = new PrintStream (baos );
225+ PrintStream ps = new PrintStream ( baos );
251226
252- // Redirect stdin and stdout
253- InputStream stdIn = System .in ;
254- PrintStream stdOut = System .out ;
255- System .setIn (inputStream );
256- System .setOut (ps );
227+ Main main = new Main ( inputStream , ps );
228+ main .connectMaybeInteractively ( shell , connectionConfig , true , false );
257229
258- try {
259- Main main = new Main ();
260- main .connectMaybeInteractively (shell , connectionConfig , true , false );
261-
262- String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
230+ String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
263231
264- assertEquals (out , "" );
265- verify (connectionConfig ).setPassword ("secret" );
266- verify (shell , times (1 )).connect (connectionConfig );
267- } finally {
268- System .setIn (stdIn );
269- System .setOut (stdOut );
270- }
232+ assertEquals ( "" , out );
233+ verify ( connectionConfig ).setUsername ( "bob" );
234+ verify ( connectionConfig ).setPassword ( "secret" );
235+ verify ( shell , times ( 2 ) ).connect ( connectionConfig );
271236 }
272237
273238 @ Test
@@ -347,28 +312,17 @@ public void doesNotRepromptIfUserIsNotProvidedIfOutputRedirected() throws Except
347312 InputStream inputStream = new ByteArrayInputStream (inputString .getBytes ());
348313
349314 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
350- PrintStream ps = new PrintStream (baos );
315+ PrintStream ps = new PrintStream ( baos );
316+ Main main = new Main ( inputStream , ps );
317+ main .connectMaybeInteractively ( shell , connectionConfig , true , false );
351318
352- // Redirect stdin and stdout
353- InputStream stdIn = System .in ;
354- PrintStream stdOut = System .out ;
355- System .setIn (inputStream );
356- System .setOut (ps );
319+ String out = new String ( baos .toByteArray (), StandardCharsets .UTF_8 );
320+
321+ assertEquals ( "" , out );
322+ verify ( connectionConfig ).setUsername ( "" );
323+ verify ( connectionConfig ).setPassword ( "secret" );
324+ verify ( shell , times ( 2 ) ).connect ( connectionConfig );
357325
358- try {
359- Main main = new Main ();
360- main .connectMaybeInteractively (shell , connectionConfig , true , false );
361-
362- String out = new String (baos .toByteArray (), StandardCharsets .UTF_8 );
363-
364- assertEquals ("" , out );
365- verify (connectionConfig ).setUsername ("" );
366- verify (connectionConfig ).setPassword ("secret" );
367- verify (shell , times (2 )).connect (connectionConfig );
368- } finally {
369- System .setIn (stdIn );
370- System .setOut (stdOut );
371- }
372326 }
373327
374328 @ Test
0 commit comments