001: package com.reeltwo.jumble.fast;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.Modifier;
006:
007: import junit.framework.Test;
008: import junit.framework.TestSuite;
009:
010: /**
011: * A test suite containing other tests, not test suites. If test suites are
012: * added, they are flattened. This is useful for ordering tests in Jumble.
013: *
014: * @author Tin Pavlinic
015: * @version $Revision: 496 $
016: */
017: public class FlatTestSuite extends TestSuite {
018: /**
019: * Constructs a new FlatTestSuite. Just calls the parent constructor.
020: */
021: public FlatTestSuite() {
022: super ();
023: }
024:
025: /**
026: * Constructs a new FlatTestSuite. Checks for a <code>suite()</code> method.
027: * If it exists, it uses that, otherwise uses JUnit's reflection method.
028: *
029: * @param theClass
030: * the class to construct the test suite from.
031: */
032: public FlatTestSuite(final Class theClass) {
033: Method suiteMethod;
034: try {
035: suiteMethod = theClass.getMethod("suite", new Class[] {});
036: if ((suiteMethod.getModifiers() & Modifier.STATIC) == 0) {
037: // No nonstatic methods
038: suiteMethod = null;
039: } else if ((suiteMethod.getModifiers() & Modifier.PUBLIC) == 0) {
040: // No nonpublic methods
041: suiteMethod = null;
042: }
043: } catch (NoSuchMethodException e) {
044: suiteMethod = null;
045: }
046:
047: if (suiteMethod == null) {
048: addTest(new TestSuite(theClass));
049: } else {
050: try {
051: Test suite = (Test) suiteMethod.invoke(null,
052: new Object[] {});
053: addTest(suite);
054: } catch (InvocationTargetException e) {
055: // Should never happen - static method
056: throw new RuntimeException("Dumb programmer", e);
057: } catch (IllegalAccessException e) {
058: // Should never happen - public method
059: throw new RuntimeException("Dumb programmer", e);
060: }
061: }
062: }
063:
064: /**
065: * Constructs a new FlatTestSuite. Just calls the parent constructor.
066: *
067: * @param theClass
068: * the class to construct the test suite from.
069: * @param name
070: * the name of the test suite
071: */
072: public FlatTestSuite(final Class theClass, final String name) {
073: super (theClass, name);
074: }
075:
076: /**
077: * Constructs a new FlatTestSuite. Just calls the parent constructor.
078: *
079: * @param name
080: * the neame of the test suite
081: */
082: public FlatTestSuite(final String name) {
083: super (name);
084: }
085:
086: /**
087: * Since this class is used mainly for timing tests, the suite hierarchy
088: * becomes meaningless, so we want to break up the hierarchy and only get the
089: * leaf tests.
090: */
091: public void addTest(final Test t) {
092: if (t instanceof TestSuite) {
093: TestSuite suite = (TestSuite) t;
094:
095: for (int i = 0; i < suite.testCount(); i++) {
096: addTest(suite.testAt(i));
097: }
098: } else {
099: super .addTest(t);
100: }
101: }
102:
103: /**
104: * Adds the test suite specified by a class using the <code>suite()</code>
105: * method first.
106: */
107: public void addTestSuite(Class testClass) {
108: addTest(new FlatTestSuite(testClass));
109: }
110: }
|