in the accessors. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271125 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -54,9 +54,9 @@ | |||
| package org.apache.tools.ant.taskdefs.optional.rjunit; | |||
| import java.io.File; | |||
| import java.util.ArrayList; | |||
| import java.util.Enumeration; | |||
| import java.util.Hashtable; | |||
| import java.util.Vector; | |||
| import junit.runner.TestCollector; | |||
| @@ -90,7 +90,7 @@ public final class ClasspathTestCollector extends ProjectComponent | |||
| String[] paths = path.list(); | |||
| for (int i = paths.length - 1; i >= 0; i--) { | |||
| File f = new File(paths[i]); | |||
| Vector included = null; | |||
| ArrayList included = null; | |||
| if (f.isDirectory()) { | |||
| included = gatherFromDirectory(f); | |||
| } else if (f.getName().endsWith(".zip") | |||
| @@ -103,16 +103,22 @@ public final class ClasspathTestCollector extends ProjectComponent | |||
| final int includedCount = included.size(); | |||
| log("Adding " + includedCount + " testcases from " + f, Project.MSG_VERBOSE); | |||
| for (int j = 0; j < includedCount; j++) { | |||
| String testname = (String) included.elementAt(j); | |||
| String testname = (String) included.get(j); | |||
| collected.put(testname, ""); | |||
| } | |||
| } | |||
| log("Collected " + collected.size() + " testcases from " + paths.length + " paths."); | |||
| log("Collected " + collected.size() + " testcases from " + paths.length + " path(s).", Project.MSG_VERBOSE); | |||
| return collected.keys(); | |||
| } | |||
| protected Vector gatherFromDirectory(File dir) { | |||
| /** | |||
| * Return the list of classnames from a directory that match | |||
| * the specified patterns. | |||
| * @param dir the base directory (must also be the base package) | |||
| * @return the list of classnames matching the pattern. | |||
| */ | |||
| protected ArrayList gatherFromDirectory(File dir) { | |||
| Project project = getProject(); | |||
| DirectoryScanner ds = new DirectoryScanner(); | |||
| ds.setBasedir(dir); | |||
| @@ -123,7 +129,13 @@ public final class ClasspathTestCollector extends ProjectComponent | |||
| return testClassNameFromFile(included); | |||
| } | |||
| protected Vector gatherFromArchive(File zip) { | |||
| /** | |||
| * Return the list of classnames from a zipfile that match | |||
| * the specified patterns. | |||
| * @param zip the zipfile (must also be the base package) | |||
| * @return the list of classnames matching the pattern. | |||
| */ | |||
| protected ArrayList gatherFromArchive(File zip) { | |||
| ZipScanner zs = new ZipScanner(); | |||
| zs.setBasedir(zip); | |||
| zs.setIncludes(patterns.getIncludePatterns(project)); | |||
| @@ -133,13 +145,17 @@ public final class ClasspathTestCollector extends ProjectComponent | |||
| return testClassNameFromFile(included); | |||
| } | |||
| protected Vector testClassNameFromFile(String[] classFileNames) { | |||
| Vector tests = new Vector(classFileNames.length); | |||
| /** | |||
| * transform a set of file into their matching classname | |||
| * @todo what about using a mapper for this ? | |||
| */ | |||
| protected ArrayList testClassNameFromFile(String[] classFileNames) { | |||
| ArrayList tests = new ArrayList(classFileNames.length); | |||
| for (int i = 0; i < classFileNames.length; i++) { | |||
| String file = classFileNames[i]; | |||
| if (isTestClass(file)) { | |||
| String classname = classNameFromFile(file); | |||
| tests.addElement(classname); | |||
| tests.add(classname); | |||
| } | |||
| } | |||
| return tests; | |||
| @@ -165,7 +181,7 @@ public final class ClasspathTestCollector extends ProjectComponent | |||
| this.path = path; | |||
| } | |||
| public Path getPath(){ | |||
| public Path getPath() { | |||
| return this.path; | |||
| } | |||
| @@ -54,14 +54,14 @@ | |||
| package org.apache.tools.ant.taskdefs.optional.rjunit; | |||
| import java.io.BufferedOutputStream; | |||
| import java.io.ByteArrayOutputStream; | |||
| import java.io.File; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import java.io.OutputStream; | |||
| import java.io.ByteArrayOutputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.Enumeration; | |||
| import java.util.Properties; | |||
| import java.util.Vector; | |||
| import junit.runner.TestCollector; | |||
| @@ -100,7 +100,7 @@ public final class ClientElement extends ProjectComponent { | |||
| private String host = "127.0.0.1"; | |||
| /** test collector elements */ | |||
| private Vector testCollectors = new Vector(); | |||
| private ArrayList testCollectors = new ArrayList(); | |||
| /** the command line to launch the TestRunner */ | |||
| private CommandlineJava cmd = new CommandlineJava(); | |||
| @@ -108,6 +108,9 @@ public final class ClientElement extends ProjectComponent { | |||
| /** the parent task */ | |||
| private RJUnitTask parent; | |||
| /** help debug the TestRunner */ | |||
| private boolean debug = false; | |||
| /** create a new client */ | |||
| public ClientElement(RJUnitTask value) { | |||
| parent = value; | |||
| @@ -133,7 +136,7 @@ public final class ClientElement extends ProjectComponent { | |||
| protected void doExecute() throws BuildException { | |||
| File tmp = configureTestRunner(); | |||
| Execute execute = new Execute(new LogStreamHandler(parent, Project.MSG_INFO, Project.MSG_WARN)); | |||
| Execute execute = new Execute(new LogStreamHandler(parent, Project.MSG_VERBOSE, Project.MSG_VERBOSE)); | |||
| execute.setCommandline(cmd.getCommandline()); | |||
| execute.setAntRun(project); | |||
| @@ -141,7 +144,7 @@ public final class ClientElement extends ProjectComponent { | |||
| int retVal = 0; | |||
| try { | |||
| retVal = execute.execute(); | |||
| if (retVal != 0){ | |||
| if (retVal != 0) { | |||
| throw new BuildException("task.process-failed.error"); | |||
| } | |||
| } catch (IOException e) { | |||
| @@ -162,14 +165,7 @@ public final class ClientElement extends ProjectComponent { | |||
| protected Enumeration collectTests() { | |||
| Enumeration[] tests = new Enumeration[testCollectors.size()]; | |||
| for (int i = 0; i < testCollectors.size(); i++) { | |||
| TestCollector te = (TestCollector) testCollectors.elementAt(i); | |||
| //@fixme I'm forced to append the classpath from batchtests | |||
| // here to make sure it will be included as part of the | |||
| // testrunner classpath. Need a better design ? | |||
| if (te instanceof BatchTestElement){ | |||
| Path path = ((BatchTestElement)te).getPath(); | |||
| cmd.getClasspath().append(path); | |||
| } | |||
| TestCollector te = (TestCollector) testCollectors.get(i); | |||
| tests[i] = te.collectTests(); | |||
| } | |||
| return new CompoundEnumeration(tests); | |||
| @@ -182,8 +178,8 @@ public final class ClientElement extends ProjectComponent { | |||
| */ | |||
| protected File configureTestRunner() throws BuildException { | |||
| Properties props = new Properties(); | |||
| props.setProperty("debug", "true"); | |||
| props.setProperty("host", "127.0.0.1"); | |||
| props.setProperty("debug", String.valueOf(debug)); | |||
| props.setProperty("host", host); | |||
| props.setProperty("port", String.valueOf(port)); | |||
| // get all test classes to run... | |||
| StringBuffer buf = new StringBuffer(10240); | |||
| @@ -233,6 +229,11 @@ public final class ClientElement extends ProjectComponent { | |||
| host = value; | |||
| } | |||
| /** set debug mode for the runner. it will log a file to working dir */ | |||
| public void setDebug(boolean flag) { | |||
| debug = flag; | |||
| } | |||
| /** Create a new JVM argument. */ | |||
| public Commandline.Argument createJvmarg() { | |||
| return cmd.createVmArgument(); | |||
| @@ -244,14 +245,16 @@ public final class ClientElement extends ProjectComponent { | |||
| } | |||
| /** add a single test element */ | |||
| public void addTest(TestElement value) { | |||
| testCollectors.addElement(value); | |||
| public void addConfiguredTest(TestElement value) { | |||
| testCollectors.add(value); | |||
| } | |||
| /** add a batch test element */ | |||
| public void addBatchTest(BatchTestElement value) { | |||
| testCollectors.addElement(value); | |||
| public void addConfiguredBatchTest(BatchTestElement value) { | |||
| // add the classpath of batchtest to cmd classpath | |||
| Path path = value.getPath(); | |||
| cmd.getClasspath().append(path); | |||
| testCollectors.add(value); | |||
| } | |||
| } | |||
| @@ -89,44 +89,44 @@ import java.util.NoSuchElementException; | |||
| */ | |||
| public class CompoundEnumeration implements Enumeration { | |||
| /** enumeration array */ | |||
| private Enumeration[] enumArray; | |||
| /** enumeration array */ | |||
| private Enumeration[] enumArray; | |||
| /** index in the enums array */ | |||
| private int index = 0; | |||
| /** index in the enums array */ | |||
| private int index = 0; | |||
| public CompoundEnumeration(Enumeration[] enumarray) { | |||
| this.enumArray = enumarray; | |||
| this.enumArray = enumarray; | |||
| } | |||
| /** | |||
| * Tests if this enumeration contains more elements. | |||
| * | |||
| * @return <code>true</code> if and only if this enumeration object | |||
| * contains at least one more element to provide; | |||
| * <code>false</code> otherwise. | |||
| */ | |||
| /** | |||
| * Tests if this enumeration contains more elements. | |||
| * | |||
| * @return <code>true</code> if and only if this enumeration object | |||
| * contains at least one more element to provide; | |||
| * <code>false</code> otherwise. | |||
| */ | |||
| public boolean hasMoreElements() { | |||
| while (index < enumArray.length) { | |||
| if (enumArray[index] != null && enumArray[index].hasMoreElements()) { | |||
| return true; | |||
| } | |||
| index++; | |||
| } | |||
| return false; | |||
| while (index < enumArray.length) { | |||
| if (enumArray[index] != null && enumArray[index].hasMoreElements()) { | |||
| return true; | |||
| } | |||
| index++; | |||
| } | |||
| return false; | |||
| } | |||
| /** | |||
| * Returns the next element of this enumeration if this enumeration | |||
| * object has at least one more element to provide. | |||
| * | |||
| * @return the next element of this enumeration. | |||
| * @throws NoSuchElementException if no more elements exist. | |||
| */ | |||
| /** | |||
| * Returns the next element of this enumeration if this enumeration | |||
| * object has at least one more element to provide. | |||
| * | |||
| * @return the next element of this enumeration. | |||
| * @throws NoSuchElementException if no more elements exist. | |||
| */ | |||
| public Object nextElement() throws NoSuchElementException { | |||
| if ( hasMoreElements() ) { | |||
| return enumArray[index].nextElement(); | |||
| } | |||
| throw new NoSuchElementException(); | |||
| if (hasMoreElements()) { | |||
| return enumArray[index].nextElement(); | |||
| } | |||
| throw new NoSuchElementException(); | |||
| } | |||
| } | |||
| @@ -55,10 +55,12 @@ package org.apache.tools.ant.taskdefs.optional.rjunit; | |||
| import java.io.File; | |||
| import java.lang.reflect.Method; | |||
| import java.lang.reflect.Modifier; | |||
| import java.net.URL; | |||
| import junit.framework.Test; | |||
| import junit.framework.TestSuite; | |||
| import junit.framework.TestCase; | |||
| import org.apache.tools.ant.types.Path; | |||
| @@ -75,14 +77,24 @@ public final class JUnitHelper { | |||
| * This method parse the output of the method <tt>toString()</tt> | |||
| * from the <tt>TestCase</tt> class. The format returned is: | |||
| * <tt>name(classname)</tt> | |||
| * @return an array with the elements in the order name, classname. | |||
| * @return the string corresponding to name. | |||
| */ | |||
| public static String[] parseTestString(String testname) { | |||
| int p1 = testname.indexOf('('); | |||
| int p2 = testname.indexOf(')', p1); | |||
| return new String[]{ | |||
| testname.substring(0, p1), | |||
| testname.substring(p1 + 1, p2)}; | |||
| public static String getTestName(String text){ | |||
| int p1 = text.indexOf('('); | |||
| int p2 = text.indexOf(')', p1); | |||
| return text.substring(0, p1); | |||
| } | |||
| /** | |||
| * This method parse the output of the method <tt>toString()</tt> | |||
| * from the <tt>TestCase</tt> class. The format returned is: | |||
| * <tt>name(classname)</tt> | |||
| * @return the string corresponding to classname. | |||
| */ | |||
| public static String getSuiteName(String text){ | |||
| int p1 = text.indexOf('('); | |||
| int p2 = text.indexOf(')', p1); | |||
| return text.substring(p1 + 1, p2); | |||
| } | |||
| /** | |||
| @@ -122,6 +134,14 @@ public final class JUnitHelper { | |||
| return (Test) suiteMethod.invoke(null, new Class[0]); | |||
| } catch (Exception e) { | |||
| } | |||
| // check if it is really a valid testcase | |||
| int modifiers = clazz.getModifiers(); | |||
| if ( !Modifier.isPublic(modifiers) || | |||
| Modifier.isAbstract(modifiers) || | |||
| !TestCase.class.isAssignableFrom(clazz)) { | |||
| return null; | |||
| } | |||
| // try to extract a test suite automatically | |||
| // this will generate warnings if the class is no suitable Test | |||
| try { | |||
| @@ -158,9 +158,9 @@ public class RJUnitTask extends Task { | |||
| public void run() { | |||
| try { | |||
| server.execute(); | |||
| System.out.println("PANIC !!!!!!"); | |||
| } catch (Exception e) { | |||
| caught = e; | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| @@ -53,8 +53,8 @@ | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.rjunit; | |||
| import java.util.Enumeration; | |||
| import java.util.Vector; | |||
| import java.io.IOException; | |||
| import java.util.ArrayList; | |||
| import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.ProjectComponent; | |||
| @@ -76,7 +76,7 @@ import org.apache.tools.ant.taskdefs.optional.rjunit.remote.Server; | |||
| public final class ServerElement extends ProjectComponent { | |||
| /** formatters that write the tests results */ | |||
| private Vector formatterElements = new Vector(); | |||
| private ArrayList formatters = new ArrayList(); | |||
| /** port to run the server on. Default to 6666 */ | |||
| private int port = 6666; | |||
| @@ -101,17 +101,17 @@ public final class ServerElement extends ProjectComponent { | |||
| public void execute() throws BuildException { | |||
| // configure the server... | |||
| server = new Server(port); | |||
| Enumeration listeners = formatterElements.elements(); | |||
| while (listeners.hasMoreElements()) { | |||
| ResultFormatterElement fe = (ResultFormatterElement)listeners.nextElement(); | |||
| Formatter formatter = fe.createFormatter(); | |||
| server.addListener( formatter ); | |||
| final int formatterCount = formatters.size(); | |||
| for (int i = 0; i < formatterCount; i++) { | |||
| final Formatter f = (Formatter) formatters.get(i); | |||
| server.addListener(f); | |||
| } | |||
| // and run it. It will stop once a client has finished. | |||
| try { | |||
| server.start(true); | |||
| server.start(false); // do not loop | |||
| server.shutdown(); | |||
| } catch (InterruptedException e){ | |||
| } catch (IOException e) { | |||
| throw new BuildException(e); | |||
| } | |||
| } | |||
| @@ -133,7 +133,8 @@ public final class ServerElement extends ProjectComponent { | |||
| } | |||
| /** add a new formatter element */ | |||
| public void addFormatter(ResultFormatterElement fe) { | |||
| formatterElements.addElement(fe); | |||
| public void addConfiguredFormatter(ResultFormatterElement fe) { | |||
| Formatter formatter = fe.createFormatter(); | |||
| formatters.add(formatter); | |||
| } | |||
| } | |||