2
* The Apache Software License, Version 1.1
4
* Copyright (c) 2000 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution, if
20
* any, must include the following acknowlegement:
21
* "This product includes software developed by the
22
* Apache Software Foundation (http://www.apache.org/)."
23
* Alternately, this acknowlegement may appear in the software itself,
24
* if and wherever such third-party acknowlegements normally appear.
26
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27
* Foundation" must not be used to endorse or promote products derived
28
* from this software without prior written permission. For written
29
* permission, please contact apache@apache.org.
31
* 5. Products derived from this software may not be called "Apache"
32
* nor may "Apache" appear in their names without prior written
33
* permission of the Apache Group.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
* ====================================================================
49
* This software consists of voluntary contributions made by many
50
* individuals on behalf of the Apache Software Foundation. For more
51
* information on the Apache Software Foundation, please see
52
* <http://www.apache.org/>.
55
package org.apache.tools.ant.taskdefs.optional.junit;
57
import org.apache.tools.ant.BuildException;
58
import org.apache.tools.ant.types.EnumeratedAttribute;
61
import java.io.FileOutputStream;
62
import java.io.OutputStream;
65
* <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>.
66
* In particular, used as a nested <code><formatter></code> element in a <code><junit></code> task.
69
* <junit printsummary="no" haltonfailure="yes" fork="false">
70
* <formatter type="plain" usefile="false" />
71
* <test name="org.apache.ecs.InternationalCharTest" />
72
* </junit></pre></code>
73
* adds a <code>plain</code> type implementation (<code>PlainJUnitResultFormatter</code>) to display the results of the test.
75
* <p> Either the <code>type</code> or the <code>classname</code> attribute
78
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
81
* @see XMLJUnitResultFormatter
82
* @see BriefJUnitResultFormatter
83
* @see PlainJUnitResultFormatter
84
* @see JUnitResultFormatter
86
public class FormatterElement {
88
private String classname;
89
private String extension;
90
private OutputStream out = System.out;
92
private boolean useFile = true;
95
* <p> Quick way to use a standard formatter.
97
* <p> At the moment, there are three supported standard formatters.
99
* <li> The <code>xml</code> type uses a <code>XMLJUnitResultFormatter</code>.
100
* <li> The <code>brief</code> type uses a <code>BriefJUnitResultFormatter</code>.
101
* <li> The <code>plain</code> type (the default) uses a <code>PlainJUnitResultFormatter</code>.
104
* <p> Sets <code>classname</code> attribute - so you can't use that attribute if you use this one.
106
public void setType(TypeAttribute type) {
107
if ("xml".equals(type.getValue())) {
108
setClassname("org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter");
109
setExtension(".xml");
111
if ("brief".equals(type.getValue())) {
112
setClassname("org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter");
113
} else { // must be plain, ensured by TypeAttribute
114
setClassname("org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter");
116
setExtension(".txt");
121
* <p> Set name of class to be used as the formatter.
123
* <p> This class must implement <code>JUnitResultFormatter</code>
125
public void setClassname(String classname) {
126
this.classname = classname;
130
* Get name of class to be used as the formatter.
132
public String getClassname() {
136
public void setExtension(String ext) {
137
this.extension = ext;
140
public String getExtension() {
145
* <p> Set the file which the formatte should log to.
147
* <p> Note that logging to file must be enabled .
149
void setOutfile(File out) {
154
* <p> Set output stream for formatter to use.
156
* <p> Defaults to standard out.
158
public void setOutput(OutputStream out) {
163
* Set whether the formatter should log to file.
165
public void setUseFile(boolean useFile) {
166
this.useFile = useFile;
170
* Get whether the formatter should log to file.
172
boolean getUseFile() {
176
JUnitResultFormatter createFormatter() throws BuildException {
177
if (classname == null) {
178
throw new BuildException("you must specify type or classname");
183
f = Class.forName(classname);
184
} catch (ClassNotFoundException e) {
185
throw new BuildException(e);
191
} catch (InstantiationException e) {
192
throw new BuildException(e);
193
} catch (IllegalAccessException e) {
194
throw new BuildException(e);
197
if (!(o instanceof JUnitResultFormatter)) {
198
throw new BuildException(classname+" is not a JUnitResultFormatter");
201
JUnitResultFormatter r = (JUnitResultFormatter) o;
203
if (useFile && outFile != null) {
205
out = new FileOutputStream(outFile);
206
} catch (java.io.IOException e) {
207
throw new BuildException(e);
215
* <p> Enumerated attribute with the values "plain", "xml" and "brief".
217
* <p> Use to enumerate options for <code>type</code> attribute.
219
public static class TypeAttribute extends EnumeratedAttribute {
220
public String[] getValues() {
221
return new String[] {"plain", "xml", "brief"};