1 /*
2  * Copyright (C) 2015 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.compatibility.common.util;
17 
18 import com.android.tradefed.util.AbiUtils;
19 
20 /**
21  * Represents a filter for including and excluding tests.
22  */
23 public class TestFilter {
24 
25     private final String mAbi;
26     private final String mName;
27     private final String mTest;
28 
29     /**
30      * Builds a new {@link TestFilter} from the given string. Filters can be in one of four forms,
31      * the instance will be initialized as;
32      * -"name"                 -> abi = null, name = "name", test = null
33      * -"name" "test..."       -> abi = null, name = "name", test = "test..."
34      * -"abi" "name"           -> abi = "abi", name = "name", test = null
35      * -"abi" "name" "test..." -> abi = "abi", name = "name", test = "test..."
36      *
37      * Test identifier can contain multiple parts, eg parameterized tests.
38      *
39      * @param filter the filter to parse
40      * @return the {@link TestFilter}
41      */
createFrom(String filter)42     public static TestFilter createFrom(String filter) {
43         if (filter.isEmpty()) {
44             throw new IllegalArgumentException("Filter was empty");
45         }
46         String[] parts = filter.split(" ");
47         String abi = null, name = null, test = null;
48         // Either:
49         // <name>
50         // <name> <test>
51         // <abi> <name>
52         // <abi> <name> <test>
53         if (parts.length == 1) {
54             name = parts[0];
55         } else {
56             int index = 0;
57             if (AbiUtils.isAbiSupportedByCompatibility(parts[0])) {
58                 abi = parts[0];
59                 index++;
60             }
61             name = parts[index];
62             index++;
63             parts = filter.split(" ", index + 1);
64             if (parts.length > index) {
65                 test = parts[index];
66             }
67         }
68         return new TestFilter(abi, name, test);
69     }
70 
71     /**
72      * Creates a new {@link TestFilter} from the given parts.
73      *
74      * @param abi The ABI must be supported {@link AbiUtils#isAbiSupportedByCompatibility(String)}
75      * @param name The module's name
76      * @param test The test's identifier eg <package>.<class>#<method>
77      */
TestFilter(String abi, String name, String test)78     public TestFilter(String abi, String name, String test) {
79         mAbi = abi;
80         mName = name;
81         mTest = test;
82     }
83 
84     /**
85      * Returns a String representation of this filter. This function is the inverse of
86      * {@link TestFilter#createFrom(String)}.
87      *
88      * For a valid filter f;
89      * <pre>
90      * {@code
91      * new TestFilter(f).toString().equals(f)
92      * }
93      * </pre>
94      */
95     @Override
toString()96     public String toString() {
97         StringBuilder sb = new StringBuilder();
98         if (mAbi != null) {
99             sb.append(mAbi.trim());
100             sb.append(" ");
101         }
102         if (mName != null) {
103             sb.append(mName.trim());
104         }
105         if (mTest != null) {
106             sb.append(" ");
107             sb.append(mTest.trim());
108         }
109         return sb.toString();
110     }
111 
112     /**
113      * @return the abi of this filter, or null if not specified.
114      */
getAbi()115     public String getAbi() {
116         return mAbi;
117     }
118 
119     /**
120      * @return the module name of this filter, or null if not specified.
121      */
getName()122     public String getName() {
123         return mName;
124     }
125 
126     /**
127      * @return the test identifier of this filter, or null if not specified.
128      */
getTest()129     public String getTest() {
130         return mTest;
131     }
132 
133 }
134