Search in sources :

Example 1 with Differences

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

the class XmlUnitService method removeDifference.

@Override
public String removeDifference(String pattern, String differences) {
    if (pattern == null || differences == null) {
        LOG.warn("Null argument");
        return null;
    }
    try {
        // Gets the difference list from the differences
        Differences current = Differences.fromString(differences);
        Differences returned = new Differences();
        // Compiles the given pattern
        Pattern compiledPattern = Pattern.compile(pattern);
        for (org.cerberus.service.xmlunit.Difference currentDiff : current.getDifferences()) {
            if (compiledPattern.matcher(currentDiff.getDiff()).matches()) {
                continue;
            }
            returned.addDifference(currentDiff);
        }
        // String XML representation
        return returned.mkString();
    } catch (DifferencesException e) {
        LOG.warn("Unable to remove differences", e);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) DifferencesException(org.cerberus.service.xmlunit.DifferencesException) Differences(org.cerberus.service.xmlunit.Differences)

Example 2 with Differences

use of org.cerberus.service.xmlunit.Differences 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

Differences (org.cerberus.service.xmlunit.Differences)2 Pattern (java.util.regex.Pattern)1 DifferencesException (org.cerberus.service.xmlunit.DifferencesException)1 InputTranslatorException (org.cerberus.service.xmlunit.InputTranslatorException)1 DetailedDiff (org.custommonkey.xmlunit.DetailedDiff)1 Difference (org.custommonkey.xmlunit.Difference)1 Document (org.w3c.dom.Document)1