|
|
@@ -69,75 +69,140 @@ import java.io.File; |
|
|
|
* @author <a href="mailto:jeff.martin@synamic.co.uk">Jeff Martin</a> |
|
|
|
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> |
|
|
|
* @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a> |
|
|
|
* |
|
|
|
* @see JUnitTest |
|
|
|
*/ |
|
|
|
public final class BatchTest extends BaseTest { |
|
|
|
|
|
|
|
/** the reference to the project */ |
|
|
|
private Project project; |
|
|
|
|
|
|
|
/** the list of filesets containing the testcase filename rules */ |
|
|
|
private Vector filesets = new Vector(); |
|
|
|
|
|
|
|
/** |
|
|
|
* create a new batchtest instance |
|
|
|
* @param project the project it depends on. |
|
|
|
*/ |
|
|
|
public BatchTest(Project project){ |
|
|
|
this.project = project; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Add a new fileset instance to this batchtest. Whatever the fileset is, |
|
|
|
* only filename that are <tt>.java</tt> or <tt>.class</tt> will be |
|
|
|
* considered as 'candidates'. |
|
|
|
* @param fs the new fileset containing the rules to get the testcases. |
|
|
|
*/ |
|
|
|
public void addFileSet(FileSet fs) { |
|
|
|
filesets.addElement(fs); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* return all <tt>JUnitTest</tt> instances obtain by applying the fileset rules. |
|
|
|
* @return an enumeration of all elements of this batchtest that are |
|
|
|
* a <tt>JUnitTest</tt> instance. |
|
|
|
* @see addTestsTo(Vector) |
|
|
|
*/ |
|
|
|
public final Enumeration elements(){ |
|
|
|
return new FileList(); |
|
|
|
JUnitTest[] tests = createAllJUnitTest(); |
|
|
|
return Enumerations.fromArray(tests); |
|
|
|
} |
|
|
|
|
|
|
|
public class FileList implements Enumeration{ |
|
|
|
private String files[]=null; |
|
|
|
|
|
|
|
private int i=0; |
|
|
|
|
|
|
|
private FileList(){ |
|
|
|
Vector v = new Vector(); |
|
|
|
for (int j=0; j<filesets.size(); j++) { |
|
|
|
FileSet fs = (FileSet) filesets.elementAt(j); |
|
|
|
DirectoryScanner ds = fs.getDirectoryScanner(project); |
|
|
|
ds.scan(); |
|
|
|
String[] f = ds.getIncludedFiles(); |
|
|
|
for (int k=0; k<f.length; k++) { |
|
|
|
if (f[k].endsWith(".java")) { |
|
|
|
v.addElement(f[k].substring(0, f[k].length()-5)); |
|
|
|
} else if (f[k].endsWith(".class")) { |
|
|
|
v.addElement(f[k].substring(0, f[k].length()-6)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
files = new String[v.size()]; |
|
|
|
v.copyInto(files); |
|
|
|
/** |
|
|
|
* Convenient method to merge the <tt>JUnitTest</tt>s of this batchtest |
|
|
|
* to a <tt>Vector</tt>. |
|
|
|
* @param v the vector to which should be added all individual tests of this |
|
|
|
* batch test. |
|
|
|
*/ |
|
|
|
final void addTestsTo(Vector v){ |
|
|
|
JUnitTest[] tests = createAllJUnitTest(); |
|
|
|
v.ensureCapacity( v.size() + tests.length); |
|
|
|
for (int i = 0; i < tests.length; i++) { |
|
|
|
v.addElement(tests[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public final boolean hasMoreElements(){ |
|
|
|
if(i<files.length)return true; |
|
|
|
return false; |
|
|
|
/** |
|
|
|
* Create all <tt>JUnitTest</tt>s based on the filesets. Each instance |
|
|
|
* is configured to match this instance properties. |
|
|
|
* @return the array of all <tt>JUnitTest</tt>s that belongs to this batch. |
|
|
|
*/ |
|
|
|
private JUnitTest[] createAllJUnitTest(){ |
|
|
|
String[] filenames = getFilenames(); |
|
|
|
JUnitTest[] tests = new JUnitTest[filenames.length]; |
|
|
|
for (int i = 0; i < tests.length; i++) { |
|
|
|
String classname = javaToClass(filenames[i]); |
|
|
|
tests[i] = createJUnitTest(classname); |
|
|
|
} |
|
|
|
return tests; |
|
|
|
} |
|
|
|
|
|
|
|
public final Object nextElement() throws NoSuchElementException{ |
|
|
|
if(hasMoreElements()){ |
|
|
|
JUnitTest test = new JUnitTest(javaToClass(files[i])); |
|
|
|
test.setHaltonerror(haltOnError); |
|
|
|
test.setHaltonfailure(haltOnFail); |
|
|
|
test.setFork(fork); |
|
|
|
test.setIf(ifProperty); |
|
|
|
test.setUnless(unlessProperty); |
|
|
|
test.setTodir(destDir); |
|
|
|
Enumeration list = formatters.elements(); |
|
|
|
while (list.hasMoreElements()) { |
|
|
|
test.addFormatter((FormatterElement)list.nextElement()); |
|
|
|
/** |
|
|
|
* Iterate over all filesets and return the filename of all files |
|
|
|
* that end with <tt>.java</tt> or <tt>.class</tt>. This is to avoid |
|
|
|
* wrapping a <tt>JUnitTest</tt> over an xml file for example. A Testcase |
|
|
|
* is obviouslly a java file (compiled or not). |
|
|
|
* @return an array of filenames without their extension. As they should |
|
|
|
* normally be taken from their root, filenames should match their fully |
|
|
|
* qualified class name (If it is not the case it will fail when running the test). |
|
|
|
* For the class <tt>org/apache/Whatever.class</tt> it will return <tt>org/apache/Whatever</tt>. |
|
|
|
*/ |
|
|
|
private String[] getFilenames(){ |
|
|
|
Vector v = new Vector(); |
|
|
|
final int size = this.filesets.size(); |
|
|
|
for (int j=0; j<size; j++) { |
|
|
|
FileSet fs = (FileSet) filesets.elementAt(j); |
|
|
|
DirectoryScanner ds = fs.getDirectoryScanner(project); |
|
|
|
ds.scan(); |
|
|
|
String[] f = ds.getIncludedFiles(); |
|
|
|
for (int k = 0; k < f.length; k++) { |
|
|
|
String pathname = f[k]; |
|
|
|
if (pathname.endsWith(".java")) { |
|
|
|
v.addElement(pathname.substring(0, pathname.length()-".java".length())); |
|
|
|
} else if (pathname.endsWith(".class")) { |
|
|
|
v.addElement(pathname.substring(0, pathname.length()-".class".length())); |
|
|
|
} |
|
|
|
i++; |
|
|
|
return test; |
|
|
|
} |
|
|
|
throw new NoSuchElementException(); |
|
|
|
} |
|
|
|
|
|
|
|
public final String javaToClass(String fileName){ |
|
|
|
return fileName.replace(java.io.File.separatorChar, '.'); |
|
|
|
String[] files = new String[v.size()]; |
|
|
|
v.copyInto(files); |
|
|
|
return files; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* convenient method to convert a pathname without extension to a |
|
|
|
* fully qualified classname. For example <tt>org/apache/Whatever</tt> will |
|
|
|
* be converted to <tt>org.apache.Whatever</tt> |
|
|
|
* @param filename the filename to "convert" to a classname. |
|
|
|
* @return the classname matching the filename. |
|
|
|
*/ |
|
|
|
public final static String javaToClass(String filename){ |
|
|
|
return filename.replace(File.separatorChar, '.'); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a <tt>JUnitTest</tt> that has the same property as this |
|
|
|
* <tt>BatchTest</tt> instance. |
|
|
|
* @param classname the name of the class that should be run as a |
|
|
|
* <tt>JUnitTest</tt>. It must be a fully qualified name. |
|
|
|
* @return the <tt>JUnitTest</tt> over the given classname. |
|
|
|
*/ |
|
|
|
private JUnitTest createJUnitTest(String classname){ |
|
|
|
JUnitTest test = new JUnitTest(); |
|
|
|
test.setName(classname); |
|
|
|
test.setHaltonerror(this.haltOnError); |
|
|
|
test.setHaltonfailure(this.haltOnFail); |
|
|
|
test.setFork(this.fork); |
|
|
|
test.setIf(this.ifProperty); |
|
|
|
test.setUnless(this.unlessProperty); |
|
|
|
test.setTodir(this.destDir); |
|
|
|
Enumeration list = this.formatters.elements(); |
|
|
|
while (list.hasMoreElements()) { |
|
|
|
test.addFormatter((FormatterElement)list.nextElement()); |
|
|
|
} |
|
|
|
return test; |
|
|
|
} |
|
|
|
|
|
|
|
} |