Home | History | Annotate | Download | only in parameters
      1 package junitparams.internal.parameters;
      2 
      3 import java.util.List;
      4 
      5 import org.junit.runners.model.FrameworkMethod;
      6 
      7 import junitparams.FileParameters;
      8 import junitparams.Parameters;
      9 
     10 import static java.lang.String.*;
     11 import static java.util.Arrays.*;
     12 
     13 public class ParametersReader {
     14 
     15     public static final String ILLEGAL_STATE_EXCEPTION_MESSAGE
     16         = format("Illegal usage of JUnitParams in method %s. " +
     17                  "Check that you have only used one supported parameters evaluation strategy. " +
     18                  "Common case is to use both %s and %s annotations.",
     19                  "%s", Parameters.class, FileParameters.class);
     20 
     21     private final FrameworkMethod frameworkMethod;
     22     private final List<ParametrizationStrategy> strategies;
     23 
     24     public ParametersReader(Class<?> testClass, FrameworkMethod frameworkMethod) {
     25         this.frameworkMethod = frameworkMethod;
     26 
     27         strategies = asList(
     28                 new ParametersFromCustomProvider(frameworkMethod),
     29                 new ParametersFromValue(frameworkMethod),
     30                 new ParametersFromExternalClassProvideMethod(frameworkMethod),
     31                 new ParametersFromExternalClassMethod(frameworkMethod),
     32                 new ParametersFromTestClassMethod(frameworkMethod, testClass)
     33         );
     34     }
     35 
     36     public Object[] read() {
     37         boolean strategyAlreadyFound = false;
     38         Object[] parameters = new Object[]{};
     39 
     40         for (ParametrizationStrategy strategy : strategies) {
     41             if (strategy.isApplicable()) {
     42                 if (strategyAlreadyFound) {
     43                     illegalState();
     44                 }
     45                 parameters = strategy.getParameters();
     46                 strategyAlreadyFound = true;
     47             }
     48         }
     49         return parameters;
     50     }
     51 
     52     private void illegalState() {
     53         throw new IllegalStateException(format(ILLEGAL_STATE_EXCEPTION_MESSAGE, frameworkMethod.getName()));
     54     }
     55 }
     56