From: <otm...@us...> - 2006-08-28 23:41:52
|
Revision: 2918 Author: otmarhumbel Date: 2006-08-28 16:41:36 -0700 (Mon, 28 Aug 2006) ViewCVS: http://svn.sourceforge.net/jython/?rev=2918&view=rev Log Message: ----------- introducing silent mode Modified Paths: -------------- trunk/installer/src/java/org/python/util/install/ChildProcess.java trunk/installer/test/java/org/python/util/install/ChildProcessTest.java Modified: trunk/installer/src/java/org/python/util/install/ChildProcess.java =================================================================== --- trunk/installer/src/java/org/python/util/install/ChildProcess.java 2006-08-27 21:31:31 UTC (rev 2917) +++ trunk/installer/src/java/org/python/util/install/ChildProcess.java 2006-08-28 23:41:36 UTC (rev 2918) @@ -11,7 +11,8 @@ * <ul> * <li>wait for the child process to finish. * <li>kill the child process after a specified timeout. - * <li>get the output of the child process (System.out and System.err) redirected to the calling process. + * <li>get the output of the child process (System.out and System.err) redirected to the calling process, uness in + * silent mode. * </ul> */ public class ChildProcess { @@ -28,10 +29,14 @@ BufferedReader stdout = new BufferedReader(new InputStreamReader(_process.getInputStream())); try { while ((line = stdout.readLine()) != null) { // blocks until input found or process dead - System.out.println(line); + if (!isSilent()) { + System.out.println(line); + } } } catch (IOException ioe) { - ioe.printStackTrace(); + if (!isSilent()) { + ioe.printStackTrace(); + } } finally { if (stdout != null) try { @@ -54,10 +59,14 @@ BufferedReader stderr = new BufferedReader(new InputStreamReader(_process.getErrorStream())); try { while ((line = stderr.readLine()) != null) { // blocks until input found or process dead - System.err.println(line); + if (!isSilent()) { + System.err.println(line); + } } } catch (IOException ioe) { - ioe.printStackTrace(); + if (!isSilent()) { + ioe.printStackTrace(); + } } finally { if (stderr != null) try { @@ -124,6 +133,11 @@ private boolean _debug = false; /** + * silent flag + */ + private boolean _silent = false; + + /** * Default constructor */ public ChildProcess() { @@ -232,6 +246,22 @@ } /** + * Set the silent flag. + * <p> + * Setting this to true will suppress output of the called command. + */ + public void setSilent(boolean silent) { + _silent = silent; + } + + /** + * Returns the silent flag. + */ + public boolean isSilent() { + return _silent; + } + + /** * Set the interval (in milliseconds) after which the subprocess is checked if it is still alive. Defaults to 1000 * ms. */ @@ -290,7 +320,9 @@ try { Thread.sleep(getPollAliveInterval()); } catch (InterruptedException ie) { - ie.printStackTrace(); + if (!isSilent()) { + ie.printStackTrace(); + } } } @@ -298,12 +330,14 @@ if (isAlive()) { // sets the exit value in case process is dead destroy(); } else { - if(isDebug()) { + if (isDebug()) { System.out.println("[ChildProcess] ended itself"); } } } catch (IOException ioe) { - ioe.printStackTrace(); + if (!isSilent()) { + ioe.printStackTrace(); + } } return getExitValue(); } Modified: trunk/installer/test/java/org/python/util/install/ChildProcessTest.java =================================================================== --- trunk/installer/test/java/org/python/util/install/ChildProcessTest.java 2006-08-27 21:31:31 UTC (rev 2917) +++ trunk/installer/test/java/org/python/util/install/ChildProcessTest.java 2006-08-28 23:41:36 UTC (rev 2918) @@ -1,56 +1,91 @@ - package org.python.util.install; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import org.apache.xalan.lib.Redirect; + import junit.framework.TestCase; public class ChildProcessTest extends TestCase { - private final static String CLASS_NAME = "org.python.util.install.ChildProcessExample"; + private final static String CLASS_NAME = "org.python.util.install.ChildProcessExample"; - /** - * test a default child process - */ - public void testChildProcess() { - ChildProcess childProcess = new ChildProcess(); - String command = buildJavaCommand(CLASS_NAME); - childProcess.setCommand(command); - childProcess.setDebug(true); - int exitValue = childProcess.run(); - assertEquals("Expected child process to end normally instead of " + exitValue, 0, exitValue); - } + /** + * test a default child process + */ + public void testDefault() { + ChildProcess childProcess = new ChildProcess(); + String command = buildJavaCommand(CLASS_NAME); + childProcess.setCommand(command); + childProcess.setDebug(true); + int exitValue = childProcess.run(); + assertEquals("Expected child process to end normally instead of " + exitValue, 0, exitValue); + } - /** - * test the child process with a timeout - */ - public void testChildProcessTimeout() { - ChildProcess childProcess = new ChildProcess(); - String command = buildJavaCommand(CLASS_NAME); - childProcess.setCommand(command); - childProcess.setDebug(true); - childProcess.setTimeout(2000); // timeout to 2 seconds - int exitValue = childProcess.run(); - assertEquals("Expected child process to be destroyed instead of " + exitValue, ChildProcess.DESTROYED_AFTER_TIMEOUT, exitValue); - } + /** + * test the child process with a timeout + */ + public void testTimeout() { + ChildProcess childProcess = new ChildProcess(); + String command = buildJavaCommand(CLASS_NAME); + childProcess.setCommand(command); + childProcess.setDebug(true); + childProcess.setTimeout(2000); // timeout to 2 seconds + int exitValue = childProcess.run(); + assertEquals("Expected child process to be destroyed instead of " + exitValue, + ChildProcess.DESTROYED_AFTER_TIMEOUT, exitValue); + } - /** - * test the child process with a command array - */ - public void testChildProcessCommandArray() { - String command = buildJavaCommand(CLASS_NAME); - String commandArray[] = command.split(" "); - ChildProcess childProcess = new ChildProcess(commandArray); - childProcess.setDebug(true); - int exitValue = childProcess.run(); - assertEquals("Expected child process to end normally instead of " + exitValue, 0, exitValue); - } + /** + * test the child process with a command array + */ + public void testCommandArray() { + String command = buildJavaCommand(CLASS_NAME); + String commandArray[] = command.split(" "); + ChildProcess childProcess = new ChildProcess(commandArray); + childProcess.setDebug(true); + int exitValue = childProcess.run(); + assertEquals("Expected child process to end normally instead of " + exitValue, 0, exitValue); + } - // - // private methods - // + /** + * test silent mode + */ + public void testSilent() throws IOException { + ChildProcess childProcess = new ChildProcess(); + String command = "lwiklsl -siwK"; + childProcess.setCommand(command); + childProcess.setDebug(false); + childProcess.setSilent(true); + ByteArrayOutputStream redirectedErr = new ByteArrayOutputStream(); + ByteArrayOutputStream redirectedOut = new ByteArrayOutputStream(); + int exitValue = 0; + PrintStream oldErr = System.err; + PrintStream oldOut = System.out; + try { + System.setErr(new PrintStream(redirectedErr)); + System.setOut(new PrintStream(redirectedOut)); + exitValue = childProcess.run(); + } finally { + System.setErr(oldErr); + System.setOut(oldOut); + } + assertTrue(0 != exitValue); + redirectedErr.flush(); + redirectedOut.flush(); + assertEquals(0, redirectedErr.size()); + assertEquals(0, redirectedOut.size()); + } - private String buildJavaCommand(String classAndArguments) { - String classpath = System.getProperty("java.class.path"); - return "java -classpath \"" + classpath + "\" " + classAndArguments; - } + // + // private methods + // + private String buildJavaCommand(String classAndArguments) { + String classpath = System.getProperty("java.class.path"); + return "java -classpath \"" + classpath + "\" " + classAndArguments; + } + } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |