ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ingmar Stein" <st...@xtramind.com>
Subject Re: JUnit writes properties for each testcase
Date Wed, 12 Sep 2001 14:09:07 GMT
Okay, here's task that takes a property file and writes the stored
properties into an XML file. If no property file is specified, it
uses the Ant and the system properties.

import java.io.*;
import java.util.Properties;
import java.util.Enumeration;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.DOMElementWriter;

/**
 * Creates an XML file with all Ant and system properties.
 * @author Ingmar Stein (stein@xtramind.com)
 * @version $Id: XMLProperties.java,v 1.1 2001/09/12 14:03:06 stein Exp $
 */
public class XMLProperties extends Task
{
 private File outFile = null;
 private File inFile = null;

 /** the properties element */
 private final static String PROPERTIES = "properties";

 /** the property element */
 private final static String PROPERTY = "property";

 /** name attribute for property, testcase and testsuite elements */
 private final static String ATTR_NAME = "name";

 /** value attribute for property elements */
 private final static String ATTR_VALUE = "value";

 private static DocumentBuilder getDocumentBuilder()
 {
  try {
   return DocumentBuilderFactory.newInstance().newDocumentBuilder();
  } catch(Exception e) {
   throw new ExceptionInInitializerError(e);
  }
 }

 public void setOut( File file )
 {
  outFile = file;
 }

 public void setIn( File file )
 {
  inFile = file;
 }

 /**
  * Runs the task.
  * @throws BuildException if someting goes wrong
  */
 public void execute() throws BuildException
 {
  Properties props = new Properties();

  if( outFile == null ) {
   throw new BuildException( "the out attribute must be set." );
  }

  if( inFile == null ) {
   /* add ant system properties */
   props.putAll( project.getProperties() );

   /* add/overlay system properties */
   props.putAll( System.getProperties() );
  } else {
   try {
    FileInputStream in = new FileInputStream( inFile );
    props.load( in );
    in.close();
   } catch( FileNotFoundException fnfe ) {
    log( "Could not find file " + inFile.getAbsolutePath(),
Project.MSG_WARN );
    return;
   } catch( IOException ioe ) {
    log( "Could not read file " + inFile.getAbsolutePath(),
Project.MSG_WARN );
    return;
   }
  }

  // create XML document
  Document doc = getDocumentBuilder().newDocument();
  Element rootElement = doc.createElement( PROPERTIES );

  // output properties
  String name;
  Enumeration e = props.propertyNames();
  while( e.hasMoreElements() ) {
   name = (String)e.nextElement();
   Element propElement = doc.createElement( PROPERTY );
   propElement.setAttribute( ATTR_NAME, name );
   propElement.setAttribute( ATTR_VALUE, props.getProperty(name) );
   rootElement.appendChild( propElement );
  }

  Writer wri = null;
  try {
   OutputStream out = new FileOutputStream( outFile );
   wri = new OutputStreamWriter( out, "UTF8" );
   wri.write( "<?xml version=\"1.0\"?>\n" );
   (new DOMElementWriter()).write( rootElement, wri, 0, "\t" );
   wri.flush();
  } catch( IOException ioe ) {
   throw new BuildException( "Unable to write XML file", ioe );
  } finally {
   if( wri != null ) {
    try {
     wri.close();
    } catch (IOException ioe) {}
   }
  }
 }
}

----- Original Message -----
From: "Erik Hatcher" <jakarta-ant@ehatchersolutions.com>
To: <ant-dev@jakarta.apache.org>
Sent: Wednesday, September 12, 2001 7:49 AM
Subject: Re: JUnit writes properties for each testcase


> Here's another possibility as a workaround:
>
> - Write out your relevant properties using <propertyfile> (I'd be willing
to
> contribute a patch that would provide a single element or switch that
would
> automatically dump all of Ant's properties with that task - seems like
that
> could be a handy capability to have available anyway).
> - Customize the XSL that <junitreport> uses and add a hyperlink to the
text
> file containing the properties so you could easily access it when viewing
> the reports.
>
> Another idea:
>
> - Implement a task that creates an XML file with all the properties listed
> - Use the <style> task to convert that to an HTML file
> - Hyperlink in the HTML file to a customized version of the <junitreport>
> XSL.
>
> Both of these are of course hacks but might suffice for your needs.
>
> Is there a way to have an XSL transformation read in two XML files?
> Perhaps merging in the properties wouldn't be that difficult if a custom
> task could generate the properties in XML format.
>
> All of these ideas also, of course, rely on a modification to the <junit>
> task to be able to turn off the properties output if desired.   Other than
> these workarounds I think it will involve serious refactoring of how the
> JUnit task works in order to accomplish what you are asking for.
>
>     Erik



Mime
View raw message