org.junit.runners.Parameterized.Parameter Java Examples

The following examples show how to use org.junit.runners.Parameterized.Parameter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParameterStatement.java    From neodymium-library with MIT License 6 votes vote down vote up
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]);
    }
}
 
Example #2
Source File: BrowserVersionClassRunnerWithParameters.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@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 + ")."));
            }
        }
    }
}
 
Example #3
Source File: FrameworkMethodWithParameters.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@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);
}
 
Example #4
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 5 votes vote down vote up
@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 + ")."));
            }
        }
    }
}
 
Example #6
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 5 votes vote down vote up
@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 + ")."));
            }
        }
    }
}
 
Example #8
Source File: BrowserVersionClassRunnerWithParameters.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private List<FrameworkField> getAnnotatedFieldsByParameter() {
    return getTestClass().getAnnotatedFields(Parameter.class);
}
 
Example #9
Source File: ParameterStatement.java    From neodymium-library with MIT License 4 votes vote down vote up
@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;
}
 
Example #10
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 4 votes vote down vote up
private List<FrameworkField> getAnnotatedFieldsByParameter() {
    return getTestClass().getAnnotatedFields(Parameter.class);
}
 
Example #11
Source File: CustomParameterizedBlockJUnit4Runner.java    From registry with Apache License 2.0 4 votes vote down vote up
private List<FrameworkField> getAnnotatedFieldsByParameter() {
    return getTestClass().getAnnotatedFields(Parameter.class);
}