QueryCommand implements DynamicCommand { @Override public void execute(final ConsoleReader reader, final PrintWriter out, final ConsoleState state) { Assertions.checkNotNull("reader", reader); Assertions.checkNotNull("out", out); Assertions.checkNotNull("state", state); if (!accept(state)) { return; } if (state.getInput().endsWith(";") || state.getInput().contains("; > ")) { if (state.getActiveCommand() != null) { state.appendBuffer(state.getInput()); state.setInput(state.getBuffer()); } String lastQuery = ""; String outputFileName = null; if (state.getInput().contains("; > ")) { final int index = state.getInput().lastIndexOf("; > "); lastQuery = state.getInput().substring(0, index + 1); outputFileName = state.getInput().substring(index + 4); } else { lastQuery = state.getInput(); } executeQuery(reader, out, state, lastQuery, outputFileName); state.setLastQuery(lastQuery); state.clearBuffer(); state.setActiveCommand(null); } else if (state.getInput().contains(";") || state.getInput().contains("; >")) { out.println("invalid statement"); out.flush(); state.clearBuffer(); state.setActiveCommand(null); } else { if (state.getActiveCommand() == null) { state.clearBuffer(); state.setActiveCommand(this); } state.appendLineBuffer(state.getInput()); } state.setInput(null); }  @Override boolean accept(final ConsoleState state); @Override void execute(final ConsoleReader reader, final PrintWriter out,
                        final ConsoleState state); @Override String getAutoCompleteCommand(); @Override String getCommand(); @Override String getDescription(); @Override String getFileCompletionCommand(); @Override FileCompletionMode getFileCompletionMode(); @Override boolean hasFileCompletion();  }
@Test(expected = IllegalArgumentException.class) public void testExecuteNull() { command.execute(null, null, null); }
@Test public void testInvalidMultiLineQueryWithTargetError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("define "); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getLastQuery(), is("")); assertThat(state.getBuffer(), is("define \n")); assertThat(state.getActiveCommand(), is(notNullValue())); state .setInput("target org.openspotlight.graph.query.console.test.domain.JavaInterface select * by link JavaTypeMethod (b); > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat( state.getLastQuery(), is("define \ntarget org.openspotlight.graph.query.console.test.domain.JavaInterface select * by link JavaTypeMethod (b);")); assertThat(state.getActiveCommand(), is(nullValue())); final File generatedFile = new File("out.txt"); assertThat(generatedFile.exists(), is(false)); }
@Test public void testInvalidMultiLineQueryWithVariablesError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select "); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getLastQuery(), is("")); assertThat(state.getBuffer(), is("select \n")); assertThat(state.getActiveCommand(), is(notNullValue())); state .setInput("** where org.openspotlight.graph.query.console.test.domain.JavaInterface property caption == $var; > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat( state.getLastQuery(), is("select \n** where org.openspotlight.graph.query.console.test.domain.JavaInterface property caption == $var;")); assertThat(state.getActiveCommand(), is(nullValue())); final File generatedFile = new File("out.txt"); assertThat(generatedFile.exists(), is(false)); }
@Test public void testInvalidQueryWithTargetError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state .setInput("define target org.openspotlight.graph.query.console.test.domain.JavaInterface select * by link JavaTypeMethod (b); > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat( state.getLastQuery(), is("define target org.openspotlight.graph.query.console.test.domain.JavaInterface select * by link JavaTypeMethod (b);")); final File generatedFile = new File("out.txt"); assertThat(generatedFile.exists(), is(false)); }
@Test public void testInvalidQueryWithVariablesError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state .setInput("select ** where org.openspotlight.graph.query.console.test.domain.JavaInterface property caption == $var; > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat( state.getLastQuery(), is("select ** where org.openspotlight.graph.query.console.test.domain.JavaInterface property caption == $var;")); final File generatedFile = new File("out.txt"); assertThat(generatedFile.exists(), is(false)); }
@Test public void testValidMultiLineParameter() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select "); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getLastQuery(), is("")); assertThat(state.getBuffer(), is("select \n")); assertThat(state.getActiveCommand(), is(notNullValue())); state.setInput("*;"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select \n*;")); assertThat(state.getActiveCommand(), is(nullValue())); }
@Test public void testValidMultiLineParameter2() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select "); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getLastQuery(), is("")); assertThat(state.getBuffer(), is("select \n")); assertThat(state.getActiveCommand(), is(notNullValue())); state.setInput("*; > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select \n*;")); assertThat(state.getActiveCommand(), is(nullValue())); final File generatedFile = new File("out.txt"); assertThat(generatedFile.isFile(), is(true)); final String fileContent = getFileContent(generatedFile); assertThat(fileContent, is(is(notNullValue()))); assertThat(fileContent.length(), is(not(0))); }
@Test public void testValidMultiLineParameterSyntaxError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select "); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getLastQuery(), is("")); assertThat(state.getBuffer(), is("select \n")); assertThat(state.getActiveCommand(), is(notNullValue())); state.setInput("*?*; > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select \n*?*;")); assertThat(state.getActiveCommand(), is(nullValue())); final File generatedFile = new File("out.txt"); assertThat(generatedFile.exists(), is(false)); }
@Test public void testValidParameter() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select *;"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select *;")); }
@Test public void testValidParameter2() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select *; > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select *;")); final File generatedFile = new File("out.txt"); assertThat(generatedFile.isFile(), is(true)); final String fileContent = getFileContent(generatedFile); assertThat(fileContent, is(notNullValue())); assertThat(fileContent.length(), is(not(0))); }
@Test public void testValidParameterSyntaxError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); command = new QueryCommand(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setInput("select *?*; > out.txt"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select *?*;")); final File generatedFile = new File("out.txt"); assertThat(generatedFile.exists(), is(false)); }