source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 7.9 KB
Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.junit;
19
20import java.io.File;
21import java.io.FileOutputStream;
22import java.io.OutputStream;
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.Task;
25import org.apache.tools.ant.types.EnumeratedAttribute;
26
27/**
28 * <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>.
29 * In particular, used as a nested <code>&lt;formatter&gt;</code> element in
30 * a <code>&lt;junit&gt;</code> task.
31 * <p> For example,
32 * <code><pre>
33 * &lt;junit printsummary="no" haltonfailure="yes" fork="false"&gt;
34 * &lt;formatter type="plain" usefile="false" /&gt;
35 * &lt;test name="org.apache.ecs.InternationalCharTest" /&gt;
36 * &lt;/junit&gt;</pre></code>
37 * adds a <code>plain</code> type implementation
38 * (<code>PlainJUnitResultFormatter</code>) to display the results of the test.
39 *
40 * <p> Either the <code>type</code> or the <code>classname</code> attribute
41 * must be set.
42 *
43 * @see JUnitTask
44 * @see XMLJUnitResultFormatter
45 * @see BriefJUnitResultFormatter
46 * @see PlainJUnitResultFormatter
47 * @see JUnitResultFormatter
48 */
49public class FormatterElement {
50
51 private String classname;
52 private String extension;
53 private OutputStream out = System.out;
54 private File outFile;
55 private boolean useFile = true;
56 private String ifProperty;
57 private String unlessProperty;
58
59 public static final String XML_FORMATTER_CLASS_NAME =
60 "org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter";
61 public static final String BRIEF_FORMATTER_CLASS_NAME =
62 "org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter";
63 public static final String PLAIN_FORMATTER_CLASS_NAME =
64 "org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter";
65
66 /**
67 * <p> Quick way to use a standard formatter.
68 *
69 * <p> At the moment, there are three supported standard formatters.
70 * <ul>
71 * <li> The <code>xml</code> type uses a <code>XMLJUnitResultFormatter</code>.
72 * <li> The <code>brief</code> type uses a <code>BriefJUnitResultFormatter</code>.
73 * <li> The <code>plain</code> type (the default) uses a <code>PlainJUnitResultFormatter</code>.
74 * </ul>
75 *
76 * <p> Sets <code>classname</code> attribute - so you can't use that
77 * attribute if you use this one.
78 */
79 public void setType(TypeAttribute type) {
80 if ("xml".equals(type.getValue())) {
81 setClassname(XML_FORMATTER_CLASS_NAME);
82 } else {
83 if ("brief".equals(type.getValue())) {
84 setClassname(BRIEF_FORMATTER_CLASS_NAME);
85 } else { // must be plain, ensured by TypeAttribute
86 setClassname(PLAIN_FORMATTER_CLASS_NAME);
87 }
88 }
89 }
90
91 /**
92 * <p> Set name of class to be used as the formatter.
93 *
94 * <p> This class must implement <code>JUnitResultFormatter</code>
95 */
96 public void setClassname(String classname) {
97 this.classname = classname;
98 if (XML_FORMATTER_CLASS_NAME.equals(classname)) {
99 setExtension(".xml");
100 } else if (PLAIN_FORMATTER_CLASS_NAME.equals(classname)) {
101 setExtension(".txt");
102 } else if (BRIEF_FORMATTER_CLASS_NAME.equals(classname)) {
103 setExtension(".txt");
104 }
105 }
106
107 /**
108 * Get name of class to be used as the formatter.
109 */
110 public String getClassname() {
111 return classname;
112 }
113
114 public void setExtension(String ext) {
115 this.extension = ext;
116 }
117
118 public String getExtension() {
119 return extension;
120 }
121
122 /**
123 * <p> Set the file which the formatte should log to.
124 *
125 * <p> Note that logging to file must be enabled .
126 */
127 void setOutfile(File out) {
128 this.outFile = out;
129 }
130
131 /**
132 * <p> Set output stream for formatter to use.
133 *
134 * <p> Defaults to standard out.
135 */
136 public void setOutput(OutputStream out) {
137 this.out = out;
138 }
139
140 /**
141 * Set whether the formatter should log to file.
142 */
143 public void setUseFile(boolean useFile) {
144 this.useFile = useFile;
145 }
146
147 /**
148 * Get whether the formatter should log to file.
149 */
150 boolean getUseFile() {
151 return useFile;
152 }
153
154 /**
155 * Set whether this formatter should be used. It will be
156 * used if the property has been set, otherwise it won't.
157 * @param ifProperty name of property
158 */
159 public void setIf(String ifProperty) {
160 this.ifProperty = ifProperty;
161 }
162
163 /**
164 * Set whether this formatter should NOT be used. It
165 * will not be used if the property has been set, orthwise it
166 * will be used.
167 * @param unlessProperty name of property
168 */
169 public void setUnless(String unlessProperty) {
170 this.unlessProperty = unlessProperty;
171 }
172
173 /**
174 * Ensures that the selector passes the conditions placed
175 * on it with <code>if</code> and <code>unless</code> properties.
176 */
177 public boolean shouldUse(Task t) {
178 if (ifProperty != null && t.getProject().getProperty(ifProperty) == null) {
179 return false;
180 } else if (unlessProperty != null
181 && t.getProject().getProperty(unlessProperty) != null) {
182 return false;
183 }
184
185 return true;
186 }
187
188 /**
189 * @since Ant 1.2
190 */
191 JUnitResultFormatter createFormatter() throws BuildException {
192 return createFormatter(null);
193 }
194
195 /**
196 * @since Ant 1.6
197 */
198 JUnitResultFormatter createFormatter(ClassLoader loader)
199 throws BuildException {
200
201 if (classname == null) {
202 throw new BuildException("you must specify type or classname");
203 }
204
205 Class f = null;
206 try {
207 if (loader == null) {
208 f = Class.forName(classname);
209 } else {
210 f = Class.forName(classname, true, loader);
211 }
212 } catch (ClassNotFoundException e) {
213 throw new BuildException(e);
214 }
215
216 Object o = null;
217 try {
218 o = f.newInstance();
219 } catch (InstantiationException e) {
220 throw new BuildException(e);
221 } catch (IllegalAccessException e) {
222 throw new BuildException(e);
223 }
224
225 if (!(o instanceof JUnitResultFormatter)) {
226 throw new BuildException(classname
227 + " is not a JUnitResultFormatter");
228 }
229
230 JUnitResultFormatter r = (JUnitResultFormatter) o;
231
232 if (useFile && outFile != null) {
233 try {
234 out = new FileOutputStream(outFile);
235 } catch (java.io.IOException e) {
236 throw new BuildException(e);
237 }
238 }
239 r.setOutput(out);
240 return r;
241 }
242
243 /**
244 * <p> Enumerated attribute with the values "plain", "xml" and "brief".
245 *
246 * <p> Use to enumerate options for <code>type</code> attribute.
247 */
248 public static class TypeAttribute extends EnumeratedAttribute {
249 public String[] getValues() {
250 return new String[] {"plain", "xml", "brief"};
251 }
252 }
253}
Note: See TracBrowser for help on using the repository browser.