protected Object getTarget(Object test, Field field) { Object target = getFieldValue(test, field); if (target instanceof ObjectToInjectHolder<?>) { target = ((ObjectToInjectHolder<?>) target).getObjectToInject(); } return target; }
/** * Gets the value from the given field. If the value is a holder for an object to inject, the wrapped object is returned. * For example in case of a field declared as Mock<MyClass>, this will return the proxy of the mock instead of the mock itself. * * @param test The test, not null * @param fieldToInject The field, not null * @return The object */ protected Object getObjectToInject(Object test, Field fieldToInject) { Object fieldValue = getFieldValue(test, fieldToInject); if (fieldValue instanceof ObjectToInjectHolder<?>) { return ((ObjectToInjectHolder<?>) fieldValue).getObjectToInject(); } return fieldValue; }
/** * Gets the type of the given field. If the field is a holder for an object to inject, the wrapped type is returned. * For example in case of a field declared as Mock<MyClass>, this will return MyClass instead of Mock<MyClass> * * @param test The test, not null * @param fieldToInject The field, not null * @return The object */ protected Type getObjectToInjectType(Object test, Field fieldToInject) { Object fieldValue = getFieldValue(test, fieldToInject); if (fieldValue instanceof ObjectToInjectHolder<?>) { return ((ObjectToInjectHolder<?>) fieldValue).getObjectToInjectType(fieldToInject); } return fieldToInject.getType(); }
/** * Gets all the field values in the given test object with their corresponding field names. * * @param testedObject The test object * @return The values and names in an identity map, empty if tested object is null */ protected Map<Object, String> getFieldValuesAndNames(Object testedObject) { Map<Object, String> result = new IdentityHashMap<Object, String>(); if (testedObject == null) { return result; } Set<Field> fields = getAllFields(testedObject.getClass()); for (Field field : fields) { Object value = getFieldValue(testedObject, field); if (value != null) { result.put(value, field.getName()); } } return result; }
/** * Checks whether the given argument value is a value of a field in the test object and, if so, returns the * name of that field. * * @param testObject The test instance, null if there is no test object * @param value The value to look for, not null * @return The field name, null if no field was found for the value */ protected String getFieldName(Object testObject, Object value) { if (testObject == null) { return null; } Set<Field> fields = getAllFields(testObject.getClass()); for (Field field : fields) { Object fieldValue = getFieldValue(testObject, field); if (value == fieldValue) { return field.getName(); } } return null; }
protected void deleteTempFileForField(Object testObject, Field field) { try { File tempFile = getFieldValue(testObject, field); tempService.deleteTempFileOrDir(tempFile); } catch (Exception e) { throw new UnitilsException("Error deleting temp file for field " + field.getName(), e); } }
protected void deleteTempDirForField(Object testObject, Field field) { try { File tempDir = getFieldValue(testObject, field); tempService.deleteTempFileOrDir(tempDir); } catch (Exception e) { throw new UnitilsException("Error deleting temp dir for field " + field.getName(), e); } } }
/** * 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); } } }
/** * 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. "); } } }
/** * * Searches all the fields with the {@link WebPage} annotation and sets the correct elements. * * @param testObject : the testobject. */ public void initElements(Object testObject) { // find fields that has the @WebPage annotation Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), WebPage.class); // find the webdriver Set<Field> webdrivers = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestWebDriver.class); if (!webdrivers.isEmpty()) { // initialise the page and set the object in the correct field. WebDriver webdriver = ReflectionUtils.getFieldValue(testObject, webdrivers.iterator().next()); for (Field field : fields) { if (webdriver != null) { ReflectionUtils.setFieldValue(testObject, field, getElement(webdriver, field.getType())); } } } else { LOGGER.error("The TestWebDriver cannot be found."); } }
protected void createAndInjectMocksIntoTest(Object testObject) { Set<Field> mockFields = getFieldsOfType(testObject.getClass(), Mock.class, false); for (Field field : mockFields) { Mock<?> mock = getFieldValue(testObject, field); if (mock != null) { mock.resetBehavior(); continue; } mock = createMock(testObject, field.getName(), getMockedClass(field)); injectMock(testObject, field, mock); } }
protected void createAndInjectPartialMocksIntoTest(Object testObject) { Set<Field> partialMockFields = getFieldsOfType(testObject.getClass(), PartialMock.class, false); for (Field field : partialMockFields) { Mock<?> mock = getFieldValue(testObject, field); if (mock != null) { mock.resetBehavior(); continue; } mock = createPartialMock(testObject, field.getName(), getMockedClass(field)); injectMock(testObject, field, mock); } }
oldValue = getFieldValue(target, fieldToInjectTo);
WebDriver webdriver = ReflectionUtils.getFieldValue(testObject, webdriverField); for (String type : webdriver.manage().logs().getAvailableLogTypes()) { try { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestWebDriver.class); for (Field field : fields) { WebDriver driver = ReflectionUtils.getFieldValue(testObject, field); File surefire = new File(TARGETSUREFIREREPORTS); if (!surefire.exists()) {
/** * 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); } }
/** * Retrieves the value of the static property from the given class * * @param targetClass The class from which the static property value is retrieved * @param staticProperty The name of the property (simple name, not a composite expression) * @return The value of the static property from the given class */ private static Object getValueStatic(Class<?> targetClass, String staticProperty) { Method staticGetter = getGetter(targetClass, staticProperty, true); if (staticGetter != null) { try { return invokeMethod(targetClass, staticGetter); } catch (InvocationTargetException e) { throw new UnitilsException("Exception thrown by target", e); } } else { Field staticField = getFieldWithName(targetClass, staticProperty, true); if (staticField != null) { return getFieldValue(targetClass, staticField); } else { throw new UnitilsException("Static property named " + staticProperty + " not found on class " + targetClass.getSimpleName()); } } }