SaveQueryCommand 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; } final String fileName = state.getInput().substring(4).trim(); final File outputFile = new File(fileName); if (!outputFile.isDirectory()) { try { outputFile.createNewFile(); final PrintWriter fileOut = new PrintWriter(outputFile); fileOut.append(state.getLastQuery()); fileOut.flush(); fileOut.close(); out.println("query saved."); } catch (final IOException e) { out.print("ERROR: "); out.println(e.getMessage()); } } else { out.print("ERROR: "); out.println("Invalid file name."); } 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 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 testValidParameter() { final ConsoleState state = new ConsoleState(null); state.setInput("save out.slql"); state.setLastQuery("select *;"); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); final File generatedFile = new File("out.slql"); assertThat(generatedFile.isFile(), is(true)); assertThat(getFileContent(generatedFile), is("select *;\n")); assertThat(state.getLastQuery(), is("select *;")); assertThat(state.getInput(), is("")); }