blob: 4109d9cc0394e441f838378ec66ebcb4afc271ab [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
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;
20import junit.framework.Test;
21import junit.framework.TestCase;
22import junit.framework.TestSuite;
23import junit.runner.BaseTestRunner;
24
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.lang.reflect.Modifier;
28import java.util.Enumeration;
29import java.util.List;
30
31/**
32 * @hide - This is part of a framework that is under development and should not be used for
33 * active development.
34 */
35public class TestCaseUtil {
36
37 private TestCaseUtil() {
38 }
39
40 @SuppressWarnings("unchecked")
41 public static List<String> getTestCaseNames(Test test, boolean flatten) {
42 List<Test> tests = (List<Test>) getTests(test, flatten);
43 List<String> testCaseNames = Lists.newArrayList();
44 for (Test aTest : tests) {
45 testCaseNames.add(getTestName(aTest));
46 }
47 return testCaseNames;
48 }
49
50 public static List<? extends Test> getTests(Test test, boolean flatten) {
51 List<Test> testCases = Lists.newArrayList();
52 if (test != null) {
53
54 Test workingTest = invokeSuiteMethodIfPossible(test.getClass());
55 if (workingTest == null) {
56 workingTest = test;
57 }
58
59 if (workingTest instanceof TestSuite) {
60 TestSuite testSuite = (TestSuite) workingTest;
61 Enumeration enumeration = testSuite.tests();
62 while (enumeration.hasMoreElements()) {
63 Test childTest = (Test) enumeration.nextElement();
64 if (flatten) {
65 testCases.addAll(getTests(childTest, flatten));
66 } else {
67 testCases.add(childTest);
68 }
69 }
70 } else {
71 testCases.add(workingTest);
72 }
73 }
74 return testCases;
75 }
76
77 private static Test invokeSuiteMethodIfPossible(Class testClass) {
78 try {
79 Method suiteMethod = testClass.getMethod(
80 BaseTestRunner.SUITE_METHODNAME, new Class[0]);
81 if (Modifier.isStatic(suiteMethod.getModifiers())) {
82 try {
83 return (Test) suiteMethod.invoke(null, (Object[]) null);
84 } catch (InvocationTargetException e) {
85 // do nothing
86 } catch (IllegalAccessException e) {
87 // do nothing
88 }
89 }
90 } catch (NoSuchMethodException e) {
91 // do nothing
92 }
93 return null;
94 }
95
96 public static String getTestName(Test test) {
97 if (test instanceof TestCase) {
98 TestCase testCase = (TestCase) test;
99 return testCase.getName();
100 } else if (test instanceof TestSuite) {
101 TestSuite testSuite = (TestSuite) test;
102 String name = testSuite.getName();
103 if (name != null) {
104 int index = name.lastIndexOf(".");
105 if (index > -1) {
106 return name.substring(index + 1);
107 } else {
108 return name;
109 }
110 }
111 }
112 return "";
113 }
114
115 public static Test getTestAtIndex(TestSuite testSuite, int position) {
116 int index = 0;
117 Enumeration enumeration = testSuite.tests();
118 while (enumeration.hasMoreElements()) {
119 Test test = (Test) enumeration.nextElement();
120 if (index == position) {
121 return test;
122 }
123 index++;
124 }
125 return null;
126 }
127
128 public static TestSuite createTestSuite(Class<? extends Test> testClass)
129 throws InstantiationException, IllegalAccessException {
130
131 Test test = invokeSuiteMethodIfPossible(testClass);
132 if (test == null) {
133 return new TestSuite(testClass);
134
135 } else if (TestCase.class.isAssignableFrom(test.getClass())) {
136 TestSuite testSuite = new TestSuite(test.getClass().getName());
137 testSuite.addTest(test);
138 return testSuite;
139 }
140
141 return (TestSuite) test;
142 }
143}