android.test.suitebuilder.TestGrouping

本文介绍Android测试框架中TestGrouping类的实现细节,包括如何添加或移除测试包、获取测试方法等核心功能,并提供了类的具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /*
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
 
22
 import android.util.Log;
25
 
30
 import java.util.ArrayList;
31
 import java.util.Arrays;
34
 import java.util.List;
35
 import java.util.Set;
36
 import java.util.SortedSet;
37
 import java.util.TreeSet;

Represents a collection of test classes present on the classpath. You can add individual classes or entire packages. By default sub-packages are included recursively, but methods are provided to allow for arbitrary inclusion or exclusion of sub-packages. Typically a  TestGrouping will have only one root package, but this is not a requirement. Not needed for 1.0 SDK.
46
 
47
 public class TestGrouping {
48
 
49
     private static final String LOG_TAG = "TestGrouping";
50
 
51
     SortedSet<Class<? extends TestCase>> testCaseClasses;
52
 
53
     public static final Comparator<Class<? extends TestCase>> SORT_BY_SIMPLE_NAME
54
             = new SortBySimpleName();
55
 
56
     public static final Comparator<Class<? extends TestCase>> SORT_BY_FULLY_QUALIFIED_NAME
57
             = new SortByFullyQualifiedName();
58
 
59
     protected String firstIncludedPackage = null;
60
     private ClassLoader classLoader;
61
 
62
     public TestGrouping(Comparator<Class<? extends TestCase>> comparator) {
63
          = new TreeSet<Class<? extends TestCase>>(comparator);
64
     }

    

Returns:
A list of all tests in the package, including small, medium, large, flaky, and suppressed tests. Includes sub-packages recursively.
69
 
70
     public List<TestMethodgetTests() {
71
         List<TestMethodtestMethods = new ArrayList<TestMethod>();
72
         for (Class<? extends TestCasetestCase : ) {
73
             for (Method testMethod : getTestMethods(testCase)) {
74
                 testMethods.add(new TestMethod(testMethodtestCase));
75
             }
76
         }
77
         return testMethods;
78
     }
79
 
80
     protected List<MethodgetTestMethods(Class<? extends TestCasetestCaseClass) {
81
         List<Methodmethods = Arrays.asList(testCaseClass.getMethods());
82
         return select(methodsnew TestMethodPredicate());
83
     }
84
 
85
     SortedSet<Class<? extends TestCase>> getTestCaseClasses() {
86
         return ;
87
     }
88
 
89
     public boolean equals(Object o) {
90
         if (this == o) {
91
             return true;
92
         }
93
         if (o == null || getClass() != o.getClass()) {
94
             return false;
95
         }
96
         TestGrouping other = (TestGroupingo;
97
         if (!this..equals(other.testCaseClasses)) {
98
             return false;
99
         }
100
        return this..comparator().equals(other.testCaseClasses.comparator());
101
    }
102
 
103
    public int hashCode() {
104
        return .hashCode();
105
    }

    
Include all tests in the given packages and all their sub-packages, unless otherwise specified. Each of the given packages must contain at least one test class, either directly or in a sub-package.

Parameters:
packageNames Names of packages to add.
Returns:
The  TestGrouping for method chaining.
114
 
115
    public TestGrouping addPackagesRecursive(String... packageNames) {
116
        for (String packageName : packageNames) {
117
            List<Class<? extends TestCase>> addedClasses = testCaseClassesInPackage(packageName);
118
            if (addedClasses.isEmpty()) {
119
                Log.w("Invalid Package: '" + packageName
120
                        + "' could not be found or has no tests");
121
            }
122
            .addAll(addedClasses);
123
            if ( == null) {
124
                 = packageName;
125
            }
126
        }
127
        return this;
128
    }

    
Exclude all tests in the given packages and all their sub-packages, unless otherwise specified.

Parameters:
packageNames Names of packages to remove.
Returns:
The  TestGrouping for method chaining.
136
 
137
    public TestGrouping removePackagesRecursive(String... packageNames) {
138
        for (String packageName : packageNames) {
139
            .removeAll(testCaseClassesInPackage(packageName));
140
        }
141
        return this;
142
    }

    

Returns:
The first package name passed to  addPackagesRecursive(java.lang.String[]), or null if that method was never called.
147
 
148
    public String getFirstIncludedPackage() {
149
        return ;
150
    }
151
 
152
    private List<Class<? extends TestCase>> testCaseClassesInPackage(String packageName) {
153
        ClassPathPackageInfoSource source = PackageInfoSources.forClassPath();
154
        ClassPathPackageInfo packageInfo = source.getPackageInfo(packageName);
155
 
156
        return selectTestClasses(packageInfo.getTopLevelClassesRecursive());
157
    }
158
 
159
    @SuppressWarnings("unchecked")
160
    private List<Class<? extends TestCase>> selectTestClasses(Set<Class<?>> allClasses) {
161
        List<Class<? extends TestCase>> testClasses = new ArrayList<Class<? extends TestCase>>();
162
        for (Class<?> testClass : select(allClasses,
163
                new TestCasePredicate())) {
164
            testClasses.add((Class<? extends TestCase>) testClass);
165
        }
166
        return testClasses;
167
    }
168
 
169
    private <T> List<T> select(Collection<T> itemsPredicate<T> predicate) {
170
        ArrayList<T> selectedItems = new ArrayList<T>();
171
        for (T item : items) {
172
            if (predicate.apply(item)) {
173
                selectedItems.add(item);
174
            }
175
        }
176
        return selectedItems;
177
    }
178
 
179
    public void setClassLoader(ClassLoader classLoader) {
180
        this. = classLoader;
181
    }

    
Sort classes by their simple names (i.e. without the package prefix), using their packages to sort classes with the same name.
186
 
187
    private static class SortBySimpleName
188
            implements Comparator<Class<? extends TestCase>>, Serializable {
189
 
190
        public int compare(Class<? extends TestCaseclass1,
191
                Class<? extends TestCaseclass2) {
192
            int result = class1.getSimpleName().compareTo(class2.getSimpleName());
193
            if (result != 0) {
194
                return result;
195
            }
196
            return class1.getName().compareTo(class2.getName());
197
        }
198
    }

    
Sort classes by their fully qualified names (i.e. with the package prefix).
203
 
204
    private static class SortByFullyQualifiedName
205
            implements Comparator<Class<? extends TestCase>>, Serializable {
206
 
207
        public int compare(Class<? extends TestCaseclass1,
208
                Class<? extends TestCaseclass2) {
209
            return class1.getName().compareTo(class2.getName());
210
        }
211
    }
212
 
213
    private static class TestCasePredicate implements Predicate<Class<?>> {
214
 
215
        public boolean apply(Class aClass) {
216
            int modifiers = ((Class<?>) aClass).getModifiers();
217
            return TestCase.class.isAssignableFrom((Class<?>) aClass)
218
                    && Modifier.isPublic(modifiers)
219
                    && !Modifier.isAbstract(modifiers)
220
                    && hasValidConstructor((Class<?>) aClass);
221
        }
222
 
223
        @SuppressWarnings("unchecked")
224
        private boolean hasValidConstructor(java.lang.Class<?> aClass) {
225
            // The cast below is not necessary with the Java 5 compiler, but necessary with the Java 6 compiler,
226
            // where the return type of Class.getDeclaredConstructors() was changed
227
            // from Constructor<T>[] to Constructor<?>[]
228
            Constructor<? extends TestCase>[] constructors
229
                    = (Constructor<? extends TestCase>[]) aClass.getConstructors();
230
            for (Constructor<? extends TestCaseconstructor : constructors) {
231
                if (Modifier.isPublic(constructor.getModifiers())) {
232
                    java.lang.Class[] parameterTypes = constructor.getParameterTypes();
233
                    if (parameterTypes.length == 0 ||
234
                            (parameterTypes.length == 1 && parameterTypes[0] == String.class)) {
235
                        return true;
236
                    }
237
                }
238
            }
239
            Log.i(, String.format(
240
                    "TestCase class %s is missing a public constructor with no parameters " +
241
                    "or a single String parameter - skipping",
242
                    aClass.getName()));
243
            return false;
244
        }
245
    }
246
 
247
    private static class TestMethodPredicate implements Predicate<Method> {
248
 
249
        public boolean apply(Method method) {
250
            return ((method.getParameterTypes().length == 0) &&
251
                    (method.getName().startsWith("test")) &&
252
                    (method.getReturnType().getSimpleName().equals("void")));
253
        }
254
    }
255
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值