KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > FormatterElement


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.junit;
20
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.BufferedOutputStream JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.types.EnumeratedAttribute;
29
30 /**
31  * <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>.
32  * In particular, used as a nested <code>&lt;formatter&gt;</code> element in
33  * a <code>&lt;junit&gt;</code> task.
34  * <p> For example,
35  * <code><pre>
36  * &lt;junit printsummary="no" haltonfailure="yes" fork="false"&gt;
37  * &lt;formatter type="plain" usefile="false" /&gt;
38  * &lt;test name="org.apache.ecs.InternationalCharTest" /&gt;
39  * &lt;/junit&gt;</pre></code>
40  * adds a <code>plain</code> type implementation
41  * (<code>PlainJUnitResultFormatter</code>) to display the results of the test.
42  *
43  * <p> Either the <code>type</code> or the <code>classname</code> attribute
44  * must be set.
45  *
46  * @see JUnitTask
47  * @see XMLJUnitResultFormatter
48  * @see BriefJUnitResultFormatter
49  * @see PlainJUnitResultFormatter
50  * @see JUnitResultFormatter
51  */

52 public class FormatterElement {
53
54     private String JavaDoc classname;
55     private String JavaDoc extension;
56     private OutputStream JavaDoc out = System.out;
57     private File JavaDoc outFile;
58     private boolean useFile = true;
59     private String JavaDoc ifProperty;
60     private String JavaDoc unlessProperty;
61
62     /** xml formatter class */
63     public static final String JavaDoc XML_FORMATTER_CLASS_NAME =
64         "org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter";
65     /** brief formatter class */
66     public static final String JavaDoc BRIEF_FORMATTER_CLASS_NAME =
67         "org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter";
68     /** plain formatter class */
69     public static final String JavaDoc PLAIN_FORMATTER_CLASS_NAME =
70         "org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter";
71
72     /**
73      * <p> Quick way to use a standard formatter.
74      *
75      * <p> At the moment, there are three supported standard formatters.
76      * <ul>
77      * <li> The <code>xml</code> type uses a <code>XMLJUnitResultFormatter</code>.
78      * <li> The <code>brief</code> type uses a <code>BriefJUnitResultFormatter</code>.
79      * <li> The <code>plain</code> type (the default) uses a <code>PlainJUnitResultFormatter</code>.
80      * </ul>
81      *
82      * <p> Sets <code>classname</code> attribute - so you can't use that
83      * attribute if you use this one.
84      * @param type the enumerated value to use.
85      */

86     public void setType(TypeAttribute type) {
87         if ("xml".equals(type.getValue())) {
88             setClassname(XML_FORMATTER_CLASS_NAME);
89         } else {
90             if ("brief".equals(type.getValue())) {
91                 setClassname(BRIEF_FORMATTER_CLASS_NAME);
92             } else { // must be plain, ensured by TypeAttribute
93
setClassname(PLAIN_FORMATTER_CLASS_NAME);
94             }
95         }
96     }
97
98     /**
99      * <p> Set name of class to be used as the formatter.
100      *
101      * <p> This class must implement <code>JUnitResultFormatter</code>
102      * @param classname the name of the formatter class.
103      */

104     public void setClassname(String JavaDoc classname) {
105         this.classname = classname;
106         if (XML_FORMATTER_CLASS_NAME.equals(classname)) {
107            setExtension(".xml");
108         } else if (PLAIN_FORMATTER_CLASS_NAME.equals(classname)) {
109            setExtension(".txt");
110         } else if (BRIEF_FORMATTER_CLASS_NAME.equals(classname)) {
111            setExtension(".txt");
112         }
113     }
114
115     /**
116      * Get name of class to be used as the formatter.
117      * @return the name of the class.
118      */

119     public String JavaDoc getClassname() {
120         return classname;
121     }
122
123     /**
124      * Set the extension to use for the report file.
125      * @param ext the extension to use.
126      */

127     public void setExtension(String JavaDoc ext) {
128         this.extension = ext;
129     }
130
131     /**
132      * Get the extension used for the report file.
133      * @return the extension.
134      */

135     public String JavaDoc getExtension() {
136         return extension;
137     }
138
139     /**
140      * <p> Set the file which the formatte should log to.
141      *
142      * <p> Note that logging to file must be enabled .
143      */

144     void setOutfile(File JavaDoc out) {
145         this.outFile = out;
146     }
147
148     /**
149      * <p> Set output stream for formatter to use.
150      *
151      * <p> Defaults to standard out.
152      * @param out the output stream to use.
153      */

154     public void setOutput(OutputStream JavaDoc out) {
155         this.out = out;
156     }
157
158     /**
159      * Set whether the formatter should log to file.
160      * @param useFile if true use a file, if false send
161      * to standard out.
162      */

163     public void setUseFile(boolean useFile) {
164         this.useFile = useFile;
165     }
166
167     /**
168      * Get whether the formatter should log to file.
169      */

170     boolean getUseFile() {
171         return useFile;
172     }
173
174     /**
175      * Set whether this formatter should be used. It will be
176      * used if the property has been set, otherwise it won't.
177      * @param ifProperty name of property
178      */

179     public void setIf(String JavaDoc ifProperty) {
180         this.ifProperty = ifProperty;
181     }
182
183     /**
184      * Set whether this formatter should NOT be used. It
185      * will not be used if the property has been set, orthwise it
186      * will be used.
187      * @param unlessProperty name of property
188      */

189     public void setUnless(String JavaDoc unlessProperty) {
190         this.unlessProperty = unlessProperty;
191     }
192
193     /**
194      * Ensures that the selector passes the conditions placed
195      * on it with <code>if</code> and <code>unless</code> properties.
196      * @param t the task the this formatter is used in.
197      * @return true if the formatter should be used.
198      */

199     public boolean shouldUse(Task t) {
200         if (ifProperty != null && t.getProject().getProperty(ifProperty) == null) {
201             return false;
202         } else if (unlessProperty != null
203                     && t.getProject().getProperty(unlessProperty) != null) {
204             return false;
205         }
206
207         return true;
208     }
209
210     /**
211      * @since Ant 1.2
212      */

213     JUnitTaskMirror.JUnitResultFormatterMirror createFormatter() throws BuildException {
214         return createFormatter(null);
215     }
216
217     /**
218      * @since Ant 1.6
219      */

220     JUnitTaskMirror.JUnitResultFormatterMirror createFormatter(ClassLoader JavaDoc loader)
221         throws BuildException {
222
223         if (classname == null) {
224             throw new BuildException("you must specify type or classname");
225         }
226         //although this code appears to duplicate that of ClasspathUtils.newInstance,
227
//we cannot use that because this formatter may run in a forked process,
228
//without that class.
229
Class JavaDoc f = null;
230         try {
231             if (loader == null) {
232                 f = Class.forName(classname);
233             } else {
234                 f = Class.forName(classname, true, loader);
235             }
236         } catch (ClassNotFoundException JavaDoc e) {
237             throw new BuildException(
238                 "Using loader " + loader + " on class " + classname
239                 + ": " + e, e);
240         } catch (NoClassDefFoundError JavaDoc e) {
241             throw new BuildException(
242                 "Using loader " + loader + " on class " + classname
243                 + ": " + e, e);
244         }
245
246         Object JavaDoc o = null;
247         try {
248             o = f.newInstance();
249         } catch (InstantiationException JavaDoc e) {
250             throw new BuildException(e);
251         } catch (IllegalAccessException JavaDoc e) {
252             throw new BuildException(e);
253         }
254
255         if (!(o instanceof JUnitTaskMirror.JUnitResultFormatterMirror)) {
256             throw new BuildException(classname
257                 + " is not a JUnitResultFormatter");
258         }
259         JUnitTaskMirror.JUnitResultFormatterMirror r =
260             (JUnitTaskMirror.JUnitResultFormatterMirror) o;
261         if (useFile && outFile != null) {
262             try {
263                 out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outFile));
264             } catch (java.io.IOException JavaDoc e) {
265                 throw new BuildException("Unable to open file " + outFile, e);
266             }
267         }
268         r.setOutput(out);
269         return r;
270     }
271
272     /**
273      * <p> Enumerated attribute with the values "plain", "xml" and "brief".
274      *
275      * <p> Use to enumerate options for <code>type</code> attribute.
276      */

277     public static class TypeAttribute extends EnumeratedAttribute {
278         /** {@inheritDoc}. */
279         public String JavaDoc[] getValues() {
280             return new String JavaDoc[] {"plain", "xml", "brief"};
281         }
282     }
283 }
284
Popular Tags