@Override protected void postProcessConfiguration(Configuration config) throws HibernateException { if (customConfigMethod != null) { try { ReflectionUtils.invokeMethod(testObject, customConfigMethod, config); } catch (InvocationTargetException e) { throw new UnitilsException("Error while invoking custom config method", e.getCause()); } } }
logger.warn("Unable to retrieve current value of field to inject into, no getter found for setter: " + setterToInjectTo + ". Will not be able to restore value after injection."); } else { oldValue = invokeMethod(target, getter); invokeMethod(target, setterToInjectTo, objectToInject);
/** * 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); } } }
/** * Sets the given value on the static property of the given targetClass * * @param targetClass The class on which the static property value should be set * @param staticProperty The name of the property (simple name, not a composite expression) * @param value The value to set */ private static void setValueStatic(Class<?> targetClass, String staticProperty, Object value) { Method staticSetter = ReflectionUtils.getSetter(targetClass, staticProperty, true); if (staticSetter != null) { try { invokeMethod(targetClass, staticSetter, value); } catch (InvocationTargetException e) { throw new UnitilsException("Exception thrown by target", e); } } else { Field staticField = getFieldWithName(targetClass, staticProperty, true); if (staticField == null) { throw new UnitilsException("Static property named " + staticProperty + " not found on class " + targetClass.getSimpleName()); } setFieldValue(targetClass, staticField, value); } } }
/** * 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()); } } }
/** * @param testObject The test instance, not null * @param jpaConfig The configuration parameters for the <code>EntityManagerFactory</code> * @return A completely configured <code>AbstractEntityManagerFactoryBean</code> */ protected AbstractEntityManagerFactoryBean createEntityManagerFactoryBean(Object testObject, JpaConfig jpaConfig) { LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(getDataSource()); factoryBean.setJpaVendorAdapter(getJpaProviderSupport().getSpringJpaVendorAdaptor()); String persistenceXmlFile = jpaConfig.getConfigFiles().iterator().next(); if (!StringUtils.isEmpty(persistenceXmlFile)) { factoryBean.setPersistenceXmlLocation(persistenceXmlFile); } factoryBean.setPersistenceUnitName(jpaConfig.getPersistenceUnitName()); LoadTimeWeaver loadTimeWeaver = getJpaProviderSupport().getLoadTimeWeaver(); if (loadTimeWeaver != null) { factoryBean.setLoadTimeWeaver(loadTimeWeaver); } if (jpaConfig.getConfigMethod() != null) { try { ReflectionUtils.invokeMethod(testObject, jpaConfig .getConfigMethod(), factoryBean); } catch (InvocationTargetException e) { throw new UnitilsException("Error while invoking custom config method", e.getCause()); } } factoryBean.afterPropertiesSet(); return factoryBean; }