1 /*
2  * Copyright (C) 2016 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 package com.android.cts.core.runner;
17 
18 import android.util.Log;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Set;
25 import javax.annotation.Nullable;
26 
27 /**
28  * A list of the tests to run.
29  */
30 class TestList {
31 
32     /** The set of test pacakges to run */
33     private final Set<String> mIncludedPackages = new HashSet<>();
34 
35     /** The set of test packages not to run */
36     private final Set<String> mExcludedPackages = new HashSet<>();
37 
38     /** The set of tests (classes or methods) to run */
39     private final Set<String> mIncludedTests = new HashSet<>();
40 
41     /** The set of tests (classes or methods) not to run */
42     private final Set<String> mExcludedTests = new HashSet<>();
43 
44     /** The list of all test classes to run (without filtering applied)*/
45     private final Collection<Class<?>> classesToRun;
46 
rootList(List<String> rootList)47     public static TestList rootList(List<String> rootList) {
48 
49         // Run from the root test class.
50         Set<String> classNamesToRun = new LinkedHashSet<>(rootList);
51         Log.d(CoreTestRunner.TAG, "Running all tests rooted at " + classNamesToRun);
52 
53         List<Class<?>> classesToRun1 = getClasses(classNamesToRun);
54 
55         return new TestList(classesToRun1);
56     }
57 
getClasses(Set<String> classNames)58     private static List<Class<?>> getClasses(Set<String> classNames) {
59         // Populate the list of classes to run.
60         List<Class<?>> classesToRun = new ArrayList<>();
61         for (String className : classNames) {
62             try {
63                 classesToRun.add(Class.forName(className));
64             } catch (ClassNotFoundException e) {
65                 throw new IllegalStateException("Could not load class '" + className, e);
66             }
67         }
68         return classesToRun;
69     }
70 
71     /**
72      * @param classes The list of classes to run.
73      */
TestList(Collection<Class<?>> classes)74     public TestList(Collection<Class<?>> classes) {
75         this.classesToRun = classes;
76     }
77 
addIncludeTestPackages(Set<String> packageNameSet)78     public void addIncludeTestPackages(Set<String> packageNameSet) {
79         mIncludedPackages.addAll(packageNameSet);
80     }
81 
addExcludeTestPackages(Set<String> packageNameSet)82     public void addExcludeTestPackages(Set<String> packageNameSet) {
83         mExcludedPackages.addAll(packageNameSet);
84     }
85 
addIncludeTests(Set<String> testNameSet)86     public void addIncludeTests(Set<String> testNameSet) {
87         mIncludedTests.addAll(testNameSet);
88     }
89 
addExcludeTests(Set<String> testNameSet)90     public void addExcludeTests(Set<String> testNameSet) {
91         mExcludedTests.addAll(testNameSet);
92     }
93 
94     /**
95      * Return all the classes to run.
96      */
getClassesToRun()97     public Class[] getClassesToRun() {
98         return classesToRun.toArray(new Class[classesToRun.size()]);
99     }
100 
101     /**
102      * Return true if the test with the specified name should be run, false otherwise.
103      */
shouldRunTest(String testName)104     public boolean shouldRunTest(String testName) {
105 
106         int index = testName.indexOf('#');
107         String className;
108         if (index == -1) {
109             className = testName;
110         } else {
111             className = testName.substring(0, index);
112         }
113         try {
114             Class<?> testClass = Class.forName(className);
115             Package testPackage = testClass.getPackage();
116             String testPackageName = "";
117             if (testPackage != null) {
118                 testPackageName = testPackage.getName();
119             }
120 
121             boolean include =
122                     (mIncludedPackages.isEmpty() || mIncludedPackages.contains(testPackageName)) &&
123                     (mIncludedTests.isEmpty() || mIncludedTests.contains(className) ||
124                             mIncludedTests.contains(testName));
125 
126             boolean exclude =
127                     mExcludedPackages.contains(testPackageName) ||
128                     mExcludedTests.contains(className) ||
129                     mExcludedTests.contains(testName);
130 
131             return include && !exclude;
132         } catch (ClassNotFoundException e) {
133             Log.w("Could not load class '" + className, e);
134             return false;
135         }
136     }
137 }
138