PR 43582 add a new AntUnitListener that immediately forwards all log output

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@1591967 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/changes.xml b/changes.xml
index d836905..8107e63 100644
--- a/changes.xml
+++ b/changes.xml
@@ -46,6 +46,11 @@
       assertions work for non-filesystem resources unlike their
       existing cousins of AntUnit 1.2.
     </action>
+    <action type="add" issue="43582">
+      A new AntUnitListener named "logforwarder" can be attached to
+      forward any log output from the project under test to the
+      project running the AntUnit task immediately.
+    </action>
   </release>
 
   <release version="1.2" date="2011-08-16">
diff --git a/docs/antunit.html b/docs/antunit.html
index 4f9840c..92b819c 100644
--- a/docs/antunit.html
+++ b/docs/antunit.html
@@ -158,12 +158,18 @@
 
     <p>Creates a test listener that gets attached to the task.</p>
 
-    <p>Two listeners are part of this antlib <a
-    href="plainlistener.html">&lt;plainlistener/&gt;</a> which
-    creates reports similar to the "plain" &lt;formatter&gt; of the
-    &lt;junit&gt; task and <a
-    href="xmllistener.html">&lt;xmllistener/&gt;</a> which is similar
-    to the "xml" &lt;formatter&gt;.</p>
+    <p>Some listeners are part of this antlib:</p>
+
+    <ul>
+      <li><a href="plainlistener.html">&lt;plainlistener/&gt;</a>
+        creates reports similar to the "plain" &lt;formatter&gt; of the
+        &lt;junit&gt; task.</li>
+      <li><a href="xmllistener.html">&lt;xmllistener/&gt;</a> is similar
+        to the "xml" &lt;formatter&gt;.</li>
+      <li><a href="logforwarder.html">&lt;logforwarder/&gt;</a>
+        forwards log output from the project under test to the project
+        executing the antunit task.</li>
+    </ul>
 
     <h4>propertyset</h4>
 
diff --git a/docs/logforwarder.html b/docs/logforwarder.html
new file mode 100644
index 0000000..1f7b20b
--- /dev/null
+++ b/docs/logforwarder.html
@@ -0,0 +1,39 @@
+<!--
+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>
+    <link rel="stylesheet" type="text/css" href="style.css">
+    <title>LogForwarder AntUnit Listener</title>
+  </head>
+
+  <body>
+    <h2><a name="logforwarder">LogForwarder</a></h2>
+
+    <h3>Description</h3>
+
+    <p>Forwards any log output from the project under test to the
+      project running the AntUnit task immediately.  This is different
+      from - for example - the plain listener which buffers the
+      log output and emits it after the test has finished.</p>
+
+    <p>This listener can be used to debug failing tests more easily or
+      monitor tests in real time.</p>
+  </body>
+</html>
diff --git a/src/etc/testcases/listener/logforwarder.xml b/src/etc/testcases/listener/logforwarder.xml
new file mode 100644
index 0000000..b09c27a
--- /dev/null
+++ b/src/etc/testcases/listener/logforwarder.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0"?>
+<!--
+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.
+-->
+
+<project name="logforwarder-test" default="forward"
+         xmlns:au="antlib:org.apache.ant.antunit">
+
+  <target name="forward">
+    <au:antunit failOnError="false">
+      <file file="${ant.file}" />
+      <au:logforwarder />
+    </au:antunit>
+  </target>
+
+  <target name="testEcho">
+    <echo level="debug">debugmessage</echo>
+    <echo level="verbose">verbosemessage</echo>
+    <echo level="info">infomessage</echo>
+    <echo level="warning">warningmessage</echo>
+    <echo level="error">errormessage</echo>
+  </target>
+
+</project>
diff --git a/src/main/org/apache/ant/antunit/antlib.xml b/src/main/org/apache/ant/antunit/antlib.xml
index dce4f70..53f27a9 100644
--- a/src/main/org/apache/ant/antunit/antlib.xml
+++ b/src/main/org/apache/ant/antunit/antlib.xml
@@ -36,6 +36,9 @@
   <typedef name="failurelistener"
     classname="org.apache.ant.antunit.listener.FailureAntUnitListener"/>
 
+  <typedef name="logforwarder"
+    classname="org.apache.ant.antunit.listener.LogForwarder"/>
+
   <typedef name="logcapturer"
            classname="org.apache.ant.antunit.LogCapturer"/>
 
diff --git a/src/main/org/apache/ant/antunit/listener/LogForwarder.java b/src/main/org/apache/ant/antunit/listener/LogForwarder.java
new file mode 100644
index 0000000..6a73323
--- /dev/null
+++ b/src/main/org/apache/ant/antunit/listener/LogForwarder.java
@@ -0,0 +1,64 @@
+/*
+ * 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.listener;
+
+import org.apache.ant.antunit.AntUnitListener;
+import org.apache.ant.antunit.AssertionFailedException;
+
+import org.apache.tools.ant.BuildEvent;
+import org.apache.tools.ant.BuildListener;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.Project;
+
+/**
+ * A test listener for &lt;antunit&gt; who's whole purpose is to
+ * forward log output from the project under test to the project
+ * executing the &lt;antunit&gt; task.
+ */
+public class LogForwarder implements AntUnitListener {
+
+    private Task parentTask;
+
+    public void setParentTask(Task t) {
+        parentTask = t;
+    }
+
+    public void setCurrentTestProject(Project p) {
+        p.addBuildListener(new 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) {
+                    parentTask.log(event.getMessage(), event.getPriority());
+                }
+            });
+    }
+
+    public void startTestSuite(Project testProject, String buildFile) {}
+    public void endTestSuite(Project testProject, String buildFile) {}
+    public void startTest(String target) {}
+    public void endTest(String target) {}
+    public void addFailure(String target, AssertionFailedException ae) {}
+    public void addError(String target, Throwable ae) {}
+}
diff --git a/src/tests/junit/org/apache/ant/antunit/listener/LogForwarderTest.java b/src/tests/junit/org/apache/ant/antunit/listener/LogForwarderTest.java
new file mode 100644
index 0000000..d44035a
--- /dev/null
+++ b/src/tests/junit/org/apache/ant/antunit/listener/LogForwarderTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.listener;
+
+import org.apache.tools.ant.BuildFileTest;
+
+/**
+ * Tests the log forwarder.
+ */
+public class LogForwarderTest extends BuildFileTest {
+    protected void setUp() throws Exception {
+        configureProject("src/etc/testcases/listener/logforwarder.xml");
+    }
+
+    public void testForward() {
+        executeTarget("forward");
+        assertLogContaining("infomessage");
+        assertLogContaining("warningmessage");
+        assertLogContaining("errormessage");
+        assertLogNotContaining("debugmessage");
+        assertLogNotContaining("verbosemessage");
+        assertDebuglogContaining("debugmessage");
+        assertDebuglogContaining("verbosemessage");
+    }
+}