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