1 18 19 package org.apache.tools.ant.taskdefs.optional.junit; 20 21 22 import java.io.File ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.Vector ; 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 43 public final class BatchTest extends BaseTest { 44 45 46 private Project project; 47 48 49 private Resources resources = new Resources(); 50 51 55 public BatchTest(Project project) { 56 this.project = project; 57 } 58 59 65 public void addFileSet(FileSet fs) { 66 add(fs); 67 68 if (fs.getProject() == null) { 77 fs.setProject(project); 78 } 79 } 80 81 82 91 public void add(ResourceCollection rc) { 92 resources.add(rc); 93 } 94 95 100 public Enumeration elements() { 101 JUnitTest[] tests = createAllJUnitTest(); 102 return Enumerations.fromArray(tests); 103 } 104 105 111 void addTestsTo(Vector 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 124 private JUnitTest[] createAllJUnitTest() { 125 String [] filenames = getFilenames(); 126 JUnitTest[] tests = new JUnitTest[filenames.length]; 127 for (int i = 0; i < tests.length; i++) { 128 String classname = javaToClass(filenames[i]); 129 tests[i] = createJUnitTest(classname); 130 } 131 return tests; 132 } 133 134 144 private String [] getFilenames() { 145 Vector v = new Vector (); 146 Iterator iter = resources.iterator(); 147 while (iter.hasNext()) { 148 Resource r = (Resource) iter.next(); 149 if (r.isExists()) { 150 String 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 [] files = new String [v.size()]; 160 v.copyInto(files); 161 return files; 162 } 163 164 171 public static String javaToClass(String filename) { 172 return filename.replace(File.separatorChar, '.').replace('/', '.') 173 .replace('\\', '.'); 174 } 175 176 183 private JUnitTest createJUnitTest(String 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 list = this.formatters.elements(); 196 while (list.hasMoreElements()) { 197 test.addFormatter((FormatterElement) list.nextElement()); 198 } 199 return test; 200 } 201 202 } 203 | Popular Tags |