Browse Source

tiny refactoring. Allow JUnit task to be less concerned about formatters (right now formatters could close System.out/err unless they guard against it).

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@732010 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
8557fcb144
5 changed files with 47 additions and 4 deletions
  1. +1
    -2
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java
  2. +5
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java
  3. +3
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java
  4. +10
    -0
      src/main/org/apache/tools/ant/util/KeepAliveInputStream.java
  5. +28
    -0
      src/main/org/apache/tools/ant/util/KeepAliveOutputStream.java

+ 1
- 2
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -567,8 +567,7 @@ public class SQLExec extends JDBCTask {
} }


try { try {
PrintStream out =
new PrintStream(new KeepAliveOutputStream(System.out));
PrintStream out = KeepAliveOutputStream.wrapSystemOut();
try { try {
if (output != null) { if (output != null) {
log("Opening PrintStream to output Resource " + output, Project.MSG_VERBOSE); log("Opening PrintStream to output Resource " + output, Project.MSG_VERBOSE);


+ 5
- 1
src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java View File

@@ -29,6 +29,7 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task; import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.KeepAliveOutputStream;


/** /**
* <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>. * <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>.
@@ -57,7 +58,7 @@ public class FormatterElement {


private String classname; private String classname;
private String extension; private String extension;
private OutputStream out = System.out;
private OutputStream out = new KeepAliveOutputStream(System.out);
private File outFile; private File outFile;
private boolean useFile = true; private boolean useFile = true;
private String ifProperty; private String ifProperty;
@@ -170,6 +171,9 @@ public class FormatterElement {
* @param out the output stream to use. * @param out the output stream to use.
*/ */
public void setOutput(OutputStream out) { public void setOutput(OutputStream out) {
if (out == System.out || out == System.err) {
out = new KeepAliveOutputStream(out);
}
this.out = out; this.out = out;
} }




+ 3
- 1
src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java View File

@@ -224,7 +224,9 @@ public class SSHExec extends SSHBase {
private void executeCommand(Session session, String cmd, StringBuffer sb) private void executeCommand(Session session, String cmd, StringBuffer sb)
throws BuildException { throws BuildException {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out));
TeeOutputStream tee =
new TeeOutputStream(out,
KeepAliveOutputStream.wrapSystemOut());


InputStream istream = null ; InputStream istream = null ;
if (inputFile != null) { if (inputFile != null) {


+ 10
- 0
src/main/org/apache/tools/ant/util/KeepAliveInputStream.java View File

@@ -54,4 +54,14 @@ public class KeepAliveInputStream extends FilterInputStream {
public void close() throws IOException { public void close() throws IOException {
// do not close the stream // do not close the stream
} }

/**
* Convenience factory method that returns a non-closing
* InputStream around System.in.
*
* @since Ant 1.8.0
*/
public static InputStream wrapSystemIn() {
return new KeepAliveInputStream(System.in);
}
} }

+ 28
- 0
src/main/org/apache/tools/ant/util/KeepAliveOutputStream.java View File

@@ -20,6 +20,7 @@ package org.apache.tools.ant.util;
import java.io.FilterOutputStream; import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream;


/** /**
* Class that can be used to wrap <tt>System.out</tt> and <tt>System.err</tt> * Class that can be used to wrap <tt>System.out</tt> and <tt>System.err</tt>
@@ -53,4 +54,31 @@ public class KeepAliveOutputStream extends FilterOutputStream {
public void close() throws IOException { public void close() throws IOException {
// do not close the stream // do not close the stream
} }

/**
* Convenience factory method that returns a non-closing
* PrintStream around System.out.
*
* @since Ant 1.8.0
*/
public static PrintStream wrapSystemOut() {
return wrap(System.out);
}

/**
* Convenience factory method that returns a non-closing
* PrintStream around System.err.
*
* @since Ant 1.8.0
*/
public static PrintStream wrapSystemErr() {
return wrap(System.err);
}

/**
* @since Ant 1.8.0
*/
private static PrintStream wrap(PrintStream ps) {
return new PrintStream(new KeepAliveOutputStream(ps));
}
} }

Loading…
Cancel
Save