Search in sources :

Example 6 with FrameworkField

use of org.junit.runners.model.FrameworkField in project junit4 by junit-team.

the class AllMembersSupplier method getSingleDataPointFields.

protected Collection<Field> getSingleDataPointFields(ParameterSignature sig) {
    List<FrameworkField> fields = clazz.getAnnotatedFields(DataPoint.class);
    Collection<Field> validFields = new ArrayList<Field>();
    for (FrameworkField frameworkField : fields) {
        validFields.add(frameworkField.getField());
    }
    return validFields;
}
Also used : Field(java.lang.reflect.Field) FrameworkField(org.junit.runners.model.FrameworkField) ArrayList(java.util.ArrayList) FrameworkField(org.junit.runners.model.FrameworkField)

Example 7 with FrameworkField

use of org.junit.runners.model.FrameworkField in project hazelcast by hazelcast.

the class AbstractParameterizedHazelcastClassRunner method createTestUsingFieldInjection.

private Object createTestUsingFieldInjection() throws Exception {
    List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
    if (annotatedFieldsByParameter.size() != fParameters.length) {
        throw new Exception("Wrong number of parameters and @Parameter fields." + " @Parameter fields counted: " + annotatedFieldsByParameter.size() + ", available parameters: " + fParameters.length + ".");
    }
    Object testClassInstance = getTestClass().getJavaClass().newInstance();
    for (FrameworkField each : annotatedFieldsByParameter) {
        Field field = each.getField();
        Parameterized.Parameter annotation = field.getAnnotation(Parameterized.Parameter.class);
        int index = annotation.value();
        try {
            field.set(testClassInstance, fParameters[index]);
        } catch (IllegalArgumentException iare) {
            throw new Exception(getTestClass().getName() + ": Trying to set " + field.getName() + " with the value " + fParameters[index] + " that is not the right type (" + fParameters[index].getClass().getSimpleName() + " instead of " + field.getType().getSimpleName() + ").", iare);
        }
    }
    return testClassInstance;
}
Also used : Field(java.lang.reflect.Field) FrameworkField(org.junit.runners.model.FrameworkField) Parameterized(org.junit.runners.Parameterized) FrameworkField(org.junit.runners.model.FrameworkField)

Example 8 with FrameworkField

use of org.junit.runners.model.FrameworkField in project randomizedtesting by randomizedtesting.

the class RandomizedRunner method getAnnotatedFieldValues.

/*
   * We're using JUnit infrastructure here, but provide constant 
   * ordering of the result. The returned list has class...super order.
   */
private <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) {
    TestClass info = AccessController.doPrivileged(new PrivilegedAction<TestClass>() {

        @Override
        public TestClass run() {
            return new TestClass(suiteClass);
        }
    });
    List<T> results = new ArrayList<T>();
    List<FrameworkField> annotatedFields = new ArrayList<FrameworkField>(info.getAnnotatedFields(annotationClass));
    // Split fields by class
    final HashMap<Class<?>, List<FrameworkField>> byClass = new HashMap<Class<?>, List<FrameworkField>>();
    for (FrameworkField field : annotatedFields) {
        Class<?> clz = field.getField().getDeclaringClass();
        if (!byClass.containsKey(clz)) {
            byClass.put(clz, new ArrayList<FrameworkField>());
        }
        byClass.get(clz).add(field);
    }
    // Consistent order at class level.
    for (List<FrameworkField> fields : byClass.values()) {
        Collections.sort(fields, new Comparator<FrameworkField>() {

            @Override
            public int compare(FrameworkField o1, FrameworkField o2) {
                return o1.getField().getName().compareTo(o2.getField().getName());
            }
        });
        Collections.shuffle(fields, new Random(runnerRandomness.getSeed()));
    }
    annotatedFields.clear();
    for (Class<?> clz = suiteClass; clz != null; clz = clz.getSuperclass()) {
        List<FrameworkField> clzFields = byClass.get(clz);
        if (clzFields != null) {
            annotatedFields.addAll(clzFields);
        }
    }
    for (FrameworkField each : annotatedFields) {
        try {
            Object fieldValue = each.get(test);
            if (valueClass.isInstance(fieldValue))
                results.add(valueClass.cast(fieldValue));
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    return results;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TestClass(org.junit.runners.model.TestClass) Random(java.util.Random) TestClass(org.junit.runners.model.TestClass) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) List(java.util.List) ArrayList(java.util.ArrayList) FrameworkField(org.junit.runners.model.FrameworkField)

Example 9 with FrameworkField

use of org.junit.runners.model.FrameworkField in project alchemy-test by SirWellington.

the class TestClassInjectors method populateGeneratedFields.

static void populateGeneratedFields(TestClass testClass, Object target) throws IllegalArgumentException, IllegalAccessException {
    //@GenerateString
    List<FrameworkField> stringGeneratedFields = testClass.getAnnotatedFields(GenerateString.class);
    for (FrameworkField field : stringGeneratedFields) {
        inflateString(field, target);
    }
    //@GenerateBoolean
    List<FrameworkField> booleanGeneratedFields = testClass.getAnnotatedFields(GenerateBoolean.class);
    for (FrameworkField field : booleanGeneratedFields) {
        inflateBoolean(field, target);
    }
    //@GenerateInteger
    List<FrameworkField> integerGeneratedFields = testClass.getAnnotatedFields(GenerateInteger.class);
    for (FrameworkField field : integerGeneratedFields) {
        inflateInteger(field, target);
    }
    //@GenerateLong
    List<FrameworkField> longGeneratedFields = testClass.getAnnotatedFields(GenerateLong.class);
    for (FrameworkField field : longGeneratedFields) {
        inflateLong(field, target);
    }
    //@GenerateDouble
    List<FrameworkField> doubleGeneratedFields = testClass.getAnnotatedFields(GenerateDouble.class);
    for (FrameworkField field : doubleGeneratedFields) {
        inflateDouble(field, target);
    }
    //@GenerateDate
    List<FrameworkField> dateGeneratedFields = testClass.getAnnotatedFields(GenerateDate.class);
    for (FrameworkField field : dateGeneratedFields) {
        inflateDate(field, target);
    }
    //@GenerateInstant
    List<FrameworkField> instantGeneratedFields = testClass.getAnnotatedFields(GenerateInstant.class);
    for (FrameworkField field : instantGeneratedFields) {
        inflateInstant(field, target);
    }
    //@GenerateURL
    List<FrameworkField> urlGeneratedFields = testClass.getAnnotatedFields(GenerateURL.class);
    for (FrameworkField field : urlGeneratedFields) {
        inflateUrl(field, target);
    }
    //@GeneratePojo
    List<FrameworkField> pojoGeneratedFields = testClass.getAnnotatedFields(GeneratePojo.class);
    for (FrameworkField field : pojoGeneratedFields) {
        inflatePojo(field, target);
    }
    //@GenerateEnum
    List<FrameworkField> enumGeneratedFields = testClass.getAnnotatedFields(GenerateEnum.class);
    for (FrameworkField field : enumGeneratedFields) {
        inflateEnum(field, target);
    }
    //@GenerateList
    List<FrameworkField> listGeneratedfields = testClass.getAnnotatedFields(GenerateList.class);
    for (FrameworkField field : listGeneratedfields) {
        inflateList(field, target);
    }
}
Also used : FrameworkField(org.junit.runners.model.FrameworkField)

Aggregations

FrameworkField (org.junit.runners.model.FrameworkField)9 Field (java.lang.reflect.Field)5 ArrayList (java.util.ArrayList)4 Parameter (org.junit.runners.Parameterized.Parameter)2 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Random (java.util.Random)1 AfterClass (org.junit.AfterClass)1 BeforeClass (org.junit.BeforeClass)1 Parameterized (org.junit.runners.Parameterized)1 TestClass (org.junit.runners.model.TestClass)1