You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2008/04/08 12:07:16 UTC

svn commit: r645822 - /ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/listener/BaseAntUnitListener.java

Author: bodewig
Date: Tue Apr  8 03:07:08 2008
New Revision: 645822

URL: http://svn.apache.org/viewvc?rev=645822&view=rev
Log:
Provide infrastructure for Plain- and XMLListener to capture log output

Modified:
    ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/listener/BaseAntUnitListener.java

Modified: ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/listener/BaseAntUnitListener.java
URL: http://svn.apache.org/viewvc/ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/listener/BaseAntUnitListener.java?rev=645822&r1=645821&r2=645822&view=diff
==============================================================================
--- ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/listener/BaseAntUnitListener.java (original)
+++ ant/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/listener/BaseAntUnitListener.java Tue Apr  8 03:07:08 2008
@@ -28,12 +28,15 @@
 
 import org.apache.ant.antunit.AntUnitListener;
 import org.apache.ant.antunit.AssertionFailedException;
+import org.apache.tools.ant.BuildEvent;
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.BuildListener;
 import org.apache.tools.ant.Location;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.taskdefs.LogOutputStream;
 import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.types.LogLevel;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.ant.util.TeeOutputStream;
 
@@ -48,6 +51,7 @@
                                   String extension) {
         logTo = defaultReportTarget;
         this.extension = extension;
+        logLevel = BaseAntUnitListener.AntUnitLogLevel.NONE;
     }
 
     /**
@@ -198,12 +202,36 @@
 
     public void setCurrentTestProject(Project p) {
         currentTest = p;
+        p.addBuildListener(new LogGrabber());
     }
 
     protected Project getCurrentTestProject() {
         return currentTest;
     }
 
+    /**
+     * The minimum level a log message must be logged at to be
+     * included in the output.
+     */
+    private AntUnitLogLevel logLevel;
+
+    /**
+     * Sets the minimum level a log message must be logged at to be
+     * included in the output.
+     */
+    public void setLogLevel(AntUnitLogLevel l) {
+        logLevel = l;
+    }
+
+    /**
+     * Gets messages from the project running the test target if their
+     * level is at least of the level specified with {@link
+     * #setLoglevel setLogLevel}.
+     *
+     * <p>This implementation is empty.</p>
+     */
+    protected void messageLogged(BuildEvent event) {}
+
     public static class SendLogTo extends EnumeratedAttribute {
         public static final String ANT_LOG = "ant";
         public static final String FILE = "file";
@@ -217,6 +245,60 @@
 
         public String[] getValues() {
             return new String[] {ANT_LOG, FILE, BOTH};
+        }
+    }
+
+    public static class AntUnitLogLevel extends EnumeratedAttribute {
+        public static final AntUnitLogLevel NONE = new AntUnitLogLevel("none");
+
+        public AntUnitLogLevel() {
+            super();
+        }
+
+        private AntUnitLogLevel(String value) {
+            super();
+            setValue(value);
+        }
+
+        public String[] getValues() {
+            return new String[] {
+                "none",
+                "error",
+                "warn",
+                "warning",
+                "info",
+                "verbose",
+                "debug"};
+        }
+
+        private static int[] levels = {
+            Project.MSG_ERR - 1,
+            Project.MSG_ERR,
+            Project.MSG_WARN,
+            Project.MSG_WARN,
+            Project.MSG_INFO,
+            Project.MSG_VERBOSE,
+            Project.MSG_DEBUG
+        };
+
+        public int getLevel() {
+            return levels[getIndex()];
+        }
+    }
+
+    public class LogGrabber implements BuildListener {
+        public void buildStarted(BuildEvent event) {}
+        public void buildFinished(BuildEvent event) {}
+        public void targetStarted(BuildEvent event) {}
+        public void targetFinished(BuildEvent event) {}
+        public void taskStarted(BuildEvent event) {}
+        public void taskFinished(BuildEvent event) {}
+        public void messageLogged(BuildEvent event) {
+            int priority = event.getPriority();
+            // Filter out messages based on priority
+            if (priority <= logLevel.getLevel()) {
+                BaseAntUnitListener.this.messageLogged(event);
+            }
         }
     }
 }