All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javasbar.framework.testng.dpextension.FileDataProvider Maven / Gradle / Ivy

Go to download

DolphinNG provides set of very useful addons for testNG. Most important features that it supports are condensed smart reports, auto creation of jira ticets for test failures, linking test fails to jira tickets, progress reporting during test runs.

There is a newer version: 1.3.0
Show newest version
package com.javasbar.framework.testng.dpextension;

import com.javasbar.framework.lib.common.IOUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.DataProvider;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Basavaraj M
 */
public class FileDataProvider
{
    private static final Logger LOG = LogManager.getLogger(FileDataProvider.class);

    private static Iterator getDataIterator(Method testMethod) throws Exception
    {
        Map arguments = DataProviderUtils.resolveDataProviderArguments(testMethod);
        List lines = FileDataProvider.getRawLinesFromFile(arguments.get("filePath"));
        List data = new ArrayList();
        for (String line : lines)
        {
            data.add(new Object[]{line});
        }
        return data.iterator();
    }

    /**
     * @param testMethod
     * @return
     * @throws Exception
     */
    @DataProvider(name = "getDataFromFile", parallel = false)
    public static Iterator getDataFromFile(Method testMethod) throws Exception
    {
        return getDataIterator(testMethod);
    }

    /**
     * @param testMethod
     * @return
     * @throws Exception
     */
    @DataProvider(name = "getDataFromFileParallely", parallel = true)
    public static Iterator getDataFromFileParallely(Method testMethod) throws Exception
    {
        return getDataIterator(testMethod);
    }

    private static List getRawLinesFromFile(Method testMethod) throws Exception
    {
        Map arguments = DataProviderUtils.resolveDataProviderArguments(testMethod);
        return FileDataProvider.getRawLinesFromFile(arguments.get("filePath"));
    }

    private static List getRawLinesFromFile(String filePath) throws Exception
    {
        InputStream is = new FileInputStream(new File(filePath));
        List lines = IOUtil.readAllLinesFromFileAsList(filePath, "#");
        is.close();
        return lines;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy