Search in sources :

Example 1 with XMLUnitException

use of org.xmlunit.XMLUnitException in project irontest by zheng-wang.

the class XMLUtils method compareXML.

/**
 * @param control
 * @param test
 * @return differences found, in a format for print
 */
public static String compareXML(String control, String test) {
    StringBuilder differencesSB = new StringBuilder();
    Diff diff;
    try {
        diff = DiffBuilder.compare(control).withTest(test).normalizeWhitespace().withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new PlaceholderDifferenceEvaluator())).build();
    } catch (XMLUnitException e) {
        throw new RuntimeException(e.getCause().getMessage());
    }
    if (diff.hasDifferences()) {
        Iterator it = diff.getDifferences().iterator();
        while (it.hasNext()) {
            Difference difference = (Difference) it.next();
            if (difference.getResult() == ComparisonResult.DIFFERENT) {
                // ignore SIMILAR comparison results
                if (differencesSB.length() > 0) {
                    differencesSB.append("\n");
                }
                differencesSB.append(difference.getComparison().toString());
            }
        }
    }
    return differencesSB.toString();
}
Also used : XMLUnitException(org.xmlunit.XMLUnitException) Diff(org.xmlunit.diff.Diff) Iterator(java.util.Iterator) Difference(org.xmlunit.diff.Difference) PlaceholderDifferenceEvaluator(io.irontest.core.assertion.PlaceholderDifferenceEvaluator)

Example 2 with XMLUnitException

use of org.xmlunit.XMLUnitException in project irontest by zheng-wang.

the class XMLUtils method compareXML.

/**
 * @param control
 * @param test
 * @param namespaceAware
 * @return differences found, in a format for print
 */
public static String compareXML(String control, String test, boolean namespaceAware) {
    Object controlObject = control;
    Object testObject = test;
    // DiffBuilder.withDocumentBuilderFactory(factory);
    if (!namespaceAware) {
        try {
            controlObject = xmlStringToDOM(control);
            testObject = xmlStringToDOM(test);
        } catch (Exception e) {
            throw new RuntimeException(e.getCause().getMessage(), e);
        }
    }
    StringBuilder differencesSB = new StringBuilder();
    Diff diff;
    try {
        diff = DiffBuilder.compare(controlObject).withTest(testObject).normalizeWhitespace().withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new PlaceholderDifferenceEvaluator("#\\{", null))).build();
    } catch (XMLUnitException e) {
        throw new RuntimeException(e.getCause().getMessage(), e);
    }
    if (diff.hasDifferences()) {
        Iterator it = diff.getDifferences().iterator();
        while (it.hasNext()) {
            Difference difference = (Difference) it.next();
            if (difference.getResult() == ComparisonResult.DIFFERENT) {
                // ignore SIMILAR comparison results
                if (differencesSB.length() > 0) {
                    differencesSB.append("\n");
                }
                differencesSB.append(difference.getComparison().toString());
            }
        }
    }
    return differencesSB.toString();
}
Also used : XMLUnitException(org.xmlunit.XMLUnitException) Diff(org.xmlunit.diff.Diff) Iterator(java.util.Iterator) Difference(org.xmlunit.diff.Difference) PlaceholderDifferenceEvaluator(org.xmlunit.placeholder.PlaceholderDifferenceEvaluator) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) XMLUnitException(org.xmlunit.XMLUnitException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with XMLUnitException

use of org.xmlunit.XMLUnitException in project irontest by zheng-wang.

the class PlaceholderDifferenceEvaluatorTest method hasIgnorePlaceholder_Exception_ExclusivelyOccupy.

@Test
public void hasIgnorePlaceholder_Exception_ExclusivelyOccupy() throws Exception {
    String control = "<elem1><elem11> #{irontest.ignore}abc</elem11></elem1>";
    String test = "<elem1><elem11>abc</elem11></elem1>";
    DiffBuilder diffBuilder = DiffBuilder.compare(control).withTest(test).withDifferenceEvaluator(new PlaceholderDifferenceEvaluator());
    try {
        diffBuilder.build();
        fail();
    } catch (XMLUnitException e) {
        assertEquals("#{irontest.ignore} must exclusively occupy the text node.", e.getCause().getMessage());
    }
}
Also used : DiffBuilder(org.xmlunit.builder.DiffBuilder) XMLUnitException(org.xmlunit.XMLUnitException) Test(org.junit.Test)

Aggregations

XMLUnitException (org.xmlunit.XMLUnitException)3 Iterator (java.util.Iterator)2 Diff (org.xmlunit.diff.Diff)2 Difference (org.xmlunit.diff.Difference)2 PlaceholderDifferenceEvaluator (io.irontest.core.assertion.PlaceholderDifferenceEvaluator)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Test (org.junit.Test)1 SAXException (org.xml.sax.SAXException)1 DiffBuilder (org.xmlunit.builder.DiffBuilder)1 PlaceholderDifferenceEvaluator (org.xmlunit.placeholder.PlaceholderDifferenceEvaluator)1