KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > ant > TestClassValidatorTask


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights 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 Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.ant;
56
57 import junitx.tool.TestClassValidator;
58 import org.apache.tools.ant.AntClassLoader;
59 import org.apache.tools.ant.BuildException;
60 import org.apache.tools.ant.DirectoryScanner;
61 import org.apache.tools.ant.Project;
62 import org.apache.tools.ant.Task;
63 import org.apache.tools.ant.types.FileSet;
64 import org.apache.tools.ant.types.Path;
65 import org.apache.tools.ant.types.Reference;
66
67 import java.io.File JavaDoc;
68 import java.util.List JavaDoc;
69 import java.util.Vector JavaDoc;
70
71 /**
72  * Validate a JUnit <tt>TestCase</tt> or <tt>TestSuite</tt> and outline
73  * potential errors in the definition of the classes.
74  *
75  * <h4>Usage</h4>
76  * <pre>
77  * &lt;taskdef name="TestValidator"
78  * classname="junitx.ant.TestClassValidatorTask"
79  * classpath="junit-addons-3.7.x.jar" />
80  *
81  * &lt;validator verbose="off">
82  * &lt;classpath>
83  * &lt;pathelement location="classes" />
84  * &lt;fileset dir="lib">
85  * &lt;include name="\*\*.jar"/>
86  * &lt;include name="\*\*.zip"/>
87  * &lt;/fileset>
88  * &lt;/classpath>
89  * &lt;fileset dir="classes">
90  * &lt;include name="*-/*Suite.class"/>
91  * &lt;include name="*-/*Test.class"/>
92  * &lt;/fileset>
93  * &lt;/validator>
94  * </pre>
95  *
96  * @version $Revision: 1.8 $ $Date: 2003/02/06 20:43:52 $
97  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
98  */

99 public class TestClassValidatorTask
100         extends Task {
101
102     private int verbosity = Project.MSG_VERBOSE;
103     private boolean quiet = false;
104     private boolean failonerror = true;
105
106     private AntClassLoader loader;
107     private Path classpath;
108     private List JavaDoc filesets = new Vector JavaDoc();
109
110     private TestClassValidator.ClassValidator validator;
111
112     /**
113      * Called by the project to let the task do it's work.
114      *
115      * @throws BuildException if someting goes wrong with the build
116      */

117     public void execute()
118             throws BuildException {
119         this.validator = new TestClassValidator.DefaultClassValidator();
120         this.validator.setListener(new TestClassValidator.ClassValidatorListener() {
121             public void info(String JavaDoc message) {
122                 log("INFO> " + message, verbosity);
123                 System.out.println("INFO> " + message);
124             }
125
126             public void warning(String JavaDoc message) {
127                 log("WARNING> " + message, verbosity);
128                 System.out.println("WARNING> " + message);
129             }
130
131             public void error(String JavaDoc message) {
132                 log("ERROR> " + message, verbosity);
133                 System.out.println("ERROR> " + message);
134             }
135         });
136
137
138         if (classpath != null) {
139             classpath.setProject(project);
140             this.loader = new AntClassLoader(project, classpath);
141         }
142
143         log(TestClassValidator.BANNER, Project.MSG_VERBOSE);
144         System.out.println(TestClassValidator.BANNER);
145         int count = 0;
146         for (int i = 0; i < filesets.size(); i++) {
147             FileSet fs = (FileSet) filesets.get(i);
148
149             try {
150                 DirectoryScanner ds = fs.getDirectoryScanner(project);
151                 ds.scan();
152
153                 String JavaDoc[] files = ds.getIncludedFiles();
154
155                 for (int k = 0; k < files.length; k++) {
156                     String JavaDoc pathname = files[k];
157                     if (pathname.endsWith(".class")) {
158                         String JavaDoc classname = pathname.substring(0, pathname.length() - ".class".length()).replace(File.separatorChar, '.');
159                         processFile(classname);
160                     }
161                 }
162                 count += files.length;
163             } catch (BuildException e) {
164                 if (failonerror) {
165                     throw e;
166                 } else {
167                     log(e.getMessage(), quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
168                 }
169             } catch (ClassNotFoundException JavaDoc e) {
170                 if (failonerror) {
171                     throw new BuildException(e);
172                 } else {
173                     log(e.getMessage(), quiet ? Project.MSG_VERBOSE : Project.MSG_WARN);
174                 }
175             }
176             log("Number of classes: " + count, Project.MSG_VERBOSE);
177             System.out.println("Number of classes: " + count);
178         }
179     }
180
181     /**
182      * Set the classpath to be used for this validation.
183      */

184     public void setClasspath(Path classpath) {
185         createClasspath().append(classpath);
186     }
187
188     /**
189      * Creates a nested classpath element
190      */

191     public Path createClasspath() {
192         if (this.classpath == null) {
193             this.classpath = new Path(project);
194         }
195         return this.classpath.createPath();
196     }
197
198     /**
199      * Adds a reference to a CLASSPATH defined elsewhere.
200      */

201     public void setClasspathRef(Reference r) {
202         createClasspath().setRefid(r);
203     }
204
205     /**
206      * Add a new fileset instance to this task. Whatever the fileset is,
207      * only filename that are <tt>.java</tt> or <tt>.class</tt> will be
208      * considered as 'candidates'.
209      *
210      * @param set the new fileset containing the rules to get the testcases.
211      */

212     public void addFileset(FileSet set) {
213         filesets.add(set);
214     }
215
216     /**
217      * Used to force listing of all names of processed files.
218      *
219      * @param verbose "true" or "on"
220      */

221     public void setVerbose(boolean verbose) {
222         if (verbose) {
223             this.verbosity = Project.MSG_INFO;
224         } else {
225             this.verbosity = Project.MSG_VERBOSE;
226         }
227     }
228
229     private void processFile(String JavaDoc classname)
230             throws ClassNotFoundException JavaDoc {
231         log("processing " + classname, verbosity);
232
233         Class JavaDoc cls;
234         if (this.loader != null) {
235             cls = this.loader.loadClass(classname);
236         } else {
237             ClassLoader JavaDoc l = this.getClass().getClassLoader();
238             // Can return null to represent the bootstrap class loader.
239
// see API docs of Class.getClassLoader.
240
if (l != null) {
241                 cls = l.loadClass(classname);
242             } else {
243                 cls = Class.forName(classname);
244             }
245         }
246         this.validator.validate(cls);
247     }
248
249 }
250
Popular Tags