Refine search
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(); Parameter annotation = field.getAnnotation(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; }
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; }
this.superDescription = superDescription; this.features = features; List<FrameworkField> testerFields = testClass.get().getAnnotatedFields(Shared.class); if (testerFields.isEmpty()) { throw new InitializationError("No public @Shared field found"); if (!frameworkField.isPublic()) { throw new InitializationError("@Shared field " + frameworkField + " must be public"); if (!frameworkField.getType().isAssignableFrom(BehaviorTester.class)) { throw new InitializationError(String.format( "@Shared field %s must be of type %s", BehaviorTester.class.getSimpleName())); testerField = frameworkField.getField(); introspection = descriptionFactory.apply(testClass.get().getJavaClass(), "Introspect");
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; }
private Set<ArchTestExecution> findArchRulesIn(FrameworkField ruleField) { boolean ignore = elementShouldBeIgnored(ruleField.getField()); if (ruleField.getType() == ArchRules.class) { return asTestExecutions(getArchRules(ruleField.getField()), ignore); } return Collections.<ArchTestExecution>singleton(new ArchRuleExecution(getTestClass().getJavaClass(), ruleField.getField(), ignore)); }
getTestClass().getAnnotatedMethods(xdean.junit.ex.param.annotation.Param.class).forEach( m -> { Method method = m.getMethod(); getTestClass().getAnnotatedFields(xdean.junit.ex.param.annotation.Param.class).forEach( f -> { Field field = f.getField(); if (!Modifier.isStatic(field.getModifiers())) { errors.add(new Exception("Param Getter Field " + f.getName() + " should be static.")); errors.add(new Exception("Param Getter Field " + f.getName() + " should be public.")); errors.add(new Exception("Param Getter Field " + f.getName() + " should be final.")); xdean.junit.ex.param.annotation.Param anno = f.getAnnotation(xdean.junit.ex.param.annotation.Param.class); if (!isParamProviderType(anno, field.getGenericType())) { errors.add(new Exception("Param Getter Field " + f.getName() + " should be " + anno.value().getParamString(getParamType()) + "."));
/******************************************************************************/ @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()); }
@Override public String getName() { return getField().getName(); }
@Override protected Object createTest() throws Exception { if (fieldAnnotated()) { Object testInstance = getTestClass().getOnlyConstructor().newInstance(); List<FrameworkField> configFields = getFieldsAnnotatedByKiWiConfig(); for (FrameworkField field : configFields) { try { field.getField().set(testInstance, config); } catch (IllegalArgumentException iae) { throw new Exception(getTestClass().getName() + ": Trying to set " + field.getName() + " that has a wrong type."); } } return testInstance; } return getTestClass().getOnlyConstructor().newInstance(config); }
@Nullable private Object resolveByAnnotation(Class<? extends Annotation> annotation, Class<?> type) { if (!ignoredAnnotations.contains(annotation)) { List<FrameworkField> fields = testClass.getAnnotatedFields(annotation); for (FrameworkField field : fields) { if (type.isAssignableFrom(field.getType())) { return ReflectionUtils.getFieldValue(field.getField(), target); } } } return null; } }
new ArrayList<FrameworkField>(info.getAnnotatedFields(annotationClass)); new HashMap<Class<?>, List<FrameworkField>>(); for (FrameworkField field : annotatedFields) { Class<?> clz = field.getField().getDeclaringClass(); if (!byClass.containsKey(clz)) { byClass.put(clz, new ArrayList<FrameworkField>()); Object fieldValue = each.get(test); if (valueClass.isInstance(fieldValue)) results.add(valueClass.cast(fieldValue));
static boolean hasAnyArquillianRule(org.junit.runners.model.TestClass testClass) { for (FrameworkField field : testClass.getAnnotatedFields()) { if (ArquillianTestClass.class.equals(field.getType()) || ArquillianTest.class.equals(field.getType())) { return true; } } return false; }
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); } }
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"); } }
/** * Creates a {@code TestClass} wrapping {@code klass}. Each time this * constructor executes, the class is scanned for annotations, which can be * an expensive process (we hope in future JDK's it will not be.) Therefore, * try to share instances of {@code TestClass} where possible. */ public TestClass(Class<?> klass) { fClass = klass; if (klass != null && klass.getConstructors().length > 1) { throw new IllegalArgumentException( "Test class can only have one constructor"); } for (Class<?> eachClass : getSuperClasses(fClass)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), fMethodsForAnnotations); } for (Field eachField : eachClass.getDeclaredFields()) { addToAnnotationLists(new FrameworkField(eachField), fFieldsForAnnotations); } } }
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) { for (Class<?> eachClass : getSuperClasses(clazz)) { for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) { addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations); } // ensuring fields are sorted to make sure that entries are inserted // and read from fieldForAnnotations in a deterministic order for (Field eachField : getSortedDeclaredFields(eachClass)) { addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations); } } }
private static List<GeoWaveStoreRunnerConfig> addRunnerConfigsForField( final FrameworkField field, final List<GeoWaveStoreRunnerConfig> currentConfigs, final Set<GeoWaveStoreType> storeTypes) throws GeoWaveITException { final GeoWaveTestStore store = field.getField().getAnnotation(GeoWaveTestStore.class); final GeoWaveStoreType[] types = store.value(); if ((types == null) || (types.length == 0)) { throw new GeoWaveITException( MessageFormat.format("{0} must have at least one GeoWaveStoreType", field.getName())); } final List<GeoWaveStoreRunnerConfig> newConfigs = new ArrayList<GeoWaveStoreRunnerConfig>(); for (final GeoWaveStoreRunnerConfig config : currentConfigs) { for (final GeoWaveStoreType type : types) { newConfigs.add(new GeoWaveStoreRunnerConfig(config, field.getName(), type)); storeTypes.add(type); } } return newConfigs; }
@Override protected void validateFields(final List<Throwable> errors) { super.validateFields(errors); if (typeIsAnnotated()) { if (fieldsAreAnnotated()) { errors.add( new GeoWaveITException( "Only type or fields can be annotated with @GeoWaveTestStore, not both")); } try { getDataStoreOptionFieldsForTypeAnnotation(); } catch (final Exception e) { errors.add(e); } } else if (fieldsAreAnnotated()) { final List<FrameworkField> annotatedFields = getStoreAnnotatedFields(); for (final FrameworkField field : annotatedFields) { if (!field.getType().isAssignableFrom(DataStorePluginOptions.class)) { errors.add( new GeoWaveITException( "'" + field.getName() + "' must be of type '" + DataStorePluginOptions.class.getName() + "'")); } } } }