From portal-commits at lists.jboss.org Thu Oct 4 05:46:25 2007 Content-Type: multipart/mixed; boundary="===============1285572732733048576==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r8528 - in modules/test/trunk/unit/src/main/org/jboss: unit/report/junit and 1 other directory. Date: Thu, 04 Oct 2007 05:46:25 -0400 Message-ID: --===============1285572732733048576== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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.ja= va modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestRe= port.java Modified: modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitReport= er.java modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstant= s.java Log: - inital junit xml reporter import Added: modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTw= o.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTwo.j= ava (rev 0) +++ modules/test/trunk/unit/src/main/org/jboss/test/unit/blah/BlahTestTwo.j= ava 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 Emanuel Muckenhuber + * @version $Revision$ + */ +public class BlahTestTwo +{ + + public static void main(String[] args) throws Exception + { + + InputStream in =3D BlahTests.class.getResourceAsStream("f1.xml"); + InputSource source =3D new InputSource(in); + ModelBuilder builder =3D new ModelBuilder(source); + TestSuiteDef suite =3D builder.build(); + + TestRunner runner =3D suite.createRunner(); + + TestRunnerEventListener junitReporter =3D 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/JUni= tReporter.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitRepor= ter.java 2007-10-04 00:13:14 UTC (rev 8527) +++ modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitRepor= ter.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 Julien Viet + * @author Emanuel Muckenhuber * @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 =3D new JUnitTestReport(testSuiteName); + toDir =3D reportToDir; + // redirecting system out and err + this.systemError =3D new ByteArrayOutputStream(); + this.systemOut =3D 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 =3D System.err; + PrintStream savedOut =3D 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 =3D s1; + this.s2 =3D 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/JUnitTe= stReport.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestR= eport.java (rev 0) +++ modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/JUnitTestR= eport.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 Emanuel Muckenhuber + * @version $Revision$ + */ +public class JUnitTestReport +{ + + /** The test suite name */ + private String testSuiteName; + = + /** The line separator */ + private final static String ls =3D System.getProperty("line.separator"); + + /** The document */ + private Document document; + = + /** The root element */ + private Element rootElement; + = + /** The system error */ + private StringBuffer systemErr =3D new StringBuffer(); + = + /** The system output */ + private StringBuffer systemOut =3D 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 =3D testSuiteName; + } + = + = + public void startTestSuite(StartRunnerEvent event) + { + this.document =3D getDocumentBuilder().newDocument(); + this.rootElement =3D this.document.createElement(XMLConstants.TESTSU= ITE); + this.rootElement.setAttribute(XMLConstants.TIMESTAMP, getTimestamp()= ); + this.rootElement.setAttribute(XMLConstants.HOSTNAME, getHostName()); + this.rootElement.setAttribute(XMLConstants.ATTR_NAME, this.testSuite= Name); + Element properties =3D document.createElement(XMLConstants.PROPERTIE= S); + 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 =3D document.createElement(XMLConstants.ERROR); + e.appendChild(document.createCDATASection(ls + event.getFailure().ge= tMessage() + ls + stackToString(event.getFailure().getCause()))); + rootElement.appendChild(e); + } + = + public void endTest(EndTestEvent event) + { + TestId testId =3D event.getTestId(); + TestResult result =3D event.getTestResult(); + Element test =3D document.createElement(XMLConstants.TESTCASE); + // The test name + StringBuffer nb =3D new StringBuffer(); + for(int i =3D 0; i < testId.getLength(); i ++) + { + if (i > 0) + { + nb.append("."); + } + nb.append(testId.getName(i)); + } + String testName =3D nb.toString(); + // Test parameters + StringBuffer parameters =3D new StringBuffer(); + for(Iterator i =3D result.getParametrization().values().iter= ator(); i.hasNext(); ) + { + String name =3D i.next(); + String value =3D result.getParametrization().get(name); + parameters.append(name + "=3D" + value + "?"); + } + + if ( parameters.length() > 0) + testName =3D testName + "?" + parameters.toString(); + = + test.setAttribute(XMLConstants.ATTR_NAME, testName); + // Testcase time + test.setAttribute(XMLConstants.ATTR_TIME, new Long(result.getDuratio= nMillis()).toString()); + // If test failed + if (result instanceof TestFailure) + { + TestFailure testFailure =3D (TestFailure) result; + Failure failure =3D testFailure.getFailure(); + = + String failureType; + // Check failure type (failed, error) + if(FailureType.ASSERTION =3D=3D failure.getType()) + { + this.failed++; + failureType =3D XMLConstants.FAILURE; + } + else = + { + this.errors++; + failureType =3D 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.toStr= ing(this.tests)); + this.rootElement.setAttribute(XMLConstants.ATTR_ERRORS, Integer.toSt= ring(this.errors)); + this.rootElement.setAttribute(XMLConstants.ATTR_FAILURES, Integer.to= String(this.failed)); + = + // Testsuite system-err + Element systemError =3D document.createElement(XMLConstants.SYSTEM_E= RR); + systemError.appendChild(document.createCDATASection(systemErr.toStri= ng())); + // Testsuite system-out + Element systemOutElement =3D document.createElement(XMLConstants.SYS= TEM_OUT); + systemOutElement.appendChild(document.createCDATASection(systemOut.t= oString())); + = + 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 =3D new File(toDir + "/" + "TEST-" + testSuiteName + ".= xml"); + = + Source source =3D new DOMSource(this.rootElement); + Transformer xtrans =3D TransformerFactory.newInstance().newTransf= ormer(); + xtrans.setOutputProperty(OutputKeys.INDENT, "yes"); + Result result =3D 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 =3D 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 =3D new StringWriter(); + final PrintWriter printWriter =3D new PrintWriter(writer); + t.printStackTrace(printWriter); + printWriter.flush(); + printWriter.close(); + return writer.toString(); + } + = + private static String getTimestamp() + { + SimpleDateFormat date =3D new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:s= s"); + 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/XMLC= onstants.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstan= ts.java 2007-10-04 00:13:14 UTC (rev 8527) +++ modules/test/trunk/unit/src/main/org/jboss/unit/report/junit/XMLConstan= ts.java 2007-10-04 09:46:25 UTC (rev 8528) @@ -92,6 +92,13 @@ = /** id attribute */ public static final String ATTR_ID =3D "id"; + = + /** timestamp of test cases */ + public static final String TIMESTAMP =3D "timestamp"; + + /** name of host running the tests */ + public static final String HOSTNAME =3D "hostname"; + } = /* --===============1285572732733048576==--