|
7 | 7 |
|
8 | 8 | import java.io.ByteArrayInputStream; |
9 | 9 | import java.io.ByteArrayOutputStream; |
10 | | -import java.io.IOException; |
| 10 | +import java.io.FileNotFoundException; |
11 | 11 | import java.io.InputStream; |
12 | 12 | import java.io.PrintStream; |
13 | 13 |
|
| 14 | +import org.neo4j.driver.exceptions.ClientException; |
14 | 15 | import org.neo4j.driver.exceptions.ServiceUnavailableException; |
15 | 16 | import org.neo4j.shell.cli.CliArgs; |
16 | 17 | import org.neo4j.shell.log.AnsiLogger; |
|
20 | 21 | import org.neo4j.shell.prettyprint.ToStringLinePrinter; |
21 | 22 |
|
22 | 23 | import static java.lang.String.format; |
23 | | -import static org.hamcrest.MatcherAssert.assertThat; |
24 | 24 | import static org.junit.Assert.assertEquals; |
25 | 25 | import static org.junit.Assert.assertTrue; |
| 26 | +import static org.mockito.Matchers.any; |
26 | 27 | import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
27 | 30 |
|
28 | 31 | public class MainIntegrationTest |
29 | 32 | { |
@@ -140,38 +143,65 @@ public void wrongPortWithNeo4j() throws Exception |
140 | 143 | } |
141 | 144 |
|
142 | 145 | @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 | + } |
147 | 149 |
|
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 | + } |
156 | 154 |
|
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"))); |
159 | 160 | } |
160 | 161 |
|
161 | 162 | @Test |
162 | 163 | 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 | + |
166 | 175 |
|
167 | 176 | // 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 ); |
169 | 197 | 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(); |
171 | 202 |
|
172 | | - // expect |
173 | | - exception.expect( IOException.class); |
174 | | - ShellRunner.getShellRunner(cliArgs, shell, mock(Logger.class), sac.connectionConfig ); |
| 203 | + // then |
| 204 | + return linePrinter.result(); |
175 | 205 | } |
176 | 206 |
|
177 | 207 | private String fileFromResource(String filename) |
|
0 commit comments