001package org.junit.experimental.theories.internal;
002
003import java.lang.reflect.Field;
004import java.util.ArrayList;
005import java.util.Arrays;
006import java.util.Collection;
007import java.util.List;
008
009import org.junit.experimental.theories.DataPoint;
010import org.junit.experimental.theories.DataPoints;
011import org.junit.experimental.theories.FromDataPoints;
012import org.junit.experimental.theories.ParameterSignature;
013import org.junit.runners.model.FrameworkMethod;
014import org.junit.runners.model.TestClass;
015
016public class SpecificDataPointsSupplier extends AllMembersSupplier {
017
018    public SpecificDataPointsSupplier(TestClass testClass) {
019        super(testClass);
020    }
021    
022    @Override
023    protected Collection<Field> getSingleDataPointFields(ParameterSignature sig) {
024        Collection<Field> fields = super.getSingleDataPointFields(sig);        
025        String requestedName = sig.getAnnotation(FromDataPoints.class).value();
026
027        List<Field> fieldsWithMatchingNames = new ArrayList<Field>();
028        
029        for (Field field : fields) {
030            String[] fieldNames = field.getAnnotation(DataPoint.class).value();
031            if (Arrays.asList(fieldNames).contains(requestedName)) {
032                fieldsWithMatchingNames.add(field);
033            }
034        }
035        
036        return fieldsWithMatchingNames;
037    }
038    
039    @Override
040    protected Collection<Field> getDataPointsFields(ParameterSignature sig) {
041        Collection<Field> fields = super.getDataPointsFields(sig);        
042        String requestedName = sig.getAnnotation(FromDataPoints.class).value();
043        
044        List<Field> fieldsWithMatchingNames = new ArrayList<Field>();
045        
046        for (Field field : fields) {
047            String[] fieldNames = field.getAnnotation(DataPoints.class).value();
048            if (Arrays.asList(fieldNames).contains(requestedName)) {
049                fieldsWithMatchingNames.add(field);
050            }
051        }
052        
053        return fieldsWithMatchingNames;
054    }
055    
056    @Override
057    protected Collection<FrameworkMethod> getSingleDataPointMethods(ParameterSignature sig) {
058        Collection<FrameworkMethod> methods = super.getSingleDataPointMethods(sig);
059        String requestedName = sig.getAnnotation(FromDataPoints.class).value();
060        
061        List<FrameworkMethod> methodsWithMatchingNames = new ArrayList<FrameworkMethod>();
062        
063        for (FrameworkMethod method : methods) {
064            String[] methodNames = method.getAnnotation(DataPoint.class).value();
065            if (Arrays.asList(methodNames).contains(requestedName)) {
066                methodsWithMatchingNames.add(method);
067            }
068        }
069        
070        return methodsWithMatchingNames;
071    }
072    
073    @Override
074    protected Collection<FrameworkMethod> getDataPointsMethods(ParameterSignature sig) {
075        Collection<FrameworkMethod> methods = super.getDataPointsMethods(sig);
076        String requestedName = sig.getAnnotation(FromDataPoints.class).value();
077        
078        List<FrameworkMethod> methodsWithMatchingNames = new ArrayList<FrameworkMethod>();
079        
080        for (FrameworkMethod method : methods) {
081            String[] methodNames = method.getAnnotation(DataPoints.class).value();
082            if (Arrays.asList(methodNames).contains(requestedName)) {
083                methodsWithMatchingNames.add(method);
084            }
085        }
086        
087        return methodsWithMatchingNames;
088    }
089
090}