KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > framework > QuiltTest


1 /* QuiltTest.java */
2
3 package org.quilt.framework;
4
5 import java.io.File JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.Hashtable JavaDoc;
8 import java.util.Properties JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 import org.apache.tools.ant.Project;
12 //import org.apache.tools.ant.taskdefs.optional.junit.*;
13

14 import org.quilt.reports.*;
15
16 /**
17  * Parameters for controlling an individual task. These are and
18  * must be compatible with names used in Ant build.xml build control
19  * files. Most are set by methods whose name is derived from
20  * the variable name -- for example <b>fork</b> is set by
21  * <b>setFork(b)</b>.
22  */

23 public class QuiltTest implements Cloneable JavaDoc {
24   
25     /** Class name of the test */
26     private String JavaDoc name = null;
27
28     // MAINTAINERS please keep the variable names and their get/set
29
// methods in alphabetical order
30

31     /** If true, running Quilt coverage checks. */
32     private boolean checkCoverage = false;
33     
34     /**
35      * Classes not be to instrumented, a comma-separated list of class
36      * names. Wildcards are OK. com.xyz.* means all classes whose
37      * name begins with com.xyz, but does not include com.xyz itself.
38      *
39      */

40     private String JavaDoc checkExcludes;
41     
42     /**
43      * Classes to instrument if checking coverage. Comma-separated,
44      * wildcards OK.
45      */

46     private String JavaDoc checkIncludes;
47
48     /** Set this property to True if an error occurs.*/
49     private String JavaDoc errorProperty;
50     
51     /** Name of property to set to True if a failure or error occurs. */
52     private String JavaDoc failureProperty;
53     
54     /** Filter Quilt and JUnit error/failure messages from output. */
55     private boolean filtertrace = true;
56     
57     /** Run the test in a separate JVM. */
58     private boolean fork = false;
59     
60     /** Names of formatters to be used. */
61     private Vector JavaDoc formatters = new Vector JavaDoc();
62
63     /** Halt the test if an unexpected error occurs. */
64     private boolean haltOnError = false;
65     
66     /** Halt the test if a failure (expected) or error occurs. */
67     private boolean haltOnFailure = false;
68
69     /** Run the test only if this property is True. */
70     private String JavaDoc ifProperty = null;
71     
72     /** Don't run the test, just report back the parameters. */
73     private boolean mockTestRun = false;
74  
75     /** Where to write the results */
76     private String JavaDoc outfile = null;
77
78     /** Copy of Ant properties */
79     private Properties JavaDoc props = new Properties JavaDoc();
80
81     /**
82      * Ant documentation seems contradictory on this, but it MUST
83      * be a QuiltTest field. Used in BaseTestRunner. XXX
84      */

85     private boolean showOutput = false;
86
87     /** Report destination directory. */
88     private File JavaDoc todir = null;
89
90     /** Run the test unless this property is True. */
91     private String JavaDoc unlessProperty = null;
92    
93     // CONSTRUCTORS /////////////////////////////////////////////////
94
/** No-arg constructor used by clone() */
95     public QuiltTest() { }
96     
97     /**
98      * Single-arg constructor.
99      *
100      * @param name Full class name of test to be run.
101      */

102     public QuiltTest(String JavaDoc name) {
103         this.name = name;
104     }
105     // NON-STANDARD VARIABLES AND GET/SET METHODS ///////////////////
106
/**
107      * DON'T BELONG HERE - TEST RESULTS. These are not accessible
108      * from Ant
109      */

110     private long errors = (long) 0;
111     private long failures = (long) 0;
112     private long runs = (long) 0;
113     private long runTime = (long) 0;
114
115     public void setCounts (long runs, long failures, long errors) {
116         this.errors = errors;
117         this.failures = failures;
118         this.runs = runs;
119     }
120     public long errorCount () { return errors; }
121     public long failureCount() { return failures; }
122     public long runCount () { return runs; }
123     // END ERRATIC NAMES ////////////////////////////////////////////
124

125     /** @return A reference to the test's Properties */
126     public Properties JavaDoc getProperties() { return props; }
127     /**
128      * Replace the test's Properties. This is quite different from
129      * the method in JUnitTask, which seems to contain errors.
130      *
131      * @param val Hashtable containing new values.
132      */

133
134     public void setProperties (Hashtable JavaDoc val) {
135         Enumeration JavaDoc e = val.keys();
136         props.clear();
137         while (e.hasMoreElements() ) {
138             Object JavaDoc key = e.nextElement();
139             props.put(key, val.get(key));
140         }
141     }
142     public long getRunTime() { return runTime; }
143     public void setRunTime (long val) { runTime = val; }
144     
145     // //////////////////////////////////////////////////////////////
146
// GET RID OF THIS METHOD ///////////////////////////////////////
147
// //////////////////////////////////////////////////////////////
148

149     /** Add this test's formatters to a vector. A convenience method
150      * with a mildly confusing name. Inherited from JUnitTask. */

151     public void addFormattersTo(Vector JavaDoc v) {
152         for (int j = 0; j < formatters.size(); j++){
153             v.addElement(formatters.elementAt(j));
154         }
155     }
156
157     // ADD/GET/SET METHODS /////////////////////////////////
158
public boolean getCheckCoverage() { return checkCoverage; }
159     public void setCheckCoverage(boolean b) { checkCoverage = b; }
160     
161     public String JavaDoc getCheckExcludes() { return checkExcludes; }
162     public String JavaDoc[] getCheckExcludesArray() {
163         String JavaDoc [] val = null;
164         if (checkExcludes != null) {
165             val = checkExcludes.split(",");
166         }
167         return val;
168     }
169     public void setCheckExcludes(String JavaDoc val) { checkExcludes = val; }
170     
171     public String JavaDoc getCheckIncludes() { return checkIncludes; }
172     public String JavaDoc[] getCheckIncludesArray() {
173         String JavaDoc [] val = null;
174         if (checkIncludes != null) {
175             val = checkIncludes.split(",");
176         }
177         return val;
178     }
179     public void setCheckIncludes(String JavaDoc val) { checkIncludes = val; }
180
181     public String JavaDoc getErrorProperty() { return errorProperty; }
182     public void setErrorProperty(String JavaDoc eP) { errorProperty = eP; }
183
184     public String JavaDoc getFailureProperty() { return failureProperty; }
185     public void setFailureProperty(String JavaDoc fP) { failureProperty = fP; }
186    
187     public boolean getFiltertrace() { return filtertrace; }
188     public void setFiltertrace(boolean b) { filtertrace = b; }
189     
190     public boolean getFork() { return fork; }
191     public void setFork(boolean b) { fork = b; }
192
193     public void addFormatter(FmtSelector elem) {
194         formatters.addElement(elem);
195     }
196     public Vector JavaDoc getFormatters () { return formatters; }
197
198     public boolean getHaltOnError() { return haltOnError; }
199     public void setHaltOnError(boolean b) { haltOnError = b; }
200
201     public boolean getHaltOnFailure() { return haltOnFailure; }
202     public void setHaltOnFailure(boolean b) { haltOnFailure = b; }
203
204     // non-standard name required for compatibility
205
public String JavaDoc getIfProperty () { return ifProperty; }
206     public void setIf(String JavaDoc name) { ifProperty = name; }
207
208     public boolean getMockTestRun() { return mockTestRun; }
209     public void setMockTestRun(boolean b) { mockTestRun = b; }
210
211     public String JavaDoc getName() { return name; }
212     public void setName (String JavaDoc val) { name = val; }
213
214     public String JavaDoc getOutfile() { return outfile; }
215     public void setOutfile (String JavaDoc val) { outfile = val; }
216    
217     public boolean getShowOutput() { return showOutput; }
218     public void setShowOutput(boolean b) { showOutput = b; }
219
220     public String JavaDoc getTodir(){
221         if (todir != null){
222             return todir.getAbsolutePath();
223         }
224         return null;
225     }
226     public void setTodir(File JavaDoc dir) { this.todir = dir; }
227
228     // non-standard name required for compatibility
229
public String JavaDoc getUnlessProperty () { return unlessProperty; }
230     public void setUnless(String JavaDoc name) { unlessProperty = name; }
231
232     // IMPLEMENT Cloneable //////////////////////////////////////////
233
/**
234      * Clones, resetting the error/failure/run counts and runTime to zero.
235      *
236      * @return An Object, a copy of this QuiltTest.
237      */

238     public Object JavaDoc clone() {
239         QuiltTest t = new QuiltTest();
240         t.name = name;
241         
242         // counts and runTime are not copied
243

244         t.checkCoverage = checkCoverage;
245         t.checkExcludes = checkExcludes;
246         t.checkIncludes = checkIncludes;
247         t.errorProperty = errorProperty;
248         t.failureProperty = failureProperty;
249         t.filtertrace = filtertrace;
250         t.formatters = (Vector JavaDoc) formatters.clone();
251         t.fork = fork;
252         t.haltOnError = haltOnError;
253         t.haltOnFailure = haltOnFailure;
254         t.ifProperty = ifProperty;
255         t.mockTestRun = mockTestRun;
256         t.outfile = outfile;
257         t.showOutput = showOutput;
258         t.todir = todir;
259         t.unlessProperty = unlessProperty;
260
261         // real Properties are harder to clone
262
Properties JavaDoc props = getProperties();
263         if (props != null) {
264             t.setProperties( (Properties JavaDoc) props.clone() );
265         }
266         return t;
267     }
268     // TOSTRING () //////////////////////////////////////////////////
269
public String JavaDoc toString() {
270         String JavaDoc fmtStr = "";
271         for (int i = 0; i < formatters.size(); i++)
272             fmtStr += formatters.elementAt(i) + " ";
273         
274         String JavaDoc pStr;
275         if (props != null) {
276             pStr = "";
277             Enumeration JavaDoc e = props.keys();
278             while (e.hasMoreElements()) {
279                 String JavaDoc name = (String JavaDoc) e.nextElement();
280                 String JavaDoc value = (String JavaDoc) props.getProperty(name);
281                 pStr += "\n (" + name + " --> " + value + ")";
282             }
283         } else {
284             pStr = "<none>";
285         }
286         String JavaDoc s =
287                 " test name: " + name
288
289             + "\n checkCoverage: " + checkCoverage
290             + "\n checkExcludes: " + (checkExcludes == null? ""
291                                                     : checkExcludes)
292             + "\n checkIncludes: " + (checkIncludes == null? ""
293                                                     : checkIncludes)
294             + "\n errorProperty: " + errorProperty
295             + "\n failureProperty: " + failureProperty
296             + "\n filtertrace: " + filtertrace
297             + "\n fork: " + fork
298             + "\n formatters: " + fmtStr
299             + "\n haltOnError: " + haltOnError
300             + "\n haltOnFailure: " + haltOnFailure
301             + "\n ifProperty: " + ifProperty
302             + "\n mockTestRun: " + mockTestRun
303             + "\n outfile: " + outfile
304             + "\n showOutput: " + showOutput
305             + "\n todir: " + todir
306             + "\n unlessProperty: " + unlessProperty
307            
308             // counts - not cloned but part of toString()
309
+ "\n errors: " + errors
310             + "\n failures: " + failures
311             + "\n runs: " + runs
312             
313             + "\n other properties:" + pStr;
314         return s;
315     }
316     // CONVENIENCE METHOD ///////////////////////////////////////////
317
/**
318      * Run this test if project properties permit.
319      *
320      * @param p The project that the QuiltTask is part of.
321      * @return True if this test should be run, false otherwise.
322      */

323     public boolean runMe (Project p) {
324         if (ifProperty != null) {
325             if (p.getProperty(ifProperty) == null)
326                 return false;
327         }
328         if (unlessProperty != null) {
329             if (p.getProperty(ifProperty) != null)
330                 return false;
331         }
332         return true;
333     }
334 }
335
Popular Tags