@Test public void runCmdWithTimeout() throws Exception { threadExecuter = Mockito.mock(IThreadExecutor.class); Mockito.doNothing().when(threadExecuter).execute(Mockito.any(Runnable.class)); process = Mockito.mock(Process.class); String aMessage = "hello"; InputStream stream = new ByteArrayInputStream(aMessage.getBytes()); Mockito.when(process.getInputStream()).thenReturn(stream); Mockito.doNothing().when(process).destroy(); Mockito.when(factory.create(Mockito.any(String[].class))).thenReturn(process); Mockito.when(factory.create(Mockito.anyString())).thenReturn(process); worker = Mockito.mock(ProcessWorker.class); Mockito.doNothing().when(worker).setExit(); externalProcessRunner.setProcessFactory(factory); externalProcessRunner.setProcessWorker(worker); externalProcessRunner.setThreadExecutor(threadExecuter); cmds[0] = "test"; long timeout = 1; String res = externalProcessRunner.runCmdWithTimeout(cmds, timeout); assertEquals(aMessage, res); }
@Override public String runCmdWithTimeout(String[] command, long timeout) throws IOException { Process process = procfactory.create(command); if (worker == null) { worker = new ProcessWorker(process, timeout); } threadExecuter.execute(worker); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { worker.setExit(); worker = null; if (input != null) { input.close(); } if (process != null) { process.destroy(); } String datastr = null; if (out != null) { datastr = out.toString(); } return datastr; } }
ExternalProcessRunnerImpl implements IExternalProcessRunner { @Override public String runCmdWithTimeout(String[] command, long timeout) throws IOException { Process process = procfactory.create(command); if (worker == null) { worker = new ProcessWorker(process, timeout); } threadExecuter.execute(worker); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { worker.setExit(); worker = null; if (input != null) { input.close(); } if (process != null) { process.destroy(); } String datastr = null; if (out != null) { datastr = out.toString(); } return datastr; } } }
ExternalProcessRunnerImpl implements IExternalProcessRunner { @Override public String runCmdWithTimeout(String[] command, long timeout) throws IOException { Process process = procfactory.create(command); if (worker == null) { worker = new ProcessWorker(process, timeout); } threadExecuter.execute(worker); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { worker.setExit(); worker = null; if (input != null) { input.close(); } if (process != null) { process.destroy(); } String datastr = null; if (out != null) { datastr = out.toString(); } return datastr; } } ExternalProcessRunnerImpl(); }
ExternalProcessRunnerImpl implements IExternalProcessRunner { @Override public String runCmdWithTimeout(String[] command, long timeout) throws IOException { Process process = procfactory.create(command); if (worker == null) { worker = new ProcessWorker(process, timeout); } threadExecuter.execute(worker); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { worker.setExit(); worker = null; if (input != null) { input.close(); } if (process != null) { process.destroy(); } String datastr = null; if (out != null) { datastr = out.toString(); } return datastr; } } ExternalProcessRunnerImpl(); @Autowired void setProcessFactory(IProcessFactory factory); @Autowired void setThreadExecutor(IThreadExecutor threadExecuter); void setProcessWorker(ProcessWorker worker); @Override String executeCmd(String cmd); @Override String executeCmd(String cmd, boolean redirectErrorStream); @Override String executeCmdRunner(String cmd, boolean earlyExit, String msg); @Override String executeCmdRunner(String cmd, boolean earlyExit, String msg, boolean redirectErrorStream); @Override String runCmd(String[] command); @Override String runCmdWithTimeout(String[] command, long timeout); @Override String runGetString(String command); @Override ByteArrayOutputStream run(String command); }
ExternalProcessRunnerImpl implements IExternalProcessRunner { @Override public String runCmdWithTimeout(String[] command, long timeout) throws IOException { Process process = procfactory.create(command); if (worker == null) { worker = new ProcessWorker(process, timeout); } threadExecuter.execute(worker); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { worker.setExit(); worker = null; if (input != null) { input.close(); } if (process != null) { process.destroy(); } String datastr = null; if (out != null) { datastr = out.toString(); } return datastr; } } ExternalProcessRunnerImpl(); @Autowired void setProcessFactory(IProcessFactory factory); @Autowired void setThreadExecutor(IThreadExecutor threadExecuter); void setProcessWorker(ProcessWorker worker); @Override String executeCmd(String cmd); @Override String executeCmd(String cmd, boolean redirectErrorStream); @Override String executeCmdRunner(String cmd, boolean earlyExit, String msg); @Override String executeCmdRunner(String cmd, boolean earlyExit, String msg, boolean redirectErrorStream); @Override String runCmd(String[] command); @Override String runCmdWithTimeout(String[] command, long timeout); @Override String runGetString(String command); @Override ByteArrayOutputStream run(String command);  }