OpenFileCommand 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(3).trim(); final File file = new File(fileName); if (file.exists() && file.isFile()) { try { state.clearBuffer(); final LineNumberReader fileReader = new LineNumberReader(new FileReader(file)); while (fileReader.ready()) { state.appendLineBuffer(fileReader.readLine()); } state.setLastQuery(state.getBuffer()); state.clearBuffer(); out.println("query loaded into buffer."); } catch (final Exception e) { state.clearBuffer(); 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("get filename.slql"); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery().length(), is(0)); assertThat(state.getInput(), is("")); }
@Test public void testValidParameter2() { final ConsoleState state = new ConsoleState(null); state.setInput("get out.slql"); state.appendBuffer("something"); command.execute(reader, out, state); assertThat(state.getBuffer().length(), is(0)); assertThat(state.getLastQuery(), is("select *;\n")); assertThat(state.getInput(), is("")); }