diff options
author | michelou <michelou@epfl.ch> | 2003-10-14 09:09:50 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2003-10-14 09:09:50 +0000 |
commit | 339f51f314fab537c2a68bc446c6b8103c68b1c4 (patch) | |
tree | b573331280339c3f4bd1826ac6a99ac9a8a56963 /sources | |
parent | f7d10e24428e2d611ba0bd940b4a45b72e94e2a6 (diff) | |
download | scala-339f51f314fab537c2a68bc446c6b8103c68b1c4.tar.gz scala-339f51f314fab537c2a68bc446c6b8103c68b1c4.tar.bz2 scala-339f51f314fab537c2a68bc446c6b8103c68b1c4.zip |
- class Command is now in a separate file
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/tools/scalatest/Command.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/sources/scala/tools/scalatest/Command.java b/sources/scala/tools/scalatest/Command.java new file mode 100644 index 0000000000..be5fbb18c0 --- /dev/null +++ b/sources/scala/tools/scalatest/Command.java @@ -0,0 +1,106 @@ +/* ___ ____ ___ __ ___ _____ +** / _// __// _ | / / / _ |/_ _/ Scala test +** __\ \/ /__/ __ |/ /__/ __ | / / (c) 2003, LAMP/EPFL +** /____/\___/_/ |_/____/_/ |_//_/ +** +** $Id$ +*/ + +package scala.tools.scalatest; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintWriter; + + +public abstract class Command { + + private Console console; + + Command(Console console) { + this.console = console; + } + + /** + * Redirects the standard output/error streams of the process into files. + * (see http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html) + */ + private class StreamGobbler extends Thread { + private InputStream is; + private OutputStream os; + + public StreamGobbler(InputStream is) { + // this(is, null); // Pico bug (mics/14.10.2003) + this.is = is; + this.os = null; + } + + public StreamGobbler(InputStream is, OutputStream os) { + this.is = is; + this.os = os; + } + + public void run() { + try { + BufferedReader rd = + new BufferedReader(new InputStreamReader(is)); + String s = rd.readLine(); + if (s != null) { + if (os == null) + do { + console.println(s); + } while ((s = rd.readLine()) != null); + else { + PrintWriter pw = new PrintWriter(os); + do { + pw.println(s); + } while ((s = rd.readLine()) != null); + pw.flush(); + } + } + } catch (Exception e) { + System.err.println(e.getMessage()); + System.exit(-1); + } + } + } + + protected boolean execute(String cmdline, OutputStream out, OutputStream err) { + boolean ok = true; + try { + Process p = Runtime.getRuntime().exec(cmdline); + StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), out); + StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), err); + + // read any output from the attempted command + outputGobbler.start(); + + // read any errors from the attempted command + errorGobbler.start(); + + int exitValue = p.waitFor(); + ok = exitValue >= 0; + + if (out != null) + out.flush(); + if (err != null) + err.flush(); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + return ok; + } + + protected boolean execute(String cmdline, OutputStream out) { + return execute(cmdline, out, null); + } + + protected boolean execute(String cmdline) { + return execute(cmdline, null, null); + } + + public abstract boolean run(String arg); + +} |