FileDocCategorySizeDatePackage
TestCaseUtil.javaAPI DocAndroid 1.5 API5916Wed May 06 22:42:02 BST 2009android.test

TestCaseUtil

public class TestCaseUtil extends Object
hide
- This is part of a framework that is under development and should not be used for active development.

Fields Summary
Constructors Summary
private TestCaseUtil()

    
Methods Summary
public static junit.framework.TestSuitecreateTestSuite(java.lang.Class testClass)


        Test test = invokeSuiteMethodIfPossible(testClass, 
                new HashSet<Class<?>>());
        if (test == null) {
            return new TestSuite(testClass);

        } else if (TestCase.class.isAssignableFrom(test.getClass())) {
            TestSuite testSuite = new TestSuite(test.getClass().getName());
            testSuite.addTest(test);
            return testSuite;
        }

        return (TestSuite) test;
    
public static junit.framework.TestgetTestAtIndex(junit.framework.TestSuite testSuite, int position)

        int index = 0;
        Enumeration enumeration = testSuite.tests();
        while (enumeration.hasMoreElements()) {
            Test test = (Test) enumeration.nextElement();
            if (index == position) {
                return test;
            }
            index++;
        }
        return null;
    
public static java.util.ListgetTestCaseNames(junit.framework.Test test, boolean flatten)

        List<Test> tests = (List<Test>) getTests(test, flatten);
        List<String> testCaseNames = Lists.newArrayList();
        for (Test aTest : tests) {
            testCaseNames.add(getTestName(aTest));
        }
        return testCaseNames;
    
public static java.lang.StringgetTestName(junit.framework.Test test)

        if (test instanceof TestCase) {
            TestCase testCase = (TestCase) test;
            return testCase.getName();
        } else if (test instanceof TestSuite) {
            TestSuite testSuite = (TestSuite) test;
            String name = testSuite.getName();
            if (name != null) {
                int index = name.lastIndexOf(".");
                if (index > -1) {
                    return name.substring(index + 1);
                } else {
                    return name;
                }
            }
        }
        return "";
    
public static java.util.ListgetTests(junit.framework.Test test, boolean flatten)

        return getTests(test, flatten, new HashSet<Class<?>>());
    
private static java.util.ListgetTests(junit.framework.Test test, boolean flatten, java.util.Set seen)

        List<Test> testCases = Lists.newArrayList();
        if (test != null) {

            Test workingTest = null;
            /*
             * If we want to run a single TestCase method only, we must not
             * invoke the suite() method, because we will run all test methods
             * of the class then.
             */
            if (test instanceof TestCase &&
                    ((TestCase)test).getName() == null) {
                workingTest = invokeSuiteMethodIfPossible(test.getClass(), 
                        seen);
            }
            if (workingTest == null) {
                workingTest = test;
            }

            if (workingTest instanceof TestSuite) {
                TestSuite testSuite = (TestSuite) workingTest;
                Enumeration enumeration = testSuite.tests();
                while (enumeration.hasMoreElements()) {
                    Test childTest = (Test) enumeration.nextElement();
                    if (flatten) {
                        testCases.addAll(getTests(childTest, flatten, seen));
                    } else {
                        testCases.add(childTest);
                    }
                }
            } else {
                testCases.add(workingTest);
            }
        }
        return testCases;
    
private static junit.framework.TestinvokeSuiteMethodIfPossible(java.lang.Class testClass, java.util.Set seen)

        try {
            Method suiteMethod = testClass.getMethod(
                    BaseTestRunner.SUITE_METHODNAME, new Class[0]);
            /*
             * Additional check necessary: If a TestCase contains a suite()
             * method that returns a TestSuite including the TestCase itself,
             * we need to stop the recursion. We use a set of classes to
             * remember which classes' suite() methods were already invoked.
             */
            if (Modifier.isStatic(suiteMethod.getModifiers())
                    && !seen.contains(testClass)) {
                seen.add(testClass);
                try {
                    return (Test) suiteMethod.invoke(null, (Object[]) null);
                } catch (InvocationTargetException e) {
                    // do nothing
                } catch (IllegalAccessException e) {
                    // do nothing
                }
            }
        } catch (NoSuchMethodException e) {
            // do nothing
        }
        return null;