XMLUnit Asserts Equal XML Are Not Equal

1.1k views Asked by At

In my JUnit tests I compare an expected XML file to a virtual document a parser created:

public void test() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    try (InputStream input = getClass().getResourceAsStream("expected.xml");) {
        Document expectedDocument = builder.parse(input);
        Document actualDocument = createVirtualDocument();

        XMLTestCase.assertXMLEqual(expectedDocument, actualDocument);
    }
}

The problem is: Even though the XML files are equal (or at very last similar), the assertion fails. The XML files look like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0" zoomAndPan="magnify">
  <g class="group">
    <rect class="shape" height="40.0" width="30.0" x="10.0" y="20.0"/>
  </g>
</svg>

And the other one:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0">
  <g class="group">
    <rect x="10.0" width="30.0" height="40.0" y="20.0" class="shape"/>
  </g>
</svg>

In short: the attribute order is different. I already tried XMLUnit.setIgnoreAttributeOrder(true) and XMLUnit.setNormalize(true), but to no avail.

The message I get when the test fails are:

[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg contentScriptType="text/ecmascript"...> at /svg[1]/@contentScriptType to <svg contentScriptType="text/ecmascript"...> at /svg[1]/@contentScriptType
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg contentStyleType="text/css"...> at /svg[1]/@contentStyleType to <svg contentStyleType="text/css"...> at /svg[1]/@contentStyleType
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg preserveAspectRatio="xMidYMid meet"...> at /svg[1]/@preserveAspectRatio to <svg preserveAspectRatio="xMidYMid meet"...> at /svg[1]/@preserveAspectRatio
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg version="1.0"...> at /svg[1]/@version to <svg version="1.0"...> at /svg[1]/@version
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg zoomAndPan="magnify"...> at /svg[1]/@zoomAndPan to <svg zoomAndPan="magnify"...> at /svg[1]/@zoomAndPan
[different] Expected presence of child node 'g' but was 'null' - comparing <g...> at /svg[1]/g[1] to  at null

I also tried:

Diff difference = new Diff(expectedDocument, actualDocument);
difference.overrideElementQualifier(new ElementNameAndAttributeQualifier());

But the difference claims to be not similar. So how can I compare these two XML documents correctly?

1

There are 1 answers

0
Stefan S. On BEST ANSWER

That's just stupid. The code works if I don't compare the Documents but transform them to strings and compare these:

ByteArrayOutputStream expected = new ByteArrayOutputStream();
storeXml(expectedDocument, expected);

ByteArrayOutputStream actual = new ByteArrayOutputStream();
storeXml(actualDocument, actual);

assertXMLEqual(expected.toString(), actual.toString());

With:

protected static void storeXml(Document document, OutputStream out) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(document), new StreamResult(out));
}