CommandExecutor { public static Result executeCommand(String... command) throws IOException, InterruptedException { Result rs = new Result(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command); StreamWrapper error = new StreamWrapper(proc.getErrorStream()); StreamWrapper output = new StreamWrapper(proc.getInputStream()); error.start(); output.start(); error.join(SLEEP_TIME); output.join(SLEEP_TIME); proc.waitFor(); rs.setExitVal(proc.exitValue()); rs.setOutput(output.getMessage()); rs.setError(error.getMessage()); proc.destroy(); return rs; }  static Result executeCommand(String... command); static Result executeCommand(String command);  }
@Test public void testExecute_JPS_Command() throws Exception { String command = "jps"; Result result = CommandExecutor.executeCommand(command); assertEquals(0, result.getExitVal()); assertNotNull(result); }
@Test public void testExecute_LS_Command() throws Exception { String command = "ls"; Result result = CommandExecutor.executeCommand(command); assertEquals(0, result.getExitVal()); assertNotNull(result); }
@Test public void testExecute_ABCD_Command() throws Exception { String command = "abcd"; exception.expect(IOException.class); Result result = CommandExecutor.executeCommand(command); assertEquals(0, result.getExitVal()); assertNotNull(result); }
@Test public void testExecute_MkRemoveTestDir_Command() throws Exception { String dir = new Date().getTime() + "-test"; String createCommand = "mkdir " + dir; Result result = CommandExecutor.executeCommand(createCommand); assertEquals(0, result.getExitVal()); assertNotNull(result); String removeCommand = "rm -r " + dir; result = CommandExecutor.executeCommand(removeCommand); assertEquals(0, result.getExitVal()); assertNotNull(result); }
@Test public void testExecute_Empty_Command() throws Exception { String command = ""; exception.expect(java.lang.IllegalArgumentException.class); Result result = CommandExecutor.executeCommand(command); assertEquals(1, result.getExitVal()); assertNotNull(result); }
@Test public void testExecute_Null_Command() throws Exception { String command = null; exception.expect(NullPointerException.class); Result result = CommandExecutor.executeCommand(command); assertEquals(1, result.getExitVal()); assertNotNull(result); }