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.tradefed.util; 17 18 import org.junit.runner.Description; 19 import org.junit.runner.manipulation.Filter; 20 21 /** 22 * Helper Class that provides the filtering for JUnit4 runner by extending the {@link Filter}. 23 */ 24 public class JUnit4TestFilter extends Filter { 25 26 private TestFilterHelper mFilterHelper; 27 28 public JUnit4TestFilter(TestFilterHelper filterHelper) { 29 mFilterHelper = filterHelper; 30 } 31 32 /** 33 * Filter function deciding whether a test should run or not. 34 */ 35 @Override 36 public boolean shouldRun(Description description) { 37 // If it's a suite, we check if at least one of the children can run, if yes then we run it 38 if (description.isSuite()) { 39 for (Description desc : description.getChildren()) { 40 if (shouldRun(desc)) { 41 return true; 42 } 43 } 44 } 45 return mFilterHelper.shouldRun(description); 46 } 47 48 @Override 49 public String describe() { 50 return "This filter is based on annotations, regex filter, class and method name to decide " 51 + "if a particular test should run."; 52 } 53 } 54