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

ru.yandex.qatools.allure.data.io.EnvironmentReader Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package ru.yandex.qatools.allure.data.io;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.qatools.allure.config.AllureConfig;
import ru.yandex.qatools.commons.model.Environment;
import ru.yandex.qatools.commons.model.Parameter;

import javax.inject.Inject;
import javax.xml.bind.JAXB;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import static ru.yandex.qatools.allure.commons.AllureFileUtils.listFilesByRegex;

/**
 * @author Dmitry Baev [email protected]
 *         Date: 18.02.15
 */
public class EnvironmentReader implements Reader {

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

    private Iterator xmlIterator;

    private Iterator propertiesIterator;

    @Inject
    public EnvironmentReader(@ResultDirectories File... inputDirectories) {
        AllureConfig config = AllureConfig.newInstance();
        xmlIterator = listFilesByRegex(
                config.getEnvironmentXmlFileRegex(),
                inputDirectories
        ).iterator();

        propertiesIterator = listFilesByRegex(
                config.getEnvironmentPropertiesFileRegex(),
                inputDirectories
        ).iterator();
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {
            @Override
            public boolean hasNext() {
                return xmlIterator.hasNext() || propertiesIterator.hasNext();
            }

            @Override
            public Environment next() {
                if (propertiesIterator.hasNext()) {
                    File file = propertiesIterator.next();
                    Properties properties = new Properties();
                    try (FileInputStream fis = new FileInputStream(file)) {
                        properties.load(fis);
                        Environment result = new Environment();
                        result.getParameter().addAll(convertToParameters(properties));
                        return result;
                    } catch (Exception e) {
                        LOGGER.error("Could not read environment .properties file " + file.getAbsolutePath(), e);
                        return next();
                    }
                }
                if (xmlIterator.hasNext()) {
                    File file = xmlIterator.next();
                    try (FileInputStream fis = new FileInputStream(file)) {
                        return JAXB.unmarshal(fis, Environment.class);
                    } catch (Exception e) {
                        LOGGER.error("Could not read environment .xml file " + file.getAbsolutePath(), e);
                        return next();
                    }
                }
                return null;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    public static Collection convertToParameters(Properties properties) {
        Collection parameters = new ArrayList<>();
        for (Object key : properties.keySet()) {
            Parameter parameter = new Parameter();
            parameter.setKey(key.toString());
            parameter.setName(key.toString());
            parameter.setValue(properties.getProperty(key.toString()));
            parameters.add(parameter);
        }
        return parameters;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy