1 /*
2  * Copyright (C) 2008 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 
17 package android.test.suitebuilder;
18 
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 
27 public class ListTestCaseNames {
getTestCaseNames(TestSuite suite)28     public static List<String> getTestCaseNames(TestSuite suite) {
29         // TODO: deprecate this method and move all callers to use getTestNames
30         List<Test> tests = Collections.<Test>list(suite.tests());
31         ArrayList<String> testCaseNames = new ArrayList<String>();
32         for (Test test : tests) {
33             if (test instanceof TestCase) {
34                 testCaseNames.add(((TestCase) test).getName());
35             } else if (test instanceof TestSuite) {
36                 testCaseNames.addAll(getTestCaseNames((TestSuite) test));
37             }
38         }
39         return testCaseNames;
40     }
41 
42     /**
43      * Returns a list of test class and method names for each TestCase in suite.
44      */
getTestNames(TestSuite suite)45     public static List<TestDescriptor> getTestNames(TestSuite suite) {
46         List<Test> tests = Collections.<Test>list(suite.tests());
47         ArrayList<TestDescriptor> testNames = new ArrayList<TestDescriptor>();
48         for (Test test : tests) {
49             if (test instanceof TestCase) {
50                 String className = test.getClass().getName();
51                 String testName = ((TestCase) test).getName();
52                 testNames.add(new TestDescriptor(className, testName));
53             } else if (test instanceof TestSuite) {
54                 testNames.addAll(getTestNames((TestSuite) test));
55             }
56         }
57         return testNames;
58     }
59 
60     /**
61      * Data holder for test case info
62      */
63     public static class TestDescriptor {
64        private String mClassName;
65        private String mTestName;
66 
TestDescriptor(String className, String testName)67        public TestDescriptor(String className, String testName) {
68            mClassName = className;
69            mTestName = testName;
70        }
71 
getClassName()72        public String getClassName() {
73            return mClassName;
74        }
75 
getTestName()76        public String getTestName() {
77            return mTestName;
78        }
79 
80        /**
81         * Override parent to do string-based class and test name comparison
82         */
83        @Override
equals(Object otherObj)84        public boolean equals(Object otherObj) {
85            if (otherObj instanceof TestDescriptor) {
86                TestDescriptor otherDesc = (TestDescriptor)otherObj;
87                return otherDesc.getClassName().equals(this.getClassName()) &&
88                       otherDesc.getTestName().equals(this.getTestName());
89 
90            }
91            return false;
92        }
93 
94        /**
95         * Override parent to return a more user-friendly display string
96         */
97        @Override
toString()98        public String toString() {
99            return getClassName() + "#" + getTestName();
100        }
101     }
102 }
103