/** * Initializes the DbUnitModule using the given Configuration * * @param configuration The config, not null */ @Override @SuppressWarnings("unchecked") public void init(Properties configuration) { this.configuration = configuration; defaultAnnotationPropertyValues = getAnnotationPropertyDefaults(DbUnitModule.class, configuration, DataSet.class, ExpectedDataSet.class); }
/** * @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; }
/** * @return The default {@link DataSetFactory} class as configured in unitils */ protected DataSetFactory getDefaultDataSetFactory() { Class<? extends DataSetFactory> dataSetFactoryClass = getClassWithName(getAnnotationPropertyDefault(DbUnitModule.class, DataSet.class, "factory", configuration)); return getDataSetFactory(dataSetFactoryClass); }
/** * Get the configured DataSetFactory for the given method * * @param annotationClass The class of the annotation, i.e. DataSet.class or ExpectedDataSet.class * @param testMethod The method for which we need the configured DataSetFactory * @param testClass The class that is looked for class-level annotations * @return The configured DataSetFactory */ @SuppressWarnings("unchecked") protected DataSetFactory getDataSetFactory(Class<? extends Annotation> annotationClass, Method testMethod, Class<?> testClass) { Class<? extends DataSetFactory> dataSetFactoryClass = getMethodOrClassLevelAnnotationProperty(annotationClass, "factory", DataSetFactory.class, testMethod, testClass); dataSetFactoryClass = (Class<? extends DataSetFactory>) getClassValueReplaceDefault(annotationClass, "factory", dataSetFactoryClass, defaultAnnotationPropertyValues, DataSetFactory.class); return getDataSetFactory(dataSetFactoryClass); }
/** * Creates the DbUnit dataset operation for loading a data set for the given method. If a value for loadStrategy is found on an * annotation, this class is used, otherwise the configured default class will be used. * * @param testMethod The method, not null * @param testClass The test class, not null * @return The DbUnit operation, not null */ @SuppressWarnings({ "unchecked" }) protected DataSetLoadStrategy getDataSetLoadStrategy(Method testMethod, Class<?> testClass, DataSet dataSet) { Class<? extends DataSetLoadStrategy> dataSetOperationClass = dataSet.loadStrategy(); dataSetOperationClass = (Class<? extends DataSetLoadStrategy>) getClassValueReplaceDefault(DataSet.class, "loadStrategy", dataSetOperationClass, defaultAnnotationPropertyValues, DataSetLoadStrategy.class); return createInstanceOfType(dataSetOperationClass, false); }
/** * Initializes this module using the given configuration. * * @param configuration The configuration, not null */ @Override public void init(Properties configuration) { defaultAnnotationPropertyValues = getAnnotationPropertyDefaults(InjectModule.class, configuration, InjectInto.class, InjectIntoStatic.class, InjectIntoByType.class, InjectIntoStaticByType.class); createTestedObjectsIfNullEnabled = PropertyUtils.getBoolean(PROPKEY_CREATE_TESTEDOBJECTS_IF_NULL_ENABLED, configuration); }
/** * 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); } }
/** * @return The default {@link DataSetLoadStrategy} class as configured in unitils */ protected DataSetLoadStrategy getDefaultDataSetLoadStrategy() { Class<? extends DataSetLoadStrategy> dataSetLoadStrategyClassName = getClassWithName(getAnnotationPropertyDefault(DbUnitModule.class, DataSet.class, "loadStrategy", configuration)); return createInstanceOfType(dataSetLoadStrategyClassName, false); }
/** * Initializes this module using the given <code>Configuration</code> * * @param configuration The config, not null */ @SuppressWarnings("unchecked") public void init(Properties configuration) { this.configuration = configuration; DatabaseConfigurationsFactory configFactory = new DatabaseConfigurationsFactory(new Configuration(configuration)); databaseConfigurations = configFactory.create(); defaultAnnotationPropertyValues = getAnnotationPropertyDefaults(DatabaseModule.class, configuration, Transactional.class); updateDatabaseSchemaEnabled = PropertyUtils.getBoolean(PROPERTY_UPDATEDATABASESCHEMA_ENABLED, configuration); wrapDataSourceInTransactionalProxy = PropertyUtils.getBoolean(PROPERTY_WRAP_DATASOURCE_IN_TRANSACTIONAL_PROXY, configuration); PlatformTransactionManager.class.getName(); }
/** * 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); } } }