source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/junit/BatchTest.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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