/** * Injects all fields that are annotated with {@link InjectIntoStatic}. * * @param test The instance to inject into, not null */ public void injectAllStatic(Object test) { Set<Field> fields = getFieldsAnnotatedWith(test.getClass(), InjectIntoStatic.class); for (Field field : fields) { injectStatic(test, field); } }
/** * Injects the currently active JPA <code>EntityManager</code> into all fields and methods that are * annotated with <code>javax.persistence.PersistenceContext</code> * * @param testObject The test object, not null */ public void injectEntityManager(Object testObject, Object target) { Set<Field> fields = getFieldsAnnotatedWith(target.getClass(), PersistenceContext.class); Set<Method> methods = getMethodsAnnotatedWith(target.getClass(), PersistenceContext.class); if (fields.isEmpty() && methods.isEmpty()) { // Jump out to make sure that we don't try to instantiate the EntityManagerFactory return; } EntityManager entityManager = getPersistenceContext(testObject); setFieldAndSetterValue(target, fields, methods, entityManager); }
/** * Compares the contents of the expected DbUnitDataSet with the contents of the database. Only the tables and columns that occur in the * expected DbUnitDataSet are compared with the database contents. * * @param testMethod The test method, not null * @param testObject The test object, not null */ public void assertDbContentAsExpected(Method testMethod, Object testObject) { Class<?> testClass = testObject.getClass(); ExpectedDataSets expectedsDataSetAnnotation = getMethodOrClassLevelAnnotation(ExpectedDataSets.class, testMethod, testClass); if (expectedsDataSetAnnotation != null) { assertMultipleExpectedDataSets(expectedsDataSetAnnotation, testObject, testMethod); } ExpectedDataSet expectedDataSetAnnotation = getMethodOrClassLevelAnnotation(ExpectedDataSet.class, testMethod, testClass); if (expectedDataSetAnnotation != null) { assertExpectedDataSets(expectedDataSetAnnotation, testObject, testMethod); } }
/** * @param testClass The test class, not null * @return The test class's custom configuration method, if any */ protected Method getCustomConfigMethod(Class<?> testClass) { Set<Method> annotatedMethods = AnnotationUtils.getMethodsAnnotatedWith(testClass, JpaEntityManagerFactory.class); for (Method annotatedMethod : annotatedMethods) { if (isCustomConfigMethod(annotatedMethod)) { return annotatedMethod; } } return null; }
/** * @see org.unitils.orm.common.OrmModule#getDatabaseName(java.lang.Object, java.lang.reflect.Method) */ @Override protected void getDatabaseName(Object testObject, Method testMethod) { //List<String> dataSources = new ArrayList<String>(); HibernateSessionFactory dataSource = AnnotationUtils.getMethodOrClassLevelAnnotation(HibernateSessionFactory.class, testMethod, testObject.getClass()); if (dataSource != null) { wrappers.add(getDatabaseModule().getWrapper(dataSource.databaseName())); //dataSources.add(dataSource.databaseName()); } Set<HibernateSessionFactory> lstDataSources = AnnotationUtils.getFieldLevelAnnotations(testObject.getClass(), HibernateSessionFactory.class); if (!lstDataSources.isEmpty()) { for (HibernateSessionFactory testDataSource : lstDataSources) { //ataSources.add(testDataSource.databaseName()); wrappers.add(getDatabaseModule().getWrapper(testDataSource.databaseName())); } } //return dataSources; }
/** * @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; }
public static void injectIntoAnnotatedMethods(Object objectToInject, Object target, Class<? extends Annotation> annotation) { Set<Method> annotatedMethods = AnnotationUtils.getMethodsAnnotatedWith(target.getClass(), annotation); for (Method annotatedMethod : annotatedMethods) { try { annotatedMethod.invoke(target, objectToInject); } catch (IllegalArgumentException e) { throw new UnitilsException("Method " + annotatedMethod.getName() + " annotated with " + annotation.getName() + " must have exactly one argument with a type equal to or a superclass / implemented interface of " + objectToInject.getClass().getSimpleName()); } catch (IllegalAccessException e) { throw new UnitilsException("Unable to inject value into following method annotated with " + annotation.getName() + ": " + annotatedMethod.getName(), e); } catch (InvocationTargetException e) { throw new UnitilsException("Unable to inject value into following method annotated with " + annotation.getName() + ": " + annotatedMethod.getName(), e); } } }
/** * @see org.unitils.orm.common.OrmModule#getDatabaseName(java.lang.Object, java.lang.reflect.Method) */ @Override protected void getDatabaseName(Object testObject, Method testMethod) { JpaEntityManagerFactory dataSource = AnnotationUtils.getMethodOrClassLevelAnnotation(JpaEntityManagerFactory.class, testMethod, testObject.getClass()); if (dataSource != null) { wrappers.add(getDatabaseModule().getWrapper(dataSource.databaseName())); } Set<JpaEntityManagerFactory> lstDataSources = AnnotationUtils.getFieldLevelAnnotations(testObject.getClass(), JpaEntityManagerFactory.class); if (!lstDataSources.isEmpty()) { for (JpaEntityManagerFactory testDataSource : lstDataSources) { wrappers.add(getDatabaseModule().getWrapper(testDataSource.databaseName())); } } } }
/** * 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); }
@Override public void afterTestMethod(Object testObject, Method testMethod, Throwable t) { if (!cleanupAfterTest) { return; } Set<Field> tmpFileFields = getFieldsAnnotatedWith(testObject.getClass(), TempFile.class); for (Field field : tmpFileFields) { deleteTempFileForField(testObject, field); } }
/** * Injects the JPA <code>EntityManagerFactory</code> into all fields and methods that are * annotated with <code>javax.persistence.PersistenceUnit</code> * * @param testObject The test object, not null */ public void injectEntityManagerFactory(Object testObject, Object target) { Set<Field> fields = getFieldsAnnotatedWith(target.getClass(), PersistenceUnit.class); Set<Method> methods = getMethodsAnnotatedWith(target.getClass(), PersistenceUnit.class); if (fields.isEmpty() && methods.isEmpty()) { // Jump out to make sure that we don't try to instantiate the EntityManagerFactory return; } EntityManagerFactory entityManagerFactory = getPersistenceUnit(testObject); setFieldAndSetterValue(target, fields, methods, entityManagerFactory); }
/** * Calls all {@link AfterCreateMock} annotated methods on the test, passing the given mock. * These annotated methods must have following signature <code>void myMethod(Object mock, String name, Class type)</code>. * If this is not the case, a runtime exception is called. * * @param testObject the test, not null * @param mockObject the mock, not null * @param name the field(=mock) name, not null */ protected void callAfterCreateMockMethods(Object testObject, Mock<?> mockObject, String name) { Set<Method> methods = getMethodsAnnotatedWith(testObject.getClass(), AfterCreateMock.class); for (Method method : methods) { try { invokeMethod(testObject, method, mockObject, name, ((MockObject<?>) mockObject).getMockedType()); } catch (InvocationTargetException e) { throw new UnitilsException("An exception occurred while invoking an after create mock method.", e); } catch (Exception e) { throw new UnitilsException("Unable to invoke after create mock method. Ensure that this method has following signature: void myMethod(Object mock, String name, Class type)", e); } } }
/** * This method will first try to load a method level defined dataset. If no such file exists, a class level defined dataset will be * loaded. If neither of these files exist, nothing is done. The name of the test data file at both method level and class level can be * overridden using the {@link DataSet} annotation. If specified using this annotation but not found, a {@link UnitilsException} is * thrown. * * @param testMethod The method, not null * @param testObject The test object, not null */ public void insertDataSet(Method testMethod, Object testObject) { Class<?> testClass = testObject.getClass(); try { DataSets dataSetsAnnotation = getMethodOrClassLevelAnnotation(DataSets.class, testMethod, testClass); if (dataSetsAnnotation != null) { insertDataSets(dataSetsAnnotation, testObject, testMethod); } DataSet dataSetAnnotation = getMethodOrClassLevelAnnotation(DataSet.class, testMethod, testClass); if (dataSetAnnotation != null) { insertDataSet(dataSetAnnotation, testObject, testMethod); } } catch (Exception e) { throw new UnitilsException("Error inserting test data from DbUnit dataset for method " + testMethod, e); } finally { closeJdbcConnection(); } }
@Override public void beforeTestSetUp(Object testObject, Method testMethod) { Set<Field> tmpFileFields = getFieldsAnnotatedWith(testObject.getClass(), TempDir.class); for (Field field : tmpFileFields) { createTempDirForField(testObject, testMethod, field); } }
/** * Assigns the <code>TestDataSource</code> to every field annotated with * {@link TestDataSource} and calls all methods annotated with * {@link TestDataSource} * * @param testObject The test instance, not null * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public void injectDataSource(Object testObject) { Set<Field> fields = getFieldsAnnotatedWith(testObject.getClass(), TestDataSource.class); Set<Method> methods = getMethodsAnnotatedWith(testObject.getClass(), TestDataSource.class); Map<String, DataSource> mapDatasources = new HashMap<String, DataSource>(); //update all databases for (Entry<String, DataSourceWrapper> wrapper : wrappers.entrySet()) { DataSource dataSource2 = getDataSource(wrapper.getKey(), mapDatasources, testObject); //look if datasource is needed in test. setFieldDataSource(wrapper.getKey(), dataSource2, testObject, fields, methods); } }
/** * Auto-injects all fields that are annotated with {@link InjectIntoByType} * * @param test The instance to inject into, not null */ public void injectAllByType(Object test) { Set<Field> fields = getFieldsAnnotatedWith(test.getClass(), InjectIntoByType.class); for (Field field : fields) { injectByType(test, field); } }
/** * Injects the persistence unit object into all fields and methods that are annotated with the annotation * defined by {@link #getPersistenceUnitConfigAnnotationClass()} * * @param testObject The test object, not null */ public void injectOrmPersistenceUnitIntoTestObject(Object testObject) { Set<Field> fields = getFieldsAnnotatedWith(testObject.getClass(), getPersistenceUnitConfigAnnotationClass()); Set<Method> methods = getMethodsAnnotatedWith(testObject.getClass(), getPersistenceUnitConfigAnnotationClass()); // filter out methods without entity manager factory argument Iterator<Method> iterator = methods.iterator(); while (iterator.hasNext()) { Class<?>[] parameterTypes = iterator.next().getParameterTypes(); if (parameterTypes.length == 0 || !getPersistenceUnitClass().isAssignableFrom(parameterTypes[0])) { iterator.remove(); } } if (fields.isEmpty() && methods.isEmpty()) { // Jump out to make sure that we don't try to instantiate the EntityManagerFactory return; } ORM_PERSISTENCE_UNIT persistenceUnit = getPersistenceUnit(testObject); setFieldAndSetterValue(testObject, fields, methods, persistenceUnit); }
/** * Injects all fields that are annotated with {@link InjectInto}. * * @param test The instance to inject into, not null */ public void injectAll(Object test) { Set<Field> fields = getFieldsAnnotatedWith(test.getClass(), InjectInto.class); for (Field field : fields) { inject(test, field); } }
Set<Field> annotatedFields = getFieldsAnnotatedWith(testClass, annotationClass); for (Field field : annotatedFields) { annotation = field.getAnnotation(annotationClass); Set<Method> annotatedMethods = getMethodsAnnotatedWith(testClass, annotationClass, false); for (Method annotatedMethod : annotatedMethods) { annotation = annotatedMethod.getAnnotation(annotationClass);
/** * Auto-injects all fields that are annotated with {@link InjectIntoStaticByType} * * @param test The instance to inject into, not null */ public void injectAllStaticByType(Object test) { Set<Field> fields = getFieldsAnnotatedWith(test.getClass(), InjectIntoStaticByType.class); for (Field field : fields) { injectStaticByType(test, field); } }