Author: bstansberry(a)jboss.com
Date: 2008-02-27 13:06:19 -0500 (Wed, 27 Feb 2008)
New Revision: 14371
Modified:
core/trunk/testing/src/main/java/org/hibernate/junit/UnitTestCase.java
Log:
Add utility to create a TestSuite where a subclass' "FailureExpected"
version of a base test is included in the suite, while the base test is excluded
Modified: core/trunk/testing/src/main/java/org/hibernate/junit/UnitTestCase.java
===================================================================
--- core/trunk/testing/src/main/java/org/hibernate/junit/UnitTestCase.java 2008-02-27
17:51:32 UTC (rev 14370)
+++ core/trunk/testing/src/main/java/org/hibernate/junit/UnitTestCase.java 2008-02-27
18:06:19 UTC (rev 14371)
@@ -23,11 +23,17 @@
*/
package org.hibernate.junit;
+import java.util.Enumeration;
+import java.util.HashSet;
import java.util.Iterator;
+import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import junit.framework.AssertionFailedError;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
/**
@@ -104,4 +110,43 @@
protected void reportSkip(String reason, String testDescription) {
SkipLog.LOG.warn( "*** skipping [" + fullTestName() + "] - " +
testDescription + " : " + reason, new Exception() );
}
+
+ // testsuite utitities ---------------------------------------------------
+
+ /**
+ * Supports easy creation of TestSuites where a subclass'
"FailureExpected"
+ * version of a base test is included in the suite, while the base test
+ * is excluded. E.g. test class FooTestCase includes method testBar(), while test
+ * class SubFooTestCase extends FooTestCase includes method testBarFailureExcluded().
+ * Passing SubFooTestCase.class to this method will return a suite that
+ * does not include testBar().
+ */
+ public static TestSuite createFailureExpectedSuite(Class testClass) {
+
+ TestSuite allTests = new TestSuite(testClass);
+ Set failureExpected = new HashSet();
+ Enumeration tests = allTests.tests();
+ while (tests.hasMoreElements()) {
+ Test t = (Test) tests.nextElement();
+ if (t instanceof TestCase) {
+ String name = ((TestCase) t).getName();
+ if (name.endsWith("FailureExpected"))
+ failureExpected.add(name);
+ }
+ }
+
+ TestSuite result = new TestSuite();
+ tests = allTests.tests();
+ while (tests.hasMoreElements()) {
+ Test t = (Test) tests.nextElement();
+ if (t instanceof TestCase) {
+ String name = ((TestCase) t).getName();
+ if (!failureExpected.contains(name + "FailureExpected")) {
+ result.addTest(t);
+ }
+ }
+ }
+
+ return result;
+ }
}