KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Vector JavaDoc;
25 import org.apache.tools.ant.Project;
26
27 /**
28  * <p> Run a single JUnit test.
29  *
30  * <p> The JUnit test is actually run by {@link JUnitTestRunner}.
31  * So read the doc comments for that class :)
32  *
33  * @since Ant 1.2
34  *
35  * @see JUnitTask
36  * @see JUnitTestRunner
37  */

38 public class JUnitTest extends BaseTest implements Cloneable JavaDoc {
39
40     /** the name of the test case */
41     private String JavaDoc name = null;
42
43     /** the name of the result file */
44     private String JavaDoc outfile = null;
45
46     // @todo this is duplicating TestResult information. Only the time is not
47
// part of the result. So we'd better derive a new class from TestResult
48
// and deal with it. (SB)
49
private long runs, failures, errors;
50     private long runTime;
51
52     // Snapshot of the system properties
53
private Properties JavaDoc props = null;
54
55     /** No arg constructor. */
56     public JUnitTest() {
57     }
58
59     /**
60      * Constructor with name.
61      * @param name the name of the test.
62      */

63     public JUnitTest(String JavaDoc name) {
64         this.name = name;
65     }
66
67     /**
68      * Constructor with options.
69      * @param name the name of the test.
70      * @param haltOnError if true halt the tests if there is an error.
71      * @param haltOnFailure if true halt the tests if there is a failure.
72      * @param filtertrace if true filter stack traces.
73      */

74     public JUnitTest(String JavaDoc name, boolean haltOnError, boolean haltOnFailure,
75                      boolean filtertrace) {
76         this.name = name;
77         this.haltOnError = haltOnError;
78         this.haltOnFail = haltOnFailure;
79         this.filtertrace = filtertrace;
80     }
81
82     /**
83      * Set the name of the test class.
84      * @param value the name to use.
85      */

86     public void setName(String JavaDoc value) {
87         name = value;
88     }
89
90     /**
91      * Set the name of the output file.
92      * @param value the name of the output file to use.
93      */

94     public void setOutfile(String JavaDoc value) {
95         outfile = value;
96     }
97
98     /**
99      * Get the name of the test class.
100      * @return the name of the test.
101      */

102     public String JavaDoc getName() {
103         return name;
104     }
105
106     /**
107      * Get the name of the output file
108      *
109      * @return the name of the output file.
110      */

111     public String JavaDoc getOutfile() {
112         return outfile;
113     }
114
115     /**
116      * Set the number of runs, failures and errors.
117      * @param runs the number of runs.
118      * @param failures the number of failures.
119      * @param errors the number of errors.
120      */

121     public void setCounts(long runs, long failures, long errors) {
122         this.runs = runs;
123         this.failures = failures;
124         this.errors = errors;
125     }
126
127     /**
128      * Set the runtime.
129      * @param runTime the time in milliseconds.
130      */

131     public void setRunTime(long runTime) {
132         this.runTime = runTime;
133     }
134
135     /**
136      * Get the number of runs.
137      * @return the number of runs.
138      */

139     public long runCount() {
140         return runs;
141     }
142
143     /**
144      * Get the number of failures.
145      * @return the number of failures.
146      */

147     public long failureCount() {
148         return failures;
149     }
150
151     /**
152      * Get the number of errors.
153      * @return the number of errors.
154      */

155     public long errorCount() {
156         return errors;
157     }
158
159     /**
160      * Get the run time.
161      * @return the run time in milliseconds.
162      */

163     public long getRunTime() {
164         return runTime;
165     }
166
167     /**
168      * Get the properties used in the test.
169      * @return the properties.
170      */

171     public Properties JavaDoc getProperties() {
172         return props;
173     }
174
175     /**
176      * Set the properties to be used in the test.
177      * @param p the properties.
178      * This is a copy of the projects ant properties.
179      */

180     public void setProperties(Hashtable JavaDoc p) {
181         props = new Properties JavaDoc();
182         for (Enumeration JavaDoc e = p.keys(); e.hasMoreElements();) {
183             Object JavaDoc key = e.nextElement();
184             props.put(key, p.get(key));
185         }
186     }
187
188     /**
189      * Check if this test should run based on the if and unless
190      * attributes.
191      * @param p the project to use to check if the if and unless
192      * properties exist in.
193      * @return true if this test or testsuite should be run.
194      */

195     public boolean shouldRun(Project p) {
196         if (ifProperty != null && p.getProperty(ifProperty) == null) {
197             return false;
198         } else if (unlessProperty != null
199                     && p.getProperty(unlessProperty) != null) {
200             return false;
201         }
202
203         return true;
204     }
205
206     /**
207      * Get the formatters set for this test.
208      * @return the formatters as an array.
209      */

210     public FormatterElement[] getFormatters() {
211         FormatterElement[] fes = new FormatterElement[formatters.size()];
212         formatters.copyInto(fes);
213         return fes;
214     }
215
216     /**
217      * Convenient method to add formatters to a vector
218      */

219     void addFormattersTo(Vector JavaDoc v) {
220         final int count = formatters.size();
221         for (int i = 0; i < count; i++) {
222             v.addElement(formatters.elementAt(i));
223         }
224     }
225
226     /**
227      * @since Ant 1.5
228      * @return a clone of this test.
229      */

230     public Object JavaDoc clone() {
231         try {
232             JUnitTest t = (JUnitTest) super.clone();
233             t.props = props == null ? null : (Properties JavaDoc) props.clone();
234             t.formatters = (Vector JavaDoc) formatters.clone();
235             return t;
236         } catch (CloneNotSupportedException JavaDoc e) {
237             // plain impossible
238
return this;
239         }
240     }
241 }
242
Popular Tags