类org.junit.runners.Parameterized.Parameter源码实例Demo

下面列出了怎么用org.junit.runners.Parameterized.Parameter的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: neodymium-library   文件: ParameterStatement.java
private void injectTestParameter() throws Exception
{
    int parameterFieldCount = statementData.getParameterFrameworkFields().size();
    Object[] parameter = statementData.getParameter();

    if (parameterFieldCount != parameter.length)
    {
        throw new Exception("Number of parameters (" + parameter.length + ") and " + //
                            "fields (" + parameterFieldCount + ") " + //
                            "annotated with @Parameter must match!");
    }

    for (FrameworkField parameterFrameworkField : statementData.getParameterFrameworkFields())
    {
        Field field = parameterFrameworkField.getField();
        int parameterIndex = field.getAnnotation(Parameter.class).value();

        LOGGER.debug("Set parameter \"" + parameterFrameworkField.getName() + "\" to \"" + parameter[parameterIndex] + "\"");
        setField(field, parameter[parameterIndex]);
    }
}
 
/**
 * {@inheritDoc}
 */
@Override
protected void validateFields(final List<Throwable> errors) {
    super.validateFields(errors);
    if (fieldsAreAnnotated()) {
        final List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
        final int[] usedIndices = new int[annotatedFieldsByParameter.size()];
        for (final FrameworkField each : annotatedFieldsByParameter) {
            final int index = each.getField().getAnnotation(Parameter.class)
                    .value();
            if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
                errors.add(new Exception("Invalid @Parameter value: "
                        + index + ". @Parameter fields counted: "
                        + annotatedFieldsByParameter.size()
                        + ". Please use an index between 0 and "
                        + (annotatedFieldsByParameter.size() - 1) + "."));
            }
            else {
                usedIndices[index]++;
            }
        }
        for (int index = 0; index < usedIndices.length; index++) {
            final int numberOfUse = usedIndices[index];
            if (numberOfUse == 0) {
                errors.add(new Exception("@Parameter(" + index
                        + ") is never used."));
            }
            else if (numberOfUse > 1) {
                errors.add(new Exception("@Parameter(" + index
                        + ") is used more than once (" + numberOfUse + ")."));
            }
        }
    }
}
 
源代码3 项目: htmlunit   文件: FrameworkMethodWithParameters.java
/**
 * {@inheritDoc}
 */
@Override
public Object invokeExplosively(final Object target, final Object... params)
    throws Throwable {
    if (!parameters_.isEmpty()) {
        final List<FrameworkField> annotatedFieldsByParameter = testClass_.getAnnotatedFields(Parameter.class);
        if (annotatedFieldsByParameter.size() != parameters_.size()) {
            throw new Exception(
                    "Wrong number of parameters and @Parameter fields."
                            + " @Parameter fields counted: "
                            + annotatedFieldsByParameter.size()
                            + ", available parameters: " + parameters_.size()
                            + ".");
        }
        for (final FrameworkField each : annotatedFieldsByParameter) {
            final Field field = each.getField();
            final Parameter annotation = field.getAnnotation(Parameter.class);
            final int index = annotation.value();
            try {
                field.set(target, parameters_.get(index));
            }
            catch (final IllegalArgumentException iare) {
                throw new Exception(testClass_.getName()
                        + ": Trying to set " + field.getName()
                        + " with the value " + parameters_.get(index)
                        + " that is not the right type ("
                        + parameters_.get(index).getClass().getSimpleName()
                        + " instead of " + field.getType().getSimpleName()
                        + ").", iare);
            }
        }
    }
    return super.invokeExplosively(target, params);
}
 
private Object createTestUsingFieldInjection() throws Exception {
    List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
    if (annotatedFieldsByParameter.size() != parameters.length) {
        throw new Exception(
                "Wrong number of parameters and @Parameter fields."
                        + " @Parameter fields counted: "
                        + annotatedFieldsByParameter.size()
                        + ", available parameters: " + parameters.length
                        + ".");
    }
    Object testClassInstance = getTestClass().getJavaClass().newInstance();
    for (FrameworkField each : annotatedFieldsByParameter) {
        Field field = each.getField();
        Parameter annotation = field.getAnnotation(Parameter.class);
        int index = annotation.value();
        try {
            field.set(testClassInstance, parameters[index]);
        } catch (IllegalArgumentException iare) {
            throw new Exception(getTestClass().getName()
                    + ": Trying to set " + field.getName()
                    + " with the value " + parameters[index]
                    + " that is not the right type ("
                    + parameters[index].getClass().getSimpleName()
                    + " instead of " + field.getType().getSimpleName()
                    + ").", iare);
        }
    }
    return testClassInstance;
}
 
@Override
protected void validateFields(List<Throwable> errors) {
    super.validateFields(errors);
    if (getInjectionType() == InjectionType.FIELD) {
        List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
        int[] usedIndices = new int[annotatedFieldsByParameter.size()];
        for (FrameworkField each : annotatedFieldsByParameter) {
            int index = each.getField().getAnnotation(Parameter.class)
                    .value();
            if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
                errors.add(new Exception("Invalid @Parameter value: "
                        + index + ". @Parameter fields counted: "
                        + annotatedFieldsByParameter.size()
                        + ". Please use an index between 0 and "
                        + (annotatedFieldsByParameter.size() - 1) + "."));
            } else {
                usedIndices[index]++;
            }
        }
        for (int index = 0; index < usedIndices.length; index++) {
            int numberOfUse = usedIndices[index];
            if (numberOfUse == 0) {
                errors.add(new Exception("@Parameter(" + index
                        + ") is never used."));
            } else if (numberOfUse > 1) {
                errors.add(new Exception("@Parameter(" + index
                        + ") is used more than once (" + numberOfUse + ")."));
            }
        }
    }
}
 
private Object createTestUsingFieldInjection() throws Exception {
    List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
    if (annotatedFieldsByParameter.size() != parameters.length) {
        throw new Exception(
                "Wrong number of parameters and @Parameter fields."
                        + " @Parameter fields counted: "
                        + annotatedFieldsByParameter.size()
                        + ", available parameters: " + parameters.length
                        + ".");
    }
    Object testClassInstance = getTestClass().getJavaClass().newInstance();
    for (FrameworkField each : annotatedFieldsByParameter) {
        Field field = each.getField();
        Parameter annotation = field.getAnnotation(Parameter.class);
        int index = annotation.value();
        try {
            field.set(testClassInstance, parameters[index]);
        } catch (IllegalArgumentException iare) {
            throw new Exception(getTestClass().getName()
                    + ": Trying to set " + field.getName()
                    + " with the value " + parameters[index]
                    + " that is not the right type ("
                    + parameters[index].getClass().getSimpleName()
                    + " instead of " + field.getType().getSimpleName()
                    + ").", iare);
        }
    }
    return testClassInstance;
}
 
@Override
protected void validateFields(List<Throwable> errors) {
    super.validateFields(errors);
    if (getInjectionType() == InjectionType.FIELD) {
        List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
        int[] usedIndices = new int[annotatedFieldsByParameter.size()];
        for (FrameworkField each : annotatedFieldsByParameter) {
            int index = each.getField().getAnnotation(Parameter.class)
                    .value();
            if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
                errors.add(new Exception("Invalid @Parameter value: "
                        + index + ". @Parameter fields counted: "
                        + annotatedFieldsByParameter.size()
                        + ". Please use an index between 0 and "
                        + (annotatedFieldsByParameter.size() - 1) + "."));
            } else {
                usedIndices[index]++;
            }
        }
        for (int index = 0; index < usedIndices.length; index++) {
            int numberOfUse = usedIndices[index];
            if (numberOfUse == 0) {
                errors.add(new Exception("@Parameter(" + index
                        + ") is never used."));
            } else if (numberOfUse > 1) {
                errors.add(new Exception("@Parameter(" + index
                        + ") is used more than once (" + numberOfUse + ")."));
            }
        }
    }
}
 
private List<FrameworkField> getAnnotatedFieldsByParameter() {
    return getTestClass().getAnnotatedFields(Parameter.class);
}
 
源代码9 项目: neodymium-library   文件: ParameterStatement.java
@SuppressWarnings("unchecked")
@Override
public List<Object> createIterationData(TestClass testClass, FrameworkMethod method) throws Exception
{
    List<FrameworkMethod> parametersMethods = testClass.getAnnotatedMethods(Parameters.class);
    Iterable<Object> parameter = null;

    for (FrameworkMethod parametersMethod : parametersMethods)
    {
        if (parametersMethod.isPublic() && parametersMethod.isStatic())
        {
            // take the first public static method. invoke it and use the result as parameter
            try
            {
                Object parametersResult = parametersMethod.invokeExplosively(null);
                if (parametersResult instanceof Iterable)
                {
                    parameter = (Iterable<Object>) parametersResult;
                }
                else if (parametersResult instanceof Object[])
                {
                    parameter = Arrays.asList((Object[]) parametersResult);
                }
                else
                {
                    String msg = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.",
                                                      testClass.getJavaClass().getName(), parametersMethod.getName());
                    throw new Exception(msg);
                }
                break;
            }
            catch (Throwable e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    if (!parametersMethods.isEmpty() && parameter == null)
    {
        throw new Exception("No public static parameters method on class " + testClass.getJavaClass().getCanonicalName());
    }

    List<FrameworkField> parameterFrameworkFields = testClass.getAnnotatedFields(Parameter.class);
    LOGGER.debug("Found " + parameterFrameworkFields.size() + " parameter fields");

    List<Object> iterations = new LinkedList<>();

    if (parameter != null)
    {
        int parameterSetCounter = 0;
        for (Object para : parameter)
        {
            Object[] p;
            if (para instanceof Object[])
            {
                p = (Object[]) para;
            }
            else
            {
                p = new Object[]
                {
                  para
                };
            }

            iterations.add(new ParameterStatementData(parameterSetCounter, p, parameterFrameworkFields));
            parameterSetCounter++;
        }
    }

    return iterations;
}
 
private List<FrameworkField> getAnnotatedFieldsByParameter() {
    return getTestClass().getAnnotatedFields(Parameter.class);
}
 
private List<FrameworkField> getAnnotatedFieldsByParameter() {
    return getTestClass().getAnnotatedFields(Parameter.class);
}
 
 类所在包
 类方法
 同包方法