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

Commit f1569ed

Browse files
committed
Code Review: More testing
- empty file - file with multiple statements - file with invalid Cypher
1 parent b84a0c2 commit f1569ed

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

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

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
import java.io.ByteArrayInputStream;
99
import java.io.ByteArrayOutputStream;
10-
import java.io.IOException;
10+
import java.io.FileNotFoundException;
1111
import java.io.InputStream;
1212
import java.io.PrintStream;
1313

14+
import org.neo4j.driver.exceptions.ClientException;
1415
import org.neo4j.driver.exceptions.ServiceUnavailableException;
1516
import org.neo4j.shell.cli.CliArgs;
1617
import org.neo4j.shell.log.AnsiLogger;
@@ -20,10 +21,12 @@
2021
import org.neo4j.shell.prettyprint.ToStringLinePrinter;
2122

2223
import static java.lang.String.format;
23-
import static org.hamcrest.MatcherAssert.assertThat;
2424
import static org.junit.Assert.assertEquals;
2525
import static org.junit.Assert.assertTrue;
26+
import static org.mockito.Matchers.any;
2627
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.verify;
29+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2730

2831
public class MainIntegrationTest
2932
{
@@ -140,38 +143,65 @@ public void wrongPortWithNeo4j() throws Exception
140143
}
141144

142145
@Test
143-
public void shouldReadCypherStatementsFromFile() throws Exception {
144-
// given
145-
CliArgs cliArgs = new CliArgs();
146-
cliArgs.setInputFilename( fileFromResource("test.cypher") );
146+
public void shouldReadSingleCypherStatementsFromFile() throws Exception {
147+
assertEquals(format( "result%n42%n" ), executeFileNonInteractively(fileFromResource("single.cypher")));
148+
}
147149

148-
// when
149-
ToStringLinePrinter linePrinter = new ToStringLinePrinter();
150-
ShellAndConnection sac = getShell( cliArgs, linePrinter );
151-
CypherShell shell = sac.shell;
152-
ConnectionConfig connectionConfig = sac.connectionConfig;
153-
main.connectMaybeInteractively( shell, connectionConfig, true, true );
154-
ShellRunner shellRunner = ShellRunner.getShellRunner(cliArgs, shell, mock(Logger.class), connectionConfig);
155-
shellRunner.runUntilEnd();
150+
@Test
151+
public void shouldReadEmptyCypherStatementsFile() throws Exception {
152+
assertEquals("", executeFileNonInteractively(fileFromResource("empty.cypher")));
153+
}
156154

157-
// then
158-
assertEquals( format("result%n42%n"), linePrinter.result() );
155+
@Test
156+
public void shouldReadMultipleCypherStatementsFromFile() throws Exception {
157+
assertEquals(format( "result%n42%n" +
158+
"result%n1337%n" +
159+
"result%n\"done\"%n"), executeFileNonInteractively(fileFromResource("multiple.cypher")));
159160
}
160161

161162
@Test
162163
public void shouldFailIfInputFileDoesntExist() throws Exception {
163-
// given
164-
CliArgs cliArgs = new CliArgs();
165-
cliArgs.setInputFilename( "what.cypher" );
164+
// expect
165+
exception.expect( FileNotFoundException.class);
166+
exception.expectMessage( "what.cypher (No such file or directory)" );
167+
executeFileNonInteractively("what.cypher");
168+
}
169+
170+
@Test
171+
public void shouldHandleInvalidCypherFromFile() throws Exception {
172+
//given
173+
Logger logger = mock(Logger.class);
174+
166175

167176
// when
168-
ShellAndConnection sac = getShell( cliArgs );
177+
String actual = executeFileNonInteractively( fileFromResource( "invalid.cypher" ), logger);
178+
179+
//then we print the first valid row
180+
assertEquals( format( "result%n42%n" ), actual );
181+
//and print errors to the error log
182+
verify(logger).printError(any( ClientException.class ));
183+
verifyNoMoreInteractions(logger);
184+
}
185+
186+
private String executeFileNonInteractively(String filename) throws Exception {
187+
return executeFileNonInteractively(filename, mock(Logger.class));
188+
}
189+
190+
private String executeFileNonInteractively(String filename, Logger logger) throws Exception
191+
{
192+
CliArgs cliArgs = new CliArgs();
193+
cliArgs.setInputFilename(filename);
194+
195+
ToStringLinePrinter linePrinter = new ToStringLinePrinter();
196+
ShellAndConnection sac = getShell( cliArgs, linePrinter );
169197
CypherShell shell = sac.shell;
170-
main.connectMaybeInteractively( shell, sac.connectionConfig, true, true );
198+
ConnectionConfig connectionConfig = sac.connectionConfig;
199+
main.connectMaybeInteractively( shell, connectionConfig, true, true );
200+
ShellRunner shellRunner = ShellRunner.getShellRunner(cliArgs, shell, logger, connectionConfig);
201+
shellRunner.runUntilEnd();
171202

172-
// expect
173-
exception.expect( IOException.class);
174-
ShellRunner.getShellRunner(cliArgs, shell, mock(Logger.class), sac.connectionConfig );
203+
// then
204+
return linePrinter.result();
175205
}
176206

177207
private String fileFromResource(String filename)

cypher-shell/src/integration-test/resources/empty.cypher

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RETURN 42 AS result;
2+
THIS IS NOT CYPHER;
3+
RETURN 1337 AS result;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//do this first
2+
RETURN 42 AS result;
3+
//then we do
4+
RETURN 1337 AS result;
5+
//and finally we do
6+
RETURN "done" AS result;

0 commit comments

Comments
 (0)