1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.woopi.ant.taskdefs.junit;
19
20 import java.io.File;
21 import java.util.Enumeration;
22 import java.util.Vector;
23 import org.apache.tools.ant.DirectoryScanner;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.types.FileSet;
26
27 /***
28 * <p> Create then run <code>JUnitTest</code>'s based on the list of files
29 * given by the fileset attribute.
30 *
31 * <p> Every <code>.java</code> or <code>.class</code> file in the fileset is
32 * assumed to be a testcase.
33 * A <code>JUnitTest</code> is created for each of these named classes with
34 * basic setup inherited from the parent <code>BatchTest</code>.
35 *
36 * @see JUnitTest
37 */
38 public final class BatchTest extends BaseTest {
39
40 /*** the reference to the project */
41 private Project project;
42
43 /*** the list of filesets containing the testcase filename rules */
44 private Vector filesets = new Vector();
45
46 /***
47 * create a new batchtest instance
48 * @param project the project it depends on.
49 */
50 public BatchTest(Project project) {
51 this.project = project;
52 }
53
54 /***
55 * Add a new fileset instance to this batchtest. Whatever the fileset is,
56 * only filename that are <tt>.java</tt> or <tt>.class</tt> will be
57 * considered as 'candidates'.
58 * @param fs the new fileset containing the rules to get the testcases.
59 */
60 public void addFileSet(FileSet fs) {
61 filesets.addElement(fs);
62 }
63
64 /***
65 * Return all <tt>JUnitTest</tt> instances obtain by applying the fileset rules.
66 * @return an enumeration of all elements of this batchtest that are
67 * a <tt>JUnitTest</tt> instance.
68 */
69 public final Enumeration elements() {
70 JUnitTest[] tests = createAllJUnitTest();
71 return Enumerations.fromArray(tests);
72 }
73
74 /***
75 * Convenient method to merge the <tt>JUnitTest</tt>s of this batchtest
76 * to a <tt>Vector</tt>.
77 * @param v the vector to which should be added all individual tests of this
78 * batch test.
79 */
80 final void addTestsTo(Vector v) {
81 JUnitTest[] tests = createAllJUnitTest();
82 v.ensureCapacity(v.size() + tests.length);
83 for (int i = 0; i < tests.length; i++) {
84 v.addElement(tests[i]);
85 }
86 }
87
88 /***
89 * Create all <tt>JUnitTest</tt>s based on the filesets. Each instance
90 * is configured to match this instance properties.
91 * @return the array of all <tt>JUnitTest</tt>s that belongs to this batch.
92 */
93 private JUnitTest[] createAllJUnitTest() {
94 String[] filenames = getFilenames();
95 JUnitTest[] tests = new JUnitTest[filenames.length];
96 for (int i = 0; i < tests.length; i++) {
97 String classname = javaToClass(filenames[i]);
98 tests[i] = createJUnitTest(classname);
99 }
100 return tests;
101 }
102
103 /***
104 * Iterate over all filesets and return the filename of all files
105 * that end with <tt>.java</tt> or <tt>.class</tt>. This is to avoid
106 * wrapping a <tt>JUnitTest</tt> over an xml file for example. A Testcase
107 * is obviously a java file (compiled or not).
108 * @return an array of filenames without their extension. As they should
109 * normally be taken from their root, filenames should match their fully
110 * qualified class name (If it is not the case it will fail when running the test).
111 * For the class <tt>org/apache/Whatever.class</tt> it will return <tt>org/apache/Whatever</tt>.
112 */
113 private String[] getFilenames() {
114 Vector v = new Vector();
115 final int size = this.filesets.size();
116 for (int j = 0; j < size; j++) {
117 FileSet fs = (FileSet) filesets.elementAt(j);
118 DirectoryScanner ds = fs.getDirectoryScanner(project);
119 ds.scan();
120 String[] f = ds.getIncludedFiles();
121 for (int k = 0; k < f.length; k++) {
122 String pathname = f[k];
123 if (pathname.endsWith(".java")) {
124 v.addElement(pathname.substring(0, pathname.length() - ".java".length()));
125 } else if (pathname.endsWith(".class")) {
126 v.addElement(pathname.substring(0, pathname.length() - ".class".length()));
127 }
128 }
129 }
130
131 String[] files = new String[v.size()];
132 v.copyInto(files);
133 return files;
134 }
135
136 /***
137 * Convenient method to convert a pathname without extension to a
138 * fully qualified classname. For example <tt>org/apache/Whatever</tt> will
139 * be converted to <tt>org.apache.Whatever</tt>
140 * @param filename the filename to "convert" to a classname.
141 * @return the classname matching the filename.
142 */
143 public static final String javaToClass(String filename) {
144 return filename.replace(File.separatorChar, '.');
145 }
146
147 /***
148 * Create a <tt>JUnitTest</tt> that has the same property as this
149 * <tt>BatchTest</tt> instance.
150 * @param classname the name of the class that should be run as a
151 * <tt>JUnitTest</tt>. It must be a fully qualified name.
152 * @return the <tt>JUnitTest</tt> over the given classname.
153 */
154 private JUnitTest createJUnitTest(String classname) {
155 JUnitTest test = new JUnitTest();
156 test.setName(classname);
157 test.setHaltonerror(this.haltOnError);
158 test.setHaltonfailure(this.haltOnFail);
159 test.setFiltertrace(this.filtertrace);
160 test.setFork(this.fork);
161 test.setIf(this.ifProperty);
162 test.setUnless(this.unlessProperty);
163 test.setTodir(this.destDir);
164 test.setFailureProperty(failureProperty);
165 test.setErrorProperty(errorProperty);
166 Enumeration list = this.formatters.elements();
167 while (list.hasMoreElements()) {
168 test.addFormatter((FormatterElement) list.nextElement());
169 }
170 return test;
171 }
172
173 }