You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by sb...@apache.org on 2002/02/03 14:52:32 UTC

cvs commit: jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit ClasspathTestCollector.java ClientElement.java CompoundEnumeration.java JUnitHelper.java RJUnitTask.java ServerElement.java

sbailliez    02/02/03 05:52:32

  Modified:    proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit
                        ClasspathTestCollector.java ClientElement.java
                        CompoundEnumeration.java JUnitHelper.java
                        RJUnitTask.java ServerElement.java
  Log:
  - Fixed elements to make sure that we have configured elements
  in the accessors.
  
  Revision  Changes    Path
  1.3       +26 -10    jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/ClasspathTestCollector.java
  
  Index: ClasspathTestCollector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/ClasspathTestCollector.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ClasspathTestCollector.java	2 Feb 2002 01:38:21 -0000	1.2
  +++ ClasspathTestCollector.java	3 Feb 2002 13:52:31 -0000	1.3
  @@ -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 @@
           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 @@
               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 @@
           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 @@
           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 @@
           this.path = path;
       }
   
  -    public Path getPath(){
  +    public Path getPath() {
           return this.path;
       }
   
  
  
  
  1.3       +23 -20    jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/ClientElement.java
  
  Index: ClientElement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/ClientElement.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ClientElement.java	2 Feb 2002 01:38:21 -0000	1.2
  +++ ClientElement.java	3 Feb 2002 13:52:31 -0000	1.3
  @@ -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 @@
       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 @@
       /** 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 @@
   
       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 @@
           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 @@
       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 @@
        */
       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 @@
           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 @@
       }
   
       /** 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);
       }
  -
   
   }
  
  
  
  1.2       +30 -30    jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/CompoundEnumeration.java
  
  Index: CompoundEnumeration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/CompoundEnumeration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CompoundEnumeration.java	1 Feb 2002 23:53:22 -0000	1.1
  +++ CompoundEnumeration.java	3 Feb 2002 13:52:31 -0000	1.2
  @@ -89,44 +89,44 @@
    */
   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();
       }
   }
  
  
  
  1.3       +27 -7     jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/JUnitHelper.java
  
  Index: JUnitHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/JUnitHelper.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JUnitHelper.java	2 Feb 2002 01:38:21 -0000	1.2
  +++ JUnitHelper.java	3 Feb 2002 13:52:31 -0000	1.3
  @@ -55,10 +55,12 @@
   
   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 @@
        * 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);
       }
   
       /**
  @@ -121,6 +133,14 @@
               Method suiteMethod = clazz.getMethod(SUITE_METHODNAME, new Class[0]);
               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
  
  
  
  1.2       +1 -1      jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/RJUnitTask.java
  
  Index: RJUnitTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/RJUnitTask.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RJUnitTask.java	1 Feb 2002 23:53:22 -0000	1.1
  +++ RJUnitTask.java	3 Feb 2002 13:52:31 -0000	1.2
  @@ -158,9 +158,9 @@
           public void run() {
               try {
                   server.execute();
  -                System.out.println("PANIC !!!!!!");
               } catch (Exception e) {
                   caught = e;
  +                e.printStackTrace();
               }
           }
   
  
  
  
  1.2       +13 -12    jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/ServerElement.java
  
  Index: ServerElement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/rjunit/ServerElement.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ServerElement.java	1 Feb 2002 23:53:22 -0000	1.1
  +++ ServerElement.java	3 Feb 2002 13:52:31 -0000	1.2
  @@ -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 @@
   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 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 @@
       }
   
       /** 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);
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>