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

net.thucydides.core.csv.CSVTestDataSource Maven / Gradle / Ivy

The newest version!
package net.thucydides.core.csv;

import au.com.bytecode.opencsv.CSVReader;
import ch.lambdaj.function.convert.Converter;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import net.thucydides.core.steps.StepFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static ch.lambdaj.Lambda.convert;

/**
 * Test data from a CSV file.
 */
public class CSVTestDataSource implements TestDataSource {
    
    private final List> testData;
    private final char separator;
    private final List headers;

    private static final Logger LOGGER = LoggerFactory.getLogger(CSVTestDataSource.class);

    public CSVTestDataSource(final String path, final char separatorValue) throws IOException {
        this.separator = separatorValue;
        List csvDataRows = getCSVDataFrom(getDataFileFor(path));
        String[] titleRow = csvDataRows.get(0);

        this.headers = convert(titleRow, new Converter() {
            @Override
            public String convert(String str) {
                return StringUtils.strip(str);
            }
        });

        testData = loadTestDataFrom(csvDataRows);

    }

    public CSVTestDataSource(final String path) throws IOException {
        this(path, CSVReader.DEFAULT_SEPARATOR);
    }

    public static boolean validTestDataPath(final String path) {
        if (validFileSystemPath(path) || isAClasspathResource(path)) {
            return true;
        } else {
            URL testDataUrl = CSVTestDataSource.class.getClassLoader().getResource(path);
            return (testDataUrl != null ) && new File(testDataUrl.getFile()).exists();
        }
    }

    private Reader getDataFileFor(final String path) throws FileNotFoundException {
        Preconditions.checkNotNull(path,"Test data source was not defined");
        if (isAClasspathResource(path)) {
        		return new InputStreamReader(getClass().getClassLoader().getResourceAsStream(path));
        } else if (validFileSystemPath(path)){
        	return new FileReader(new File(path));
        }
    	throw new FileNotFoundException("Could not load test data from " + path);
    }

    private static boolean isAClasspathResource(final String path) {
    	if (CSVTestDataSource.class.getClassLoader().getResourceAsStream(path) == null){
    		return false;
    	}
        return true;
        
    }

    private static boolean validFileSystemPath(final String path) {
        File file = new File(path);
        return file.exists();
    }

    protected List getCSVDataFrom(final Reader testDataReader) throws IOException {

        CSVReader reader = new CSVReader(testDataReader, separator);
        List rows = Lists.newArrayList();
        try {
            rows = reader.readAll();
        } finally {
            reader.close();
        }
        return rows;
    }

    protected List> loadTestDataFrom(List rows) throws IOException {

        List> loadedData = new ArrayList>();
        String[] titleRow = rows.get(0);

        for (int i = 1; i < rows.size(); i++) {
            String[] dataRow = rows.get(i);
            loadedData.add(dataEntryFrom(titleRow, dataRow));
        }
        return loadedData;
    }


    private Map dataEntryFrom(final String[] titleRow, final String[] dataRow) {
        Map dataset = new HashMap();

        for (int column = 0; column < titleRow.length; column++) {
            if (column < dataRow.length) {
                String title = titleRow[column].trim();
                String value = dataRow[column].trim();
                dataset.put(title, value);
            }
        }

        return dataset;
    }

    public List> getData() {
        return testData;
    }

    public List getHeaders() {
        return headers;
    }

    /**
     * Returns the test data as a list of JavaBean instances.
     */
    public  List getDataAsInstancesOf(final Class clazz, final Object... constructorArgs) {
        List> data = getData();

        List resultsList = new ArrayList();
        for (Map rowData : data) {
            resultsList.add(newInstanceFrom(clazz, rowData, constructorArgs));
        }
        return resultsList;
    }

    public  List getInstanciatedInstancesFrom(final Class clazz, final StepFactory factory) {
        List> data = getData();
        
        List resultsList = new ArrayList();
        for (Map rowData : data) {
            resultsList.add(newInstanceFrom(clazz, factory, rowData));
        }
        return resultsList;
    }

    private  T newInstanceFrom(final Class clazz,
                                  final Map rowData,
                                  final Object... constructorArgs) {

        T newObject = createNewInstanceOf(clazz, constructorArgs);
        assignPropertiesFromTestData(clazz, rowData, newObject);
        return newObject;
    }

    private  T newInstanceFrom(final Class clazz,
                                  final StepFactory factory,
                                  final Map rowData) {
    	
    	T newObject = factory.getUniqueStepLibraryFor(clazz);
        assignPropertiesFromTestData(clazz, rowData, newObject);
        return newObject;
    }

    private  void assignPropertiesFromTestData(final Class clazz,
                                                  final Map rowData,
                                                  final T newObject) {
        Set propertyNames = rowData.keySet();

        boolean validPropertyFound = false;
        for (String columnHeading : propertyNames) {
            String value = rowData.get(columnHeading);
            String property = FieldName.from(columnHeading).inNormalizedForm();

            if (assignPropertyValue(newObject, property, value)) {
                validPropertyFound = true;
            }
        }
        if (!validPropertyFound) {
            throw new FailedToInitializeTestData("No properties or public fields matching the data columns were found "
                                                 + "or could be assigned for the class " + clazz.getName()
                                                 + "using test data: " + rowData);
        }
    }

    protected  T createNewInstanceOf(final Class clazz, final Object... constructorArgs) {
        try {
            return InstanceBuilder.newInstanceOf(clazz, constructorArgs);
        } catch (Exception e) {
            LOGGER.error("Could not create test data bean", e);
            throw new FailedToInitializeTestData("Could not create test data beans", e);
        }
    }

    protected  boolean assignPropertyValue(final T newObject, final String property, final String value) {
        boolean valueWasAssigned = true;
        try {
            InstanceBuilder.inObject(newObject).setPropertyValue(property, value);
        } catch (FailedToInitializeTestData e) {
            valueWasAssigned = false;
        }
        return valueWasAssigned;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy