@Test public void runCmdTest() throws IOException { 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); externalProcessRunner.setProcessFactory(factory); cmds[0] = "test"; String res = externalProcessRunner.runCmd(cmds); assertEquals(aMessage, res); }
@Override public String runCmd(String[] command) throws IOException { Process process = procfactory.create(command); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { 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 runCmd(String[] command) throws IOException { Process process = procfactory.create(command); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { 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 runCmd(String[] command) throws IOException { Process process = procfactory.create(command); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { 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 runCmd(String[] command) throws IOException { Process process = procfactory.create(command); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { 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 runCmd(String[] command) throws IOException { Process process = procfactory.create(command); InputStream input = process.getInputStream(); try (ByteArrayOutputStream out = readInputStream(input)) { 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);  }