/** * 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); }
/** * 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); }
/** * 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); }