Author: julien(a)jboss.com
Date: 2007-10-22 09:22:14 -0400 (Mon, 22 Oct 2007)
New Revision: 8734
Modified:
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/AbstractJUnitReport.java
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitTestsuiteReport.java
Log:
implemented aggregation of same testsuite in junit reporter
Modified:
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/AbstractJUnitReport.java
===================================================================
---
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/AbstractJUnitReport.java 2007-10-22
13:11:05 UTC (rev 8733)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/AbstractJUnitReport.java 2007-10-22
13:22:14 UTC (rev 8734)
@@ -207,7 +207,7 @@
int errors;
/** The failed count */
- int failed;
+ int failures;
/** The started Tests */
Map<TestId, TestInfo> startedTests = new HashMap<TestId, TestInfo>();
Modified:
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitTestsuiteReport.java
===================================================================
---
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitTestsuiteReport.java 2007-10-22
13:11:05 UTC (rev 8733)
+++
modules/test/trunk/unit/src/main/org/jboss/unit/report/impl/junit/JUnitTestsuiteReport.java 2007-10-22
13:22:14 UTC (rev 8734)
@@ -79,20 +79,14 @@
/** The document */
private Document testsuitesDocument;
- /** The root element */
- private Element testsuitesElement;
-
/** . */
- private Map<TestSuite, List<Element>> testSuiteListMap = new
HashMap<TestSuite, List<Element>>();
-
- /** . */
private File htmlReportDir;
/** . */
private File xmlReportDir;
/** . */
- private Map<String, Integer> fileNameCounters = new HashMap<String,
Integer>();
+ private Map<TestSuiteKey, TestSuiteReport> reportMap = new
HashMap<TestSuiteKey, TestSuiteReport>();
public JUnitTestsuiteReport(File xmlReportDir, File htmlReportDir)
{
@@ -103,11 +97,75 @@
protected void startRunner()
{
testsuitesDocument = createDocument();
- testsuitesElement = testsuitesDocument.createElement(XMLConstants.TESTSUITES);
}
protected void endRunner()
{
+ Element testsuitesElement =
testsuitesDocument.createElement(XMLConstants.TESTSUITES);
+ testsuitesDocument.appendChild(testsuitesElement);
+
+ //
+ for (Map.Entry<TestSuiteKey, TestSuiteReport> entry : reportMap.entrySet())
+ {
+ TestSuiteReport report = entry.getValue();
+
+ report.elements.add(testsuitesDocument.createElement(XMLConstants.PROPERTIES));
+ Element systemError =
testsuitesDocument.createElement(XMLConstants.SYSTEM_ERR);
+
systemError.appendChild(testsuitesDocument.createCDATASection(report.err.toString()));
+ report.elements.add(systemError);
+ Element systemOutElement =
testsuitesDocument.createElement(XMLConstants.SYSTEM_OUT);
+
systemOutElement.appendChild(testsuitesDocument.createCDATASection(report.out.toString()));
+ report.elements.add(systemOutElement);
+
+ //
+ Element testSuiteElt =
testsuitesDocument.createElement(XMLConstants.TESTSUITE);
+
+ //
+ testSuiteElt.setAttribute(XMLConstants.HOSTNAME, getHostName());
+ testSuiteElt.setAttribute(XMLConstants.ATTR_PACKAGE,
entry.getKey().packageName);
+ testSuiteElt.setAttribute(XMLConstants.ATTR_NAME, entry.getKey().className);
+ testSuiteElt.setAttribute(XMLConstants.ATTR_TESTS,
Integer.toString(report.tests));
+ testSuiteElt.setAttribute(XMLConstants.ATTR_ERRORS,
Integer.toString(report.errors));
+ testSuiteElt.setAttribute(XMLConstants.ATTR_FAILURES,
Integer.toString(report.failures));
+ testSuiteElt.setAttribute(XMLConstants.ATTR_TIME,
DURATION_FORMAT.format(report.durationMillis / 1000.0));
+
+ //
+ for (Element elt : report.elements)
+ {
+ testSuiteElt.appendChild(elt);
+ }
+
+ //
+ if (xmlReportDir != null)
+ {
+ Document tmp = createDocument();
+ Element copy = (Element)tmp.importNode(testSuiteElt, true);
+ tmp.appendChild(copy);
+ String a = copy.getAttribute("package");
+ String b = copy.getAttribute("name");
+ String c = a.length() == 0 ? b : a + "." + b;
+ copy.setAttribute("name", c);
+ copy.removeAttribute("package");
+ try
+ {
+ File f = new File(xmlReportDir, TEST_FILE_NAME_PREFIX + c +
".xml");
+ if (!f.exists())
+ {
+ f.createNewFile();
+ }
+ dump(tmp, f);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ //
+ testsuitesElement.appendChild(testSuiteElt);
+ }
+
+ //
if (htmlReportDir != null)
{
try
@@ -141,21 +199,29 @@
errorElt.appendChild(testsuitesDocument.createCDATASection(stackToString(failure.getStackTrace())));
//
- testSuiteListMap.get(testSuite).add(errorElt);
+ get(testSuite).elements.add(errorElt);
}
protected void startTestSuite(TestSuite testSuite)
{
- List<Element> elements = new ArrayList<Element>();
- testSuiteListMap.put(testSuite, elements);
}
+ private TestSuiteReport get(TestSuite testSuite)
+ {
+ TestSuiteKey key = new TestSuiteKey(testSuite);
+ TestSuiteReport report = reportMap.get(key);
+ if (report == null)
+ {
+ report = new TestSuiteReport();
+ reportMap.put(key, report);
+ }
+ return report;
+ }
+
protected void endTestSuite(TestSuite testSuite)
{
- StringBuffer fqn = testSuite.getFQN();
+ TestSuiteReport report = get(testSuite);
- List<Element> elements = testSuiteListMap.remove(testSuite);
-
//
if( testSuite.startedTests.size() > 0 )
{
@@ -163,94 +229,25 @@
{
Element errorNotEnded =
testsuitesDocument.createElement(XMLConstants.ERROR);
errorNotEnded.setAttribute(XMLConstants.ATTR_MESSAGE, "Testcase: "
+ id + " was not ended properly. (No EndTestEvent)");
- elements.add(errorNotEnded);
+ report.elements.add(errorNotEnded);
}
}
//
- int pos = fqn.lastIndexOf(".");
- String packageName = (pos == -1) ? "" : fqn.substring(0, pos);
- String className = (pos == -1) ? fqn.toString() : fqn.substring(pos + 1);
-
- //
- StringWriter out = new StringWriter();
- StringWriter err = new StringWriter();
try
{
- testSuite.view.writeTo(out, err);
+ testSuite.view.writeTo(report.out, report.err);
}
catch (IOException e)
{
e.printStackTrace();
}
- Element systemError = testsuitesDocument.createElement(XMLConstants.SYSTEM_ERR);
- systemError.appendChild(testsuitesDocument.createCDATASection(err.toString()));
- Element systemOutElement =
testsuitesDocument.createElement(XMLConstants.SYSTEM_OUT);
-
systemOutElement.appendChild(testsuitesDocument.createCDATASection(out.toString()));
-
//
- Element testSuiteElt = testsuitesDocument.createElement(XMLConstants.TESTSUITE);
-
- //
- testSuiteElt.setAttribute(XMLConstants.TIMESTAMP, createTimestamp());
- testSuiteElt.setAttribute(XMLConstants.HOSTNAME, getHostName());
- testSuiteElt.setAttribute(XMLConstants.ATTR_PACKAGE, packageName);
- testSuiteElt.setAttribute(XMLConstants.ATTR_NAME, className);
- testSuiteElt.setAttribute(XMLConstants.ATTR_TESTS,
Integer.toString(testSuite.tests));
- testSuiteElt.setAttribute(XMLConstants.ATTR_ERRORS,
Integer.toString(testSuite.errors));
- testSuiteElt.setAttribute(XMLConstants.ATTR_FAILURES,
Integer.toString(testSuite.failed));
- testSuiteElt.setAttribute(XMLConstants.ATTR_TIME,
DURATION_FORMAT.format(testSuite.durationMillis / 1000.0));
-
- //
- for (Element elt : elements)
- {
- testSuiteElt.appendChild(elt);
- }
-
- //
-
testSuiteElt.appendChild(testsuitesDocument.createElement(XMLConstants.PROPERTIES));
- testSuiteElt.appendChild(systemError);
- testSuiteElt.appendChild(systemOutElement);
-
- //
- if (xmlReportDir != null)
- {
- Document tmp = createDocument();
- Element copy = (Element)tmp.importNode(testSuiteElt, true);
- tmp.appendChild(copy);
- String a = copy.getAttribute("package");
- String b = copy.getAttribute("name");
- String c = a.length() == 0 ? b : a + "." + b;
- copy.setAttribute("name", c);
- copy.removeAttribute("package");
- try
- {
- Integer counter = fileNameCounters.get(c);
- if (counter == null)
- {
- counter = 0;
- }
- else
- {
- counter = counter + 1;
- }
- fileNameCounters.put(c, counter);
- File f = new File(xmlReportDir, TEST_FILE_NAME_PREFIX + c + (counter == 0 ?
"" : "_" + counter) + ".xml");
- if (!f.exists())
- {
- f.createNewFile();
- }
- dump(tmp, f);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
-
- //
- testsuitesElement.appendChild(testSuiteElt);
+ report.tests += testSuite.tests;
+ report.failures += testSuite.failures;
+ report.errors += testSuite.errors;
+ report.durationMillis += testSuite.durationMillis;
}
private void dump(Node node, File dest)
@@ -288,7 +285,7 @@
// Check failure type (failed, error)
if(FailureType.ASSERTION == failure.getType())
{
- testSuite.failed++;
+ testSuite.failures++;
failureType = XMLConstants.FAILURE;
}
else
@@ -301,7 +298,7 @@
}
//
- testSuiteListMap.get(testSuite).add(test);
+ get(testSuite).elements.add(test);
//
@@ -379,4 +376,82 @@
throw new Error(e);
}
}
+
+ private static class TestSuiteKey
+ {
+
+ /** . */
+ private final String packageName;
+
+ /** . */
+ private final String className;
+
+ private TestSuiteKey(TestSuite testSuite)
+ {
+ StringBuffer fqn = testSuite.getFQN();
+ int pos = fqn.lastIndexOf(".");
+ packageName = (pos == -1) ? "" : fqn.substring(0, pos);
+ className = (pos == -1) ? fqn.toString() : fqn.substring(pos + 1);
+ }
+
+ private TestSuiteKey(String packageName, String className)
+ {
+ if (packageName == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (className == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ this.packageName = packageName;
+ this.className = className;
+ }
+
+ public boolean equals(Object o)
+ {
+ if (o == this)
+ {
+ return true;
+ }
+ if (o instanceof TestSuiteKey)
+ {
+ TestSuiteKey that = (TestSuiteKey)o;
+ return that.packageName.equals(packageName) &&
that.className.equals(className);
+ }
+ return false;
+ }
+
+ public int hashCode()
+ {
+ return packageName.hashCode() * 41 + className.hashCode();
+ }
+ }
+
+ private static class TestSuiteReport
+ {
+
+ /** . */
+ private final List<Element> elements = new ArrayList<Element>();
+
+ /** . */
+ private final StringWriter out = new StringWriter();
+
+ /** . */
+ private final StringWriter err = new StringWriter();
+
+ /** . */
+ private int tests;
+
+ /** . */
+ private int errors;
+
+ /** . */
+ private int failures;
+
+ /** . */
+ private long durationMillis;
+
+ }
+
}
\ No newline at end of file