Created
December 6, 2009 07:50
-
-
Save bigeasy/250120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.goodworkalan.spawn; | |
import static org.testng.Assert.assertEquals; | |
import java.io.File; | |
import org.testng.annotations.Test; | |
public class SpawnTest { | |
@Test | |
public void cat() { | |
Spawn<Slurp, Slurp> spawn = Spawn.spawn(); | |
// Throw an exception for all exit codes except 0. | |
spawn.setUnexceptionalExitCodes(0); | |
// Run in the directory /etc. | |
spawn.setWorkingDirectory(new File("/etc")); | |
try { | |
// Run a program and get standard out, error and the exit code. | |
Exit<Slurp, Slurp> exit = spawn.execute("cat", "resolve.conf"); | |
for (String line : exit.getStdOut().getLines()) { | |
System.out.println(line); | |
} | |
assertEquals(exit.getCode(), 0); | |
// We don't really need to check the error code, it is turned | |
// into an exception for unless the code is "unexceptional" above. | |
for (String line : spawn.execute("cat", "hosts").getStdOut().getLines()) { | |
System.out.println(line); | |
} | |
// Spawn itself acts as the environment. | |
spawn.setWorkingDirectory(new File(".")); | |
// We can run commands and react only to exceptions. | |
spawn.execute("touch", "monkey.txt"); | |
} catch (SpawnException e) { | |
// Error processing all in one place. | |
System.out.println(e.getCommandLine().get(0) + " " + e.getCode()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment