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/19 21:13:42 UTC

svn commit: r432863 - in /ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit: BaseAntUnitListener.java PlainAntUnitListener.java

Author: bodewig
Date: Sat Aug 19 12:13:42 2006
New Revision: 432863

URL: http://svn.apache.org/viewvc?rev=432863&view=rev
Log:
factor out some code that may come handy when implementing the XML listener

Added:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/BaseAntUnitListener.java   (with props)
Modified:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/PlainAntUnitListener.java

Added: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/BaseAntUnitListener.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/BaseAntUnitListener.java?rev=432863&view=auto
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/BaseAntUnitListener.java (added)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/BaseAntUnitListener.java Sat Aug 19 12:13:42 2006
@@ -0,0 +1,171 @@
+/*
+ * 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.
+ *
+ */
+
+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.FileUtils;
+import org.apache.tools.ant.util.TeeOutputStream;
+
+/**
+ * A test listener for <antunit> modeled aftern the Plain JUnit
+ * test listener that is part of Ant.
+ */
+public abstract class BaseAntUnitListener extends ProjectComponent
+    implements AntUnitListener {
+
+    protected BaseAntUnitListener(SendLogTo defaultReportTarget) {
+        logTo = defaultReportTarget;
+    }
+
+    /**
+     * Formatter for timings.
+     */
+    protected static final NumberFormat nf = NumberFormat.getInstance();
+
+    /**
+     * Directory to write reports to.
+     */
+    private File toDir;
+
+    /**
+     * Directory to write reports to.
+     */
+    protected final File getToDir() {
+        return toDir;
+    }
+
+    /**
+     * Sets the directory to write test reports to.
+     */
+    public void setToDir(File f) {
+        toDir = f;
+    }
+
+    /**
+     * Where to send log.
+     */
+    private SendLogTo logTo;
+
+    /**
+     * keeps track of the numer of executed targets, the failures an errors.
+     */
+    protected int runCount, failureCount, errorCount;
+    /**
+     * time for the starts of the current test-suite and test-target.
+     */
+    protected long start, testStart;
+
+    /**
+     * Where to send the test report.
+     */
+    protected void setSendLogTo(SendLogTo logTo) {
+        this.logTo = logTo;
+    }
+
+    public void startTestSuite(Project testProject, String buildFile) {
+        start = System.currentTimeMillis();
+        runCount = failureCount = errorCount = 0;
+    }
+
+    protected final void close(OutputStream out) {
+        if (out != System.out && out != System.err) {
+            FileUtils.close(out);
+        }
+    }
+
+    public void startTest(String target) {
+        testStart = System.currentTimeMillis();
+        runCount++;
+    }
+    public void addFailure(String target, AssertionFailedException ae) {
+        failureCount++;
+    }
+    public void addError(String target, Throwable ae) {
+        errorCount++;
+    }
+
+    protected final 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)) {
+
+            buildFile = FileUtils.getFileUtils()
+                .removeLeadingPath(getProject().getBaseDir(),
+                                   new File(buildFile));
+            if (buildFile.length() > 0
+                && buildFile.charAt(0) == File.separatorChar) {
+                buildFile = buildFile.substring(1);
+            }
+            
+            String fileName = "TEST-" +
+                buildFile.replace(File.separatorChar, '.').replace(':', '.')
+                + ".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};
+        }
+    }
+}
\ No newline at end of file

Propchange: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/BaseAntUnitListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=432863&r1=432862&r2=432863&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 Sat Aug 19 12:13:42 2006
@@ -40,14 +40,7 @@
  * A test listener for <antunit> modeled aftern the Plain JUnit
  * test listener that is part of Ant.
  */
-public class PlainAntUnitListener extends ProjectComponent
-    implements AntUnitListener {
-
-    /**
-     * Formatter for timings.
-     */
-    private NumberFormat nf = NumberFormat.getInstance();
-
+public class PlainAntUnitListener extends BaseAntUnitListener {
     private OutputStream out = null;
     /**
      * Helper to store intermediate output.
@@ -58,43 +51,21 @@
      */
     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;
-    /**
-     * time for the starts of the current test-suite and test-target.
-     */
-    private long start, testStart;
-
-    /**
-     * Sets the directory to write test reports to.
-     */
-    public void setToDir(File f) {
-        toDir = f;
+    public PlainAntUnitListener() {
+        super(new BaseAntUnitListener.SendLogTo(SendLogTo.ANT_LOG));
     }
 
     /**
      * Where to send the test report.
      */
-    public void setSendLogTo(SendLogTo logTo) {
-        this.logTo = logTo;
+    public void setSendLogTo(BaseAntUnitListener.SendLogTo logTo) {
+        super.setSendLogTo(logTo);
     }
 
     public void startTestSuite(Project testProject, String buildFile) {
+        super.startTestSuite(testProject, buildFile);
         inner = new StringWriter();
         wri = new PrintWriter(inner);
-        runCount = failureCount = errorCount;
         out = getOut(buildFile);
         String newLine = System.getProperty("line.separator");
         StringBuffer sb = new StringBuffer("Build File: ");
@@ -106,7 +77,6 @@
         } catch (IOException ex) {
             throw new BuildException("Unable to write output", ex);
         }
-        start = System.currentTimeMillis();
     }
 
     public void endTestSuite(Project testProject, String buildFile) {
@@ -132,21 +102,11 @@
             } catch (IOException ioex) {
                 throw new BuildException("Unable to write output", ioex);
             } finally {
-                if (out != System.out && out != System.err) {
-                    try {
-                        out.close();
-                    } catch (IOException e) {
-                        // ignore
-                    }
-                }
+                close(out);
             }
         }
     }
 
-    public void startTest(String target) {
-        testStart = System.currentTimeMillis();
-        runCount++;
-    }
     public void endTest(String target) {
         wri.print("Target: " + target);
         double seconds = (System.currentTimeMillis() - testStart) / 1000.0;
@@ -154,11 +114,11 @@
     }
 
     public void addFailure(String target, AssertionFailedException ae) {
-        failureCount++;
+        super.addFailure(target, ae);
         formatError("\tFAILED", ae);
     }
     public void addError(String target, Throwable ae) {
-        errorCount++;
+        super.addError(target, ae);
         formatError("\tCaused an ERROR", ae);
     }
 
@@ -167,58 +127,4 @@
         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)) {
-
-            buildFile = FileUtils.getFileUtils()
-                .removeLeadingPath(getProject().getBaseDir(),
-                                   new File(buildFile));
-            if (buildFile.length() > 0
-                && buildFile.charAt(0) == File.separatorChar) {
-                buildFile = buildFile.substring(1);
-            }
-            
-            String fileName = "TEST-" +
-                buildFile.replace(File.separatorChar, '.').replace(':', '.')
-                + ".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