Merge "Clean up TestCaseUtil" am: 43851d01d8

am: e38d503394

Change-Id: I62a80b405d05ec7b4da050bd6b01df4c965d6187
This commit is contained in:
Paul Duffin
2017-06-26 09:33:23 +00:00
committed by android-build-merger
2 changed files with 29 additions and 55 deletions

View File

@@ -40,16 +40,6 @@ public class TestCaseUtil {
private TestCaseUtil() { private TestCaseUtil() {
} }
@SuppressWarnings("unchecked")
public static List<String> getTestCaseNames(Test test, boolean flatten) {
List<Test> tests = (List<Test>) getTests(test, flatten);
List<String> testCaseNames = new ArrayList<>();
for (Test aTest : tests) {
testCaseNames.add(getTestName(aTest));
}
return testCaseNames;
}
public static List<? extends Test> getTests(Test test, boolean flatten) { public static List<? extends Test> getTests(Test test, boolean flatten) {
return getTests(test, flatten, new HashSet<Class<?>>()); return getTests(test, flatten, new HashSet<Class<?>>());
} }
@@ -92,7 +82,7 @@ public class TestCaseUtil {
return testCases; return testCases;
} }
private static Test invokeSuiteMethodIfPossible(Class testClass, static Test invokeSuiteMethodIfPossible(Class testClass,
Set<Class<?>> seen) { Set<Class<?>> seen) {
try { try {
Method suiteMethod = testClass.getMethod( Method suiteMethod = testClass.getMethod(
@@ -120,7 +110,7 @@ public class TestCaseUtil {
return null; return null;
} }
public static String getTestName(Test test) { static String getTestName(Test test) {
if (test instanceof TestCase) { if (test instanceof TestCase) {
TestCase testCase = (TestCase) test; TestCase testCase = (TestCase) test;
return testCase.getName(); return testCase.getName();
@@ -138,34 +128,4 @@ public class TestCaseUtil {
} }
return ""; return "";
} }
public static Test getTestAtIndex(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 TestSuite createTestSuite(Class<? extends Test> testClass)
throws InstantiationException, IllegalAccessException {
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;
}
} }

View File

@@ -16,6 +16,8 @@
package android.test; package android.test;
import java.util.ArrayList;
import java.util.HashSet;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@@ -24,35 +26,47 @@ import java.util.List;
public class TestCaseUtilTest extends TestCase { public class TestCaseUtilTest extends TestCase {
public void testGetTestCaseNamesForTestSuiteWithSuiteMethod() throws Exception { @SuppressWarnings("unchecked")
private static List<String> getTestCaseNames(Test test) {
List<Test> tests = (List<Test>) TestCaseUtil.getTests(test, false);
List<String> testCaseNames = new ArrayList<>();
for (Test aTest : tests) {
testCaseNames.add(TestCaseUtil.getTestName(aTest));
}
return testCaseNames;
}
public void testGetTests_ForTestSuiteWithSuiteMethod() throws Exception {
TestSuite testSuite = new TwoTestsInTestSuite(); TestSuite testSuite = new TwoTestsInTestSuite();
List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testSuite, false); List<String> testCaseNames = getTestCaseNames(testSuite);
assertEquals(0, testCaseNames.size()); assertEquals(0, testCaseNames.size());
} }
public void testGetTestCaseNamesForTestCaseWithSuiteMethod() throws Exception { public void testGetTests_ForTestCaseWithSuiteMethod() throws Exception {
TestCase testCase = new OneTestTestCaseWithSuite(); TestCase testCase = new OneTestTestCaseWithSuite();
List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testCase, false); List<String> testCaseNames = getTestCaseNames(testCase);
assertEquals(1, testCaseNames.size()); assertEquals(1, testCaseNames.size());
assertTrue(testCaseNames.get(0).endsWith("testOne")); assertTrue(testCaseNames.get(0).endsWith("testOne"));
} }
public void testCreateTestForTestCase() throws Exception { public void testInvokeSuiteMethodIfPossible_ForTestCase() throws Exception {
Test test = TestCaseUtil.createTestSuite(OneTestTestCase.class); Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCase.class, new HashSet<>());
assertEquals(1, test.countTestCases()); assertNull(test);
} }
public void testCreateTestForTestSuiteWithSuiteMethod() throws Exception { public void testInvokeSuiteMethodIfPossible_ForTestSuiteWithSuiteMethod() throws Exception {
Test test = TestCaseUtil.createTestSuite(TwoTestsInTestSuite.class); Test test = TestCaseUtil.invokeSuiteMethodIfPossible(TwoTestsInTestSuite.class, new HashSet<>());
assertNotNull(test);
assertEquals(2, test.countTestCases()); assertEquals(2, test.countTestCases());
} }
public void testCreateTestForTestCaseWithSuiteMethod() throws Exception { public void testInvokeSuiteMethodIfPossible_ForTestCaseWithSuiteMethod() throws Exception {
Test test = TestCaseUtil.createTestSuite(OneTestTestCaseWithSuite.class); Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCaseWithSuite.class, new HashSet<>());
assertNotNull(test);
assertEquals(1, test.countTestCases()); assertEquals(1, test.countTestCases());
} }