public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) { List<T> results = new ArrayList<T>(); for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { results.add(valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } return results; }
private AuthorizationRule getAuthorizationRule(TestClass testClass, Object target) throws IllegalAccessException { List<FrameworkField> ruleFields = testClass.getAnnotatedFields(Rule.class); for (FrameworkField ruleField : ruleFields) { if (ruleField.getType().isAssignableFrom(AuthorizationRule.class)) { return (AuthorizationRule) ruleField.get(target); } } throw new IllegalStateException("Test class must have AuthorizationRule set"); } }
private void prepareFixtures() throws Exception { checkForDuplicateFixtureFieldName(); InjectionContext context = getInjectionContext(); final List<FrameworkField> fieldsWithFixtures = this.getTestClass().getAnnotatedFields(Fixture.class); for (FrameworkField f : fieldsWithFixtures) { if (!f.isStatic()) { throw new IllegalArgumentException("Field " + f.getName() + " is a @Fixture, but is not declared static"); } f.getField().setAccessible(true); Object fixture = f.get(null); if (! (fixture instanceof TestFixture)) { throw new IllegalArgumentException("Field " + f.getName() + " is a @Fixture, but does not reflect a legal TestFixture"); } context.injectMembers(fixture); final String fixtureKey = getFixtureKey(f.getField().getDeclaringClass(), f.getName()); fixtures.put(fixtureKey, (TestFixture) fixture); } }
Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) results.add(valueClass.cast(fieldValue));
/******************************************************************************/ @SuppressWarnings("unchecked") protected List<P> getParamValues() { List<Pair<ParamType, Object>> list = new ArrayList<>(); getTestClass().getAnnotatedFields(xdean.junit.ex.param.annotation.Param.class) .forEach(f -> list.add(Pair.of(f.getAnnotation(xdean.junit.ex.param.annotation.Param.class).value(), uncheck(() -> f.get(null))))); getTestClass().getAnnotatedMethods(xdean.junit.ex.param.annotation.Param.class) .forEach(m -> list.add(Pair.of(m.getAnnotation(xdean.junit.ex.param.annotation.Param.class).value(), invoke(m)))); return list.stream().flatMap(pair -> { ParamType type = pair.getLeft(); Object value = pair.getRight(); switch (type) { case VALUE: return Stream.of((P) value); case ARRAY: return Stream.of((P[]) PrimitiveTypeUtil.toWrapperArray(value)); case LIST: return ((List<P>) value).stream(); case UNDEFINED: default: throw new Error("Never happened"); } }).collect(Collectors.toList()); }
Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) results.add(valueClass.cast(fieldValue));
/** * Finds the fields annotated with the specified annotation and having the specified type, * retrieves the values and passes those to the specified consumer. * * @since 4.13 */ public <T> void collectAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass, MemberValueConsumer<T> consumer) { for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { consumer.accept(each, valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } }
public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) { List<T> results = new ArrayList<T>(); for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { results.add(valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } return results; }
public Object get(Object target) throws IllegalArgumentException, IllegalAccessException { return extensionField.get(target); }
/** * method taken as is from BlockJUnit4ClassRunner 4.7 in order to preserve * its functionality over following versions */ private MethodRule createRule(Object test, FrameworkField each) { try { return (MethodRule) each.get(test); } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?"); } }
public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) { List<T> results = new ArrayList<T>(); for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { results.add(valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } return results; }
public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) { List<T> results = new ArrayList<T>(); for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { results.add(valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } return results; }
public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) { List<T> results = new ArrayList<T>(); for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { results.add(valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } return results; }
public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) { List<T> results = new ArrayList<T>(); for (FrameworkField each : getAnnotatedFields(annotationClass)) { try { Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) { results.add(valueClass.cast(fieldValue)); } } catch (IllegalAccessException e) { throw new RuntimeException( "How did getFields return a field we couldn't access?", e); } } return results; }
private List<Object> findInjectableRules(Class<? extends Annotation> ruleClass, Object instance) throws Exception { List<Object> injectableRules = newArrayList(); for(FrameworkField f : getTestClass().getAnnotatedFields(ruleClass)) { Object ruleInstance = f.get(instance); if (ruleInstance instanceof RuleChain) { injectableRules.addAll(findInjectableRulesInChain((RuleChain)ruleInstance)); } else { if(ruleInstance.getClass().isAnnotationPresent(InjectableRule.class)) { injectableRules.add(ruleInstance); } } } return injectableRules; }