Author: emuckenhuber
Date: 2007-10-04 05:46:25 -0400 (Thu, 04 Oct 2007)
New Revision: 8528
Added:
modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTwo.java
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestReport.java
Modified:
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitReporter.java
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstants.java
Log:
- inital junit xml reporter import
Added: modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTwo.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTwo.java
(rev 0)
+++ modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTwo.java 2007-10-04
09:46:25 UTC (rev 8528)
@@ -0,0 +1,61 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.test.unit.blah;
+
+import java.io.InputStream;
+
+import org.jboss.test.unit.PrintListener;
+import org.jboss.unit.report.junit.JUnitReporter;
+import org.jboss.unit.runner.TestRunner;
+import org.jboss.unit.runner.TestRunnerEventListener;
+import org.jboss.unit.runner.model.ModelBuilder;
+import org.jboss.unit.runner.model.TestSuiteDef;
+import org.xml.sax.InputSource;
+
+/**
+ * @author <a href="mailto:emuckenh@redhat.com">Emanuel
Muckenhuber</a>
+ * @version $Revision$
+ */
+public class BlahTestTwo
+{
+
+ public static void main(String[] args) throws Exception
+ {
+
+ InputStream in = BlahTests.class.getResourceAsStream("f1.xml");
+ InputSource source = new InputSource(in);
+ ModelBuilder builder = new ModelBuilder(source);
+ TestSuiteDef suite = builder.build();
+
+ TestRunner runner = suite.createRunner();
+
+ TestRunnerEventListener junitReporter = new JUnitReporter("output",
BlahTestTwo.class.getName());
+ // add listeners
+ runner.addListener(junitReporter);
+ runner.addListener(new PrintListener());
+
+ // run the test
+ runner.run();
+
+ }
+}
Modified: modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitReporter.java
===================================================================
---
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitReporter.java 2007-10-04
00:13:14 UTC (rev 8527)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitReporter.java 2007-10-04
09:46:25 UTC (rev 8528)
@@ -22,20 +22,142 @@
******************************************************************************/
package org.jboss.unit.report.junit;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
import org.jboss.unit.runner.TestRunnerEventListener;
import org.jboss.unit.runner.TestRunnerEvent;
+import org.jboss.unit.runner.event.EndRunnerEvent;
+import org.jboss.unit.runner.event.EndTestEvent;
+import org.jboss.unit.runner.event.RunnerFailureEvent;
+import org.jboss.unit.runner.event.StartRunnerEvent;
+import org.jboss.unit.runner.event.StartTestEvent;
/**
- * @todo
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @author <a href="mailto:emuckenh@redhat.com">Emanuel
Muckenhuber</a>
* @version $Revision: 1.1 $
*/
public class JUnitReporter implements TestRunnerEventListener
{
-
+ /** The JUnit test report */
+ private JUnitTestReport report;
+
+ /** The system error printStream */
+ private ByteArrayOutputStream systemError;
+
+ /** The system out printStream */
+ private ByteArrayOutputStream systemOut;
+
+ /** To dir */
+ private static String toDir;
+
+ public JUnitReporter(String reportToDir,
+ String testSuiteName)
+ {
+ this.report = new JUnitTestReport(testSuiteName);
+ toDir = reportToDir;
+ // redirecting system out and err
+ this.systemError = new ByteArrayOutputStream();
+ this.systemOut = new ByteArrayOutputStream();
+ this.setStreams(systemError, systemOut);
+ }
+
public void onEvent(TestRunnerEvent event)
{
+ if (event instanceof StartRunnerEvent)
+ {
+ report.startTestSuite((StartRunnerEvent) event);
+ }
+ else if (event instanceof EndRunnerEvent)
+ {
+ // append system out and system err
+ report.appendSystemError(systemError.toString());
+ report.appendSystemOutput(systemOut.toString());
+
+ // Testsuite End
+ report.endTestSuite((EndRunnerEvent) event);
+
+ // Export xml
+ report.exportXML(toDir);
+ }
+ else if (event instanceof RunnerFailureEvent)
+ {
+ report.runnerFailed((RunnerFailureEvent) event);
+ }
+ else if (event instanceof StartTestEvent)
+ {
+ report.startTest((StartTestEvent) event);
+ }
+ else if (event instanceof EndTestEvent)
+ {
+ report.endTest((EndTestEvent) event);
+ }
+ }
+
+ private void setStreams(ByteArrayOutputStream a, ByteArrayOutputStream b)
+ {
+ PrintStream savedError = System.err;
+ PrintStream savedOut = System.out;
+
+ System.setErr(new PrintStream(new CaptureOutputStream(savedError, a)));
+ System.setOut(new PrintStream(new CaptureOutputStream(savedOut, b)));
+
+ }
+
+ private class CaptureOutputStream extends OutputStream
+ {
+ private OutputStream s1;
+ private OutputStream s2;
+
+ public CaptureOutputStream(OutputStream s1, OutputStream s2)
+ {
+ this.s1 = s1;
+ this.s2 = s2;
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ try
+ {
+ s1.close();
+ }
+ finally
+ {
+ s2.close();
+ }
+ }
+ @Override
+ public void flush() throws IOException
+ {
+ s1.flush();
+ s2.flush();
+ }
+
+ @Override
+ public void write(int b) throws IOException
+ {
+ s1.write(b);
+ s2.write(b);
+ }
+
+ @Override
+ public void write(byte[] b) throws IOException
+ {
+ s1.write(b);
+ s2.write(b);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ s1.write(b, off, len);
+ s2.write(b, off, len);
+ }
}
}
Added: modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestReport.java
===================================================================
--- modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestReport.java
(rev 0)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestReport.java 2007-10-04
09:46:25 UTC (rev 8528)
@@ -0,0 +1,292 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.unit.report.junit;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.jboss.unit.Failure;
+import org.jboss.unit.FailureType;
+import org.jboss.unit.TestId;
+import org.jboss.unit.runner.TestResult;
+import org.jboss.unit.runner.event.EndRunnerEvent;
+import org.jboss.unit.runner.event.EndTestEvent;
+import org.jboss.unit.runner.event.RunnerFailureEvent;
+import org.jboss.unit.runner.event.StartRunnerEvent;
+import org.jboss.unit.runner.event.StartTestEvent;
+import org.jboss.unit.runner.results.TestFailure;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * @author <a href="mailto:emuckenh@redhat.com">Emanuel
Muckenhuber</a>
+ * @version $Revision$
+ */
+public class JUnitTestReport
+{
+
+ /** The test suite name */
+ private String testSuiteName;
+
+ /** The line separator */
+ private final static String ls = System.getProperty("line.separator");
+
+ /** The document */
+ private Document document;
+
+ /** The root element */
+ private Element rootElement;
+
+ /** The system error */
+ private StringBuffer systemErr = new StringBuffer();
+
+ /** The system output */
+ private StringBuffer systemOut = new StringBuffer();
+
+ /** The test count */
+ private int tests;
+
+ /** The error count */
+ private int errors;
+
+ /** The failed count */
+ private int failed;
+
+ public JUnitTestReport(String testSuiteName)
+ {
+ this.testSuiteName = testSuiteName;
+ }
+
+
+ public void startTestSuite(StartRunnerEvent event)
+ {
+ this.document = getDocumentBuilder().newDocument();
+ this.rootElement = this.document.createElement(XMLConstants.TESTSUITE);
+ this.rootElement.setAttribute(XMLConstants.TIMESTAMP, getTimestamp());
+ this.rootElement.setAttribute(XMLConstants.HOSTNAME, getHostName());
+ this.rootElement.setAttribute(XMLConstants.ATTR_NAME, this.testSuiteName);
+ Element properties = document.createElement(XMLConstants.PROPERTIES);
+ this.rootElement.appendChild(properties);
+ // add properties list
+
+ }
+
+ public void startTest(StartTestEvent event)
+ {
+ this.tests++;
+ }
+
+ public void runnerFailed(RunnerFailureEvent event)
+ {
+ this.tests++;
+ this.errors++;
+ Element e = document.createElement(XMLConstants.ERROR);
+ e.appendChild(document.createCDATASection(ls + event.getFailure().getMessage() + ls
+ stackToString(event.getFailure().getCause())));
+ rootElement.appendChild(e);
+ }
+
+ public void endTest(EndTestEvent event)
+ {
+ TestId testId = event.getTestId();
+ TestResult result = event.getTestResult();
+ Element test = document.createElement(XMLConstants.TESTCASE);
+ // The test name
+ StringBuffer nb = new StringBuffer();
+ for(int i = 0; i < testId.getLength(); i ++)
+ {
+ if (i > 0)
+ {
+ nb.append(".");
+ }
+ nb.append(testId.getName(i));
+ }
+ String testName = nb.toString();
+ // Test parameters
+ StringBuffer parameters = new StringBuffer();
+ for(Iterator<String> i = result.getParametrization().values().iterator();
i.hasNext(); )
+ {
+ String name = i.next();
+ String value = result.getParametrization().get(name);
+ parameters.append(name + "=" + value + "?");
+ }
+
+ if ( parameters.length() > 0)
+ testName = testName + "?" + parameters.toString();
+
+ test.setAttribute(XMLConstants.ATTR_NAME, testName);
+ // Testcase time
+ test.setAttribute(XMLConstants.ATTR_TIME, new
Long(result.getDurationMillis()).toString());
+ // If test failed
+ if (result instanceof TestFailure)
+ {
+ TestFailure testFailure = (TestFailure) result;
+ Failure failure = testFailure.getFailure();
+
+ String failureType;
+ // Check failure type (failed, error)
+ if(FailureType.ASSERTION == failure.getType())
+ {
+ this.failed++;
+ failureType = XMLConstants.FAILURE;
+ }
+ else
+ {
+ this.errors++;
+ failureType = XMLConstants.ERROR;
+ }
+
+ test.appendChild(addError(failureType, failure.getCause()));
+ }
+ this.rootElement.appendChild(test);
+ }
+
+ public void endTestSuite(EndRunnerEvent event)
+ {
+ // Testsuite counts (tests, errors, failures)
+ this.rootElement.setAttribute(XMLConstants.ATTR_TESTS,
Integer.toString(this.tests));
+ this.rootElement.setAttribute(XMLConstants.ATTR_ERRORS,
Integer.toString(this.errors));
+ this.rootElement.setAttribute(XMLConstants.ATTR_FAILURES,
Integer.toString(this.failed));
+
+ // Testsuite system-err
+ Element systemError = document.createElement(XMLConstants.SYSTEM_ERR);
+ systemError.appendChild(document.createCDATASection(systemErr.toString()));
+ // Testsuite system-out
+ Element systemOutElement = document.createElement(XMLConstants.SYSTEM_OUT);
+ systemOutElement.appendChild(document.createCDATASection(systemOut.toString()));
+
+ this.rootElement.appendChild(systemError);
+ this.rootElement.appendChild(systemOutElement);
+
+ // System.out.println(exportXML());
+ }
+
+ public void appendSystemOutput(String output)
+ {
+ this.systemOut.append(output);
+ }
+
+ public void appendSystemError(String error)
+ {
+ this.systemErr.append(error);
+ }
+
+ public void exportXML(String toDir)
+ {
+ try
+ {
+ File file = new File(toDir + "/" + "TEST-" + testSuiteName +
".xml");
+
+ Source source = new DOMSource(this.rootElement);
+ Transformer xtrans = TransformerFactory.newInstance().newTransformer();
+ xtrans.setOutputProperty(OutputKeys.INDENT, "yes");
+ Result result = new StreamResult(new FileOutputStream(file));
+ xtrans.transform(source, result);
+ }
+ catch(FileNotFoundException e)
+ {
+ e.printStackTrace();
+ }
+ catch(TransformerException e)
+ {
+ e.printStackTrace();
+ }
+ catch(IllegalArgumentException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ private Element addError(String type, Throwable t)
+ {
+ Element error = document.createElement(type);
+ // Testcase error message
+ error.setAttribute(XMLConstants.ATTR_MESSAGE, t.getMessage());
+ // Testcase error type
+ error.setAttribute(XMLConstants.ATTR_TYPE, t.getClass().getName());
+ // Testcase stacktrace
+ error.appendChild(document.createCDATASection(stackToString(t)));
+ return error;
+ }
+
+ private static String stackToString(Throwable t)
+ {
+ final Writer writer = new StringWriter();
+ final PrintWriter printWriter = new PrintWriter(writer);
+ t.printStackTrace(printWriter);
+ printWriter.flush();
+ printWriter.close();
+ return writer.toString();
+ }
+
+ private static String getTimestamp()
+ {
+ SimpleDateFormat date = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+ return date.format(new Date());
+ }
+
+ private static String getHostName()
+ {
+ try
+ {
+ return InetAddress.getLocalHost().getHostName();
+ }
+ catch(UnknownHostException e)
+ {
+ return "localhost";
+ }
+ }
+
+ private static DocumentBuilder getDocumentBuilder()
+ {
+ try
+ {
+ return DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ }
+ catch(ParserConfigurationException e)
+ {
+ e.printStackTrace();
+ return null;
+ }
+ }
+}
\ No newline at end of file
Modified: modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstants.java
===================================================================
---
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstants.java 2007-10-04
00:13:14 UTC (rev 8527)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstants.java 2007-10-04
09:46:25 UTC (rev 8528)
@@ -92,6 +92,13 @@
/** id attribute */
public static final String ATTR_ID = "id";
+
+ /** timestamp of test cases */
+ public static final String TIMESTAMP = "timestamp";
+
+ /** name of host running the tests */
+ public static final String HOSTNAME = "hostname";
+
}
/*