@SuppressWarnings("resource") @Test public void testTryExecuteIn() throws IOException { Session session = mock(Session.class); when(client.startSession()).thenReturn(session); Command command = mock(Command.class); when(session.exec("\"cd\" \"/test/dir\" && \"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")); SshConfig config = new SshConfig("localhost", "me"); new SshConnection(config, client) .tryExecuteIn("/test/dir", "test command", "param"); }
public ExecutionResult tryExecuteIn(String workingDirectory, String... cmd) throws IOException { String cmdString = escapeCommand(cmd); if (workingDirectory != null) { cmdString = escapeCommand("cd", workingDirectory) + " && " + cmdString; } try (Command command = client.startSession().exec(cmdString)) { command.join(); String stdout = IOUtils.toString(command.getInputStream()); String stderr = IOUtils.toString(command.getErrorStream()); Integer exitCode = command.getExitStatus(); if (exitCode == null) { throw new IOException("Failed to get exit status"); } return new ExecutionResult(exitCode, stdout, stderr); } }
SshConnection implements AutoCloseable, CommandExecutor { public ExecutionResult tryExecuteIn(String workingDirectory, String... cmd) throws IOException { String cmdString = escapeCommand(cmd); if (workingDirectory != null) { cmdString = escapeCommand("cd", workingDirectory) + " && " + cmdString; } try (Command command = client.startSession().exec(cmdString)) { command.join(); String stdout = IOUtils.toString(command.getInputStream()); String stderr = IOUtils.toString(command.getErrorStream()); Integer exitCode = command.getExitStatus(); if (exitCode == null) { throw new IOException("Failed to get exit status"); } return new ExecutionResult(exitCode, stdout, stderr); } } }
SshConnection implements AutoCloseable, CommandExecutor { public ExecutionResult tryExecuteIn(String workingDirectory, String... cmd) throws IOException { String cmdString = escapeCommand(cmd); if (workingDirectory != null) { cmdString = escapeCommand("cd", workingDirectory) + " && " + cmdString; } try (Command command = client.startSession().exec(cmdString)) { command.join(); String stdout = IOUtils.toString(command.getInputStream()); String stderr = IOUtils.toString(command.getErrorStream()); Integer exitCode = command.getExitStatus(); if (exitCode == null) { throw new IOException("Failed to get exit status"); } return new ExecutionResult(exitCode, stdout, stderr); } } SshConnection(SshConfig config, SSHClient client); }
SshConnection implements AutoCloseable, CommandExecutor { public ExecutionResult tryExecuteIn(String workingDirectory, String... cmd) throws IOException { String cmdString = escapeCommand(cmd); if (workingDirectory != null) { cmdString = escapeCommand("cd", workingDirectory) + " && " + cmdString; } try (Command command = client.startSession().exec(cmdString)) { command.join(); String stdout = IOUtils.toString(command.getInputStream()); String stderr = IOUtils.toString(command.getErrorStream()); Integer exitCode = command.getExitStatus(); if (exitCode == null) { throw new IOException("Failed to get exit status"); } return new ExecutionResult(exitCode, stdout, stderr); } } 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 { public ExecutionResult tryExecuteIn(String workingDirectory, String... cmd) throws IOException { String cmdString = escapeCommand(cmd); if (workingDirectory != null) { cmdString = escapeCommand("cd", workingDirectory) + " && " + cmdString; } try (Command command = client.startSession().exec(cmdString)) { command.join(); String stdout = IOUtils.toString(command.getInputStream()); String stderr = IOUtils.toString(command.getErrorStream()); Integer exitCode = command.getExitStatus(); if (exitCode == null) { throw new IOException("Failed to get exit status"); } return new ExecutionResult(exitCode, stdout, stderr); } } 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);  }