allow plainlistener to write reports to files

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@432741 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stefan Bodewig 2006-08-18 21:59:15 +00:00
parent 52342dc3b5
commit 77d58419c4
6 changed files with 152 additions and 25 deletions

View File

@ -127,9 +127,10 @@ public class LogCapturer {
<p>Creates a test listener that gets attached to the task.</p>
<p>The only listener that is part of this antlib is
&lt;plainlistener/&gt; this one creates reports similar to the
"plain" &lt;formatter&gt; of the &lt;junit&gt; task.</p>
<p>The only listener that is part of this antlib is <a
href="plainlistener.html">&lt;plainlistener/&gt;</a> this one
creates reports similar to the "plain" &lt;formatter&gt; of the
&lt;junit&gt; task.</p>
<h4>propertyset</h4>

56
docs/plainlistener.html Normal file
View File

@ -0,0 +1,56 @@
<!--
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>
<title>PlainListener AntUnit Listener</title>
</head>
<body>
<h2><a name="plainlistener">Plain AntUnit Listener</a></h2>
<h3>Description</h3>
<p>Writes reports similar to the "plain" JUnit formatter.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">sendLogTo</td>
<td valign="top">Where the output will be written to. Valid
values are "ant" for Ant's logging system, "file" for a
file and "both" for either of both. Defaults to "ant".</td>
<td align="center">No.</td>
</tr>
<tr>
<td valign="top">toDir</td>
<td valign="top">Directory where testreports will be written
to. Default's to the project's basedir.</td>
<td align="center">No.</td>
</tr>
</table>
</body>
</html>

View File

@ -68,9 +68,11 @@ under the License.
</target>
<target name="antunit-copy">
<property name="reportsdir" location="../../../build/reports"/>
<mkdir dir="${reportsdir}"/>
<au:antunit>
<fileset dir="antunit" includes="copy.xml"/>
<au:plainlistener/>
<au:plainlistener sendLogTo="both" toDir="${reportsdir}"/>
</au:antunit>
</target>
</project>

View File

@ -382,14 +382,13 @@ public class AntUnit extends Task {
BuildToAntUnitListener(String buildFile, AntUnitListener a) {
this.buildFile = buildFile;
this.a = a;
a.setOutput(new LogOutputStream(AntUnit.this, Project.MSG_INFO));
}
public void buildStarted(BuildEvent event) {
a.startTestSuite(buildFile);
a.startTestSuite(event.getProject(), buildFile);
}
public void buildFinished(BuildEvent event) {
a.endTestSuite(buildFile);
a.endTestSuite(event.getProject(), buildFile);
}
public void targetStarted(BuildEvent event) {
String tName = event.getTarget().getName();

View File

@ -20,27 +20,20 @@
package org.apache.ant.antunit;
import java.io.OutputStream;
import org.apache.tools.ant.Project;
/**
* A test listener for &lt;antunit&gt;.
*/
public interface AntUnitListener {
/**
* Sets the stream the listener shall write its output to.
*
* <p>Usually points to Ant's logging system.</p>
*/
void setOutput(OutputStream out);
/**
* Invoked once per build file, before any targets get executed.
*/
void startTestSuite(String buildFile);
void startTestSuite(Project testProject, String buildFile);
/**
* Invoked once per build file, after all targets have been executed.
*/
void endTestSuite(String buildFile);
void endTestSuite(Project testProject, String buildFile);
/**
* Invoked before a test target gets executed.
*/

View File

@ -21,18 +21,27 @@
package org.apache.ant.antunit;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.NumberFormat;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.LogOutputStream;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.TeeOutputStream;
/**
* A test listener for &lt;antunit&gt; modeled aftern the Plain JUnit
* test listener that is part of Ant.
*/
public class PlainAntUnitListener implements AntUnitListener {
public class PlainAntUnitListener extends ProjectComponent
implements AntUnitListener {
/**
* Formatter for timings.
*/
@ -48,6 +57,16 @@ public class PlainAntUnitListener implements AntUnitListener {
*/
private PrintWriter wri;
/**
* Directory to write reports to.
*/
private File toDir;
/**
* Where to send log.
*/
private SendLogTo logTo = new SendLogTo(SendLogTo.ANT_LOG);
/**
* keeps track of the numer of executed targets, the failures an errors.
*/
@ -57,17 +76,25 @@ public class PlainAntUnitListener implements AntUnitListener {
*/
private long start, testStart;
public void setOutput(OutputStream out) {
this.out = out;
/**
* Sets the directory to write test reports to.
*/
public void setToDir(File f) {
toDir = f;
}
public void startTestSuite(String buildFile) {
/**
* Where to send the test report.
*/
public void setSendLogTo(SendLogTo logTo) {
this.logTo = logTo;
}
public void startTestSuite(Project testProject, String buildFile) {
inner = new StringWriter();
wri = new PrintWriter(inner);
runCount = failureCount = errorCount;
if (out == null) {
return; // Quick return - no output do nothing.
}
out = getOut(buildFile);
String newLine = System.getProperty("line.separator");
StringBuffer sb = new StringBuffer("Build File: ");
sb.append(buildFile);
@ -81,7 +108,7 @@ public class PlainAntUnitListener implements AntUnitListener {
start = System.currentTimeMillis();
}
public void endTestSuite(String buildFile) {
public void endTestSuite(Project testProject, String buildFile) {
long runTime = System.currentTimeMillis() - start;
String newLine = System.getProperty("line.separator");
StringBuffer sb = new StringBuffer("Tests run: ");
@ -139,4 +166,53 @@ public class PlainAntUnitListener implements AntUnitListener {
wri.println(t.getMessage());
}
private OutputStream getOut(String buildFile) {
OutputStream l, f;
l = f = null;
if (logTo.getValue().equals(SendLogTo.ANT_LOG)
|| logTo.getValue().equals(SendLogTo.BOTH)) {
l = new LogOutputStream(this, Project.MSG_INFO);
if (logTo.getValue().equals(SendLogTo.ANT_LOG)) {
return l;
}
}
if (logTo.getValue().equals(SendLogTo.FILE)
|| logTo.getValue().equals(SendLogTo.BOTH)) {
if (buildFile.length() > 0
&& buildFile.charAt(0) == File.separatorChar) {
buildFile = buildFile.substring(1);
}
String fileName =
buildFile.replace(File.separatorChar, '.') + ".txt";
File file = toDir == null
? getProject().resolveFile(fileName)
: new File(toDir, fileName);
try {
f = new FileOutputStream(file);
} catch (IOException e) {
throw new BuildException(e);
}
if (logTo.getValue().equals(SendLogTo.FILE)) {
return f;
}
}
return new TeeOutputStream(l, f);
}
public static class SendLogTo extends EnumeratedAttribute {
public static final String ANT_LOG = "ant";
public static final String FILE = "file";
public static final String BOTH = "both";
public SendLogTo() {}
public SendLogTo(String s) {
setValue(s);
}
public String[] getValues() {
return new String[] {ANT_LOG, FILE, BOTH};
}
}
}