~ubuntu-branches/ubuntu/jaunty/ant/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-14 14:28:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020214142848-2ww7ynmqkj31vlmn
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 *
 
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
 
17
 *    distribution.
 
18
 *
 
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.
 
25
 *
 
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.
 
30
 *
 
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.
 
34
 *
 
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
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
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/>.
 
53
 */
 
54
 
 
55
package org.apache.tools.ant.taskdefs.optional.junit;
 
56
 
 
57
import org.apache.tools.ant.BuildException;
 
58
import org.apache.tools.ant.types.EnumeratedAttribute;
 
59
 
 
60
import java.io.File;
 
61
import java.io.FileOutputStream;
 
62
import java.io.OutputStream;
 
63
 
 
64
/**
 
65
 * <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>.
 
66
 * In particular, used as a nested <code>&lt;formatter&gt;</code> element in a <code>&lt;junit&gt;</code> task.
 
67
 * <p> For example, 
 
68
 * <code><pre>
 
69
 *       &lt;junit printsummary="no" haltonfailure="yes" fork="false"&gt;
 
70
 *           &lt;formatter type="plain" usefile="false" /&gt;
 
71
 *           &lt;test name="org.apache.ecs.InternationalCharTest" /&gt;
 
72
 *       &lt;/junit&gt;</pre></code>
 
73
 * adds a <code>plain</code> type implementation (<code>PlainJUnitResultFormatter</code>) to display the results of the test.
 
74
 *
 
75
 * <p> Either the <code>type</code> or the <code>classname</code> attribute
 
76
 * must be set. 
 
77
 *
 
78
 * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
 
79
 *
 
80
 * @see JUnitTask
 
81
 * @see XMLJUnitResultFormatter
 
82
 * @see BriefJUnitResultFormatter
 
83
 * @see PlainJUnitResultFormatter
 
84
 * @see JUnitResultFormatter
 
85
 */
 
86
public class FormatterElement {
 
87
 
 
88
    private String classname;
 
89
    private String extension;
 
90
    private OutputStream out = System.out;
 
91
    private File outFile;
 
92
    private boolean useFile = true;
 
93
 
 
94
    /**
 
95
     * <p> Quick way to use a standard formatter.
 
96
     *
 
97
     * <p> At the moment, there are three supported standard formatters.
 
98
     * <ul>
 
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>.
 
102
     * </ul>
 
103
     *
 
104
     * <p> Sets <code>classname</code> attribute - so you can't use that attribute if you use this one.
 
105
     */
 
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");
 
110
        } else {
 
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");
 
115
            }
 
116
            setExtension(".txt");
 
117
        }
 
118
    }
 
119
 
 
120
    /**
 
121
     * <p> Set name of class to be used as the formatter.
 
122
     *
 
123
     * <p> This class must implement <code>JUnitResultFormatter</code>
 
124
     */
 
125
    public void setClassname(String classname) {
 
126
        this.classname = classname;
 
127
    }
 
128
 
 
129
    /**
 
130
     * Get name of class to be used as the formatter.
 
131
     */
 
132
    public String getClassname() {
 
133
        return classname;
 
134
    }
 
135
 
 
136
    public void setExtension(String ext) {
 
137
        this.extension = ext;
 
138
    }
 
139
 
 
140
    public String getExtension() {
 
141
        return extension;
 
142
    }
 
143
 
 
144
    /**
 
145
     * <p> Set the file which the formatte should log to.
 
146
     *
 
147
     * <p> Note that logging to file must be enabled .
 
148
     */
 
149
    void setOutfile(File out) {
 
150
        this.outFile = out;
 
151
    }
 
152
 
 
153
    /**
 
154
     * <p> Set output stream for formatter to use.
 
155
     *
 
156
     * <p> Defaults to standard out.
 
157
     */
 
158
    public void setOutput(OutputStream out) {
 
159
        this.out = out;
 
160
    }
 
161
 
 
162
    /**
 
163
     * Set whether the formatter should log to file.
 
164
     */
 
165
    public void setUseFile(boolean useFile) {
 
166
        this.useFile = useFile;
 
167
    }
 
168
 
 
169
    /**
 
170
     * Get whether the formatter should log to file.
 
171
     */
 
172
    boolean getUseFile() {
 
173
        return useFile;
 
174
    }
 
175
 
 
176
    JUnitResultFormatter createFormatter() throws BuildException {
 
177
        if (classname == null) {
 
178
            throw new BuildException("you must specify type or classname");
 
179
        }
 
180
        
 
181
        Class f = null;
 
182
        try {
 
183
            f = Class.forName(classname);
 
184
        } catch (ClassNotFoundException e) {
 
185
            throw new BuildException(e);
 
186
        }
 
187
 
 
188
        Object o = null;
 
189
        try {
 
190
            o = f.newInstance();
 
191
        } catch (InstantiationException e) {
 
192
            throw new BuildException(e);
 
193
        } catch (IllegalAccessException e) {
 
194
            throw new BuildException(e);
 
195
        }
 
196
 
 
197
        if (!(o instanceof JUnitResultFormatter)) {
 
198
            throw new BuildException(classname+" is not a JUnitResultFormatter");
 
199
        }
 
200
 
 
201
        JUnitResultFormatter r = (JUnitResultFormatter) o;
 
202
 
 
203
        if (useFile && outFile != null) {
 
204
            try {
 
205
                out = new FileOutputStream(outFile);
 
206
            } catch (java.io.IOException e) {
 
207
                throw new BuildException(e);
 
208
            }
 
209
        }
 
210
        r.setOutput(out);
 
211
        return r;
 
212
    }
 
213
 
 
214
    /**
 
215
     * <p> Enumerated attribute with the values "plain", "xml" and "brief".
 
216
     * 
 
217
     * <p> Use to enumerate options for <code>type</code> attribute.
 
218
     */
 
219
    public static class TypeAttribute extends EnumeratedAttribute {
 
220
        public String[] getValues() {
 
221
            return new String[] {"plain", "xml", "brief"};
 
222
        }
 
223
    }
 
224
}