The best way to validate XML in a unit test?
Asked Answered
K

3

10

I have a class with a ToString method that produces XML. I want to unit test it to ensure it is producing valid xml. I have a DTD to validate the XML against.

Should I include the DTD as a string within the unit test to avoid a dependency on it, or is there a smarter way to do this?

Kaneshakang answered 9/9, 2008 at 12:46 Comment(0)
C
6

If your program validates the XML against the DTD during normal execution, then you should just get the DTD from wherever your program will get it.

If not and the DTD is extremely short (only a few lines), then storing it as a string in your code is probably okay.

Otherwise, I'd put it in an external file and have your unit test read it from that file.

Chelsiechelsy answered 9/9, 2008 at 12:49 Comment(1)
The DTD should be locally stored so the unit test does not rely on network services being available.Postmeridian
A
6

I've used XmlUnit in the past and found it to be useful.

It can be used to validate XML against a schema or compare your XML against a string. It is clever enough to understand XML's parsing rules. For example it knows that "<e1/>" is equivalent to "<e1></e1>" and can be configured to ignore or include whitespace.

Amboceptor answered 9/9, 2008 at 13:14 Comment(1)
Thanks for the XmlUnit link, sounds interesting and helpfulElbertina
L
0

Using a DTD in the unit test to test its validity is one thing, testing for the correct content is another.

You can use the DTD to check for the validity of the generated xml which I would simply read the way you do in your program. I personally would not include it inline (as a String); there is always a dependency between you application code and the unit test. When the generated xml changes, the DTD will also change.

To test for the correct content I would go for XMLUnit.

Asserting xml using XMLUnit:

XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

Diff diff = new Diff(expectedDocument, obtainedDocument);
XMLAssert.assertXMLIdentical("xml invalid", diff, true);

One thing you might come across is the fact that the generated xml might contain changing identifiers (id/uid attributes or alike). This can be solved by using a DifferenceListener when asserting the generated xml.

Example implementation of such DifferenceListener:

public class IgnoreVariableAttributesDifferenceListener implements DifferenceListener {

    private final List<String> IGNORE_ATTRS;
    private final boolean ignoreAttributeOrder;

    public IgnoreVariableAttributesDifferenceListener(List<String> attributesToIgnore, boolean ignoreAttributeOrder) {
        this.IGNORE_ATTRS = attributesToIgnore;
        this.ignoreAttributeOrder = ignoreAttributeOrder;
    }

    @Override
    public int differenceFound(Difference difference) {
        // for attribute value differences, check for ignored attributes
        if (difference.getId() == DifferenceConstants.ATTR_VALUE_ID) {
            if (IGNORE_ATTRS.contains(difference.getControlNodeDetail().getNode().getNodeName())) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
        }
        // attribute order mismatch (optionally ignored)
        else if (difference.getId() == DifferenceConstants.ATTR_SEQUENCE_ID && ignoreAttributeOrder) {
            return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
        }
        // attribute missing / not expected
        else if (difference.getId() == DifferenceConstants.ATTR_NAME_NOT_FOUND_ID) {
            if (IGNORE_ATTRS.contains(difference.getTestNodeDetail().getValue())) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
        }

        return RETURN_ACCEPT_DIFFERENCE;
    }

    @Override
    public void skippedComparison(Node control, Node test) {
        // nothing to do
    }
}

using DifferenceListener:

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

    Diff diff = new Diff(expectedDocument, obtainedDocument);
    diff.overrideDifferenceListener(new IgnoreVariableAttributesDifferenceListener(Arrays.asList("id", "uid"), true));

    XMLAssert.assertXMLIdentical("xml invalid", diff, true);
Lisabeth answered 4/6, 2015 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.