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
This commit is contained in:
Stefan Bodewig 2005-07-04 13:35:24 +00:00
parent 9d993df70f
commit bb23f0df31
5 changed files with 198 additions and 1 deletions

View File

@ -36,6 +36,61 @@
Currently only a single implementation of this interface is
provided with this ant library.</p>
<p>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:</p>
<pre>
/*
* 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 <code>logLevel == Project.MSG_ERR</code>.
*/
public String getErrLog();
/**
* All messages with <code>logLevel == Project.MSG_WARN</code> or
* more severe.
*/
public String getWarnLog();
/**
* All messages with <code>logLevel == Project.MSG_INFO</code> or
* more severe.
*/
public String getInfoLog();
/**
* All messages with <code>logLevel == Project.MSG_VERBOSE</code> or
* more severe.
*/
public String getVerboseLog();
/**
* All messages with <code>logLevel == Project.MSG_DEBUG</code> or
* more severe.
*/
public String getDebugLog();
}
</pre>
<h3>Parameters</h3>
<p>This task doesn't support any attributes.</p>

View File

@ -45,6 +45,10 @@
<fail message="test5 exits with error"/>
</target>
<target name="testLogCaptureActive">
<au:assertReferenceSet refid="ant.antunit.log"/>
</target>
<target name="tearDown">
<echo>tearDown</echo>
</target>

View File

@ -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) {

View File

@ -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.
*
* <p>This class captures all messaged generated during the build and
* adds itself as project reference to the project using the id
* <code>ant.antunit.log</code>.</p>
*/
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 <code>logLevel == Project.MSG_ERR</code>.
*/
public String getErrLog() {
return err.toString();
}
/**
* All messages with <code>logLevel == Project.MSG_WARN</code> or
* more severe.
*/
public String getWarnLog() {
return warn.toString();
}
/**
* All messages with <code>logLevel == Project.MSG_INFO</code> or
* more severe.
*/
public String getInfoLog() {
return info.toString();
}
/**
* All messages with <code>logLevel == Project.MSG_VERBOSE</code> or
* more severe.
*/
public String getVerboseLog() {
return verbose.toString();
}
/**
* All messages with <code>logLevel == Project.MSG_DEBUG</code> 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());
}
}
}

View File

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