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