Search in sources :

Example 6 with ShellStatementParser

use of org.neo4j.shell.parser.ShellStatementParser in project neo4j by neo4j.

the class InteractiveShellRunnerTest method testSignalHandleDuringExecution.

@Test
public void testSignalHandleDuringExecution() throws Exception {
    // given
    BoltStateHandler boltStateHandler = mock(BoltStateHandler.class);
    FakeInterruptableShell fakeShell = spy(new FakeInterruptableShell(logger, boltStateHandler));
    InputStream inputStream = new ByteArrayInputStream("RETURN 1;\n".getBytes());
    InteractiveShellRunner runner = new InteractiveShellRunner(fakeShell, fakeShell, fakeShell, logger, new ShellStatementParser(), inputStream, historyFile, userMessagesHandler, connectionConfig);
    // during
    Thread t = new Thread(runner::runUntilEnd);
    t.start();
    // wait until execution has begun
    while (!t.getState().equals(Thread.State.TIMED_WAITING)) {
        Thread.sleep(100L);
    }
    // when
    runner.handle(new Signal(InteractiveShellRunner.INTERRUPT_SIGNAL));
    // then
    verify(fakeShell).execute("RETURN 1;");
    verify(fakeShell).reset();
    verify(boltStateHandler).reset();
}
Also used : ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) Signal(sun.misc.Signal) ByteArrayInputStream(java.io.ByteArrayInputStream) BoltStateHandler(org.neo4j.shell.state.BoltStateHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 7 with ShellStatementParser

use of org.neo4j.shell.parser.ShellStatementParser in project neo4j by neo4j.

the class InteractiveShellRunnerTest method emptyLineIsIgnored.

@Test
public void emptyLineIsIgnored() throws Exception {
    // given
    String inputString = "     \nCREATE (n:Person) RETURN n;\n";
    InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
    InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, databaseManager, logger, new ShellStatementParser(), inputStream, historyFile, userMessagesHandler, connectionConfig);
    // when
    List<String> statements = runner.readUntilStatement();
    // then
    assertEquals(1, statements.size());
    assertThat(statements.get(0), is("CREATE (n:Person) RETURN n;"));
}
Also used : ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 8 with ShellStatementParser

use of org.neo4j.shell.parser.ShellStatementParser in project neo4j by neo4j.

the class InteractiveShellRunnerTest method setupInteractiveTestShellRunner.

private TestInteractiveShellRunner setupInteractiveTestShellRunner(String input) throws Exception {
    // NOTE: Tests using this will test a bit more of the stack using OfflineTestShell
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    BoltStateHandler mockedBoltStateHandler = mock(BoltStateHandler.class);
    when(mockedBoltStateHandler.getProtocolVersion()).thenReturn("");
    final PrettyPrinter mockedPrettyPrinter = mock(PrettyPrinter.class);
    Logger logger = new AnsiLogger(false, Format.VERBOSE, new PrintStream(output), new PrintStream(error));
    OfflineTestShell offlineTestShell = new OfflineTestShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
    CommandHelper commandHelper = new CommandHelper(logger, Historian.empty, offlineTestShell);
    offlineTestShell.setCommandHelper(commandHelper);
    InputStream inputStream = new ByteArrayInputStream(input.getBytes());
    InteractiveShellRunner runner = new InteractiveShellRunner(offlineTestShell, offlineTestShell, offlineTestShell, logger, new ShellStatementParser(), inputStream, historyFile, userMessagesHandler, connectionConfig);
    return new TestInteractiveShellRunner(runner, output, error, mockedBoltStateHandler);
}
Also used : PrintStream(java.io.PrintStream) ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OfflineTestShell(org.neo4j.shell.OfflineTestShell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(org.neo4j.shell.log.Logger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) CommandHelper(org.neo4j.shell.commands.CommandHelper) PrettyPrinter(org.neo4j.shell.prettyprint.PrettyPrinter) ByteArrayInputStream(java.io.ByteArrayInputStream) BoltStateHandler(org.neo4j.shell.state.BoltStateHandler)

Example 9 with ShellStatementParser

use of org.neo4j.shell.parser.ShellStatementParser in project neo4j by neo4j.

the class InteractiveShellRunnerTest method emptyStringThrowsNoMoreInput.

@Test
public void emptyStringThrowsNoMoreInput() throws Exception {
    // then
    thrown.expect(NoMoreInputException.class);
    // given
    String inputString = "";
    InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
    InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, databaseManager, logger, new ShellStatementParser(), inputStream, historyFile, userMessagesHandler, connectionConfig);
    // when
    runner.readUntilStatement();
}
Also used : ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 10 with ShellStatementParser

use of org.neo4j.shell.parser.ShellStatementParser in project neo4j by neo4j.

the class InteractiveShellRunnerTest method setup.

@Before
public void setup() throws Exception {
    statementParser = new ShellStatementParser();
    logger = mock(Logger.class);
    cmdExecuter = mock(StatementExecuter.class);
    txHandler = mock(TransactionHandler.class);
    databaseManager = mock(DatabaseManager.class);
    connectionConfig = mock(ConnectionConfig.class);
    historyFile = temp.newFile();
    badLineError = new ClientException("Found a bad line");
    userMessagesHandler = mock(UserMessagesHandler.class);
    when(databaseManager.getActualDatabaseAsReportedByServer()).thenReturn("mydb");
    when(userMessagesHandler.getWelcomeMessage()).thenReturn("Welcome to cypher-shell!");
    when(userMessagesHandler.getExitMessage()).thenReturn("Exit message");
    when(connectionConfig.username()).thenReturn("myusername");
    doThrow(badLineError).when(cmdExecuter).execute(contains("bad"));
    doReturn(System.out).when(logger).getOutputStream();
}
Also used : ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) StatementExecuter(org.neo4j.shell.StatementExecuter) DatabaseManager(org.neo4j.shell.DatabaseManager) TransactionHandler(org.neo4j.shell.TransactionHandler) ClientException(org.neo4j.driver.exceptions.ClientException) Logger(org.neo4j.shell.log.Logger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) ConnectionConfig(org.neo4j.shell.ConnectionConfig) UserMessagesHandler(org.neo4j.shell.UserMessagesHandler) Before(org.junit.Before)

Aggregations

ShellStatementParser (org.neo4j.shell.parser.ShellStatementParser)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 Test (org.junit.Test)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 Before (org.junit.Before)2 ClientException (org.neo4j.driver.exceptions.ClientException)2 AnsiLogger (org.neo4j.shell.log.AnsiLogger)2 Logger (org.neo4j.shell.log.Logger)2 BoltStateHandler (org.neo4j.shell.state.BoltStateHandler)2 Signal (sun.misc.Signal)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 ConnectionConfig (org.neo4j.shell.ConnectionConfig)1 DatabaseManager (org.neo4j.shell.DatabaseManager)1 OfflineTestShell (org.neo4j.shell.OfflineTestShell)1 StatementExecuter (org.neo4j.shell.StatementExecuter)1 TransactionHandler (org.neo4j.shell.TransactionHandler)1 UserMessagesHandler (org.neo4j.shell.UserMessagesHandler)1 CommandHelper (org.neo4j.shell.commands.CommandHelper)1