protected List<ConversionStrategy<?>> createConversionStrategies(Properties configuration, String propertyName) { List<String> conversionStrategyClassNames = getStringList(propertyName, configuration); List<ConversionStrategy<?>> conversionStrategies = new ArrayList<ConversionStrategy<?>>(conversionStrategyClassNames.size()); for (String conversionStrategyClassName : conversionStrategyClassNames) { ConversionStrategy<?> conversionStrategy = createInstanceOfType(conversionStrategyClassName, false); conversionStrategies.add(conversionStrategy); } return conversionStrategies; } }
/** * Creates an instance of the given type. First we try to create an instance using the default constructor. * If this doesn't work, eg if there is no default constructor, we try using objenesis. This way the class doesn't * have to offer an empty constructor in order for this method to succeed. * * @param <T> The type of the instance * @param clazz The class for which an instance is requested * @return An instance of the given class */ @SuppressWarnings("unchecked") public static <T> T createInitializedOrUninitializedInstanceOfType(Class<T> clazz) { try { return createInstanceOfType(clazz, true); } catch (UnitilsException e) { logger.warn("Could not create initialized instance of type " + clazz.getSimpleName() + ". No no-arg constructor found. All fields in the instance will have the java default values. Add a default constructor (can be private) if the fields should be initialized. If this concerns an innerclass, make sure it is declared static. Partial mocking of non-static innerclasses is not supported."); } // unable to create type using regular constuctor, try objenesis return createUninitializedInstanceOfType(clazz); }
/** * Creates an objects of the given fields' declared type and assigns it to this field on the given testObject * * @param testObject The test instance, not null * @param testedObjectField The tested object field, not null */ protected void createObjectForField(Object testObject, Field testedObjectField) { Class<?> declaredClass = testedObjectField.getType(); if (declaredClass.isInterface()) { logger.warn("Field " + testedObjectField.getName() + " (annotated with @TestedObject) has type " + testedObjectField.getType().getSimpleName() + " which is an interface type. It is not automatically instantiated."); } else if (isAbstract(declaredClass.getModifiers())) { logger.warn("Field " + testedObjectField.getName() + " (annotated with @TestedObject) has type " + testedObjectField.getDeclaringClass().getSimpleName() + " which is an abstract class. It is not automatically instantiated."); } else { try { declaredClass.getDeclaredConstructor(); Object instance = createInstanceOfType(declaredClass, true); setFieldValue(testObject, testedObjectField, instance); } catch (NoSuchMethodException e) { logger.warn("Field " + testedObjectField.getName() + " (annotated with @TestedObject) has type " + testedObjectField.getDeclaringClass().getSimpleName() + " which has no default (parameterless) constructor. It is not automatically instantiated."); } } }
/** * Creates an instance of {@link org.unitils.orm.common.spring.OrmSpringSupport}, that * implements the dependency to the {@link org.unitils.spring.SpringModule}. If the * {@link org.unitils.spring.SpringModule} is not active, or if a dependency of * {@link org.unitils.orm.common.spring.OrmSpringSupport} could not be found in the classpath, * the instance is not loaded. */ protected void initOrmSpringSupport() { if (!isSpringModuleEnabled()) { return; } ormSpringSupport = createInstanceOfType(getOrmSpringSupportImplClassName(), false); }
/** * @return The default {@link DataSetLoadStrategy} class as configured in unitils */ protected DataSetLoadStrategy getDefaultDataSetLoadStrategy() { Class<? extends DataSetLoadStrategy> dataSetLoadStrategyClassName = getClassWithName(getAnnotationPropertyDefault(DbUnitModule.class, DataSet.class, "loadStrategy", configuration)); return createInstanceOfType(dataSetLoadStrategyClassName, false); }
/** * Creates the DbUnit dataset operation for loading a data set for the given method. If a value for loadStrategy is found on an * annotation, this class is used, otherwise the configured default class will be used. * * @param testMethod The method, not null * @param testClass The test class, not null * @return The DbUnit operation, not null */ @SuppressWarnings({ "unchecked" }) protected DataSetLoadStrategy getDataSetLoadStrategy(Method testMethod, Class<?> testClass, DataSet dataSet) { Class<? extends DataSetLoadStrategy> dataSetOperationClass = dataSet.loadStrategy(); dataSetOperationClass = (Class<? extends DataSetLoadStrategy>) getClassValueReplaceDefault(DataSet.class, "loadStrategy", dataSetOperationClass, defaultAnnotationPropertyValues, DataSetLoadStrategy.class); return createInstanceOfType(dataSetOperationClass, false); }
/** * Creates and initializes a data set factory of the given type. * * @param dataSetFactoryClass The type, not null * @return The {@link DataSetFactory} with the given class */ protected DataSetFactory getDataSetFactory(Class<? extends DataSetFactory> dataSetFactoryClass) { DataSetFactory dataSetFactory = createInstanceOfType(dataSetFactoryClass, false); dataSetFactory.init(configuration, getDefaultDbSupport().getSchemaName()); return dataSetFactory; }
/** * Inserts the test data coming from the given DbUnit dataset file. * * @param dataSetFile The test data set, not null * @param dataSetFactoryClass The class of the factory that must be used to read this dataset * @param dataSetLoadStrategyClass The class of the load strategy that must be used to load this dataset */ public void insertDataSet(File dataSetFile, Class<? extends DataSetFactory> dataSetFactoryClass, Class<? extends DataSetLoadStrategy> dataSetLoadStrategyClass) { DataSetLoadStrategy dataSetLoadStrategy = createInstanceOfType(dataSetLoadStrategyClass, false); DataSetFactory dataSetFactory = getDataSetFactory(dataSetFactoryClass); MultiSchemaDataSet multiSchemaDataSet = dataSetFactory.createDataSet(dataSetFile); insertDataSet(multiSchemaDataSet, dataSetLoadStrategy); }
/** * checks which browser is chosen by the {@link BrowserChoice} and creates the {@link WebDriver}. * * @param browserChoice : the chosen browser (see: {@link WebDriverModule#BROWSER_NAME_KEY}). * @param proxyUrl : the proxy url * @param downloadPath : the location where all the files should be downloaded. * @param fileType : all the types that can be downloaded automatically. * @return {@link WebDriver} */ public static WebDriver createDriver(BrowserChoice browserChoice, String proxyUrl, String downloadPath, String fileType) { if (browserChoice == null || BrowserChoice.FIREFOX.equals(browserChoice)) { return createFireFoxDriver(proxyUrl, downloadPath, fileType); } else if (BrowserChoice.IE.equals(browserChoice)) { return createInternetExplorerDriver(proxyUrl); } else if(BrowserChoice.REMOTE.equals(browserChoice)) { return createRemoteWebdriver(proxyUrl, fileType, downloadPath); } else { return ReflectionUtils.createInstanceOfType(browserChoice.getDriverClass(), true); } }
/** * The webdriver will be created in this method. * * @param browserChoice : the chosen browser (see: {@link WebDriverModule#BROWSER_NAME_KEY}). * @param downloadPath : the location where all the files should be downloaded. * @param fileType : all the types that can be downloaded automatically. * @return {@link WebDriver} */ public static WebDriver createDriver(BrowserChoice browserChoice, String downloadPath, String fileType) { if (browserChoice == null || BrowserChoice.FIREFOX.equals(browserChoice)) { return createFireFoxDriver(downloadPath, fileType); } else if (BrowserChoice.CHROME.equals(browserChoice)) { return createChromeDriver(); } else if (BrowserChoice.IE.equals(browserChoice)) { return new InternetExplorerDriver(createCapabilitiesIE("")); } else if(BrowserChoice.REMOTE.equals(browserChoice)) { return createRemoteWebdriver("", fileType, downloadPath); } else { return ReflectionUtils.createInstanceOfType(browserChoice.getDriverClass(), true); } }