Assert that two XML documents are similar
@param err Message to be displayed on assertion failure
@param control XML to be compared against
@param test XML to be tested
@throws SAXException
@throws IOException
967050
public static void assertXMLEqual(String err, Reader control, Reader test)
        throws SAXException, IOException {
        Diff diff = new Diff(control, test);
        assertXMLEqual(err, diff, true);
    }
880531
protected boolean equalsNamespace(Node control, Node test) {
        String controlNS = control.getNamespaceURI();
        String testNS = test.getNamespaceURI();
        if (controlNS == null) {
            return testNS == null;
        }
        return controlNS.equals(testNS);
    }
969476
public static void assertXpathNotExists(String xPathExpression, 
                                            Document inDocument)
        throws XpathException {
        XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
        NodeList nodeList = simpleXpathEngine.getMatchingNodes(
                                                               xPathExpression, inDocument);
        int matches = nodeList.getLength();
        assertEquals("Should be zero matches for Xpath " + 
                     xPathExpression, 0, matches);
    }
1116762
public static void assertXpathsNotEqual(String controlXpath,
                                            String testXpath,
                                            Document document)
        throws XpathException {
        assertXpathsNotEqual(controlXpath, document, testXpath, document);
    }
861773
public static void assertJsonPartNotEquals(Object expected, Object fullJson, String path, Configuration configuration) {
        Diff diff = create(expected, fullJson, FULL_JSON, path, configuration);
        if (diff.similar()) {
            if (ROOT.equals(path)) {
                doFail("Expected different values but the values were equal.");
            } else {
                doFail(String.format("Expected different values in node \"%s\" but the values were equal.", path));
            }
        }
    }
228943
public static void assertXpathValuesEqual(String controlXpath,
                                              String testXpath,
                                              InputSource document)
        throws SAXException, IOException, XpathException {
        assertXpathValuesEqual(controlXpath, testXpath,
                               XMLUnit.buildControlDocument(document));
    }
791177
public static void assertXpathsNotEqual(String controlXpath,
                                            InputSource controlDocument,
                                            String testXpath,
                                            InputSource testDocument)
        throws SAXException, IOException, XpathException {
        assertXpathsNotEqual(controlXpath,
                             XMLUnit.buildControlDocument(controlDocument),
                             testXpath,
                             XMLUnit.buildTestDocument(testDocument));
    }
112131
private ComparisonState compareNodeLists(Iterable<Node> controlSeq,
                                             final XPathContext controlContext,
                                             Iterable<Node> testSeq,
                                             final XPathContext testContext) {
        ComparisonState chain = new OngoingComparisonState();

        Iterable<Map.Entry<Node, Node>> matches =
            getNodeMatcher().match(controlSeq, testSeq);
        List<Node> controlList = Linqy.asList(controlSeq);
        List<Node> testList = Linqy.asList(testSeq);
        Set<Node> seen = new HashSet<Node>();
        for (Map.Entry<Node, Node> pair : matches) {
            final Node control = pair.getKey();
            seen.add(control);
            final Node test = pair.getValue();
            seen.add(test);
            int controlIndex = controlList.indexOf(control);
            int testIndex = testList.indexOf(test);

            controlContext.navigateToChild(controlIndex);
            testContext.navigateToChild(testIndex);
            try {
                chain =
                    chain.andThen(new Comparison(ComparisonType.CHILD_NODELIST_SEQUENCE,
                                                 control, getXPath(controlContext),
                                                 Integer.valueOf(controlIndex), getParentXPath(controlContext),
                                                 test, getXPath(testContext),
                                                 Integer.valueOf(testIndex), getParentXPath(testContext)))
                    .andThen(new DeferredComparison() {
                            @Override
                            public ComparisonState apply() {
                                return compareNodes(control, controlContext,
                                                    test, testContext);
                            }
                        });
            } finally {
                testContext.navigateToParent();
                controlContext.navigateToParent();
            }
        }

        return chain.andThen(new UnmatchedControlNodes(controlList, controlContext, seen, testContext))
            .andThen(new UnmatchedTestNodes(testList, testContext, seen, controlContext));
    }
1068928
public static Diff compareXML(String control, String test)
        throws SAXException, IOException {
        return new Diff(control, test);
    }
1068539
public void checkXmlFile(File file) throws IOException, SAXException {
        validator.reset();
        InputSource is = new InputSource(new FileInputStream(file));
        is.setSystemId(file.toURI().toURL().toString());
        checkAsXML(is);
    }
421544
protected void compareElement(Element control, Element test,
                                  DifferenceListener listener) throws DifferenceFoundException {
        compare(getUnNamespacedNodeName(control), getUnNamespacedNodeName(test), 
                control, test, listener, ELEMENT_TAG_NAME);

        NamedNodeMap controlAttr = control.getAttributes();
        Integer controlNonXmlnsAttrLength =
            getNonSpecialAttrLength(controlAttr);
        NamedNodeMap testAttr = test.getAttributes();
        Integer testNonXmlnsAttrLength = getNonSpecialAttrLength(testAttr);
        compare(controlNonXmlnsAttrLength, testNonXmlnsAttrLength,
                control, test, listener, ELEMENT_NUM_ATTRIBUTES);

        compareElementAttributes(control, test, controlAttr, testAttr,
                                 listener);
    }
758772
private static void assertXpathEquality(String controlXpath,
                                            Document controlDocument,
                                            String testXpath,
                                            Document testDocument,
                                            boolean equal)
        throws XpathException {
        XpathEngine xpath = XMLUnit.newXpathEngine();
        Diff diff = new Diff(asXpathResultDocument(XMLUnit.newControlParser(),
                                                   xpath.getMatchingNodes(controlXpath,
                                                                          controlDocument)),
                             asXpathResultDocument(XMLUnit.newTestParser(),
                                                   xpath.getMatchingNodes(testXpath,
                                                                          testDocument)));
        assertXMLEqual(diff, equal);
    }
553258
public void checkSimpleXMLString(String xml) throws IOException {
    Pattern pattern = Pattern.compile("(<error.*?/>)", Pattern.DOTALL|Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(xml);
    int pos = 0;
    while (matcher.find(pos)) {
      String errorElement = matcher.group();
      pos = matcher.end();
      if (errorElement.contains("\n") || errorElement.contains("\r")) {
        throw new IOException("<error ...> may not contain line breaks");
      }
      char beforeError = xml.charAt(matcher.start()-1);
      if (beforeError != '\n' && beforeError != '\r') {
        throw new IOException("Each <error ...> must start on a new line");
      }
    }
  }
742882
private void validate(Source src, Result res) throws XMLStreamException {
        try {
            Validator v = schemaFactory.newSchema().newValidator();
            v.setResourceResolver(schemaFactory.getResourceResolver());
            v.validate(src, res);
        } catch (SAXException | IOException e) {
            throw new XMLStreamException(e);
        }
    }
1244021
public static Diff compareXML(InputSource control, InputSource test)
        throws SAXException, IOException {
        return new Diff(control, test);
    }
875741
private static AssertionError createComparisonFailure(final String reason, final String controlString,
            final String testString) {
        try {
            if (comparisonFailureConstructor == null) {
                final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                Class<?> comparisonFailureClass = classLoader.loadClass("org.junit.ComparisonFailure");
                comparisonFailureConstructor = comparisonFailureClass.getConstructor(String.class, String.class,
                    String.class);
            }
            return (AssertionError) comparisonFailureConstructor.newInstance(reason, controlString, testString);
        } catch (Exception e) {
            // ClassNotFoundException, NoSuchMethodException, InstantiationException,
            // IllegalAccessException, InvocationTargetException
            LOGGER.info("Either add junit to your classpath or do not call '.throwComparisonFailure()'. " + e);
        }

        return null;
    }
961926
@Override
    boolean equivalentXml(Object target) {
        boolean result = false;

        if (target instanceof XML) {
            //    TODO    This is a horrifyingly inefficient way to do this so we should make it better.  It may also not work.
            return this.node.toXmlString(getProcessor()).equals( ((XML)target).node.toXmlString(getProcessor()) );
        } else if (target instanceof XMLList) {
            //    TODO    Is this right?  Check the spec ...
            XMLList otherList = (XMLList) target;

            if (otherList.length() == 1) {
                result = equivalentXml(otherList.getXML());
            }
        } else if (hasSimpleContent()) {
            String otherStr = ScriptRuntime.toString(target);

            result = toString().equals(otherStr);
        }

        return result;
    }