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

com.github.simy4.xpath.fixtures.FixtureAccessor Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
package com.github.simy4.xpath.fixtures;

import com.github.simy4.xpath.helpers.OrderedProperties;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Scanner;

/**
 * XmlBuilder unified fixture accessor.
 */
public final class FixtureAccessor {

    private static final String XML_PROPERTIES_PATH_FORMAT =
            "/com/github/simy4/xpath/fixtures/%1$s/%1$s.properties";
    private static final String XML_PUT_PATH_FORMAT = "/com/github/simy4/xpath/fixtures/%1$s/%1$s-put.%2$s";
    private static final String XML_PUT_VALUE_PATH_FORMAT = "/com/github/simy4/xpath/fixtures/%1$s/%1$s-put-value.%2$s";

    private final String fixtureName;
    private final String fixtureType;

    public FixtureAccessor(String fixtureName) {
        this(fixtureName, "xml");
    }

    public FixtureAccessor(String fixtureName, String fixtureType) {
        this.fixtureName = fixtureName;
        this.fixtureType = fixtureType;
    }

    /**
     * Reads XPath to Value properties from fixture resource as an ordered map.
     *
     * @return ordered XPath to Value mappings
     */
    public Map getXmlProperties() {
        final String resource = String.format(XML_PROPERTIES_PATH_FORMAT, fixtureName);
        try (InputStream xpathPropertiesStream = getClass().getResourceAsStream(resource)) {
            OrderedProperties xpathProperties = new OrderedProperties();
            xpathProperties.load(xpathPropertiesStream);
            return xpathProperties.toMap();
        } catch (IOException ioe) {
            throw new UncheckedIOException("Unable to fetch XML properties " + resource, ioe);
        }
    }

    public String getPutXml() {
        return getXml(XML_PUT_PATH_FORMAT);
    }

    public String getPutValueXml() {
        return getXml(XML_PUT_VALUE_PATH_FORMAT);
    }

    private String getXml(String format) {
        final String resource = String.format(format, fixtureName, fixtureType);
        try (InputStream xmlStream = getClass().getResourceAsStream(resource)) {
            return new Scanner(xmlStream, "UTF-8").useDelimiter("\\A").next();
        } catch (IOException ioe) {
            throw new UncheckedIOException("Unable to fetch XML document " + resource, ioe);
        }
    }

    @Override
    public String toString() {
        return fixtureName + " for " + fixtureType;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy