ExecuteLastQueryCommand extends QueryCommand { @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; } String outputFileName = null; if (state.getInput().contains(" > ")) { final int index = state.getInput().lastIndexOf(" > "); outputFileName = state.getInput().substring(index + 3); } if (state.getLastQuery() != null || state.getLastQuery().trim().length() > 0) { executeQuery(reader, out, state, state.getLastQuery(), outputFileName); } else { out.println("there is no query at buffer."); } out.flush(); state.setInput(null); state.clearBuffer(); }  @Override boolean accept(final ConsoleState state); @Override void execute(final ConsoleReader reader,
                         final PrintWriter out,
                         final ConsoleState state); @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 testInvalidQueryWithTargetError() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", "sample").from(GraphLocation.SERVER)); state .setLastQuery("define target org.openspotlight.graph.query.console.test.domain.JavaInterface select * by link JavaTypeMethod (b);"); state.setInput("/ > 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(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state .setLastQuery("select ** where org.openspotlight.graph.query.console.test.domain.JavaInterface property caption == $var;"); state.setInput("/ > 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 testValidParameter() throws SLException, IOException, ClassNotFoundException { final GraphConnection graphConnection = new GraphConnection(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setLastQuery("select *;"); state.setInput("/"); 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(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setLastQuery("select *;"); state.setInput("/ > 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(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(); state = new ConsoleState(graphConnection.connect("localhost", 7070, "sa", "sa", DEFAULT_REPOSITORY_NAME).from( GraphLocation.SERVER)); state.setLastQuery("select *?*;"); state.setInput("/ > 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)); }