• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 com.android.cts.tradefed.testtype;
18 
19 import com.android.ddmlib.testrunner.TestIdentifier;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 
29 /**
30  * Filter for {@link TestIdentifier}s.
31  */
32 public class TestFilter {
33 
34     private final Set<String> mExcludedClasses;
35     private final Set<TestIdentifier> mExcludedTests;
36     private String mIncludedClass = null;
37     private String mIncludedMethod = null;
38 
39     /**
40      * Creates a {@link TestFilter}
41      */
TestFilter()42     public TestFilter() {
43         mExcludedClasses = new HashSet<String>();
44         mExcludedTests = new HashSet<TestIdentifier>();
45     }
46 
47     /**
48      * Adds a test class to the filter.
49      * <p/>
50      * All tests in this class should be filtered.
51      */
addExcludedClass(String className)52     public void addExcludedClass(String className) {
53         mExcludedClasses.add(className);
54     }
55 
56     /**
57      * Adds a test class to the filter. All tests in this class should be excluded.
58      */
addExcludedTest(TestIdentifier test)59     public void addExcludedTest(TestIdentifier test) {
60         mExcludedTests.add(test);
61     }
62 
63     /**
64      * Get the test classes to exclude.
65      * <p/>
66      * Exposed for unit testing
67      */
getExcludedClasses()68     Set<String> getExcludedClasses() {
69         return mExcludedClasses;
70     }
71 
72     /**
73      * Get the tests to exclude.
74      * <p/>
75      * Exposed for unit testing
76      */
getExcludedTests()77     Set<TestIdentifier> getExcludedTests() {
78         return mExcludedTests;
79     }
80 
81     /**
82      * Sets the class name and optionally method that should pass this filter. If non-null, all
83      * other tests will be excluded.
84      *
85      * @param className the test class name to exclusively include
86      * @param method the test method name to exclusively include
87      */
setTestInclusion(String className, String method)88     public void setTestInclusion(String className, String method) {
89         mIncludedClass = className;
90         mIncludedMethod = method;
91     }
92 
93     /**
94      * Filter the list of tests based on rules in this filter
95      *
96      * @param tests the list of tests to filter
97      * @return a new sorted list of tests that passed the filter
98      */
filter(Collection<TestIdentifier > tests)99     public Collection<TestIdentifier> filter(Collection<TestIdentifier > tests) {
100         List<TestIdentifier> filteredTests = new ArrayList<TestIdentifier>(tests.size());
101         for (TestIdentifier test : tests) {
102             if (mIncludedClass != null && !test.getClassName().equals(mIncludedClass)) {
103                 // skip
104                 continue;
105             }
106             if (mIncludedMethod != null && !test.getTestName().equals(mIncludedMethod)) {
107                 // skip
108                 continue;
109             }
110             if (mExcludedClasses.contains(test.getClassName())) {
111                 // skip
112                 continue;
113             }
114             if (mExcludedTests.contains(test)) {
115                 // skip
116                 continue;
117             }
118             filteredTests.add(test);
119         }
120         Collections.sort(filteredTests, new TestIdComparator());
121         return filteredTests;
122     }
123 
124     /**
125      * Return true if there are exclusions rules defined.
126      */
hasExclusion()127     public boolean hasExclusion() {
128         return !mExcludedClasses.isEmpty() || !mExcludedTests.isEmpty();
129     }
130 
131     /**
132      * A {@link Comparator} for {@link TestIdentifier} that compares using
133      * {@link TestIdentifier#toString()}
134      */
135     private class TestIdComparator implements Comparator<TestIdentifier> {
136 
137         @Override
compare(TestIdentifier o1, TestIdentifier o2)138         public int compare(TestIdentifier o1, TestIdentifier o2) {
139             return o1.toString().compareTo(o2.toString());
140         }
141     }
142 }
143