Search in sources :

Example 1 with Parameter

use of org.junit.runners.Parameterized.Parameter in project registry by hortonworks.

the class CustomParameterizedBlockJUnit4Runner method validateFields.

@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 + ")."));
            }
        }
    }
}
Also used : Parameter(org.junit.runners.Parameterized.Parameter) FrameworkField(org.junit.runners.model.FrameworkField) MultipleFailureException(org.junit.runners.model.MultipleFailureException)

Example 2 with Parameter

use of org.junit.runners.Parameterized.Parameter in project registry by hortonworks.

the class CustomParameterizedBlockJUnit4Runner method createTestUsingFieldInjection.

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;
}
Also used : Field(java.lang.reflect.Field) FrameworkField(org.junit.runners.model.FrameworkField) Parameter(org.junit.runners.Parameterized.Parameter) FrameworkField(org.junit.runners.model.FrameworkField) MultipleFailureException(org.junit.runners.model.MultipleFailureException)

Example 3 with Parameter

use of org.junit.runners.Parameterized.Parameter in project junit4 by junit-team.

the class BlockJUnit4ClassRunnerWithParameters method createTestUsingFieldInjection.

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 (IllegalAccessException e) {
            IllegalAccessException wrappedException = new IllegalAccessException("Cannot set parameter '" + field.getName() + "'. Ensure that the field '" + field.getName() + "' is public.");
            wrappedException.initCause(e);
            throw wrappedException;
        } 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;
}
Also used : Field(java.lang.reflect.Field) FrameworkField(org.junit.runners.model.FrameworkField) Parameter(org.junit.runners.Parameterized.Parameter) FrameworkField(org.junit.runners.model.FrameworkField)

Example 4 with Parameter

use of org.junit.runners.Parameterized.Parameter in project junit4 by junit-team.

the class BlockJUnit4ClassRunnerWithParameters method validateFields.

@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 + ")."));
            }
        }
    }
}
Also used : Parameter(org.junit.runners.Parameterized.Parameter) FrameworkField(org.junit.runners.model.FrameworkField)

Aggregations

Parameter (org.junit.runners.Parameterized.Parameter)4 FrameworkField (org.junit.runners.model.FrameworkField)4 Field (java.lang.reflect.Field)2 MultipleFailureException (org.junit.runners.model.MultipleFailureException)2