KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.io.File JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.types.FileSet;
28 import org.apache.tools.ant.types.Resource;
29 import org.apache.tools.ant.types.ResourceCollection;
30 import org.apache.tools.ant.types.resources.Resources;
31
32 /**
33  * <p> Create then run <code>JUnitTest</code>'s based on the list of files
34  * given by the fileset attribute.
35  *
36  * <p> Every <code>.java</code> or <code>.class</code> file in the fileset is
37  * assumed to be a testcase.
38  * A <code>JUnitTest</code> is created for each of these named classes with
39  * basic setup inherited from the parent <code>BatchTest</code>.
40  *
41  * @see JUnitTest
42  */

43 public final class BatchTest extends BaseTest {
44
45     /** the reference to the project */
46     private Project project;
47
48     /** the list of filesets containing the testcase filename rules */
49     private Resources resources = new Resources();
50
51     /**
52      * create a new batchtest instance
53      * @param project the project it depends on.
54      */

55     public BatchTest(Project project) {
56         this.project = project;
57     }
58
59     /**
60      * Add a new fileset instance to this batchtest. Whatever the fileset is,
61      * only filename that are <tt>.java</tt> or <tt>.class</tt> will be
62      * considered as 'candidates'.
63      * @param fs the new fileset containing the rules to get the testcases.
64      */

65     public void addFileSet(FileSet fs) {
66         add(fs);
67
68         // this one is here because the changes to support ResourceCollections
69
// have broken Magic's JUnitTestTask.
70
//
71
// The task adds a FileSet to a BatchTest instance using the
72
// Java API and without telling the FileSet about its project
73
// instance. The original code would pass in project on the
74
// call to getDirectoryScanner - which is now hidden deep into
75
// Resources.iterator() and not reachable.
76
if (fs.getProject() == null) {
77             fs.setProject(project);
78         }
79     }
80
81
82     /**
83      * Add a new ResourceCollection instance to this
84      * batchtest. Whatever the collection is, only names that are
85      * <tt>.java</tt> or <tt>.class</tt> will be considered as
86      * 'candidates'.
87      * @param rc the new ResourceCollection containing the rules to
88      * get the testcases.
89      * @since Ant 1.7
90      */

91     public void add(ResourceCollection rc) {
92         resources.add(rc);
93     }
94
95     /**
96      * Return all <tt>JUnitTest</tt> instances obtain by applying the fileset rules.
97      * @return an enumeration of all elements of this batchtest that are
98      * a <tt>JUnitTest</tt> instance.
99      */

100     public Enumeration JavaDoc elements() {
101         JUnitTest[] tests = createAllJUnitTest();
102         return Enumerations.fromArray(tests);
103     }
104
105     /**
106      * Convenient method to merge the <tt>JUnitTest</tt>s of this batchtest
107      * to a <tt>Vector</tt>.
108      * @param v the vector to which should be added all individual tests of this
109      * batch test.
110      */

111     void addTestsTo(Vector JavaDoc v) {
112         JUnitTest[] tests = createAllJUnitTest();
113         v.ensureCapacity(v.size() + tests.length);
114         for (int i = 0; i < tests.length; i++) {
115             v.addElement(tests[i]);
116         }
117     }
118
119     /**
120      * Create all <tt>JUnitTest</tt>s based on the filesets. Each instance
121      * is configured to match this instance properties.
122      * @return the array of all <tt>JUnitTest</tt>s that belongs to this batch.
123      */

124     private JUnitTest[] createAllJUnitTest() {
125         String JavaDoc[] filenames = getFilenames();
126         JUnitTest[] tests = new JUnitTest[filenames.length];
127         for (int i = 0; i < tests.length; i++) {
128             String JavaDoc classname = javaToClass(filenames[i]);
129             tests[i] = createJUnitTest(classname);
130         }
131         return tests;
132     }
133
134     /**
135      * Iterate over all filesets and return the filename of all files
136      * that end with <tt>.java</tt> or <tt>.class</tt>. This is to avoid
137      * wrapping a <tt>JUnitTest</tt> over an xml file for example. A Testcase
138      * is obviously a java file (compiled or not).
139      * @return an array of filenames without their extension. As they should
140      * normally be taken from their root, filenames should match their fully
141      * qualified class name (If it is not the case it will fail when running the test).
142      * For the class <tt>org/apache/Whatever.class</tt> it will return <tt>org/apache/Whatever</tt>.
143      */

144     private String JavaDoc[] getFilenames() {
145         Vector JavaDoc v = new Vector JavaDoc();
146         Iterator JavaDoc iter = resources.iterator();
147         while (iter.hasNext()) {
148             Resource r = (Resource) iter.next();
149             if (r.isExists()) {
150                 String JavaDoc pathname = r.getName();
151                 if (pathname.endsWith(".java")) {
152                     v.addElement(pathname.substring(0, pathname.length() - ".java".length()));
153                 } else if (pathname.endsWith(".class")) {
154                     v.addElement(pathname.substring(0, pathname.length() - ".class".length()));
155                 }
156             }
157         }
158
159         String JavaDoc[] files = new String JavaDoc[v.size()];
160         v.copyInto(files);
161         return files;
162     }
163
164     /**
165      * Convenient method to convert a pathname without extension to a
166      * fully qualified classname. For example <tt>org/apache/Whatever</tt> will
167      * be converted to <tt>org.apache.Whatever</tt>
168      * @param filename the filename to "convert" to a classname.
169      * @return the classname matching the filename.
170      */

171     public static String JavaDoc javaToClass(String JavaDoc filename) {
172         return filename.replace(File.separatorChar, '.').replace('/', '.')
173             .replace('\\', '.');
174     }
175
176     /**
177      * Create a <tt>JUnitTest</tt> that has the same property as this
178      * <tt>BatchTest</tt> instance.
179      * @param classname the name of the class that should be run as a
180      * <tt>JUnitTest</tt>. It must be a fully qualified name.
181      * @return the <tt>JUnitTest</tt> over the given classname.
182      */

183     private JUnitTest createJUnitTest(String JavaDoc classname) {
184         JUnitTest test = new JUnitTest();
185         test.setName(classname);
186         test.setHaltonerror(this.haltOnError);
187         test.setHaltonfailure(this.haltOnFail);
188         test.setFiltertrace(this.filtertrace);
189         test.setFork(this.fork);
190         test.setIf(this.ifProperty);
191         test.setUnless(this.unlessProperty);
192         test.setTodir(this.destDir);
193         test.setFailureProperty(failureProperty);
194         test.setErrorProperty(errorProperty);
195         Enumeration JavaDoc list = this.formatters.elements();
196         while (list.hasMoreElements()) {
197             test.addFormatter((FormatterElement) list.nextElement());
198         }
199         return test;
200     }
201
202 }
203
Popular Tags