From f78bb6947ec10bc2c889d8cfe376891ee0a0fe3a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 8 Apr 2008 10:30:44 +0000 Subject: [PATCH] Add logging of messages to plainlistener, based on code submitted by David Jackman git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@645829 13f79535-47bb-0310-9956-ffa450edef68 --- docs/plainlistener.html | 9 +- docs/xmllistener.html | 8 + src/etc/testcases/listener/plainlistener.xml | 104 ++++++++++++ .../listener/PlainAntUnitListener.java | 27 +++- .../antunit/listener/PlainListenerTest.java | 148 ++++++++++++++++++ 5 files changed, 291 insertions(+), 5 deletions(-) create mode 100755 src/etc/testcases/listener/plainlistener.xml create mode 100755 src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java diff --git a/docs/plainlistener.html b/docs/plainlistener.html index a459288..a7a7428 100644 --- a/docs/plainlistener.html +++ b/docs/plainlistener.html @@ -51,7 +51,14 @@ under the License. basedir. No. + + logLevel + Log level for messages from the tests to + include in the report. Must be one of: none, error, warn, + warning, info, verbose, and debug. Messages at the given level + or below will be included. + No. Default is 'none'. + - diff --git a/docs/xmllistener.html b/docs/xmllistener.html index 2c08497..9a2e048 100644 --- a/docs/xmllistener.html +++ b/docs/xmllistener.html @@ -51,6 +51,14 @@ under the License. to. Defaults to the project's basedir. No. + + logLevel + Log level for messages from the tests to + include in the report. Must be one of: none, error, warn, + warning, info, verbose, and debug. Messages at the given level + or below will be included. + No. Default is 'none'. +

Examples

diff --git a/src/etc/testcases/listener/plainlistener.xml b/src/etc/testcases/listener/plainlistener.xml new file mode 100755 index 0000000..aa37b6c --- /dev/null +++ b/src/etc/testcases/listener/plainlistener.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + suiteSetUp + + + + suiteTearDown + + + + setUp + + + + tearDown + + + + debugmessage + verbosemessage + infomessage + warningmessage + errormessage + + + + test2 + + + diff --git a/src/main/org/apache/ant/antunit/listener/PlainAntUnitListener.java b/src/main/org/apache/ant/antunit/listener/PlainAntUnitListener.java index 45d3c02..906b3db 100644 --- a/src/main/org/apache/ant/antunit/listener/PlainAntUnitListener.java +++ b/src/main/org/apache/ant/antunit/listener/PlainAntUnitListener.java @@ -27,6 +27,7 @@ import java.io.StringWriter; import org.apache.ant.antunit.AssertionFailedException; +import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Location; import org.apache.tools.ant.Project; @@ -45,6 +46,12 @@ public class PlainAntUnitListener extends BaseAntUnitListener { * Convenience layer on top of {@link #inner inner}. */ private PrintWriter wri; + /** + * Collects log messages. + */ + private StringBuffer log = new StringBuffer(); + + private static final String NEW_LINE = System.getProperty("line.separator"); public PlainAntUnitListener() { super(new BaseAntUnitListener.SendLogTo(SendLogTo.ANT_LOG), "txt"); @@ -62,10 +69,9 @@ public class PlainAntUnitListener extends BaseAntUnitListener { inner = new StringWriter(); wri = new PrintWriter(inner); out = getOut(buildFile); - String newLine = System.getProperty("line.separator"); StringBuffer sb = new StringBuffer("Build File: "); sb.append(buildFile); - sb.append(newLine); + sb.append(NEW_LINE); try { out.write(sb.toString().getBytes()); out.flush(); @@ -76,7 +82,6 @@ public class PlainAntUnitListener extends BaseAntUnitListener { public void endTestSuite(Project testProject, String buildFile) { long runTime = System.currentTimeMillis() - start; - String newLine = System.getProperty("line.separator"); StringBuffer sb = new StringBuffer("Tests run: "); sb.append(runCount); sb.append(", Failures: "); @@ -86,7 +91,16 @@ public class PlainAntUnitListener extends BaseAntUnitListener { sb.append(", Time elapsed: "); sb.append(nf.format(runTime/ 1000.0)); sb.append(" sec"); - sb.append(newLine); + sb.append(NEW_LINE); + + if (log.length() > 0) { + sb.append("------------- Log Output ---------------"); + sb.append(NEW_LINE); + sb.append(log.toString()); + log.setLength(0); + sb.append("------------- ---------------- ---------------"); + sb.append(NEW_LINE); + } if (out != null) { try { @@ -135,4 +149,9 @@ public class PlainAntUnitListener extends BaseAntUnitListener { wri.print("\t"); } + protected void messageLogged(BuildEvent event) { + log.append(event.getMessage()); + log.append(NEW_LINE); + } + } \ No newline at end of file diff --git a/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java b/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java new file mode 100755 index 0000000..50b692d --- /dev/null +++ b/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java @@ -0,0 +1,148 @@ +/* + * 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 plain listener. + */ +public class PlainListenerTest extends BuildFileTest { + protected void setUp() throws Exception { + configureProject("src/etc/testcases/listener/plainlistener.xml"); + } + + public void testStdoutPlacement() { + executeTarget("showinfo"); + String log = getLog(); + int indexElapsed = log.indexOf("Time elapsed"); + int indexTarget = log.indexOf("Target"); + int index = log.indexOf("------------- Log Output ---------------"); + assertTrue("Standard output message not present", index > -1); + assertTrue("Standard output message not located after summary.", index > indexElapsed); + assertTrue("Standard output message not located before test details.", index < indexTarget); + int indexTest1 = log.indexOf("infomessage", index); + int indexTest2 = log.indexOf("test2", index); + assertTrue("infomessage", indexTest1 > -1); + assertTrue("test2", indexTest2 > -1); + index = log.indexOf("------------- ---------------- ---------------", Math.max(indexTest1, indexTest2)); + assertTrue("End of standard output message not present.", index > -1); + assertTrue("End of standard output message not located before test details.", index < indexTarget); + } + + public void testShowDefault() { + executeTarget("showdefault"); + String log = getLog(); + assertTrue("Should not have shown error message", -1 == log.indexOf("errormessage")); + assertTrue("Should not have shown warning message", -1 == log.indexOf("warningmessage")); + assertTrue("Should not have shown info message", -1 == log.indexOf("infomessage")); + assertTrue("Should not have shown verbose message", -1 == log.indexOf("verbosemessage")); + assertTrue("Should not have shown debug message", -1 == log.indexOf("debugmessage")); + } + + public void testShowError() { + executeTarget("showerror"); + String log = getLog(); + assertTrue("Should have shown error message", -1 != log.indexOf("errormessage")); + assertTrue("Should not have shown warning message", -1 == log.indexOf("warningmessage")); + assertTrue("Should not have shown info message", -1 == log.indexOf("infomessage")); + assertTrue("Should not have shown verbose message", -1 == log.indexOf("verbosemessage")); + assertTrue("Should not have shown debug message", -1 == log.indexOf("debugmessage")); + } + + public void testShowWarning() { + executeTarget("showwarning"); + String log = getLog(); + assertTrue("Should have shown error message", -1 != log.indexOf("errormessage")); + assertTrue("Should have shown warning message", -1 != log.indexOf("warningmessage")); + assertTrue("Should not have shown info message", -1 == log.indexOf("infomessage")); + assertTrue("Should not have shown verbose message", -1 == log.indexOf("verbosemessage")); + assertTrue("Should not have shown debug message", -1 == log.indexOf("debugmessage")); + } + + public void testShowInfo() { + executeTarget("showinfo"); + String log = getLog(); + assertTrue("Should have shown error message", -1 != log.indexOf("errormessage")); + assertTrue("Should have shown warning message", -1 != log.indexOf("warningmessage")); + assertTrue("Should have shown info message", -1 != log.indexOf("infomessage")); + assertTrue("Should not have shown verbose message", -1 == log.indexOf("verbosemessage")); + assertTrue("Should not have shown debug message", -1 == log.indexOf("debugmessage")); + } + + public void testShowVerbose() { + executeTarget("showverbose"); + String log = getLog(); + assertTrue("Should have shown error message", -1 != log.indexOf("errormessage")); + assertTrue("Should have shown warning message", -1 != log.indexOf("warningmessage")); + assertTrue("Should have shown info message", -1 != log.indexOf("infomessage")); + assertTrue("Should have shown verbose message", -1 != log.indexOf("verbosemessage")); + assertTrue("Should not have shown debug message", -1 == log.indexOf("debugmessage")); + } + + public void testShowDebug() { + executeTarget("showdebug"); + String log = getLog(); + assertTrue("Should have shown error message", -1 != log.indexOf("errormessage")); + assertTrue("Should have shown warning message", -1 != log.indexOf("warningmessage")); + assertTrue("Should have shown info message", -1 != log.indexOf("infomessage")); + assertTrue("Should have shown verbose message", -1 != log.indexOf("verbosemessage")); + assertTrue("Should have shown debug message", -1 != log.indexOf("debugmessage")); + } + + public void testShowNone() { + executeTarget("shownone"); + String log = getLog(); + assertTrue("Should not have shown error message", -1 == log.indexOf("errormessage")); + assertTrue("Should not have shown warning message", -1 == log.indexOf("warningmessage")); + assertTrue("Should not have shown info message", -1 == log.indexOf("infomessage")); + assertTrue("Should not have shown verbose message", -1 == log.indexOf("verbosemessage")); + assertTrue("Should not have shown debug message", -1 == log.indexOf("debugmessage")); + } + + public void testSetUpTearDown() { + executeTarget("showinfo"); + String log = getLog(); + int index = log.indexOf("setUp"); + assertTrue("First setUp not present", index > -1); + index = log.indexOf("setUp", index); + assertTrue("Second setUp not present", index > -1); + index = log.indexOf("tearDown"); + assertTrue("First tearDown not present", index > -1); + index = log.indexOf("tearDown", index); + assertTrue("Second tearDown not present", index > -1); + } + + /* + public void testSuiteSetUpTearDown() { + executeTarget("showinfo"); + String log = getLog(); + int index = log.indexOf("suiteSetUp"); + assertTrue("suiteSetUp not present", index > -1); + index = log.indexOf("suiteSetUp", index + 1); + assertTrue("suiteSetUp present more than once", index == -1); + index = log.indexOf("suiteTearDown"); + assertTrue("suiteTearDown not present", index > -1); + index = log.indexOf("suiteTearDown", index + 1); + assertTrue("suiteTearDown present more than once", index == -1); + } + */ +} \ No newline at end of file