Search in sources :

Example 1 with InputTranslatorException

use of org.cerberus.service.xmlunit.InputTranslatorException in project cerberus-source by cerberustesting.

the class XmlUnitService method initInputTranslator.

/**
 * Initializes {@link #inputTranslator} by two {@link InputTranslator}
 * <ul>
 * <li>One for handle the <code>url</code> prefix</li>
 * <li>One for handle without prefix</li>
 * </ul>
 */
private void initInputTranslator() {
    inputTranslator = new InputTranslatorManager<Document>();
    // Add handling on the "url" prefix, to get URL input
    inputTranslator.addTranslator(new AInputTranslator<Document>("url") {

        @Override
        public Document translate(String input) throws InputTranslatorException {
            try {
                URL urlInput = new URL(InputTranslatorUtil.getValue(input));
                return XmlUtil.fromURL(urlInput);
            } catch (MalformedURLException e) {
                throw new InputTranslatorException(e);
            } catch (XmlUtilException e) {
                throw new InputTranslatorException(e);
            }
        }
    });
    // Add handling for raw XML input
    inputTranslator.addTranslator(new AInputTranslator<Document>(null) {

        @Override
        public Document translate(String input) throws InputTranslatorException {
            try {
                return XmlUtil.fromString(input);
            } catch (XmlUtilException e) {
                throw new InputTranslatorException(e);
            }
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) InputTranslatorException(org.cerberus.service.xmlunit.InputTranslatorException) XmlUtilException(org.cerberus.util.XmlUtilException) Document(org.w3c.dom.Document) URL(java.net.URL)

Example 2 with InputTranslatorException

use of org.cerberus.service.xmlunit.InputTranslatorException in project cerberus-source by cerberustesting.

the class XmlUnitService method getDifferencesFromXml.

@Override
public String getDifferencesFromXml(String left, String right) {
    try {
        // Gets the detailed diff between left and right argument
        Document leftDocument = inputTranslator.translate(left);
        Document rightDocument = inputTranslator.translate(right);
        DetailedDiff diffs = new DetailedDiff(XMLUnit.compareXML(leftDocument, rightDocument));
        // Creates the result structure which will contain difference list
        Differences resultDiff = new Differences();
        // Add each difference to our result structure
        for (Object diff : diffs.getAllDifferences()) {
            if (!(diff instanceof Difference)) {
                LOG.warn("Unable to handle no XMLUnit Difference " + diff);
                continue;
            }
            Difference wellTypedDiff = (Difference) diff;
            String xPathLocation = wellTypedDiff.getControlNodeDetail().getXpathLocation();
            // Then we retrieve XPath from the right structure.
            if (xPathLocation == null) {
                xPathLocation = wellTypedDiff.getTestNodeDetail().getXpathLocation();
            }
            // This case should never happen
            if (xPathLocation == null) {
                LOG.warn("Null left and right differences found");
                xPathLocation = NULL_XPATH;
            }
            resultDiff.addDifference(new org.cerberus.service.xmlunit.Difference(xPathLocation));
        }
        // Finally returns the String representation of our result structure
        return resultDiff.mkString();
    } catch (InputTranslatorException e) {
        LOG.warn("Unable to get differences from XML", e);
    }
    return null;
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) InputTranslatorException(org.cerberus.service.xmlunit.InputTranslatorException) Difference(org.custommonkey.xmlunit.Difference) Document(org.w3c.dom.Document) Differences(org.cerberus.service.xmlunit.Differences)

Aggregations

InputTranslatorException (org.cerberus.service.xmlunit.InputTranslatorException)2 Document (org.w3c.dom.Document)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Differences (org.cerberus.service.xmlunit.Differences)1 XmlUtilException (org.cerberus.util.XmlUtilException)1 DetailedDiff (org.custommonkey.xmlunit.DetailedDiff)1 Difference (org.custommonkey.xmlunit.Difference)1