ShellCommandUtil { public static Result runCommand(List<String> args, Map<String, String> vars, InteractiveHandler interactiveHandler, boolean sudo) throws IOException, InterruptedException { return runCommand(args.toArray(new String[args.size()]), vars, interactiveHandler, sudo); }  static void logOpenSslExitCode(String command, int exitCode); static String hideOpenSslPassword(String command); static String getOpenSslCommandResult(String command, int exitCode); static String getUnixFilePermissions(String path); static void setUnixFilePermissions(String mode, String path); static Result setFileOwner(String path, String ownerName); static Result setFileGroup(String path,  String groupName); static Result setFileMode(String path,
                                   boolean ownerReadable, boolean ownerWritable, boolean ownerExecutable,
                                   boolean groupReadable, boolean groupWritable, boolean groupExecutable,
                                   boolean otherReadable, boolean otherWritable, boolean otherExecutable); static Result pathExists(String path, boolean sudo); static Result mkdir(String directoryPath, boolean sudo); static Result copyFile(String srcFile, String destFile, boolean force, boolean sudo); static Result delete(String file, boolean force, boolean sudo); static Result runCommand(List<String> args, Map<String, String> vars, InteractiveHandler interactiveHandler, boolean sudo); static Result runCommand(String[] args, Map<String, String> vars, InteractiveHandler interactiveHandler, boolean sudo); static Result runCommand(String[] args, Map<String, String> vars); static Result runCommand(String[] args); static final boolean WINDOWS; static final boolean LINUX; static final boolean MAC; static final boolean UNIX_LIKE; static final String MASK_OWNER_ONLY_RW; static final String MASK_OWNER_ONLY_RWX; static final String MASK_EVERYBODY_RWX; }
@Test public void testRunCommand() throws Exception { ShellCommandUtil.Result result; if (ShellCommandUtil.LINUX) { result = ShellCommandUtil. runCommand(new String [] {"echo", "dummy"}); Assert.assertEquals(0, result.getExitCode()); Assert.assertEquals("dummy\n", result.getStdout()); Assert.assertEquals("", result.getStderr()); Assert.assertTrue(result.isSuccessful()); result = ShellCommandUtil. runCommand(new String [] {"false"}); Assert.assertEquals(1, result.getExitCode()); Assert.assertFalse(result.isSuccessful()); } else { } }
@Test public void testRunInteractiveCommand() throws Exception { ShellCommandUtil.InteractiveHandler interactiveHandler = new ShellCommandUtil.InteractiveHandler() { boolean done = false; @Override public boolean done() { return done; } @Override public String getResponse(String query) { if (query.contains("Arg1")) { return "a1"; } else if (query.contains("Arg2")) { done = true; return "a2"; } else { return null; } } @Override public void start() { } }; ShellCommandUtil.Result result = ShellCommandUtil.runCommand(new String[]{"./src/test/resources/interactive_shell_test.sh"}, null, interactiveHandler, false); Assert.assertEquals(0, result.getExitCode()); Assert.assertTrue(result.isSuccessful()); Assert.assertEquals("a1\na2\n", result.getStdout()); }