### Question:
CommandExecutor implements Executor { @Override public int execute(final List<String> commandInformation, File workingDir) throws IOException, InterruptedException { int exitValue = -99; try { ProcessBuilder pb = new ProcessBuilder(commandInformation); pb.directory(workingDir); Process process = pb.start(); OutputStream stdOutput = process.getOutputStream(); InputStream inputStream = process.getInputStream(); InputStream errorStream = process.getErrorStream(); inputStreamHandler = new ThreadedStreamHandler(inputStream, stdOutput, adminPassword); errorStreamHandler = new ThreadedStreamHandler(errorStream); inputStreamHandler.start(); errorStreamHandler.start(); exitValue = process.waitFor(); inputStreamHandler.interrupt(); errorStreamHandler.interrupt(); inputStreamHandler.join(); errorStreamHandler.join(); } catch (IOException e) { throw e; } catch (InterruptedException e) { throw e; } finally { return exitValue; } } CommandExecutor(); @Override int execute(final List<String> commandInformation, File workingDir); @Override int startServer(final List<String> commandInformation, File workingDir); @Override int startServer(final List<String> commandInformation, final Map<String,String> envVars, File workingDir); @Override StringBuilder getStdout(); @Override StringBuilder getStderr(); @Override void stopServers(); static List<Process> processes; }### Answer:
@Test public void testSimpleCommand() throws IOException, InterruptedException { List<String> commands = new ArrayList<String>(); commands.add("/bin/sh"); commands.add("-c"); commands.add("ls -l /var/tmp | wc -l"); Executor executor = SingletonServiceFactory.getBean(Executor.class); int result = executor.execute(commands, new File(System.getProperty("user.home"))); }
@Test public void testLsCommand() throws IOException, InterruptedException { List<String> commands = new ArrayList<>(); commands.add("ls"); Executor executor = SingletonServiceFactory.getBean(Executor.class); int result = executor.execute(commands, new File(System.getProperty("user.home"))); }
@Test public void testGitClone() throws IOException, InterruptedException { List<String> commands = new ArrayList<>(); commands.add("bash"); commands.add("-c"); commands.add("rm -rf light-4j"); Executor executor = SingletonServiceFactory.getBean(Executor.class); int result = executor.execute(commands, new File(System.getProperty("java.io.tmpdir"))); commands = new ArrayList<>(); commands.add("bash"); commands.add("-c"); commands.add("git clone https: result = executor.execute(commands, new File(System.getProperty("java.io.tmpdir"))); commands = new ArrayList<>(); commands.add("bash"); commands.add("-c"); commands.add("git checkout master"); Path path = Paths.get(System.getProperty("java.io.tmpdir"),"light-4j"); result = executor.execute(commands, path.toFile()); }