/** * @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; }
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); } } }
/** * 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 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); }
/** * 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); } }
/** * 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); }
Set<Method> annotatedMethods = getMethodsAnnotatedWith(testClass, annotationClass, false); for (Method annotatedMethod : annotatedMethods) { annotation = annotatedMethod.getAnnotation(annotationClass);
/** * 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); } } }