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",
protected void validateTestTarget(final List<Throwable> errors) { final List<FrameworkField> annotatedFields = testClass.getAnnotatedFields(TestTarget.class); if (annotatedFields.size() != 1) { errors.add(new Exception("Test class should have exactly one field annotated with " + TestTarget.class.getName())); } else if (!Target.class.isAssignableFrom(annotatedFields.get(0).getType())) { errors.add(new Exception("Field annotated with " + TestTarget.class.getName() + " should implement " + Target.class.getName() + " interface")); } }
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"); } }
@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; } }
public JpaUnitRunner(final Class<?> clazz) throws InitializationError { super(clazz); executor = new DecoratorExecutor(); final List<FrameworkField> ruleFields = getTestClass().getAnnotatedFields(Rule.class); if (ruleFields.stream().anyMatch(f -> f.getType().equals(JpaUnitRule.class))) { throw new InitializationError("JpaUnitRunner and JpaUnitRule exclude each other"); } }
/** * Registers all mocks in the injector. Note that null values or multiple fields of the same type * will cause the injector to throw an exception. * * @param injector the injector to use */ private void registerAllMocks(Injector injector) { for (FrameworkField frameworkField : testClass.getAnnotatedFields(Mock.class)) { // Unchecked so we don't need to cast the field's value... Class clazz = frameworkField.getType(); injector.register(clazz, ReflectionUtils.getFieldValue(frameworkField.getField(), target)); } fieldsToInject = new HashSet<>(); for (FrameworkField frameworkField : testClass.getAnnotatedFields(InjectDelayed.class)) { fieldsToInject.add(frameworkField.getType()); } }
@Override protected void validateFields(List<Throwable> errors) { super.validateFields(errors); if (fieldAnnotated()) { List<FrameworkField> configFields = getFieldsAnnotatedByKiWiConfig(); for (FrameworkField field : configFields) { if (!field.getType().isAssignableFrom(KiWiConfiguration.class)) { errors.add(new Exception(String.format("Invalid type %s for field %s, must be %s", field.getType().getName(), field.getName(), KiWiConfiguration.class.getSimpleName()))); } } } }
@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() + "'")); } } } }
private static void inflateList(FrameworkField field, Object target) throws IllegalArgumentException, IllegalAccessException { Class<?> typeOfField = field.getType(); checkThat(List.class.isAssignableFrom(typeOfField), "@GenerateList can only be used on List Types"); GenerateList annotation = field.getAnnotation(GenerateList.class); AlchemyGenerator<?> generator = GenerateList.Values.createGeneratorFor(annotation); Object value = generator.get(); inflate(field, target, value); }
private static void inflateEnum(FrameworkField field, Object target) throws IllegalArgumentException, IllegalAccessException { Class<?> typeOfField = field.getType(); checkThat(typeOfField.isEnum(), "@GenerateEnum can only be used on Enum Types"); Class<? extends Enum> typeOfEnum = (Class<? extends Enum>) typeOfField; GenerateEnum annotation = field.getAnnotation(GenerateEnum.class); AlchemyGenerator<?> generator = GenerateEnum.Values.createGeneratorFor(annotation, typeOfEnum); Object value = generator.get(); inflate(field, target, value); }
@Override public void onBeforeBefore(final TestClass testClass, final FrameworkMethod method, final Object target) { List<FrameworkField> mockFields = testClass.getAnnotatedFields(Mock.class); for (FrameworkField mockField : mockFields) { Object mock = mocker.mock(mockField.getType()); ReflectionUtils.setFieldValue(mockField.getField(), target, mock); } } }
private static void inflatePojo(FrameworkField field, Object target) throws IllegalArgumentException, IllegalAccessException { Class<?> typeOfPojo = field.getType(); GeneratePojo annotation = field.getAnnotation(GeneratePojo.class); AlchemyGenerator<?> generator = GeneratePojo.Values.createGeneratorFor(annotation, typeOfPojo); Object value = generator.get(); inflate(field, target, value); }
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)); }
private static void inflateDate(FrameworkField field, Object target) throws IllegalArgumentException, IllegalAccessException { GenerateDate annotation = field.getAnnotation(GenerateDate.class); checkNotNull(annotation, "missing annotation"); AlchemyGenerator<Date> generator = GenerateDate.Values.createGeneratorFor(annotation); Object value; if (field.getType() == java.sql.Timestamp.class) { AlchemyGenerator<Timestamp> timestampGenerator = DateGeneratorsKt.asSqlTimestampGenerator(generator); value = timestampGenerator.get(); } else if (field.getType() == java.sql.Date.class) { AlchemyGenerator<java.sql.Date> sqlGenerator = DateGeneratorsKt.asSqlDateGenerator(generator); value = sqlGenerator.get(); } else { value = generator.get(); } inflate(field, target, value); }