1 package junitparams.internal; 2 3 import java.lang.reflect.Method; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import org.junit.runner.Description; 8 import org.junit.runner.manipulation.Filter; 9 import org.junit.runner.manipulation.Filterable; 10 import org.junit.runner.manipulation.NoTestsRemainException; 11 import org.junit.runners.model.FrameworkMethod; 12 13 /** 14 * A {@link FrameworkMethod} that represents a parameterized method. 15 * 16 * <p>This contains a list of {@link InstanceFrameworkMethod} that represent the individual 17 * instances of this method, one per parameter set. 18 */ 19 public class ParameterisedFrameworkMethod extends DescribableFrameworkMethod implements Filterable { 20 21 /** 22 * The base description, used as a template when creating {@link Description}. 23 */ 24 private final Description baseDescription; 25 26 /** 27 * The list of {@link InstanceFrameworkMethod} that represent individual instances of this 28 * method. 29 */ 30 private List<InstanceFrameworkMethod> instanceMethods; 31 32 /** 33 * The {@link Description}, created lazily and updated after filtering. 34 */ 35 private Description description; 36 37 public ParameterisedFrameworkMethod(Method method, Description baseDescription, 38 List<InstanceFrameworkMethod> instanceMethods) { 39 super(method); 40 this.baseDescription = baseDescription; 41 this.instanceMethods = instanceMethods; 42 } 43 44 @Override 45 public Description getDescription() { 46 if (description == null) { 47 description = baseDescription.childlessCopy(); 48 for (InstanceFrameworkMethod instanceMethod : instanceMethods) { 49 description.addChild(instanceMethod.getInstanceDescription()); 50 } 51 } 52 53 return description; 54 } 55 56 public List<InstanceFrameworkMethod> getMethods() { 57 return instanceMethods; 58 } 59 60 @Override 61 public void filter(Filter filter) throws NoTestsRemainException { 62 int count = instanceMethods.size(); 63 for (Iterator<InstanceFrameworkMethod> i = instanceMethods.iterator(); i.hasNext(); ) { 64 InstanceFrameworkMethod instanceMethod = i.next(); 65 if (filter.shouldRun(instanceMethod.getInstanceDescription())) { 66 try { 67 filter.apply(instanceMethod); 68 } catch (NoTestsRemainException e) { 69 i.remove(); 70 } 71 } else { 72 i.remove(); 73 } 74 } 75 76 if (instanceMethods.size() != count) { 77 // Some instance methods have been filtered out, so invalidate the description. 78 description = null; 79 } 80 81 if (instanceMethods.isEmpty()) { 82 throw new NoTestsRemainException(); 83 } 84 } 85 } 86