1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.test;
18
19import com.google.android.collect.Lists;
20
21import junit.framework.Test;
22import junit.framework.TestCase;
23import junit.framework.TestSuite;
24import junit.runner.BaseTestRunner;
25
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
28import java.lang.reflect.Modifier;
29import java.util.Enumeration;
30import java.util.HashSet;
31import java.util.List;
32import java.util.Set;
33
34/**
35 * @hide - This is part of a framework that is under development and should not be used for
36 * active development.
37 */
38@Deprecated
39public class TestCaseUtil {
40
41    private TestCaseUtil() {
42    }
43
44    @SuppressWarnings("unchecked")
45    public static List<String> getTestCaseNames(Test test, boolean flatten) {
46        List<Test> tests = (List<Test>) getTests(test, flatten);
47        List<String> testCaseNames = Lists.newArrayList();
48        for (Test aTest : tests) {
49            testCaseNames.add(getTestName(aTest));
50        }
51        return testCaseNames;
52    }
53
54    public static List<? extends Test> getTests(Test test, boolean flatten) {
55        return getTests(test, flatten, new HashSet<Class<?>>());
56    }
57
58    private static List<? extends Test> getTests(Test test, boolean flatten,
59            Set<Class<?>> seen) {
60        List<Test> testCases = Lists.newArrayList();
61        if (test != null) {
62
63            Test workingTest = null;
64            /*
65             * If we want to run a single TestCase method only, we must not
66             * invoke the suite() method, because we will run all test methods
67             * of the class then.
68             */
69            if (test instanceof TestCase &&
70                    ((TestCase)test).getName() == null) {
71                workingTest = invokeSuiteMethodIfPossible(test.getClass(),
72                        seen);
73            }
74            if (workingTest == null) {
75                workingTest = test;
76            }
77
78            if (workingTest instanceof TestSuite) {
79                TestSuite testSuite = (TestSuite) workingTest;
80                Enumeration enumeration = testSuite.tests();
81                while (enumeration.hasMoreElements()) {
82                    Test childTest = (Test) enumeration.nextElement();
83                    if (flatten) {
84                        testCases.addAll(getTests(childTest, flatten, seen));
85                    } else {
86                        testCases.add(childTest);
87                    }
88                }
89            } else {
90                testCases.add(workingTest);
91            }
92        }
93        return testCases;
94    }
95
96    private static Test invokeSuiteMethodIfPossible(Class testClass,
97            Set<Class<?>> seen) {
98        try {
99            Method suiteMethod = testClass.getMethod(
100                    BaseTestRunner.SUITE_METHODNAME, new Class[0]);
101            /*
102             * Additional check necessary: If a TestCase contains a suite()
103             * method that returns a TestSuite including the TestCase itself,
104             * we need to stop the recursion. We use a set of classes to
105             * remember which classes' suite() methods were already invoked.
106             */
107            if (Modifier.isStatic(suiteMethod.getModifiers())
108                    && !seen.contains(testClass)) {
109                seen.add(testClass);
110                try {
111                    return (Test) suiteMethod.invoke(null, (Object[]) null);
112                } catch (InvocationTargetException e) {
113                    // do nothing
114                } catch (IllegalAccessException e) {
115                    // do nothing
116                }
117            }
118        } catch (NoSuchMethodException e) {
119            // do nothing
120        }
121        return null;
122    }
123
124    public static String getTestName(Test test) {
125        if (test instanceof TestCase) {
126            TestCase testCase = (TestCase) test;
127            return testCase.getName();
128        } else if (test instanceof TestSuite) {
129            TestSuite testSuite = (TestSuite) test;
130            String name = testSuite.getName();
131            if (name != null) {
132                int index = name.lastIndexOf(".");
133                if (index > -1) {
134                    return name.substring(index + 1);
135                } else {
136                    return name;
137                }
138            }
139        }
140        return "";
141    }
142
143    public static Test getTestAtIndex(TestSuite testSuite, int position) {
144        int index = 0;
145        Enumeration enumeration = testSuite.tests();
146        while (enumeration.hasMoreElements()) {
147            Test test = (Test) enumeration.nextElement();
148            if (index == position) {
149                return test;
150            }
151            index++;
152        }
153        return null;
154    }
155
156    public static TestSuite createTestSuite(Class<? extends Test> testClass)
157            throws InstantiationException, IllegalAccessException {
158
159        Test test = invokeSuiteMethodIfPossible(testClass,
160                new HashSet<Class<?>>());
161        if (test == null) {
162            return new TestSuite(testClass);
163
164        } else if (TestCase.class.isAssignableFrom(test.getClass())) {
165            TestSuite testSuite = new TestSuite(test.getClass().getName());
166            testSuite.addTest(test);
167            return testSuite;
168        }
169
170        return (TestSuite) test;
171    }
172}
173