Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 79a62aa

Browse files
committed
More test failures
1 parent 1677827 commit 79a62aa

File tree

3 files changed

+111
-59
lines changed

3 files changed

+111
-59
lines changed

cypher-shell/src/integration-test/java/org/neo4j/shell/MainIntegrationTest.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,42 @@ public void promptsOnWrongAuthenticationIfInteractive() throws Exception {
8989
// should be connected
9090
assertTrue(shell.isConnected());
9191
// should have prompted and set the username and password
92-
assertEquals( format( "username: neo4j%npassword: ***%n" ), baos.toString() );
92+
assertEquals(format("username: neo4j%npassword: ***%n"), baos.toString());
9393
assertEquals("neo4j", connectionConfig.username());
9494
assertEquals("neo", connectionConfig.password());
95-
96-
String out = baos.toString();
97-
assertEquals(String.format( "username: neo4j%npassword: ***%n" ), out);
9895
}
9996

10097
@Test
101-
public void doesNotPromptToStdOutOnWrongAuthenticationIfOutputRedirected() throws Exception
102-
{
98+
public void doesNotPromptToStdOutOnWrongAuthenticationIfOutputRedirected() throws Exception {
10399
// when
104-
assertEquals( "", connectionConfig.username() );
105-
assertEquals( "", connectionConfig.password() );
106-
107-
// Create a Main with the standard in and out
108-
Main realMain = new Main( inputStream, printStream );
109-
realMain.connectMaybeInteractively( shell, connectionConfig, true, false );
100+
assertEquals("", connectionConfig.username());
101+
assertEquals("", connectionConfig.password());
110102

111-
// then
112-
// should be connected
113-
assertTrue( shell.isConnected() );
114-
// should have prompted silently and set the username and password
115-
assertEquals( "neo4j", connectionConfig.username() );
116-
assertEquals( "neo", connectionConfig.password() );
103+
// Redirect System.in and System.out
104+
InputStream stdIn = System.in;
105+
PrintStream stdOut = System.out;
106+
System.setIn(inputStream);
107+
System.setOut(printStream);
117108

118-
String out = baos.toString();
119-
assertEquals( "", out );
109+
// Create a Main with the standard in and out
110+
try {
111+
Main realMain = new Main();
112+
realMain.connectMaybeInteractively(shell, connectionConfig, true, false);
113+
114+
// then
115+
// should be connected
116+
assertTrue(shell.isConnected());
117+
// should have prompted silently and set the username and password
118+
assertEquals("neo4j", connectionConfig.username());
119+
assertEquals("neo", connectionConfig.password());
120+
121+
String out = baos.toString();
122+
assertEquals("", out);
123+
} finally {
124+
// Restore in and out
125+
System.setIn(stdIn);
126+
System.setOut(stdOut);
127+
}
120128
}
121129

122130
@Test

cypher-shell/src/main/java/org/neo4j/shell/Main.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import jline.console.ConsoleReader;
44

55
import java.io.InputStream;
6+
import java.io.OutputStream;
67
import java.io.PrintStream;
78
import javax.annotation.Nonnull;
89
import javax.annotation.Nullable;
@@ -17,8 +18,6 @@
1718
import org.neo4j.shell.log.Logger;
1819
import org.neo4j.shell.prettyprint.PrettyConfig;
1920

20-
import java.io.OutputStream;
21-
2221
import static org.neo4j.shell.ShellRunner.isInputInteractive;
2322
import static org.neo4j.shell.ShellRunner.isOutputInteractive;
2423

@@ -41,7 +40,7 @@ public static void main(String[] args) {
4140
main.startShell(cliArgs);
4241
}
4342

44-
private Main() {
43+
Main() {
4544
this(System.in, System.out, false);
4645
}
4746

cypher-shell/src/test/java/org/neo4j/shell/MainTest.java

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nsecret\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\nsecret\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

Comments
 (0)