Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad654a3a authored by Paul Duffin's avatar Paul Duffin Committed by android-build-merger
Browse files

Merge "Clean up TestCaseUtil" am: 43851d01 am: e38d5033

am: b5b6e7cf

Change-Id: I98265e973df1bf7ac28b7f79f6ccd0a865762304
parents a18500b5 b5b6e7cf
Loading
Loading
Loading
Loading
+2 −42
Original line number Diff line number Diff line
@@ -40,16 +40,6 @@ public class 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) {
        return getTests(test, flatten, new HashSet<Class<?>>());
    }
@@ -92,7 +82,7 @@ public class TestCaseUtil {
        return testCases;
    }

    private static Test invokeSuiteMethodIfPossible(Class testClass,
    static Test invokeSuiteMethodIfPossible(Class testClass,
            Set<Class<?>> seen) {
        try {
            Method suiteMethod = testClass.getMethod(
@@ -120,7 +110,7 @@ public class TestCaseUtil {
        return null;
    }

    public static String getTestName(Test test) {
    static String getTestName(Test test) {
        if (test instanceof TestCase) {
            TestCase testCase = (TestCase) test;
            return testCase.getName();
@@ -138,34 +128,4 @@ public class TestCaseUtil {
        }
        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;
    }
}
+27 −13
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.test;

import java.util.ArrayList;
import java.util.HashSet;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -24,35 +26,47 @@ import java.util.List;

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();

        List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testSuite, false);
        List<String> testCaseNames = getTestCaseNames(testSuite);

        assertEquals(0, testCaseNames.size());
    }
    
    public void testGetTestCaseNamesForTestCaseWithSuiteMethod() throws Exception {
    public void testGetTests_ForTestCaseWithSuiteMethod() throws Exception {
        TestCase testCase = new OneTestTestCaseWithSuite();

        List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testCase, false);
        List<String> testCaseNames = getTestCaseNames(testCase);

        assertEquals(1, testCaseNames.size());
        assertTrue(testCaseNames.get(0).endsWith("testOne"));
    }

    public void testCreateTestForTestCase() throws Exception {
        Test test = TestCaseUtil.createTestSuite(OneTestTestCase.class);
        assertEquals(1, test.countTestCases());
    public void testInvokeSuiteMethodIfPossible_ForTestCase() throws Exception {
        Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCase.class, new HashSet<>());
        assertNull(test);
    }

    public void testCreateTestForTestSuiteWithSuiteMethod() throws Exception {
        Test test = TestCaseUtil.createTestSuite(TwoTestsInTestSuite.class);
    public void testInvokeSuiteMethodIfPossible_ForTestSuiteWithSuiteMethod() throws Exception {
        Test test = TestCaseUtil.invokeSuiteMethodIfPossible(TwoTestsInTestSuite.class, new HashSet<>());
        assertNotNull(test);
        assertEquals(2, test.countTestCases());
    }

    public void testCreateTestForTestCaseWithSuiteMethod() throws Exception {
        Test test = TestCaseUtil.createTestSuite(OneTestTestCaseWithSuite.class);
    public void testInvokeSuiteMethodIfPossible_ForTestCaseWithSuiteMethod() throws Exception {
        Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCaseWithSuite.class, new HashSet<>());
        assertNotNull(test);
        assertEquals(1, test.countTestCases());
    }