Changing default output/error streams
Brought to you by:
johanwalles,
kerneldebugger
Added an option to ExpectJ class to set a default output/error stream for subsequent Spawn processes which they will use instead of copying all output to System.out and System.err. Set to null this permits 'silent' processes to avoid filling logfiles with ExpectJ output.
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
Trying to attach a patch gives "File upload: ArtifactFile: Could not open file for writing" so here it is
# Bazaar merge directive format 2 (Bazaar 0.90)
# revision_id: duncan.mackintosh@gmail.com-20110701105247-\ # 9are7ij6c4wyrh81
# target_branch: bzr://expectj.bzr.sourceforge.net/bzrroot/expectj\ # /trunk/
# testament_sha1: d350a93b805018e808954186577b7b14f1c3de1e
# timestamp: 2011-07-01 11:53:57 +0100
# base_revision_id: johan.walles@gmail.com-20100726135155-\ # otvph98xkyi9ozn2
#
# Begin patch
=== modified file 'src/main/java/expectj/ExpectJ.java'
--- src/main/java/expectj/ExpectJ.java 2010-07-18 08:38:51 +0000
+++ src/main/java/expectj/ExpectJ.java 2011-07-01 10:52:47 +0000
@@ -1,6 +1,7 @@
package expectj;
import java.io.IOException;
+import java.io.PrintStream;
import java.net.UnknownHostException;
import com.jcraft.jsch.Channel;
@@ -12,6 +13,14 @@
* @author Sachin Shekar Shetty
*/
public class ExpectJ {
+
+ /** Default PrintStream for spawns to copy stdout to */
+ private PrintStream m_defaultOutput = System.out;
+
+ /** Default PrintStream for spawns to copy stderr to */
+ private PrintStream m_defaultError = System.err;
+
+
/** Default timeout, -1 indicating wait for indefinite time */
private long m_lDefaultTimeOutSeconds = -1;
@@ -33,6 +42,48 @@
}
/**
+ * Set the default standard output stream. All subsequently spawned
+ * processes will copy their standard output to this stream in addition to
+ * storing it internally. By default this is System.out. Set to null or use
+ * {@link #removeDefaultOutputStream()} to stop copying stdout.
+ *
+ * @param out New PrintStream to receive all standard output.
+ */
+ public void setDefaultOutputStream(PrintStream out) {
+ this.m_defaultOutput = out;
+ }
+
+ /**
+ * Set the default standard error stream. All subsequently spawned processes
+ * will copy their standard error to this stream in addition to storing it
+ * internally. By default this is System.err. Set to null or use
+ * {@link #removeDefaultErrorStream()} to stop copying stderr.
+ *
+ * @param err New PrintStream to receive all standard error.
+ */
+ public void setDefaultErrorStream(PrintStream err) {
+ this.m_defaultError = err;
+ }
+
+ /**
+ * Stop subsequently created Spawn processes from output to a second
+ * PrintStream. This does not stop the Spawn storing output internally for
+ * {@link Spawn#getCurrentStandardOutContents()}.
+ */
+ public void removeDefaultOutputStream() {
+ setDefaultOutputStream(null);
+ }
+
+ /**
+ * Stop subsequently created Spawn prcoess from copying error output to a
+ * second PrintStream. This does not stop the Spawn storing stderr output
+ * internally for {@link Spawn#getCurrentStandardErrContents()}.
+ */
+ public void removeDefaultErrorStream() {
+ setDefaultErrorStream(null);
+ }
+
+ /**
* This method launches a {@link Spawnable}. Further expect commands can be
* invoked on the returned {@link Spawn} object.
*
@@ -41,7 +92,7 @@
* @throws IOException if the spawning fails
*/
public Spawn spawn(Spawnable spawnable) throws IOException {
- return new Spawn(spawnable, m_lDefaultTimeOutSeconds);
+ return new Spawn(spawnable, m_lDefaultTimeOutSeconds, m_defaultOutput, m_defaultError);
}
/**
=== modified file 'src/main/java/expectj/Spawn.java'
--- src/main/java/expectj/Spawn.java 2010-07-16 18:50:16 +0000
+++ src/main/java/expectj/Spawn.java 2011-07-01 10:52:47 +0000
@@ -4,6 +4,7 @@
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
+import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.Pipe;
@@ -80,16 +81,18 @@
*
* @param spawn This is what we'll control.
* @param lDefaultTimeOutSeconds Default timeout for expect commands
+ * @param copyOutput Copy stdout to this printstream; null for no copying
+ * @param copyError Copy stderror to this printstream; null for no copying
* @throws IOException on trouble launching the spawn
*/
- Spawn(Spawnable spawn, long lDefaultTimeOutSeconds) throws IOException {
+ Spawn(Spawnable spawn, long lDefaultTimeOutSeconds, PrintStream copyOutput, PrintStream copyError) throws IOException {
if (lDefaultTimeOutSeconds < -1) {
throw new IllegalArgumentException("Timeout must be >= -1, was "
+ lDefaultTimeOutSeconds);
}
m_lDefaultTimeOutSeconds = lDefaultTimeOutSeconds;
- slave = new SpawnableHelper(spawn, lDefaultTimeOutSeconds);
+ slave = new SpawnableHelper(spawn, lDefaultTimeOutSeconds, copyOutput, copyError);
slave.start();
LOG.debug("Spawned Process: " + spawn);
=== modified file 'src/main/java/expectj/SpawnableHelper.java'
--- src/main/java/expectj/SpawnableHelper.java 2010-07-16 18:50:16 +0000
+++ src/main/java/expectj/SpawnableHelper.java 2011-07-01 10:52:47 +0000
@@ -2,6 +2,7 @@
import java.io.IOException;
import java.io.OutputStream;
+import java.io.PrintStream;
import java.nio.channels.Channels;
import java.nio.channels.Pipe;
@@ -29,10 +30,22 @@
private Spawnable spawnable;
/**
+ * Stream to receive a copy of all stdout writes. Defaults to System.out.
+ */
+ private PrintStream copyOutput;
+
+ /**
+ * Stream to receive a copy of all stderr writes. Defaults to System.err.
+ */
+ private PrintStream copyError;
+
+ /**
* @param timeOutSeconds time interval in seconds to be allowed for spawn execution
* @param runMe the spawnable to execute
+ * @param copyOutput Stream to receive a copy of all stdout writes. By default System.out; null for no copying.
+ * @param copyError Stream to receiver stderr in the same way as copyOutput.
*/
- SpawnableHelper(Spawnable runMe, long timeOutSeconds) {
+ SpawnableHelper(Spawnable runMe, long timeOutSeconds, PrintStream copyOutput, PrintStream copyError) {
if (timeOutSeconds < -1) {
throw new IllegalArgumentException("Time-out is invalid");
}
@@ -40,13 +53,15 @@
timer = new Timer(timeOutSeconds, this);
}
this.spawnable = runMe;
+ this.copyError = copyError;
+ this.copyOutput = copyOutput;
}
/**
* @param runMe the spawnable to execute
*/
SpawnableHelper(Spawnable runMe) {
- this(runMe, -1);
+ this(runMe, -1, System.out, System.err);
}
/** Timer object to monitor our Spawnable */
@@ -106,7 +121,9 @@
* @see Spawn#interact()
*/
synchronized void stopPipingToStandardOut() {
- spawnOutToSystemOut.stopPipingToStandardOut();
+ if (spawnOutToSystemOut != null) {
+ spawnOutToSystemOut.stopPipingToStandardOut();
+ }
if (spawnErrToSystemErr != null) {
spawnErrToSystemErr.stopPipingToStandardOut();
}
@@ -118,7 +135,9 @@
* @see Spawn#interact()
*/
synchronized void startPipingToStandardOut() {
- spawnOutToSystemOut.startPipingToStandardOut();
+ if (spawnOutToSystemOut != null) {
+ spawnOutToSystemOut.startPipingToStandardOut();
+ }
if (spawnErrToSystemErr != null) {
spawnErrToSystemErr.startPipingToStandardOut();
}
@@ -139,9 +158,10 @@
}
// Starting the piped streams and StreamPiper objects
+
systemOut = Pipe.open();
systemOut.source().configureBlocking(false);
- spawnOutToSystemOut = new StreamPiper(System.out,
+ spawnOutToSystemOut = new StreamPiper(copyOutput,
spawnable.getStdout(),
Channels.newOutputStream(systemOut.sink()));
spawnOutToSystemOut.start();
@@ -149,12 +169,12 @@
if (spawnable.getStderr() != null) {
systemErr = Pipe.open();
systemErr.source().configureBlocking(false);
-
- spawnErrToSystemErr = new StreamPiper(System.err,
+ spawnErrToSystemErr = new StreamPiper(copyError,
spawnable.getStderr(),
Channels.newOutputStream(systemErr.sink()));
spawnErrToSystemErr.start();
}
+
}
/**
=== modified file 'src/test/java/expectj/TestExpect.java'
--- src/test/java/expectj/TestExpect.java 2010-07-16 18:50:16 +0000
+++ src/test/java/expectj/TestExpect.java 2011-07-01 10:52:47 +0000
@@ -1,10 +1,12 @@
package expectj;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PrintStream;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
@@ -136,6 +138,40 @@
}
/**
+ * Verify {@link ExpectJ#setDefaultOutputStream(PrintStream)}.
+ *
+ * @throws Exception if things go wrong.
+ */
+ public void testSetDefaultOutput() throws Exception {
+ ExpectJ expect=new ExpectJ();
+ ByteArrayOutputStream testOut=new ByteArrayOutputStream();
+ expect.setDefaultOutputStream(new PrintStream(testOut));
+ Spawn testMe=expect.spawn(new StagedSpawnable(new String[] {"flaska", "gris"}));
+ testMe.expect("flaska");
+ testMe.expect("gris");
+ assertEquals("flaskagris", testMe.getCurrentStandardOutContents());
+ testMe.expectClose();
+ assertEquals("flaskagris", testMe.getCurrentStandardOutContents());
+ assertEquals("flaskagris", testOut.toString());
+ }
+
+ /**
+ * Verify {@link ExpectJ#removeDefaultOutputStream()}
+ *
+ * @throws Exception if things go wrong
+ */
+ public void testNoDefaultOutput() throws Exception {
+ ExpectJ expect=new ExpectJ();
+ expect.removeDefaultOutputStream();
+ Spawn testMe=expect.spawn(new StagedSpawnable(new String[] {"flaska", "gris"}));
+ testMe.expect("flaska");
+ testMe.expect("gris");
+ assertEquals("flaskagris", testMe.getCurrentStandardOutContents());
+ testMe.expectClose();
+ assertEquals("flaskagris", testMe.getCurrentStandardOutContents());
+ }
+
+ /**
* Test that we time out properly when we don't find what we're looking for.
* @throws Exception if things go wrong.
*/
# Begin bundle
IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWRw1jkgABsDfgGRyeff//35z
zQq////6YAy/GL0wbVQMiAWqqtU2xtYAVFVSAULA4yZNGIaaGAmhiaNMmIGRhNGmmEGTCSQBNNAm
lT9InpPSNppHpDRtJoD0jR6gZBoOMmTRiGmhgJoYmjTJiBkYTRpphBkwk1EjUmpN5NIm1DI21TDU
2pkB6TQNA0AADjJk0YhpoYCaGJo0yYgZGE0aaYQZMJEhAENEwEaaCYqfqaaT2U0mm1BoHpAemUrQ
AnUNfo0QMrqhWBsPr2D6jnz5d9DdbjvHRSupx+s6UIbbX6XYdfboVWdiutYw7ZBGYICo7CAZMgMs
8J9pBkMhkYvPJBQ/NwGNqMcFAng4dLZOQyybPp48PFhKtEkDw10da2D2s+ECd5jTm0XSxYDeDUNx
s+DHDZxBtweDwkEA1JTPUy9o7t5vb6zoqHhSAQXQAqrwH7HdXj3GJL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