From 1ae3bcd1fc311a262f1f5c1248a0ab5af087ed45 Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Thu, 25 Oct 2007 23:08:30 +0000 Subject: [PATCH] Junit changes: testAll() xml test case now tests all possible test cases even if one fails custom JUnit runner is used to send several failures to the notifier build.xml: common parameterized tasks are used to run tests and reports for "test" and "test14" if "outputTestResultsToFile" property is set, a text summary is also generated after testing (xsl transformation is using etc/xslt/junit-txt-output.xsl) git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5587 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/bin/build.xml | 108 +++++++++--------- pmd/etc/xslt/junit-txt-output.xsl | 22 ++++ .../basic/xml/UselessOverridingMethod.xml | 3 +- .../pmd/testframework/RuleTst.java | 4 +- .../testframework/SimpleAggregatorTst.java | 48 +++++++- 5 files changed, 125 insertions(+), 60 deletions(-) create mode 100644 pmd/etc/xslt/junit-txt-output.xsl diff --git a/pmd/bin/build.xml b/pmd/bin/build.xml index 016b460908..dd568a6ab3 100644 --- a/pmd/bin/build.xml +++ b/pmd/bin/build.xml @@ -162,63 +162,31 @@ - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + - + - + @@ -227,12 +195,46 @@ - - + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pmd/etc/xslt/junit-txt-output.xsl b/pmd/etc/xslt/junit-txt-output.xsl new file mode 100644 index 0000000000..84f7a0c9b5 --- /dev/null +++ b/pmd/etc/xslt/junit-txt-output.xsl @@ -0,0 +1,22 @@ + + + + +Junit test results + +Test suite failed (failures: , errors: ) + + Test case failed (failures: , errors: ) + + failure: . + + + error: . + + + +Summary + failures: , errors: . + + + diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/basic/xml/UselessOverridingMethod.xml b/pmd/regress/test/net/sourceforge/pmd/rules/basic/xml/UselessOverridingMethod.xml index f607a90732..9a0fa09b67 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/basic/xml/UselessOverridingMethod.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/basic/xml/UselessOverridingMethod.xml @@ -235,8 +235,7 @@ public Object clone() throws CloneNotSupportedException { 0 diff --git a/pmd/regress/test/net/sourceforge/pmd/testframework/RuleTst.java b/pmd/regress/test/net/sourceforge/pmd/testframework/RuleTst.java index c199150e27..203b60b42b 100644 --- a/pmd/regress/test/net/sourceforge/pmd/testframework/RuleTst.java +++ b/pmd/regress/test/net/sourceforge/pmd/testframework/RuleTst.java @@ -82,9 +82,9 @@ public abstract class RuleTst { res = processUsingStringReader(test.getCode(), rule, test.getSourceType()).size(); } catch (Throwable t) { t.printStackTrace(); - throw new RuntimeException("Test \"" + test.getDescription() + "\" on Rule \"" + test.getRule().getName() + "\"failed"); + throw new RuntimeException('"' + test.getDescription() + "\" failed"); } - assertEquals("Test \"" + test.getDescription() + "\" on Rule \"" + test.getRule().getName() + "\" resulted in wrong number of failures,", + assertEquals('"' + test.getDescription() + "\" resulted in wrong number of failures,", test.getNumberOfProblemsExpected(), res); } finally { //Restore old properties diff --git a/pmd/regress/test/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java b/pmd/regress/test/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java index f3b3fb9917..a08d683a26 100644 --- a/pmd/regress/test/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java +++ b/pmd/regress/test/net/sourceforge/pmd/testframework/SimpleAggregatorTst.java @@ -6,13 +6,19 @@ package test.net.sourceforge.pmd.testframework; import java.util.ArrayList; import java.util.List; -import org.junit.Test; - import net.sourceforge.pmd.Rule; +import org.junit.Test; +import org.junit.internal.runners.TestClassMethodsRunner; +import org.junit.runner.Description; +import org.junit.runner.RunWith; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunNotifier; + /** * Standard methods for (simple) testcases. */ +@RunWith(SimpleAggregatorTst.CustomXmlTestClassMethodsRunner.class) public abstract class SimpleAggregatorTst extends RuleTst { /** * Run a set of tests defined in an XML test-data file for a rule. The file @@ -56,9 +62,45 @@ public abstract class SimpleAggregatorTst extends RuleTst { */ @Test public void testAll() { + ArrayList l = new ArrayList(); for (Rule r : rules) { - runTests(r); + TestDescriptor[] tests = extractTestsFromXml(r); + for (int i = 0; i < tests.length; i++) { + try { + runTest(tests[i]); + } catch (Throwable t) { + Failure f = CustomXmlTestClassMethodsRunner.createFailure(r, t); + l.add(f); + } + } + } + for(Failure f: l) { + CustomXmlTestClassMethodsRunner.addFailure(f); } } + public static class CustomXmlTestClassMethodsRunner extends TestClassMethodsRunner { + public CustomXmlTestClassMethodsRunner(Class klass) { + super(klass); + } + + public static Failure createFailure(Rule rule, Throwable targetException) { + return new Failure(Description.createTestDescription( + SimpleAggregatorTst.class, "xml." + rule.getRuleSetName() + '.' + rule.getName()), + targetException); + } + + public static void addFailure(Failure failure) { + NOTIFIER.fireTestFailure(failure); + } + + @Override + public synchronized void run(RunNotifier n) { + // synchronized so that access to NOTIFIER is safe + NOTIFIER = n; + super.run(n); + } + + private static RunNotifier NOTIFIER; + } }