/** * 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); } }
@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); } }
@Override public void beforeTestSetUp(Object testObject, Method testMethod) { Set<Field> tmpFileFields = getFieldsAnnotatedWith(testObject.getClass(), TempDir.class); for (Field field : tmpFileFields) { createTempDirForField(testObject, testMethod, field); } }
/** * 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 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); } }
/** * 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); } }
public static void injectIntoAnnotatedFields(Object objectToInject, Object target, Class<? extends Annotation> annotation) { Set<Field> annotatedFields = AnnotationUtils.getFieldsAnnotatedWith(target.getClass(), annotation); for (Field annotatedField : annotatedFields) { setFieldValue(target, annotatedField, objectToInject); } }
@Override public void beforeTestSetUp(Object testObject, Method testMethod) { Set<Field> tmpFileFields = getFieldsAnnotatedWith(testObject.getClass(), TempFile.class); for (Field field : tmpFileFields) { createTempFileForField(testObject, testMethod, field); } }
/** * All the elements with the @BaseUrlString will be initialised with the base url value of the unitils.properties. * * @param testObject : the testobject. */ public void initBaseUrl(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), BaseUrl.class); for (Field field : fields) { ReflectionUtils.setFieldValue(testObject, field, baseUrl); } }
@Override public void afterTestMethod(Object testObject, Method testMethod, Throwable t) { if (!cleanupAfterTest) { return; } Set<Field> tmpFileFields = getFieldsAnnotatedWith(testObject.getClass(), TempDir.class); for (Field field : tmpFileFields) { deleteTempDirForField(testObject, field); } }
/** * All fields that have an {@link FileContent} annotation will be handled before the setup of the test. * This will convert the content of the requested file to the target type of the field and inject the * result into the field. * * @param testObject The test instance, not null * @param testMethod The test method, not null */ @Override public void beforeTestSetUp(Object testObject, Method testMethod) { Set<Field> fieldsAnnotatedWithFileContent = getFieldsAnnotatedWith(testObject.getClass(), FileContent.class); for (Field field : fieldsAnnotatedWithFileContent) { readFileContentForField(testObject, field); } }
/** * checks for the {@link Dummy} annotation on the testObject. If so it is created by the DummyObjectUtil. The two aproaches possible are * stuffed or normal depending on the value in the {@link Dummy} annotation. * * @param testObject The tested object not null */ protected void createAndInjectDummiesIntoTest(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), Dummy.class); for (Field field : fields) { Object dummy = createDummy(field.getType()); setFieldValue(testObject, field, dummy); } }
/** * This will initialize a new SMTP server and the method will put it in the correct field (SimpleSmtpServer) of the testedObject. * * @param testObject of type {@link Object} */ public void initSmtpServer(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestSmtpServer.class); if (fields.size() > 1) { throw new UnitilsException("Multiple SmtpServer declarations found. Module support only one. "); } if (fields.size() == 1) { Field field = fields.iterator().next(); ReflectionUtils.setFieldValue(testObject, field, new SmtpServerImpl(port)); } }
/** * For all fields annotated with {@link TestedObject} that are still null after the test fixture, an object is * created of the field's declared type and assigned to the field. If the field's declared type is an interface or * abstract class, or if the type doesn't have a default constructor, a warning is produced. * * @param testObject The test instance, not null */ public void createTestedObjectsIfNull(Object testObject) { Set<Field> testedObjectFields = getFieldsAnnotatedWith(testObject.getClass(), TestedObject.class); for (Field testedObjectField : testedObjectFields) { if (getFieldValue(testObject, testedObjectField) == null) { createObjectForField(testObject, testedObjectField); } } }
/** * 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); }
/** * Stop the {@link SmtpServer}. * * @param testObject : the actual testobject used by JUnit. */ public void stopSmtpServer(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestSmtpServer.class); for (Field field : fields) { SmtpServer server = ReflectionUtils.getFieldValue(testObject, field); server.stop(); if (server.isRunning()) { LOGGER.warn("smtp server was not shut down correctly, port " + port + " could still be open. "); } } }
/** * 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); } }
/** * This is the actual method that creates an object of the correct type and initialises all the elements. * * @param webdriver : should be of type {@link WebDriver} * @param type: the class that should be initialised. * @return {@link Object} * */ protected Object getElement(WebDriver webdriver, Class<?> type) { Object webpage = PageFactory.initElements(webdriver, type); for (Field field : AnnotationUtils.getFieldsAnnotatedWith(type, TestWebDriver.class)) { ReflectionUtils.setFieldValue(webpage, field, webdriver); } initBaseUrl(webpage); return webpage; }
/** * All the webdrivers (all the fields with the {@link TestWebDriver} of the testObject will be killed. * * @param testObject : the testobject. */ protected void killWebDriver(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestWebDriver.class); for (Field field : fields) { WebDriver driver = ReflectionUtils.getFieldValue(testObject, field); LOGGER.debug("closing a driver that is on page : " + driver.getCurrentUrl()); driver.close(); driver.quit(); nastyDoubleCheck(driver); nastyDoubleCheck(driver); } }