/** * @param testObject The test object, not null * @param testMethod The test method, not null * @return The {@link TransactionMode} for the given object */ protected TransactionMode getTransactionMode(Object testObject, Method testMethod) { TransactionMode transactionMode = getMethodOrClassLevelAnnotationProperty(Transactional.class, "value", DEFAULT, testMethod, testObject.getClass()); transactionMode = getEnumValueReplaceDefault(Transactional.class, "value", transactionMode, defaultAnnotationPropertyValues); return transactionMode; }
/** * Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target class. * The target is either an explicitly specified target field of the test, or the field that is annotated with * {@link TestedObject} * * @param test The instance to inject into, not null * @param fieldToAutoInjectStatic The field from which the value is injected into the target, not null */ protected void injectStaticByType(Object test, Field fieldToAutoInjectStatic) { InjectIntoStaticByType injectIntoStaticByTypeAnnotation = fieldToAutoInjectStatic.getAnnotation(InjectIntoStaticByType.class); Class<?>[] targetClass = injectIntoStaticByTypeAnnotation.target(); Object objectToInject = getObjectToInject(test, fieldToAutoInjectStatic); Type objectToInjectType = getObjectToInjectType(test, fieldToAutoInjectStatic); Class objectToInjectClass = getClassForType(objectToInjectType); Restore restore = getEnumValueReplaceDefault(InjectIntoStaticByType.class, "restore", injectIntoStaticByTypeAnnotation.restore(), defaultAnnotationPropertyValues); PropertyAccess propertyAccess = getEnumValueReplaceDefault(InjectIntoStaticByType.class, "propertyAccess", injectIntoStaticByTypeAnnotation.propertyAccess(), defaultAnnotationPropertyValues); try { for (Class<?> clzz : targetClass) { Object oldValue = InjectionUtils.injectIntoStaticByType(objectToInject, objectToInjectType, clzz, propertyAccess); storeValueToRestoreAfterTest(clzz, null, objectToInjectClass, propertyAccess, oldValue, restore); } } catch (UnitilsException e) { throw new UnitilsException(getSituatedErrorMessage(InjectIntoStaticByType.class, fieldToAutoInjectStatic, e.getMessage()), e); } }
/** * Injects the fieldToAutoInjectStatic into the specified target class. * * @param test Instance to inject into, not null * @param fieldToInjectStatic The field from which the value is injected into the target, not null */ protected void injectStatic(Object test, Field fieldToInjectStatic) { InjectIntoStatic injectIntoStaticAnnotation = fieldToInjectStatic.getAnnotation(InjectIntoStatic.class); Class<?>[] targetClass = injectIntoStaticAnnotation.target(); String property = injectIntoStaticAnnotation.property(); if (StringUtils.isEmpty(property)) { throw new UnitilsException(getSituatedErrorMessage(InjectIntoStatic.class, fieldToInjectStatic, "Property cannot be empty")); } Object objectToInject = getObjectToInject(test, fieldToInjectStatic); Restore restore = getEnumValueReplaceDefault(InjectIntoStatic.class, "restore", injectIntoStaticAnnotation.restore(), defaultAnnotationPropertyValues); try { for (Class<?> clzz : targetClass) { Object oldValue = InjectionUtils.injectIntoStatic(objectToInject, clzz, property); storeValueToRestoreAfterTest(clzz, property, fieldToInjectStatic.getType(), null, oldValue, restore); } } catch (UnitilsException e) { throw new UnitilsException(getSituatedErrorMessage(InjectIntoStatic.class, fieldToInjectStatic, e.getMessage()), e); } }
/** * Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target. * The target is either an explicitly specified target field of the test, or the field(s) that is/are annotated with * {@link TestedObject} * * @param test The instance to inject into, not null * @param fieldToInject The field from which the value is injected into the target, not null */ protected void injectByType(Object test, Field fieldToInject) { InjectIntoByType injectIntoByTypeAnnotation = fieldToInject.getAnnotation(InjectIntoByType.class); Object objectToInject = getObjectToInject(test, fieldToInject); Type objectToInjectType = getObjectToInjectType(test, fieldToInject); PropertyAccess propertyAccess = getEnumValueReplaceDefault(InjectIntoByType.class, "propertyAccess", injectIntoByTypeAnnotation.propertyAccess(), defaultAnnotationPropertyValues); List<Object> targets = getTargets(InjectIntoByType.class, fieldToInject, injectIntoByTypeAnnotation.target(), test); if (targets.size() == 0) { throw new UnitilsException(getSituatedErrorMessage(InjectIntoByType.class, fieldToInject, "The target should either be specified explicitly using the target property, or by using the @" + TestedObject.class.getSimpleName() + " annotation")); } for (Object target : targets) { try { InjectionUtils.injectIntoByType(objectToInject, objectToInjectType, target, propertyAccess); } catch (UnitilsException e) { throw new UnitilsException(getSituatedErrorMessage(InjectIntoByType.class, fieldToInject, e.getMessage()), e); } } }