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
This commit is contained in:
Stefan Bodewig 2008-04-08 10:30:44 +00:00
parent 70c430e51c
commit f78bb6947e
5 changed files with 291 additions and 5 deletions

View File

@ -51,7 +51,14 @@ under the License.
basedir.</td>
<td align="center">No.</td>
</tr>
<tr>
<td valign="top">logLevel</td>
<td valign="top">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.</td>
<td align="center">No. Default is 'none'.</td>
</tr>
</table>
</body>
</html>

View File

@ -51,6 +51,14 @@ under the License.
to. Defaults to the project's basedir.</td>
<td align="center">No.</td>
</tr>
<tr>
<td valign="top">logLevel</td>
<td valign="top">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.</td>
<td align="center">No. Default is 'none'.</td>
</tr>
</table>
<h3>Examples</h3>

View File

@ -0,0 +1,104 @@
<?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="plainlistener-test"
default="all"
xmlns:au="antlib:org.apache.ant.antunit">
<target name="showdefault">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener />
</au:antunit>
</target>
<target name="showerror">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener logLevel="error" />
</au:antunit>
</target>
<target name="showwarning">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener logLevel="warning" />
</au:antunit>
</target>
<target name="showinfo">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener logLevel="info" />
</au:antunit>
</target>
<target name="showverbose">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener logLevel="verbose" />
</au:antunit>
</target>
<target name="showdebug">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener logLevel="debug" />
</au:antunit>
</target>
<target name="shownone">
<au:antunit failOnError="false">
<file file="${ant.file}" />
<au:plainlistener logLevel="none" />
</au:antunit>
</target>
<target name="suiteSetUp">
<echo>suiteSetUp</echo>
</target>
<target name="suiteTearDown">
<echo>suiteTearDown</echo>
</target>
<target name="setUp">
<echo>setUp</echo>
</target>
<target name="tearDown">
<echo>tearDown</echo>
</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>
<target name="test2">
<echo>test2</echo>
</target>
</project>

View File

@ -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);
}
}

View File

@ -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);
}
*/
}