You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2001/07/11 11:29:56 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit JUnitTask.java JUnitTest.java JUnitTestRunner.java XMLConstants.java XMLJUnitResultFormatter.java

bodewig     01/07/11 02:29:56

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional/junit
                        JUnitTask.java JUnitTest.java JUnitTestRunner.java
                        XMLConstants.java XMLJUnitResultFormatter.java
  Log:
  Log properties for JUnit tests.
  
  Submitted by:	Erik Hatcher <er...@hatcher.net>
  
  Revision  Changes    Path
  1.20      +29 -2     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  
  Index: JUnitTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- JUnitTask.java	2001/07/11 08:18:57	1.19
  +++ JUnitTask.java	2001/07/11 09:29:50	1.20
  @@ -68,12 +68,15 @@
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.EnumeratedAttribute;
   
  -
   import java.io.File;
  +import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.OutputStream;
   
   import java.util.Enumeration;
  +import java.util.Hashtable;
  +import java.util.Properties;
  +import java.util.Random;
   import java.util.Vector;
   
   /**
  @@ -90,6 +93,7 @@
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
    * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
    * @author <a href="mailto:Gerrit.Riessen@web.de">Gerrit Riessen</a> -
  + * @author <a href="mailto:erik@hatcher.net">Erik Hatcher</a>
    */
   public class JUnitTask extends Task {
   
  @@ -363,6 +367,23 @@
               formatterArg.setLength(0);
           }
   
  +        // Create a temporary file to pass the Ant properties to the forked test
  +        File propsFile = new File("junit" + (new Random(System.currentTimeMillis())).nextLong() + ".properties");
  +        cmd.createArgument().setValue("propsfile=" + propsFile.getAbsolutePath());
  +        Hashtable p = project.getProperties(); 
  +        Properties props = new Properties();
  +        for (Enumeration enum = p.keys(); enum.hasMoreElements(); ) {
  +            Object key = enum.nextElement();
  +            props.put(key, p.get(key));
  +        }
  +        try {
  +            FileOutputStream outstream = new FileOutputStream(propsFile);
  +            props.save(outstream,"Ant JUnitTask generated properties file");
  +            outstream.close();
  +        } catch (java.io.IOException e) {
  +            throw new BuildException("Error creating temporary properties file.", e, location);
  +        }
  +
           Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN), watchdog);
           execute.setCommandline(cmd.getCommandline());
           if (dir != null) {
  @@ -371,11 +392,16 @@
           }
   
           log("Executing: "+cmd.toString(), Project.MSG_VERBOSE);
  +        int retVal; 
           try {
  -            return execute.execute();
  +            retVal = execute.execute();
           } catch (IOException e) {
               throw new BuildException("Process fork failed.", e, location);
  +        } finally {
  +          if (! propsFile.delete()) throw new BuildException("Could not delete temporary properties file.");
           }
  +
  +        return retVal;
       }
   
       // in VM is not very nice since it could probably hang the
  @@ -386,6 +412,7 @@
        * Execute inside VM.
        */
       private int executeInVM(JUnitTest test) throws BuildException {
  +        test.setProperties(project.getProperties());
           if (dir != null) {
               log("dir attribute ignored if running in the same VM", Project.MSG_WARN);
           }
  
  
  
  1.6       +16 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTest.java
  
  Index: JUnitTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JUnitTest.java	2001/02/01 15:56:42	1.5
  +++ JUnitTest.java	2001/07/11 09:29:50	1.6
  @@ -58,6 +58,9 @@
   import org.apache.tools.ant.types.Commandline;
   
   import java.io.File;
  +import java.util.Enumeration;
  +import java.util.Hashtable;
  +import java.util.Properties;
   import java.util.Vector;
   
   /**
  @@ -80,6 +83,9 @@
       private long runs, failures, errors;
       private long runTime;
   
  +    // Snapshot of the system properties
  +    private Properties props = null;
  +
       public JUnitTest() {
       }
   
  @@ -127,6 +133,15 @@
       public long errorCount() {return errors;}
       public long getRunTime() {return runTime;}
   
  +    public Properties getProperties() { return props;}
  +    public void setProperties(Hashtable p) { 
  +        props = new Properties();  
  +        for (Enumeration enum = p.keys(); enum.hasMoreElements(); ) {
  +            Object key = enum.nextElement();
  +            props.put(key, p.get(key));
  +        }
  +    }
  +
       public boolean shouldRun(Project p) {
           if (ifProperty != null && p.getProperty(ifProperty) == null) {
               return false;
  @@ -134,6 +149,7 @@
                      p.getProperty(unlessProperty) != null) {
               return false;
           }
  +
           return true;
       }
   
  
  
  
  1.10      +18 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
  
  Index: JUnitTestRunner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- JUnitTestRunner.java	2001/05/10 15:22:55	1.9
  +++ JUnitTestRunner.java	2001/07/11 09:29:51	1.10
  @@ -59,6 +59,9 @@
   import junit.framework.*;
   import java.lang.reflect.*;
   import java.io.*;
  +import java.util.Enumeration;
  +import java.util.Hashtable;
  +import java.util.Properties;
   import java.util.StringTokenizer;
   import java.util.Vector;
   
  @@ -75,6 +78,7 @@
    * <p>Summary output is generated at the end.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  + * @author <a href="mailto:erik@hatcher.net">Erik Hatcher</a>
    */
   
   public class JUnitTestRunner implements TestListener {
  @@ -346,6 +350,7 @@
           boolean exitAtEnd = true;
           boolean haltError = false;
           boolean haltFail = false;
  +        Properties props = new Properties();
   
           if (args.length == 0) {
               System.err.println("required argument TestClassName missing");
  @@ -364,10 +369,23 @@
                       System.err.println(be.getMessage());
                       System.exit(ERRORS);
                   }
  +            } else if (args[i].startsWith("propsfile=")) {
  +                FileInputStream in = new FileInputStream(args[i].substring(10));
  +                props.load(in);
  +                in.close();
               }
           }
           
           JUnitTest t = new JUnitTest(args[0]);
  +
  +        // Add/overlay system properties on the properties from the Ant project
  +        Hashtable p = System.getProperties();
  +        for (Enumeration enum = p.keys(); enum.hasMoreElements(); ) {
  +            Object key = enum.nextElement();
  +            props.put(key, p.get(key));
  +        }
  +        t.setProperties(props);
  +
           JUnitTestRunner runner = new JUnitTestRunner(t, haltError, haltFail);
           transferFormatters(runner);
           runner.run();
  
  
  
  1.3       +11 -1     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLConstants.java
  
  Index: XMLConstants.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLConstants.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XMLConstants.java	2001/05/03 15:04:02	1.2
  +++ XMLConstants.java	2001/07/11 09:29:51	1.3
  @@ -89,7 +89,7 @@
       /** package attribute for the aggregate document */
       public final static String ATTR_PACKAGE = "package";
   
  -    /** name attribute for testcase and testsuite elements */
  +    /** name attribute for property, testcase and testsuite elements */
       public final static String ATTR_NAME = "name";
   
       /** time attribute for testcase and testsuite elements */
  @@ -109,4 +109,14 @@
   
       /** message attribute for failure elements */
       public final static String ATTR_MESSAGE = "message";
  +
  +    /** the properties element */
  +    public final static String PROPERTIES = "properties";
  +
  +    /** the property element */
  +    public final static String PROPERTY = "property";
  +
  +    /** value attribute for property elements */
  +    public final static String ATTR_VALUE = "value";
  +
   }
  
  
  
  1.8       +16 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java
  
  Index: XMLJUnitResultFormatter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- XMLJUnitResultFormatter.java	2001/05/03 15:04:03	1.7
  +++ XMLJUnitResultFormatter.java	2001/07/11 09:29:52	1.8
  @@ -72,6 +72,7 @@
    * Prints XML output of the test to a specified Writer.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  + * @author <a href="mailto:erik@hatcher.net">Erik Hatcher</a>
    */
   
   public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants {
  @@ -132,6 +133,21 @@
           doc = getDocumentBuilder().newDocument();
           rootElement = doc.createElement(TESTSUITE);
           rootElement.setAttribute(ATTR_NAME, suite.getName());
  +
  +        // Output properties
  +        Element propsElement = doc.createElement(PROPERTIES);
  +        rootElement.appendChild(propsElement);
  +        Properties props = suite.getProperties();
  +        if (props != null) {
  +            Enumeration e = props.propertyNames();
  +            while (e.hasMoreElements()) {
  +                String name = (String) e.nextElement();
  +                Element propElement = doc.createElement(PROPERTY);
  +                propElement.setAttribute(ATTR_NAME, name);
  +                propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
  +                propsElement.appendChild(propElement);
  +            }
  +        }
       }
   
       /**