| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import junit.framework.Test; |
| 21 |
import junit.framework.TestCase; |
| 22 |
import junit.framework.TestSuite; |
| 23 |
import junit.runner.BaseTestRunner; |
| 24 |
|
| 25 |
import java.lang.reflect.InvocationTargetException; |
| 26 |
import java.lang.reflect.Method; |
| 27 |
import java.lang.reflect.Modifier; |
| 28 |
import java.util.Enumeration; |
| 29 |
import java.util.HashSet; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Set; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
@Deprecated |
| 38 |
public class TestCaseUtil { |
| 39 |
|
| 40 |
private TestCaseUtil() { |
| 41 |
} |
| 42 |
|
| 43 |
public static List<? extends Test> getTests(Test test, boolean flatten) { |
| 44 |
return getTests(test, flatten, new HashSet<Class<?>>()); |
| 45 |
} |
| 46 |
|
| 47 |
private static List<? extends Test> getTests(Test test, boolean flatten, |
| 48 |
Set<Class<?>> seen) { |
| 49 |
List<Test> testCases = new ArrayList<>(); |
| 50 |
if (test != null) { |
| 51 |
|
| 52 |
Test workingTest = null; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
if (test instanceof TestCase && |
| 59 |
((TestCase)test).getName() == null) { |
| 60 |
workingTest = invokeSuiteMethodIfPossible(test.getClass(), |
| 61 |
seen); |
| 62 |
} |
| 63 |
if (workingTest == null) { |
| 64 |
workingTest = test; |
| 65 |
} |
| 66 |
|
| 67 |
if (workingTest instanceof TestSuite) { |
| 68 |
TestSuite testSuite = (TestSuite) workingTest; |
| 69 |
Enumeration enumeration = testSuite.tests(); |
| 70 |
while (enumeration.hasMoreElements()) { |
| 71 |
Test childTest = (Test) enumeration.nextElement(); |
| 72 |
if (flatten) { |
| 73 |
testCases.addAll(getTests(childTest, flatten, seen)); |
| 74 |
} else { |
| 75 |
testCases.add(childTest); |
| 76 |
} |
| 77 |
} |
| 78 |
} else { |
| 79 |
testCases.add(workingTest); |
| 80 |
} |
| 81 |
} |
| 82 |
return testCases; |
| 83 |
} |
| 84 |
|
| 85 |
static Test invokeSuiteMethodIfPossible(Class testClass, |
| 86 |
Set<Class<?>> seen) { |
| 87 |
try { |
| 88 |
Method suiteMethod = testClass.getMethod( |
| 89 |
BaseTestRunner.SUITE_METHODNAME, new Class[0]); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
if (Modifier.isStatic(suiteMethod.getModifiers()) |
| 97 |
&& !seen.contains(testClass)) { |
| 98 |
seen.add(testClass); |
| 99 |
try { |
| 100 |
return (Test) suiteMethod.invoke(null, (Object[]) null); |
| 101 |
} catch (InvocationTargetException e) { |
| 102 |
|
| 103 |
} catch (IllegalAccessException e) { |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
} catch (NoSuchMethodException e) { |
| 108 |
|
| 109 |
} |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
static String getTestName(Test test) { |
| 114 |
if (test instanceof TestCase) { |
| 115 |
TestCase testCase = (TestCase) test; |
| 116 |
return testCase.getName(); |
| 117 |
} else if (test instanceof TestSuite) { |
| 118 |
TestSuite testSuite = (TestSuite) test; |
| 119 |
String name = testSuite.getName(); |
| 120 |
if (name != null) { |
| 121 |
int index = name.lastIndexOf("."); |
| 122 |
if (index > -1) { |
| 123 |
return name.substring(index + 1); |
| 124 |
} else { |
| 125 |
return name; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
return ""; |
| 130 |
} |
| 131 |
} |