Search in sources :

Example 1 with BaseDataProvider

use of com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider in project carina by zebrunner.

the class DataProviderFactory method getDataProvider.

public static Object[][] getDataProvider(Annotation[] annotations, ITestContext context, ITestNGMethod m) {
    Map<String, String> testNameArgsMap = Collections.synchronizedMap(new HashMap<>());
    Map<String, String> testMethodOwnerArgsMap = Collections.synchronizedMap(new HashMap<>());
    Map<String, String> testRailsArgsMap = Collections.synchronizedMap(new HashMap<>());
    List<String> doNotRunTests = Collections.synchronizedList(new ArrayList<>());
    Object[][] provider = new Object[][] {};
    for (Annotation annotation : annotations) {
        try {
            Class<? extends Annotation> type = annotation.annotationType();
            String providerClass = "";
            for (Method method : type.getDeclaredMethods()) {
                if (method.getName().equalsIgnoreCase("classname")) {
                    providerClass = (String) method.invoke(annotation);
                    break;
                }
            }
            if (providerClass.isEmpty())
                continue;
            Class<?> clazz;
            Object object = null;
            try {
                clazz = Class.forName(providerClass);
                Constructor<?> ctor = clazz.getConstructor();
                object = ctor.newInstance();
            } catch (Exception e) {
                LOGGER.error("DataProvider failure", e);
            }
            if (object instanceof com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider) {
                BaseDataProvider activeProvider = (BaseDataProvider) object;
                provider = ArrayUtils.addAll(provider, activeProvider.getDataProvider(annotation, context, m));
                testNameArgsMap.putAll(activeProvider.getTestNameArgsMap());
                testMethodOwnerArgsMap.putAll(activeProvider.getTestMethodOwnerArgsMap());
                testRailsArgsMap.putAll(activeProvider.getTestRailsArgsMap());
                doNotRunTests.addAll(activeProvider.getDoNotRunRowsIDs());
            }
        } catch (Exception e) {
            LOGGER.error("DataProvider failure", e);
        }
    }
    if (!GroupByMapper.getInstanceInt().isEmpty() || !GroupByMapper.getInstanceStrings().isEmpty()) {
        provider = getGroupedList(provider);
    }
    context.setAttribute(SpecialKeywords.TEST_NAME_ARGS_MAP, testNameArgsMap);
    // clear group by settings
    GroupByMapper.getInstanceInt().clear();
    GroupByMapper.getInstanceStrings().clear();
    return provider;
}
Also used : ITestNGMethod(org.testng.ITestNGMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) GroupByException(com.qaprosoft.carina.core.foundation.dataprovider.core.groupping.exceptions.GroupByException) BaseDataProvider(com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider)

Example 2 with BaseDataProvider

use of com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider in project carina by qaprosoft.

the class DataProviderFactory method getDataProvider.

public static Object[][] getDataProvider(Annotation[] annotations, ITestContext context, ITestNGMethod m) {
    Map<String, String> testNameArgsMap = Collections.synchronizedMap(new HashMap<>());
    Map<String, String> testMethodOwnerArgsMap = Collections.synchronizedMap(new HashMap<>());
    Map<String, String> testRailsArgsMap = Collections.synchronizedMap(new HashMap<>());
    List<String> doNotRunTests = Collections.synchronizedList(new ArrayList<>());
    Object[][] provider = new Object[][] {};
    for (Annotation annotation : annotations) {
        try {
            Class<? extends Annotation> type = annotation.annotationType();
            String providerClass = "";
            for (Method method : type.getDeclaredMethods()) {
                if (method.getName().equalsIgnoreCase("classname")) {
                    providerClass = (String) method.invoke(annotation);
                    break;
                }
            }
            if (providerClass.isEmpty())
                continue;
            Class<?> clazz;
            Object object = null;
            try {
                clazz = Class.forName(providerClass);
                Constructor<?> ctor = clazz.getConstructor();
                object = ctor.newInstance();
            } catch (Exception e) {
                LOGGER.error("DataProvider failure", e);
            }
            if (object instanceof com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider) {
                BaseDataProvider activeProvider = (BaseDataProvider) object;
                provider = ArrayUtils.addAll(provider, activeProvider.getDataProvider(annotation, context, m));
                testNameArgsMap.putAll(activeProvider.getTestNameArgsMap());
                testMethodOwnerArgsMap.putAll(activeProvider.getTestMethodOwnerArgsMap());
                testRailsArgsMap.putAll(activeProvider.getTestRailsArgsMap());
                doNotRunTests.addAll(activeProvider.getDoNotRunRowsIDs());
            }
        } catch (Exception e) {
            LOGGER.error("DataProvider failure", e);
        }
    }
    if (!GroupByMapper.getInstanceInt().isEmpty() || !GroupByMapper.getInstanceStrings().isEmpty()) {
        provider = getGroupedList(provider);
    }
    context.setAttribute(SpecialKeywords.TEST_NAME_ARGS_MAP, testNameArgsMap);
    // clear group by settings
    GroupByMapper.getInstanceInt().clear();
    GroupByMapper.getInstanceStrings().clear();
    return provider;
}
Also used : ITestNGMethod(org.testng.ITestNGMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) GroupByException(com.qaprosoft.carina.core.foundation.dataprovider.core.groupping.exceptions.GroupByException) BaseDataProvider(com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider)

Aggregations

GroupByException (com.qaprosoft.carina.core.foundation.dataprovider.core.groupping.exceptions.GroupByException)2 BaseDataProvider (com.qaprosoft.carina.core.foundation.dataprovider.core.impl.BaseDataProvider)2 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 ITestNGMethod (org.testng.ITestNGMethod)2