From bb23f0df317e576dad9e6154166be3ac0c662714 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 4 Jul 2005 13:35:24 +0000 Subject: [PATCH] Make log output available to test runs git-svn-id: https://svn.apache.org/repos/asf/ant/sandbox/antlibs/antunit/trunk@209073 13f79535-47bb-0310-9956-ffa450edef68 --- docs/antunit.html | 55 +++++++ src/etc/testcases/antunit/base.xml | 4 + src/main/org/apache/ant/antunit/AntUnit.java | 1 + .../org/apache/ant/antunit/LogCapturer.java | 135 ++++++++++++++++++ .../org/apache/ant/antunit/AntUnitTest.java | 4 +- 5 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 src/main/org/apache/ant/antunit/LogCapturer.java diff --git a/docs/antunit.html b/docs/antunit.html index cc65853..df61c54 100644 --- a/docs/antunit.html +++ b/docs/antunit.html @@ -36,6 +36,61 @@ Currently only a single implementation of this interface is provided with this ant library.

+

Log output during each antunit test case is captured by an + instance of the LogCapturer class that is available via a project + reference named ant.antunit.log. The published interface of that + class is:

+ +
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed 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;
+
+public class LogCapturer {
+    public static final String REFERENCE_ID = "ant.antunit.log";
+
+    /**
+     * All messages with logLevel == Project.MSG_ERR.
+     */
+    public String getErrLog();
+    /**
+     * All messages with logLevel == Project.MSG_WARN or
+     * more severe.
+     */
+    public String getWarnLog();
+    /**
+     * All messages with logLevel == Project.MSG_INFO or
+     * more severe.
+     */
+    public String getInfoLog();
+    /**
+     * All messages with logLevel == Project.MSG_VERBOSE or
+     * more severe.
+     */
+    public String getVerboseLog();
+    /**
+     * All messages with logLevel == Project.MSG_DEBUG or
+     * more severe.
+     */
+    public String getDebugLog();
+}
+
+

Parameters

This task doesn't support any attributes.

diff --git a/src/etc/testcases/antunit/base.xml b/src/etc/testcases/antunit/base.xml index 73d9a66..f4df687 100644 --- a/src/etc/testcases/antunit/base.xml +++ b/src/etc/testcases/antunit/base.xml @@ -45,6 +45,10 @@ + + + + tearDown diff --git a/src/main/org/apache/ant/antunit/AntUnit.java b/src/main/org/apache/ant/antunit/AntUnit.java index 097bb25..fc847e8 100644 --- a/src/main/org/apache/ant/antunit/AntUnit.java +++ b/src/main/org/apache/ant/antunit/AntUnit.java @@ -107,6 +107,7 @@ public class AntUnit extends Task { v.add(SETUP); } v.add(name); + LogCapturer lc = new LogCapturer(newProject); try { newProject.executeTargets(v); } catch (AssertionFailedException e) { diff --git a/src/main/org/apache/ant/antunit/LogCapturer.java b/src/main/org/apache/ant/antunit/LogCapturer.java new file mode 100644 index 0000000..ed79067 --- /dev/null +++ b/src/main/org/apache/ant/antunit/LogCapturer.java @@ -0,0 +1,135 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed 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 org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.BuildListener; +import org.apache.tools.ant.Project; + +/** + * Captures log messages generated during an antunit task run and + * makes it available to tasks via a project reference. + * + *

This class captures all messaged generated during the build and + * adds itself as project reference to the project using the id + * ant.antunit.log.

+ */ +public class LogCapturer implements BuildListener { + public static final String REFERENCE_ID = "ant.antunit.log"; + + private StringBuffer err = new StringBuffer(); + private StringBuffer warn = new StringBuffer(); + private StringBuffer info = new StringBuffer(); + private StringBuffer verbose = new StringBuffer(); + private StringBuffer debug = new StringBuffer(); + private Project p; + + public LogCapturer(Project p) { + this.p = p; + p.addBuildListener(this); + p.addReference(REFERENCE_ID, this); + } + + /** + * All messages with logLevel == Project.MSG_ERR. + */ + public String getErrLog() { + return err.toString(); + } + /** + * All messages with logLevel == Project.MSG_WARN or + * more severe. + */ + public String getWarnLog() { + return warn.toString(); + } + /** + * All messages with logLevel == Project.MSG_INFO or + * more severe. + */ + public String getInfoLog() { + return info.toString(); + } + /** + * All messages with logLevel == Project.MSG_VERBOSE or + * more severe. + */ + public String getVerboseLog() { + return verbose.toString(); + } + /** + * All messages with logLevel == Project.MSG_DEBUG or + * more severe. + */ + public String getDebugLog() { + return debug.toString(); + } + + /** + * Empty. + */ + public void buildStarted(BuildEvent event) {} + /** + * Empty. + */ + public void targetStarted(BuildEvent event) {} + /** + * Empty. + */ + public void targetFinished(BuildEvent event) {} + /** + * Empty. + */ + public void taskStarted(BuildEvent event) {} + /** + * Empty. + */ + public void taskFinished(BuildEvent event) {} + + /** + * De-register. + */ + public void buildFinished(BuildEvent event) { + if (p != null && event.getProject() == p) { + p.removeBuildListener(this); + p.getReferences().remove(REFERENCE_ID); + p = null; + } + } + /** + * Record the message. + */ + public void messageLogged(BuildEvent event) { + if (event.getPriority() <= Project.MSG_ERR) { + err.append(event.getMessage()); + } + if (event.getPriority() <= Project.MSG_WARN) { + warn.append(event.getMessage()); + } + if (event.getPriority() <= Project.MSG_INFO) { + info.append(event.getMessage()); + } + if (event.getPriority() <= Project.MSG_VERBOSE) { + verbose.append(event.getMessage()); + } + if (event.getPriority() <= Project.MSG_DEBUG) { + debug.append(event.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/testcases/org/apache/ant/antunit/AntUnitTest.java b/src/testcases/org/apache/ant/antunit/AntUnitTest.java index 411b3e4..21f2b45 100644 --- a/src/testcases/org/apache/ant/antunit/AntUnitTest.java +++ b/src/testcases/org/apache/ant/antunit/AntUnitTest.java @@ -37,7 +37,7 @@ public class AntUnitTest extends BuildFileTest { index = log.indexOf("sandbox/antlibs/antunit/trunk/src/etc/testcases/" + "antunit/base.xml", index); assertTrue("file name", index > -1); - index = log.indexOf("Tests run: 4, Failures: 1, Errors: 1, Time " + index = log.indexOf("Tests run: 5, Failures: 1, Errors: 1, Time " + "elapsed: ", index); assertTrue("summary", index > -1); assertTrue("test1", log.indexOf("test1", index) > -1); @@ -45,6 +45,8 @@ public class AntUnitTest extends BuildFileTest { assertTrue("test3", log.indexOf("test3", index) == -1); assertTrue("test4", log.indexOf("test4", index) > -1); assertTrue("test5", log.indexOf("test5", index) > -1); + assertTrue("testLogCaptureActive", + log.indexOf("testLogCaptureActive", index) > -1); int index2 = log.indexOf("Caused an ERROR", index); assertTrue("test5 error", index2 > -1 && log.indexOf("test5 exits with error", index2) > -1);