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 17 package vogar.target; 18 19 import java.util.concurrent.atomic.AtomicReference; 20 import org.junit.runner.Description; 21 import org.junit.runner.manipulation.Filter; 22 import vogar.target.junit.JUnitUtils; 23 24 /** 25 * A {@link Filter} that skips past (discards) all tests up to and including one that failed 26 * previously causing the process to exit. 27 * 28 * <p>If {@link #skipPastReference} has a null value then this lets all tests run. Otherwise, this 29 * filters out all tests up to and including the one whose name matches the value in the reference 30 * at which point the reference is set to null, so all following tests are kept. 31 */ 32 public class SkipPastFilter extends Filter { 33 34 private final AtomicReference<String> skipPastReference; 35 36 public SkipPastFilter(AtomicReference<String> skipPastReference) { 37 this.skipPastReference = skipPastReference; 38 } 39 40 @Override 41 public boolean shouldRun(Description description) { 42 String skipPast = skipPastReference.get(); 43 if (description.isTest() && skipPast != null) { 44 String name = JUnitUtils.getTestName(description); 45 if (skipPast.equals(name)) { 46 skipPastReference.set(null); 47 } 48 return false; 49 } 50 51 return true; 52 } 53 54 @Override 55 public String describe() { 56 return "SkipPastFilter"; 57 } 58 } 59