ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit JUnitResultFormatter.java JUnitTask.java JUnitTestRunner.java PlainJUnitResultFormatter.java SummaryJUnitResultFormatter.java XMLConstants.java XMLJUnitResultFormatter.java
Date Thu, 03 May 2001 15:04:17 GMT
bodewig     01/05/03 08:04:16

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional/junit
                        JUnitResultFormatter.java JUnitTask.java
                        JUnitTestRunner.java PlainJUnitResultFormatter.java
                        SummaryJUnitResultFormatter.java XMLConstants.java
                        XMLJUnitResultFormatter.java
  Log:
  capture System.out and System.err while running the junit tests.
  
  Submitted by:	Gerrit Riessen <Gerrit.Riessen@web.de>
  
  Revision  Changes    Path
  1.4       +11 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitResultFormatter.java
  
  Index: JUnitResultFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitResultFormatter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JUnitResultFormatter.java	2001/01/03 14:18:44	1.3
  +++ JUnitResultFormatter.java	2001/05/03 15:03:55	1.4
  @@ -78,4 +78,15 @@
        * Sets the stream the formatter is supposed to write its results to.
        */
       public void setOutput(java.io.OutputStream out);
  +
  +    /**
  +     * This is what the test has written to System.out
  +     */
  +    public void setSystemOutput( String out );
  +
  +    /**
  +     * This is what the test has written to System.err
  +     */
  +    public void setSystemError( String err );
  +
   }
  
  
  
  1.18      +30 -4     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  
  Index: JUnitTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- JUnitTask.java	2001/02/02 12:36:56	1.17
  +++ JUnitTask.java	2001/05/03 15:03:58	1.18
  @@ -66,6 +66,7 @@
   import org.apache.tools.ant.types.Environment;
   import org.apache.tools.ant.types.CommandlineJava;
   import org.apache.tools.ant.types.Path;
  +import org.apache.tools.ant.types.EnumeratedAttribute;
   
   
   import java.io.File;
  @@ -88,6 +89,7 @@
    * @author Thomas Haas
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
    * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
  + * @author <a href="mailto:Gerrit.Riessen@web.de">Gerrit Riessen</a> -
    */
   public class JUnitTask extends Task {
   
  @@ -99,6 +101,7 @@
   
       private Integer timeout = null;
       private boolean summary = false;
  +    private String summaryValue = "";
   
       /**
        * Tells this task to halt when there is an error in a test.
  @@ -128,6 +131,7 @@
           }
       }
   
  +
       /**
        * Tells whether a JVM should be forked for each testcase. It avoids interference
        * between testcases and possibly avoids hanging the build.
  @@ -147,11 +151,31 @@
   
       /**
        * Tells whether the task should print a short summary of the task.
  -     * @param   value   <tt>true</tt> to print a summary, <tt>false</tt>
otherwise.
  +     * @param value <tt>true</tt> to print a summary,
  +     *   <tt>withOutAndErr</tt> to include the test&apos;s output as
  +     *   well, <tt>false</tt> otherwise.
        * @see SummaryJUnitResultFormatter
  +     */
  +    public void setPrintsummary(SummaryAttribute value) {
  +        summaryValue = value.getValue();
  +        summary = value.asBoolean();
  +    }
  +
  +    /** 
  +     * Print summary enumeration values.
        */
  -    public void setPrintsummary(boolean value) {
  -        summary = value;
  +    public static class SummaryAttribute extends EnumeratedAttribute {
  +        public String[] getValues() {
  +            return new String[] {"true", "yes", "false", "no", 
  +                                 "on", "off", "withOutAndErr"};
  +        }
  +
  +        public boolean asBoolean() {
  +            return "true".equals(value)
  +                || "on".equals(value)
  +                || "yes".equals(value)
  +                || "withOutAndErr".equals(value);
  +        }
       }
   
       /**
  @@ -389,7 +413,9 @@
               if (summary) {
                   log("Running " + test.getName(), Project.MSG_INFO);
   
  -                SummaryJUnitResultFormatter f = new SummaryJUnitResultFormatter();
  +                SummaryJUnitResultFormatter f = 
  +                  new SummaryJUnitResultFormatter();
  +                f.setWithOutAndErr( "withoutanderr".equalsIgnoreCase( summaryValue ));
                   f.setOutput( getDefaultOutput() );
                   runner.addFormatter(f);
               }
  
  
  
  1.8       +35 -4     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
  
  Index: JUnitTestRunner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JUnitTestRunner.java	2001/03/02 15:59:24	1.7
  +++ JUnitTestRunner.java	2001/05/03 15:03:59	1.8
  @@ -206,10 +206,31 @@
               junitTest.setCounts(1, 0, 1);
               junitTest.setRunTime(0);
           } else {
  -            suite.run(res);
  -            junitTest.setCounts(res.runCount(), res.failureCount(), 
  -                                res.errorCount());
  -            junitTest.setRunTime(System.currentTimeMillis() - start);
  +
  +
  +            PrintStream oldErr = System.err;
  +            PrintStream oldOut = System.out;
  +              
  +            ByteArrayOutputStream errStrm = new ByteArrayOutputStream();
  +            System.setErr(new PrintStream(errStrm));
  +            
  +            ByteArrayOutputStream outStrm = new ByteArrayOutputStream();
  +            System.setOut(new PrintStream(outStrm));
  +
  +            try {
  +                suite.run(res);
  +            } finally {
  +                System.err.close();
  +                System.out.close();
  +                System.setErr(oldErr);
  +                System.setOut(oldOut);
  +                sendOutAndErr(new String(outStrm.toByteArray()),
  +                              new String(errStrm.toByteArray()));
  +
  +                junitTest.setCounts(res.runCount(), res.failureCount(), 
  +                                    res.errorCount());
  +                junitTest.setRunTime(System.currentTimeMillis() - start);
  +            }
           }
           fireEndTestSuite();
   
  @@ -271,6 +292,16 @@
       public void addError(Test test, Throwable t) {
           if (haltOnError) {
               res.stop();
  +        }
  +    }
  +
  +    private void sendOutAndErr(String out, String err) {
  +        for (int i=0; i<formatters.size(); i++) {
  +            JUnitResultFormatter formatter = 
  +                ((JUnitResultFormatter)formatters.elementAt(i));
  +            
  +            formatter.setSystemOutput(out);
  +            formatter.setSystemError(err);
           }
       }
   
  
  
  
  1.5       +32 -3     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java
  
  Index: PlainJUnitResultFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PlainJUnitResultFormatter.java	2001/01/29 16:04:37	1.4
  +++ PlainJUnitResultFormatter.java	2001/05/03 15:04:00	1.5
  @@ -96,6 +96,9 @@
        */
       private boolean failed = true;
   
  +    private String systemOutput = null;
  +    private String systemError = null;
  +
       public PlainJUnitResultFormatter() {
           inner = new StringWriter();
           wri = new PrintWriter(inner);
  @@ -105,6 +108,14 @@
           this.out = out;
       }
   
  +    public void setSystemOutput(String out) {
  +        systemOutput = out;
  +    }
  +
  +    public void setSystemError(String err) {
  +        systemError = err;
  +    }
  +
       /**
        * Empty.
        */
  @@ -115,9 +126,10 @@
        * The whole testsuite ended.
        */
       public void endTestSuite(JUnitTest suite) throws BuildException {
  +        String newLine = System.getProperty("line.separator");
           StringBuffer sb = new StringBuffer("Testsuite: ");
           sb.append(suite.getName());
  -        sb.append(System.getProperty("line.separator"));
  +        sb.append(newLine);
           sb.append("Tests run: ");
           sb.append(suite.runCount());
           sb.append(", Failures: ");
  @@ -127,8 +139,25 @@
           sb.append(", Time elapsed: ");
           sb.append(nf.format(suite.getRunTime()/1000.0));
           sb.append(" sec");
  -        sb.append(System.getProperty("line.separator"));
  -        sb.append(System.getProperty("line.separator"));
  +        sb.append(newLine);
  +        sb.append(newLine);
  +        
  +        // append the err and output streams to the log
  +        if (systemOutput != null && systemOutput.length() > 0) {
  +            sb.append("------------- Standard Output ---------------" )
  +                .append(newLine)
  +                .append(systemOutput)
  +                .append("------------- ---------------- ---------------" )
  +                .append(newLine);
  +        }
  +        
  +        if (systemError != null && systemError.length() > 0) {
  +            sb.append("------------- Standard Error -----------------" )
  +                .append(newLine)
  +                .append(systemError)
  +                .append("------------- ---------------- ---------------" )
  +                .append(newLine);
  +        }
   
           if (out != null) {
               try {
  
  
  
  1.6       +35 -1     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java
  
  Index: SummaryJUnitResultFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SummaryJUnitResultFormatter.java	2001/01/29 16:04:37	1.5
  +++ SummaryJUnitResultFormatter.java	2001/05/03 15:04:01	1.6
  @@ -79,6 +79,10 @@
        */
       private OutputStream out;
   
  +    private boolean withOutAndErr = false;
  +    private String systemOutput = null;
  +    private String systemError = null;
  +
       /**
        * Empty
        */
  @@ -116,10 +120,27 @@
           this.out = out;
       }
   
  +    public void setSystemOutput( String out ) {
  +        systemOutput = out;
  +    }
  +
  +    public void setSystemError( String err ) {
  +        systemError = err;
  +    }
  +
       /**
  +     * Should the output to System.out and System.err be written to
  +     * the summary.
  +     */
  +    public void setWithOutAndErr( boolean value ) {
  +        withOutAndErr = value;
  +    }
  +
  +    /**
        * The whole testsuite ended.
        */
       public void endTestSuite(JUnitTest suite) throws BuildException {
  +        String newLine = System.getProperty("line.separator");
           StringBuffer sb = new StringBuffer("Tests run: ");
           sb.append(suite.runCount());
           sb.append(", Failures: ");
  @@ -129,7 +150,20 @@
           sb.append(", Time elapsed: ");
           sb.append(nf.format(suite.getRunTime()/1000.0));
           sb.append(" sec");
  -        sb.append(System.getProperty("line.separator"));
  +        sb.append(newLine);
  +
  +        if (withOutAndErr) {
  +            if (systemOutput != null && systemOutput.length() > 0) {
  +                sb.append( "Output:" ).append(newLine).append(systemOutput)
  +                    .append(newLine);
  +            }
  +            
  +            if (systemError != null && systemError.length() > 0) {
  +                sb.append( "Error: " ).append(newLine).append(systemError)
  +                    .append(newLine);
  +            }
  +        }
  +
           try {
               out.write(sb.toString().getBytes());
               out.flush();
  
  
  
  1.2       +6 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLConstants.java
  
  Index: XMLConstants.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLConstants.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLConstants.java	2001/02/01 15:28:21	1.1
  +++ XMLConstants.java	2001/05/03 15:04:02	1.2
  @@ -80,6 +80,12 @@
       /** the failure element */
       public final static String FAILURE = "failure";
   
  +    /** the system-err element */
  +    public final static String SYSTEM_ERR = "system-err";
  +
  +    /** the system-out element */
  +    public final static String SYSTEM_OUT = "system-out";
  +
       /** package attribute for the aggregate document */
       public final static String ATTR_PACKAGE = "package";
   
  
  
  
  1.7       +15 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java
  
  Index: XMLJUnitResultFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- XMLJUnitResultFormatter.java	2001/02/01 15:56:43	1.6
  +++ XMLJUnitResultFormatter.java	2001/05/03 15:04:03	1.7
  @@ -117,6 +117,14 @@
           this.out = out;
       }
   
  +    public void setSystemOutput(String out) {
  +        formatOutput(SYSTEM_OUT, out);
  +    }
  +
  +    public void setSystemError(String out) {
  +        formatOutput(SYSTEM_ERR, out);
  +    }
  +
       /**
        * The whole testsuite started.
        */
  @@ -228,6 +236,13 @@
           t.printStackTrace(new PrintWriter(swr, true));
           Text trace = doc.createTextNode(swr.toString());
           nested.appendChild(trace);
  +    }
  +
  +    private void formatOutput(String type, String output) {
  +        Element nested = doc.createElement(type);
  +        rootElement.appendChild(nested);
  +        Text content = doc.createTextNode(output);
  +        nested.appendChild(content);
       }
   
   } // XMLJUnitResultFormatter
  
  
  

Mime
View raw message