Part of the work of removing JUnit and dependent android.test classes from the Android API involves providing a static library that developers can include in their test applications to ease migration. That library will be built directly from the source (as opposed to android.jar which is built from stubs) and so developers will be able to see classes and methods that are not present in the stubs. This change is one of a number of similar changes that cleanup the existing non-API code in order to minimize the additional methods and classes exposed externally. The basic approach is to remove unused classes and methods, use least visible access modifier possible and generally minimize the amount of publicly visible code. TestCaseUtil.getTestCaseNames() is only used by tests but its tests did provide some coverage of the getTests() method so remove the method and the tests the method was simply moved into TestCaseUtilTest and the tests renamed to make it clearer that they are testing TestCaseUtil.getTests(). Similarly, TestCaseUtil.createTestSuite() was only used by tests but its tests did provide some coverage of the invokeSuiteMethodIfPossible() method so the tests were modified and renamed to preserve that coverage. TestCaseUtil.getTestAtIndex() was completely unused so was just removed. Bug: 30188076 Test: make checkbuild and ran FrameworkTestRunnerTests Change-Id: I62bbdbab428d7560f0c7df11f313fe60cfd31d13
132 lines
4.4 KiB
Java
132 lines
4.4 KiB
Java
/*
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package android.test;
|
|
|
|
import java.util.ArrayList;
|
|
import junit.framework.Test;
|
|
import junit.framework.TestCase;
|
|
import junit.framework.TestSuite;
|
|
import junit.runner.BaseTestRunner;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Modifier;
|
|
import java.util.Enumeration;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* @hide - This is part of a framework that is under development and should not be used for
|
|
* active development.
|
|
*/
|
|
@Deprecated
|
|
public class TestCaseUtil {
|
|
|
|
private TestCaseUtil() {
|
|
}
|
|
|
|
public static List<? extends Test> getTests(Test test, boolean flatten) {
|
|
return getTests(test, flatten, new HashSet<Class<?>>());
|
|
}
|
|
|
|
private static List<? extends Test> getTests(Test test, boolean flatten,
|
|
Set<Class<?>> seen) {
|
|
List<Test> testCases = new ArrayList<>();
|
|
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;
|
|
}
|
|
|
|
static Test invokeSuiteMethodIfPossible(Class testClass,
|
|
Set<Class<?>> 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;
|
|
}
|
|
|
|
static String getTestName(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 "";
|
|
}
|
|
}
|