You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2006/08/18 23:59:17 UTC

svn commit: r432741 - in /ant/antlibs/antunit/trunk: docs/ src/etc/testcases/ src/main/org/apache/ant/antunit/

Author: bodewig
Date: Fri Aug 18 14:59:15 2006
New Revision: 432741

URL: http://svn.apache.org/viewvc?rev=432741&view=rev
Log:
allow plainlistener to write reports to files

Added:
    ant/antlibs/antunit/trunk/docs/plainlistener.html   (with props)
Modified:
    ant/antlibs/antunit/trunk/docs/antunit.html
    ant/antlibs/antunit/trunk/src/etc/testcases/antunit.xml
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitListener.java
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/PlainAntUnitListener.java

Modified: ant/antlibs/antunit/trunk/docs/antunit.html
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/docs/antunit.html?rev=432741&r1=432740&r2=432741&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/docs/antunit.html (original)
+++ ant/antlibs/antunit/trunk/docs/antunit.html Fri Aug 18 14:59:15 2006
@@ -127,9 +127,10 @@
 
     <p>Creates a test listener that gets attached to the task.</p>
 
-    <p>The only listener that is part of this antlib is
-    &lt;plainlistener/&gt; this one creates reports similar to the
-    "plain" &lt;formatter&gt; of the &lt;junit&gt; task.</p>
+    <p>The only listener that is part of this antlib is <a
+    href="plainlistener.html">&lt;plainlistener/&gt;</a> this one
+    creates reports similar to the "plain" &lt;formatter&gt; of the
+    &lt;junit&gt; task.</p>
 
     <h4>propertyset</h4>
 

Added: ant/antlibs/antunit/trunk/docs/plainlistener.html
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/docs/plainlistener.html?rev=432741&view=auto
==============================================================================
--- ant/antlibs/antunit/trunk/docs/plainlistener.html (added)
+++ ant/antlibs/antunit/trunk/docs/plainlistener.html Fri Aug 18 14:59:15 2006
@@ -0,0 +1,56 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<html>
+  <head>
+    <meta http-equiv="Content-Language" content="en-us"></meta>
+    <title>PlainListener AntUnit Listener</title>
+  </head>
+
+  <body>
+    <h2><a name="plainlistener">Plain AntUnit Listener</a></h2>
+
+    <h3>Description</h3>
+
+    <p>Writes reports similar to the "plain" JUnit formatter.</p>
+
+    <h3>Parameters</h3>
+    <table border="1" cellpadding="2" cellspacing="0">
+      <tr>
+        <td valign="top"><b>Attribute</b></td>
+        <td valign="top"><b>Description</b></td>
+        <td align="center" valign="top"><b>Required</b></td>
+      </tr>
+      <tr>
+        <td valign="top">sendLogTo</td>
+        <td valign="top">Where the output will be written to.  Valid
+            values are "ant" for Ant's logging system, "file" for a
+            file and "both" for either of both.  Defaults to "ant".</td>
+        <td align="center">No.</td>
+      </tr>
+      <tr>
+        <td valign="top">toDir</td>
+        <td valign="top">Directory where testreports will be written
+            to.  Default's to the project's basedir.</td>
+        <td align="center">No.</td>
+      </tr>
+    </table>
+
+
+  </body>
+</html>

Propchange: ant/antlibs/antunit/trunk/docs/plainlistener.html
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: ant/antlibs/antunit/trunk/src/etc/testcases/antunit.xml
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/etc/testcases/antunit.xml?rev=432741&r1=432740&r2=432741&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/etc/testcases/antunit.xml (original)
+++ ant/antlibs/antunit/trunk/src/etc/testcases/antunit.xml Fri Aug 18 14:59:15 2006
@@ -68,9 +68,11 @@
   </target>
 
   <target name="antunit-copy">
+    <property name="reportsdir" location="../../../build/reports"/>
+    <mkdir dir="${reportsdir}"/>
     <au:antunit>
       <fileset dir="antunit" includes="copy.xml"/>
-      <au:plainlistener/>
+      <au:plainlistener sendLogTo="both" toDir="${reportsdir}"/>
     </au:antunit>
   </target>
 </project>

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java?rev=432741&r1=432740&r2=432741&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnit.java Fri Aug 18 14:59:15 2006
@@ -382,14 +382,13 @@
         BuildToAntUnitListener(String buildFile, AntUnitListener a) {
             this.buildFile = buildFile;
             this.a = a;
-            a.setOutput(new LogOutputStream(AntUnit.this, Project.MSG_INFO));
         }
 
         public void buildStarted(BuildEvent event) {
-            a.startTestSuite(buildFile);
+            a.startTestSuite(event.getProject(), buildFile);
         }
         public void buildFinished(BuildEvent event) {
-            a.endTestSuite(buildFile);
+            a.endTestSuite(event.getProject(), buildFile);
         }
         public void targetStarted(BuildEvent event) {
             String tName = event.getTarget().getName();

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitListener.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitListener.java?rev=432741&r1=432740&r2=432741&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitListener.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/AntUnitListener.java Fri Aug 18 14:59:15 2006
@@ -20,27 +20,20 @@
 
 package org.apache.ant.antunit;
 
-import java.io.OutputStream;
+import org.apache.tools.ant.Project;
 
 /**
  * A test listener for &lt;antunit&gt;.
  */
 public interface AntUnitListener {
     /**
-     * Sets the stream the listener shall write its output to.
-     *
-     * <p>Usually points to Ant's logging system.</p>
-     */
-    void setOutput(OutputStream out);
-
-    /**
      * Invoked once per build file, before any targets get executed.
      */
-    void startTestSuite(String buildFile);
+    void startTestSuite(Project testProject, String buildFile);
     /**
      * Invoked once per build file, after all targets have been executed.
      */
-    void endTestSuite(String buildFile);
+    void endTestSuite(Project testProject, String buildFile);
     /**
      * Invoked before a test target gets executed.
      */

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/PlainAntUnitListener.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/PlainAntUnitListener.java?rev=432741&r1=432740&r2=432741&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/PlainAntUnitListener.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/PlainAntUnitListener.java Fri Aug 18 14:59:15 2006
@@ -21,18 +21,27 @@
 package org.apache.ant.antunit;
 
 import java.io.IOException;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.text.NumberFormat;
 
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.ProjectComponent;
+import org.apache.tools.ant.taskdefs.LogOutputStream;
+import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.util.TeeOutputStream;
 
 /**
  * A test listener for &lt;antunit&gt; modeled aftern the Plain JUnit
  * test listener that is part of Ant.
  */
-public class PlainAntUnitListener implements AntUnitListener {
+public class PlainAntUnitListener extends ProjectComponent
+    implements AntUnitListener {
+
     /**
      * Formatter for timings.
      */
@@ -49,6 +58,16 @@
     private PrintWriter wri;
 
     /**
+     * Directory to write reports to.
+     */
+    private File toDir;
+
+    /**
+     * Where to send log.
+     */
+    private SendLogTo logTo = new SendLogTo(SendLogTo.ANT_LOG);
+
+    /**
      * keeps track of the numer of executed targets, the failures an errors.
      */
     private int runCount, failureCount, errorCount;
@@ -57,17 +76,25 @@
      */
     private long start, testStart;
 
-    public void setOutput(OutputStream out) {
-        this.out = out;
+    /**
+     * Sets the directory to write test reports to.
+     */
+    public void setToDir(File f) {
+        toDir = f;
     }
 
-    public void startTestSuite(String buildFile) {
+    /**
+     * Where to send the test report.
+     */
+    public void setSendLogTo(SendLogTo logTo) {
+        this.logTo = logTo;
+    }
+
+    public void startTestSuite(Project testProject, String buildFile) {
         inner = new StringWriter();
         wri = new PrintWriter(inner);
         runCount = failureCount = errorCount;
-        if (out == null) {
-            return; // Quick return - no output do nothing.
-        }
+        out = getOut(buildFile);
         String newLine = System.getProperty("line.separator");
         StringBuffer sb = new StringBuffer("Build File: ");
         sb.append(buildFile);
@@ -81,7 +108,7 @@
         start = System.currentTimeMillis();
     }
 
-    public void endTestSuite(String buildFile) {
+    public void endTestSuite(Project testProject, String buildFile) {
         long runTime = System.currentTimeMillis() - start;
         String newLine = System.getProperty("line.separator");
         StringBuffer sb = new StringBuffer("Tests run: ");
@@ -139,4 +166,53 @@
         wri.println(t.getMessage());
     }
 
+    private OutputStream getOut(String buildFile) {
+        OutputStream l, f;
+        l = f = null;
+        if (logTo.getValue().equals(SendLogTo.ANT_LOG)
+            || logTo.getValue().equals(SendLogTo.BOTH)) {
+            l = new LogOutputStream(this, Project.MSG_INFO);
+            if (logTo.getValue().equals(SendLogTo.ANT_LOG)) {
+                return l;
+            }
+        }
+        if (logTo.getValue().equals(SendLogTo.FILE)
+            || logTo.getValue().equals(SendLogTo.BOTH)) {
+            if (buildFile.length() > 0
+                && buildFile.charAt(0) == File.separatorChar) {
+                buildFile = buildFile.substring(1);
+            }
+            
+            String fileName =
+                buildFile.replace(File.separatorChar, '.') + ".txt";
+            File file = toDir == null
+                ? getProject().resolveFile(fileName)
+                : new File(toDir, fileName);
+            try {
+                f = new FileOutputStream(file);
+            } catch (IOException e) {
+                throw new BuildException(e);
+            }
+            if (logTo.getValue().equals(SendLogTo.FILE)) {
+                return f;
+            }
+        }
+        return new TeeOutputStream(l, f);
+    }
+
+    public static class SendLogTo extends EnumeratedAttribute {
+        public static final String ANT_LOG = "ant";
+        public static final String FILE = "file";
+        public static final String BOTH = "both";
+
+        public SendLogTo() {}
+
+        public SendLogTo(String s) {
+            setValue(s);
+        }
+
+        public String[] getValues() {
+            return new String[] {ANT_LOG, FILE, BOTH};
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org