@Test public void testTryExecute() throws IOException { SshConfig config = new SshConfig("localhost", "me"); Session session = mock(Session.class); when(client.startSession()).thenReturn(session); Command command = mock(Command.class); when(session.exec("\"test command\" \"param\"")).thenReturn(command); when(command.getExitStatus()).thenReturn(1); when(command.getInputStream()).thenReturn(IOUtils.toInputStream("output\n")); when(command.getErrorStream()).thenReturn(IOUtils.toInputStream("error line\n")); @SuppressWarnings("resource") ExecutionResult result = new SshConnection(config, client).tryExecute( "test command", "param"); verify(command).join(); assertThat(result.getOutput(), is("output\n")); assertThat(result.getErrorOutput(), is("error line\n")); assertThat(result.getExitCode(), is(1)); }
@Override public ExecutionResult tryExecute(Path workingDirectory, String... cmd) throws IOException { String workdir = workingDirectory == null ? null : workingDirectory.toString(); return tryExecuteIn(workdir, cmd); }
SshConnection implements AutoCloseable, CommandExecutor { @Override public ExecutionResult tryExecute(Path workingDirectory, String... cmd) throws IOException { String workdir = workingDirectory == null ? null : workingDirectory.toString(); return tryExecuteIn(workdir, cmd); } }
SshConnection implements AutoCloseable, CommandExecutor { @Override public ExecutionResult tryExecute(Path workingDirectory, String... cmd) throws IOException { String workdir = workingDirectory == null ? null : workingDirectory.toString(); return tryExecuteIn(workdir, cmd); } SshConnection(SshConfig config, SSHClient client); }
SshConnection implements AutoCloseable, CommandExecutor { @Override public ExecutionResult tryExecute(Path workingDirectory, String... cmd) throws IOException { String workdir = workingDirectory == null ? null : workingDirectory.toString(); return tryExecuteIn(workdir, cmd); } SshConnection(SshConfig config, SSHClient client); SshConfig getConfig(); InteractiveSession startShell(); @Override void close(); @Override ExecutionResult tryExecute(Path workingDirectory, String... cmd); ExecutionResult executeIn(String workingDirectory, String... cmd); ExecutionResult tryExecuteIn(String workingDirectory, String... cmd); void uploadFile(Path localFile, String targetPath); void writeTextFile(String targetPath, String contents); String readTextFile(String remotePath); void downloadFile(String remotePath, Path localPath); }
SshConnection implements AutoCloseable, CommandExecutor { @Override public ExecutionResult tryExecute(Path workingDirectory, String... cmd) throws IOException { String workdir = workingDirectory == null ? null : workingDirectory.toString(); return tryExecuteIn(workdir, cmd); } SshConnection(SshConfig config, SSHClient client); SshConfig getConfig(); InteractiveSession startShell(); @Override void close(); @Override ExecutionResult tryExecute(Path workingDirectory, String... cmd); ExecutionResult executeIn(String workingDirectory, String... cmd); ExecutionResult tryExecuteIn(String workingDirectory, String... cmd); void uploadFile(Path localFile, String targetPath); void writeTextFile(String targetPath, String contents); String readTextFile(String remotePath); void downloadFile(String remotePath, Path localPath);  }