org.zeroturnaround.exec.ProcessExecutor Java Examples
The following examples show how to use
org.zeroturnaround.exec.ProcessExecutor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProcessExecutorMainTest.java From zt-exec with Apache License 2.0 | 6 votes |
@Test public void testProcessExecutorSetDirectory() throws Exception { // Use timeout in case we get stuck List<String> args = new ArrayList<String>() { { add("java"); add("-cp"); add("test-classes"); add(HelloWorld.class.getName()); } }; ProcessExecutor exec = new ProcessExecutor().directory(new File("target")); exec.command(args); ProcessResult result = exec.readOutput(true).execute(); Assert.assertEquals("Hello world!", result.outputUTF8()); }
Example #2
Source File: ProcessExecutorMainTest.java From zt-exec with Apache License 2.0 | 6 votes |
@Test public void testProcessExecutorCommand() throws Exception { // Use timeout in case we get stuck List<String> args = new ArrayList<String>() { { add("java"); add("-cp"); add("target/test-classes"); add(HelloWorld.class.getName()); } }; ProcessExecutor exec = new ProcessExecutor(); exec.command(args); ProcessResult result = exec.readOutput(true).execute(); Assert.assertEquals("Hello world!", result.outputUTF8()); }
Example #3
Source File: Edition070_Audio_Verification.java From appiumpro with Apache License 2.0 | 6 votes |
public static AudioFingerprint calcFP(File wavFile) throws Exception { String output = new ProcessExecutor() .command(FPCALC, "-raw", wavFile.getAbsolutePath()) .readOutput(true).execute() .outputUTF8(); Pattern fpPattern = Pattern.compile("^FINGERPRINT=(.+)$", Pattern.MULTILINE); Matcher fpMatcher = fpPattern.matcher(output); String fingerprint = null; if (fpMatcher.find()) { fingerprint = fpMatcher.group(1); } if (fingerprint == null) { throw new Exception("Could not get fingerprint via Chromaprint fpcalc"); } return new AudioFingerprint(fingerprint); }
Example #4
Source File: ProcessExecutorTimeoutTest.java From zt-exec with Apache License 2.0 | 6 votes |
@Test public void testExecuteTimeoutIssue56_1() throws Exception { try { List<String> commands = new ArrayList<String>(); if (SystemUtils.IS_OS_WINDOWS) { // native sleep command is not available on Windows platform // mock using standard ping to localhost instead // (Windows ping does 4 requests which takes about 3 seconds) commands.add("ping"); commands.add("127.0.0.1"); } else { commands.add("sleep"); commands.add("3"); } new ProcessExecutor() .command(commands) .timeout(1, TimeUnit.SECONDS) .execute(); Assert.fail("TimeoutException expected."); } catch (TimeoutException e) { Assert.assertThat(e.getMessage(), CoreMatchers.containsString("1 second")); } }
Example #5
Source File: ProjectCommandExecutorTests.java From spring-cloud-release-tools with Apache License 2.0 | 6 votes |
@Test public void should_throw_exception_when_process_exits_with_invalid_code() { ReleaserProperties properties = new ReleaserProperties(); properties.getBash().setBuildCommand("exit 1"); properties.setWorkingDir(tmpFile("/builder/unresolved").getPath()); ProjectCommandExecutor builder = new ProjectCommandExecutor() { @Override ReleaserProcessExecutor executor(String workingDir) { return new ReleaserProcessExecutor(properties.getWorkingDir()) { @Override ProcessResult doExecute(ProcessExecutor processExecutor) throws IOException, InterruptedException, TimeoutException { return new ProcessResult(1, null); } }; } }; thenThrownBy(() -> builder.build(properties, original(), new ProjectVersion("foo", "1.0.0.BUILD-SNAPSHOT"))).hasMessageContaining( "The process has exited with exit code [1]"); }
Example #6
Source File: DockerComposeRuntime.java From carnotzet with Apache License 2.0 | 6 votes |
@Override public ExecResult exec(String serviceName, int timeout, TimeUnit timeoutUnit, String... command) { Container container = getContainer(serviceName); List<String> fullCommand = new ArrayList<>(Arrays.asList("docker", "exec", container.getId())); fullCommand.addAll(Arrays.asList(command)); try { ProcessResult pr = new ProcessExecutor() .command(fullCommand) .readOutput(true) .timeout(timeout, timeoutUnit) .execute(); return new ExecResult(pr.getExitValue(), pr.getOutput().getUTF8()); } catch (IOException | InterruptedException | TimeoutException e) { throw new RuntimeException("Failed to execute " + Arrays.toString(command) + " in container [" + serviceName + "]", e); } }
Example #7
Source File: InputRedirectTest.java From zt-exec with Apache License 2.0 | 6 votes |
@Test public void testRedirectInput() throws Exception { String binTrue; if (SystemUtils.IS_OS_LINUX) { binTrue = "/bin/true"; } else if (SystemUtils.IS_OS_MAC_OSX) { binTrue = "/usr/bin/true"; } else { Assume.assumeTrue("Unsupported OS " + SystemUtils.OS_NAME, false); return; // Skip this test } // We need to put something in the buffer ByteArrayInputStream bais = new ByteArrayInputStream("foo".getBytes()); ProcessExecutor exec = new ProcessExecutor().command(binTrue); // Test that we don't get IOException: Stream closed int exit = exec.redirectInput(bais).readOutput(true).execute().getExitValue(); log.debug("Exit: {}", exit); }
Example #8
Source File: ProcessExecutorInputStreamTest.java From zt-exec with Apache License 2.0 | 6 votes |
@Test public void testDataIsFlushedToProcessWithANonEndingInputStream() throws Exception { String str = "Tere Minu Uus vihik " + System.nanoTime(); // Setup InputStream that will block on a read() PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ProcessExecutor exec = new ProcessExecutor("java", "-cp", "target/test-classes", PrintInputToOutput.class.getName()); exec.redirectInput(pis).redirectOutput(baos); StartedProcess startedProcess = exec.start(); pos.write(str.getBytes()); pos.write("\n\n\n".getBytes()); // PrintInputToOutput processes at most 3 lines // Assert that we don't get a TimeoutException startedProcess.getFuture().get(5, TimeUnit.SECONDS); Assert.assertEquals(str, baos.toString()); }
Example #9
Source File: ProcessExecutorTimeoutTest.java From zt-exec with Apache License 2.0 | 6 votes |
@Test public void testStartTimeoutIssue56_2() throws Exception { try { List<String> commands = new ArrayList<String>(); if (SystemUtils.IS_OS_WINDOWS) { // native sleep command is not available on Windows platform // mock using standard ping to localhost instead // (Windows ping does 4 requests which takes about 3 seconds) commands.add("ping"); commands.add("127.0.0.1"); } else { commands.add("sleep"); commands.add("3"); } new ProcessExecutor() .command(commands) .start() .getFuture() .get(1, TimeUnit.SECONDS); Assert.fail("TimeoutException expected."); } catch (TimeoutException e) { Assert.assertNull(e.getMessage()); } }
Example #10
Source File: RabbitMqPluginsTest.java From embedded-rabbitmq with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { EmbeddedRabbitMqConfig embeddedRabbitMqConfig = new EmbeddedRabbitMqConfig.Builder() .extractionFolder(tempFolder.getRoot()) .processExecutorFactory(factory) .build(); this.processExecutor = Mockito.mock(ProcessExecutor.class, new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { if (invocationOnMock.getMethod().getName().equals("start")){ return startedProcess; } return invocationOnMock.getMock(); } }); when(factory.createInstance()).thenReturn(processExecutor); rabbitMqPlugins = new RabbitMqPlugins(embeddedRabbitMqConfig); String appFolder = PredefinedVersion.LATEST.getExtractionFolder(); File executableFilesFolder = tempFolder.newFolder(appFolder, RabbitMqCommand.BINARIES_FOLDER); executableFile = new File(executableFilesFolder, "rabbitmq-plugins" + RabbitMqCommand.getCommandExtension()); assertTrue("Fake executable file couldn't be created!", executableFile.createNewFile()); }
Example #11
Source File: CommandLine.java From testcontainers-java with MIT License | 6 votes |
/** * Run a shell command synchronously. * * @param command command to run and arguments * @return the stdout output of the command */ public static String runShellCommand(String... command) { String joinedCommand = String.join(" ", command); LOGGER.debug("Executing shell command: `{}`", joinedCommand); try { ProcessResult result = new ProcessExecutor() .command(command) .readOutput(true) .exitValueNormal() .execute(); return result.outputUTF8().trim(); } catch (IOException | InterruptedException | TimeoutException | InvalidExitValueException e) { throw new ShellCommandException("Exception when executing " + joinedCommand, e); } }
Example #12
Source File: DynamicInstrumentationLoader.java From invesdwin-instrument with GNU Lesser General Public License v3.0 | 6 votes |
private static void loadAgent(final File tempAgentJar, final String pid) throws Exception { if (DynamicInstrumentationReflections.isBeforeJava9()) { DynamicInstrumentationLoadAgentMain.loadAgent(pid, tempAgentJar.getAbsolutePath()); } else { //-Djdk.attach.allowAttachSelf https://www.bountysource.com/issues/45231289-self-attach-fails-on-jdk9 //workaround this limitation by attaching from a new process final File loadAgentJar = createTempJar(DynamicInstrumentationLoadAgentMain.class, false, de.invesdwin.instrument.internal.DummyAttachProvider.class); final String javaExecutable = getJavaHome() + File.separator + "bin" + File.separator + "java"; final List<String> command = new ArrayList<String>(); command.add(javaExecutable); command.add("-classpath"); command.add(loadAgentJar.getAbsolutePath()); //tools.jar not needed since java9 command.add(DynamicInstrumentationLoadAgentMain.class.getName()); command.add(pid); command.add(tempAgentJar.getAbsolutePath()); new ProcessExecutor().command(command) .destroyOnExit() .exitValueNormal() .redirectOutput(Slf4jStream.of(DynamicInstrumentationLoader.class).asInfo()) .redirectError(Slf4jStream.of(DynamicInstrumentationLoader.class).asWarn()) .execute(); } }
Example #13
Source File: RabbitMqCommandTest.java From embedded-rabbitmq with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { version = PredefinedVersion.LATEST; configBuilder = new EmbeddedRabbitMqConfig.Builder() .extractionFolder(tempFolder.getRoot()) .version(this.version) .processExecutorFactory(this.factory); command = RandomStringUtils.randomAlphabetic(10); this.processExecutor = Mockito.mock(ProcessExecutor.class, new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { if (invocationOnMock.getMethod().getName().equals("start")){ return startedProcess; } return invocationOnMock.getMock(); } }); when(factory.createInstance()).thenReturn(processExecutor); String appFolder = version.getExtractionFolder(); File executableFilesFolder = tempFolder.newFolder(appFolder, RabbitMqCommand.BINARIES_FOLDER); executableFile = new File(executableFilesFolder, command + RabbitMqCommand.getCommandExtension()); assertTrue("Fake executable file couldn't be created!", executableFile.createNewFile()); }
Example #14
Source File: LoggingProcessListenerTest.java From embedded-rabbitmq with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { command = RandomStringUtils.randomAlphabetic(5); processExecutor = new ProcessExecutor(command); loggingProcessListener = new LoggingProcessListener(logger); loggingProcessListener.beforeStart(processExecutor); }
Example #15
Source File: SystemUtils.java From video-recorder-java with MIT License | 5 votes |
public static String runCommand(final String... args) { log.info("Trying to execute the following command: " + Arrays.asList(args)); try { return new ProcessExecutor() .command(args) .readOutput(true) .execute() .outputUTF8(); } catch (IOException | InterruptedException | TimeoutException e) { log.warn("Unable to execute command: " + e); throw new RecordingException(e); } }
Example #16
Source File: ProcessListenerSuccessTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Override public void beforeStart(ProcessExecutor executor) { Assert.assertNotNull(executor); Assert.assertNull(this.executor); Assert.assertNull(this.process); Assert.assertNull(this.result); this.executor = executor; }
Example #17
Source File: ProcessListenerSuccessTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Test public void testJavaVersion() throws Exception { ProcessListenerImpl listener = new ProcessListenerImpl(); ProcessResult result = new ProcessExecutor("java", "-version").addListener(listener).execute(); int exit = result.getExitValue(); Assert.assertEquals(0, exit); Assert.assertNotNull(listener.executor); Assert.assertNotNull(listener.process); Assert.assertNotNull(listener.result); Assert.assertEquals(result, listener.result); }
Example #18
Source File: GolangGetMojo.java From mvn-golang with Apache License 2.0 | 5 votes |
private boolean processCustomScriptCallForPackage(@Nonnull final String packageName, @Nonnull final File rootCvsFolder, @Nonnull final CustomScript script) { final List<String> command = new ArrayList<>(); command.add(script.path); if (script.options != null) { command.addAll(Arrays.asList(script.options)); } if (getLog().isDebugEnabled()) { getLog().debug("CLI : " + command); getLog().debug("Package name : " + packageName); getLog().debug("Root CVS folder : " + rootCvsFolder); } getLog().warn(String.format("Starting script in VCS folder [%s] : %s", packageName, StringUtils.join(command.toArray(), ' '))); final ProcessExecutor processExecutor = new ProcessExecutor(command.toArray(new String[0])); processExecutor .exitValueAny() .directory(rootCvsFolder) .environment("MVNGO_CVS_BRANCH", GetUtils.ensureNonNull(this.branch, "")) .environment("MVNGO_CVS_TAG", GetUtils.ensureNonNull(this.tag, "")) .environment("MVNGO_CVS_REVISION", GetUtils.ensureNonNull(this.revision, "")) .environment("MVNGO_CVS_PACKAGE", packageName) .redirectError(System.err) .redirectOutput(System.out); boolean result = false; try { final ProcessResult process = processExecutor.executeNoTimeout(); final int exitValue = process.getExitValue(); result = script.ignoreFail || exitValue == 0; } catch (IOException | InterruptedException | InvalidExitValueException ex) { getLog().error("Error in proces custom script", ex); } return result; }
Example #19
Source File: ProcessExecutorMainTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Test public void testProcessDestroyerEventsOnStreamsFail() throws Exception { MockProcessDestroyer mock = new MockProcessDestroyer(); ExecuteStreamHandler streams = new SetFailExecuteStreamHandler(); try { new ProcessExecutor().command("java", "-version").streams(streams).destroyer(mock).execute(); Assert.fail("IOException expected"); } catch (IOException e) { // Good } Assert.assertNull(mock.added); Assert.assertNull(mock.removed); }
Example #20
Source File: ProcessExecutorMainTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Test public void testJavaVersionOutputTwice() throws Exception { ProcessExecutor executor = new ProcessExecutor().command("java", "-version").readOutput(true); ProcessResult result = executor.execute(); String str = result.outputUTF8(); Assert.assertFalse(StringUtils.isEmpty(str)); Assert.assertEquals(str, executor.execute().outputUTF8()); }
Example #21
Source File: LoggingProcessListenerTest.java From embedded-rabbitmq with Apache License 2.0 | 5 votes |
@Test public void expectedExitValueDoesLogError() throws Exception { String command = RandomStringUtils.randomAlphabetic(5); ProcessExecutor processExecutor = new ProcessExecutor(command).exitValue(0); LoggingProcessListener loggingProcessListener = new LoggingProcessListener(logger); loggingProcessListener.beforeStart(processExecutor); loggingProcessListener.afterFinish(null, new ProcessResult(1, null)); ArgumentCaptor<String> msg = ArgumentCaptor.forClass(String.class); verify(logger).error(msg.capture(), anyString()); assertThat(msg.getValue(), containsString("unexpected exit code")); }
Example #22
Source File: SystemUtils.java From video-recorder-java with MIT License | 5 votes |
public static String runCommand(final List<String> args) { log.info("Trying to execute the following command: " + args); try { return new ProcessExecutor() .command(args) .readOutput(true) .execute() .outputUTF8(); } catch (IOException | InterruptedException | TimeoutException e) { log.warn("Unable to execute command: " + e); throw new RecordingException(e); } }
Example #23
Source File: DistributionTest.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
private static ProcessExecutor getCommandExecutor(Path directory) { return new ProcessExecutor() .directory(directory.toFile()) .redirectOutput(System.out) .redirectError(System.err) .readOutput(true); }
Example #24
Source File: RegistryAuthLocator.java From testcontainers-java with MIT License | 5 votes |
private String runCredentialProgram(String hostName, String credentialHelperName) throws InvalidResultException, InterruptedException, TimeoutException, IOException { return new ProcessExecutor() .command(credentialHelperName, "get") .redirectInput(new ByteArrayInputStream(hostName.getBytes())) .readOutput(true) .exitValueNormal() .timeout(30, TimeUnit.SECONDS) .execute() .outputUTF8() .trim(); }
Example #25
Source File: Runner.java From mangooio with Apache License 2.0 | 5 votes |
@SuppressWarnings("all") private ProcessExecutor buildProcessExecutor() { List<String> commandLine = new ArrayList<>(); String javaHome = System.getProperty("java.home"); String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; commandLine.add(javaBin); if (jpdaPort > 0) { LOG.info("Listening for jpda connection at " + jpdaPort); commandLine.add("-Xdebug"); commandLine.add(String.format("-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=%s", jpdaPort)); } commandLine.add("-noverify"); if (StringUtils.isNotBlank(jvmArgs)){ Arrays.stream(jvmArgs.split(" ")) //NOSONAR .filter(arg -> arg.length() > 0) .forEach(arg -> commandLine.add(arg)); } commandLine.add("-Dapplication.mode=dev"); commandLine.add("-cp"); commandLine.add(classpath); commandLine.add(mainClass); return new ProcessExecutor(commandLine) .directory(mavenBaseDir) .destroyOnExit() .addListener(new ProcessListener() { @Override public void afterStop(Process process) { if (!restarting.get()) { LOG.error("JVM process terminated (next file change will attempt to restart it)"); } } }) .redirectErrorStream(true) .redirectOutput(this.outputStream); }
Example #26
Source File: WriterLoopStarterAfterExit.java From zt-exec with Apache License 2.0 | 5 votes |
public void run() { try { new ProcessExecutor("java", "-cp", "target/test-classes", WriterLoop.class.getName()).destroyOnExit().start(); Thread.sleep(SLEEP_AFTER_START); } catch (Exception e) { e.printStackTrace(); } }
Example #27
Source File: MySQLBackupProcessor.java From gocd with Apache License 2.0 | 5 votes |
private ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) { ConnectionUrl connectionUrlInstance = ConnectionUrl.getConnectionUrlInstance(dbProperties.url(), dbProperties.connectionProperties()); LinkedHashMap<String, String> env = new LinkedHashMap<>(); if (isNotBlank(dbProperties.password())) { env.put("MYSQL_PWD", dbProperties.password()); } // override with any user specified environment env.putAll(dbProperties.extraBackupEnv()); ArrayList<String> argv = new ArrayList<>(); argv.add("mysqldump"); String dbName = connectionUrlInstance.getDatabase(); HostInfo mainHost = connectionUrlInstance.getMainHost(); if (mainHost != null) { argv.add("--host=" + mainHost.getHost()); argv.add("--port=" + mainHost.getPort()); } if (isNotBlank(dbProperties.user())) { argv.add("--user=" + dbProperties.user()); } // append any user specified args for mysqldump if (isNotBlank(dbProperties.extraBackupCommandArgs())) { Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs())); } argv.add("--result-file=" + new File(targetDir, "db." + dbName).toString()); argv.add(connectionUrlInstance.getDatabase()); ProcessExecutor processExecutor = new ProcessExecutor(); processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug()); processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug()); processExecutor.environment(env); processExecutor.command(argv); return processExecutor; }
Example #28
Source File: ProcessExecutorMainTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Test public void testJavaVersionLogInfoAndOutput() throws Exception { // Just expect no errors - don't check the log file itself ProcessResult result = new ProcessExecutor().command("java", "-version").redirectOutput(Slf4jStream.of("testJavaVersionLogInfoAndOutput").asInfo()).readOutput(true).execute(); String str = result.outputUTF8(); Assert.assertFalse(StringUtils.isEmpty(str)); }
Example #29
Source File: ProcessExecutorShutdownHookTest.java From zt-exec with Apache License 2.0 | 5 votes |
private void testDestroyOnExit(Class<?> starter, boolean fileIsAlwaysCreated) throws Exception { File file = WriterLoop.getFile(); if (file.exists()) FileUtils.forceDelete(file); new ProcessExecutor("java", "-cp", SystemUtils.JAVA_CLASS_PATH, starter.getName()).redirectOutputAsInfo().execute(); // After WriterLoopStarter has finished we expect that WriterLoop is also finished - no-one is updating the file if (fileIsAlwaysCreated || file.exists()) { checkFileStaysTheSame(file); FileUtils.forceDelete(file); } }
Example #30
Source File: ProcessListenerSuccessTest.java From zt-exec with Apache License 2.0 | 5 votes |
@Override public void afterStart(Process process, ProcessExecutor executor) { Assert.assertNotNull(process); Assert.assertNotNull(executor); Assert.assertNotNull(this.executor); Assert.assertNull(this.process); Assert.assertNull(this.result); Assert.assertEquals(this.executor, executor); this.process = process; }