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

org.opengis.cite.iso19136.TestNGController Maven / Gradle / Ivy

Go to download

Checks GML application schemas or data sets for conformance to ISO 19136:2007.

There is a newer version: 3.2.1-r18
Show newest version
package org.opengis.cite.iso19136;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;

import org.opengis.cite.iso19136.util.TestSuiteLogger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.occamlab.te.spi.executors.TestRunExecutor;
import com.occamlab.te.spi.executors.testng.TestNGExecutor;
import com.occamlab.te.spi.jaxrs.TestSuiteController;

/**
 * Main test run controller that executes TestNG test suites.
 */
public class TestNGController implements TestSuiteController {

    public static final String TE_BASE = "TE_BASE";
    private TestRunExecutor executor;
    private Properties etsProperties = new Properties();

    /**
     * A convenience method to facilitate test development.
     * 
     * @param args
     *            Test run arguments (optional). The first argument must refer
     *            to an XML properties file containing the expected set of test
     *            run arguments. If no argument is supplied, the file located at
     *            ${user.home}/test-run-props.xml will be used.
     * @throws Exception
     *             If the test run cannot be executed (usually due to
     *             unsatisfied pre-conditions).
     */
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        File testArgs = null;
        if (args.length > 0) {
            testArgs = (args[0].startsWith("file:")) ? new File(
                    URI.create(args[0])) : new File(args[0]);
        } else {
            String homeDir = System.getProperty("user.home");
            testArgs = new File(homeDir, "test-run-props.xml");
        }
        if (!testArgs.exists()) {
            throw new IllegalArgumentException(
                    "Test run arguments not found at " + testArgs);
        }
        Document testRunArgs = db.parse(testArgs);
        TestNGController controller = new TestNGController();
        Source testResults = controller.doTestRun(testRunArgs);
        System.out.println("Test results: " + testResults.getSystemId());
    }

    /**
     * Default constructor uses the location given by the "user.home" system
     * property as the root output directory.
     */
    public TestNGController() {
        this(new File(System.getProperty("user.home")).toURI().toString());
    }

    /**
     * Construct a controller that writes results to the given output directory.
     * 
     * @param outputDir
     *            A file URI that specifies the location of the directory in
     *            which test results will be written. It will be created if it
     *            does not exist.
     */
    public TestNGController(String outputDir) {
        InputStream is = getClass().getResourceAsStream("ets.properties");
        try {
            this.etsProperties.load(is);
        } catch (IOException ex) {
            TestSuiteLogger.log(Level.WARNING,
                    "Unable to load ets.properties. " + ex.getMessage());
        }
        URL tngSuite = TestNGController.class.getResource("testng.xml");
        File resultsDir = new File(URI.create(outputDir));
        TestSuiteLogger.log(Level.CONFIG, "Using TestNG config: " + tngSuite);
        TestSuiteLogger.log(Level.CONFIG,
                "Using outputDirPath: " + resultsDir.getAbsolutePath());
        // NOTE: setting third argument to 'true' enables the default listeners
        this.executor = new TestNGExecutor(tngSuite.toString(),
                resultsDir.getAbsolutePath(), false);
    }

    @Override
    public String getCode() {
        return etsProperties.getProperty("ets-code");
    }

    @Override
    public String getVersion() {
        return etsProperties.getProperty("ets-version");
    }

    @Override
    public String getTitle() {
        return etsProperties.getProperty("ets-title");
    }

    @Override
    public Source doTestRun(Document testRunArgs) throws Exception {
        validateTestRunArgs(testRunArgs);
        return executor.execute(testRunArgs);
    }

    /**
     * Validates the given set of test run arguments. The test run is aborted if
     * any of the checks fail.
     * 
     * At least one of the test run arguments
     * {@link org.opengis.cite.iso19136.TestRunArg#GML} or
     * {@link org.opengis.cite.iso19136.TestRunArg#IUT} must be present.
     * 
     * @param testRunArgs
     *            A DOM Document containing a set of XML properties (key-value
     *            pairs).
     * @throws IllegalArgumentException
     *             If any arguments are missing or deemed to be invalid for some
     *             reason.
     *
     * @see java.util.Properties#loadFromXML(java.io.InputStream) loadFromXML
     */
    void validateTestRunArgs(Document testRunArgs) {
        if (null == testRunArgs
                || !testRunArgs.getDocumentElement().getNodeName()
                        .equals("properties")) {
            throw new IllegalArgumentException(
                    "Input is not an XML properties document.");
        }
        NodeList entries = testRunArgs.getDocumentElement()
                .getElementsByTagName("entry");
        if (entries.getLength() == 0) {
            throw new IllegalArgumentException("No test run arguments found.");
        }
        Map args = new HashMap();
        for (int i = 0; i < entries.getLength(); i++) {
            Element entry = (Element) entries.item(i);
            args.put(entry.getAttribute("key"), entry.getTextContent());
        }
        if (!args.containsKey(TestRunArg.IUT.toString())
                && !args.containsKey(TestRunArg.GML.toString())) {
            throw new IllegalArgumentException(String.format(
                    "Missing argument: '%s' or '%s' must be present.",
                    TestRunArg.IUT, TestRunArg.GML));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy