-
Notifications
You must be signed in to change notification settings - Fork 79
Description
Summary
When comparing XML documents containing attributes with null values, NullPointerException is thrown.
Steps to reproduce
Test code below should trigger the NPE:
@Test
void testSetAttribute_whenNull_shouldBeEmptyAttribute() throws ParserConfigurationException {
// given
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.newDocument();
// when
Element element = document.createElement("Tag");
element.setAttribute("name", null);
document.appendChild(element);
// then
assertThat(document)
.and(Input.fromString("<Tag name=\"\"></Tag>"))
.areIdentical();
}
Actual behavior
AssertionError is thrown with:
org.xmlunit.XMLUnitException: Caught exception during comparison at org.xmlunit.diff.DOMDifferenceEngine.compare(DOMDifferenceEngine.java:120),
which under the hood is
ava.lang.NullPointerException: Cannot invoke "org.w3c.dom.Node.getNodeType()" because "n" is null at org.xmlunit.diff.NodeFilters$1.test(NodeFilters.java:33)
Root cause analysis
IterableNodeListwith underlyingNodeListIteratoris used as collection of nodes incompareNodesmethodNodeListIteratorrefers toNodeList'sgetLength()anditem(int index)methods- in default Xerces DOM implementation for attribute node the
AttrImpl.getLength()andAttrImpl.item(0)methods are called AttrImpl.item(int index)has a condition resulting innullif (index != 0 || value == null) { return null; }nullis later passed toNodeFilters.Defaultpredicate and fails onn.getNodeType()
Expected behavior
Attributes with null values could be treated as having empty values ("") and considered equal.
The javax.xml.transform API already serializes them as empty strings.
Proposed fix
Adding a null check in the NodeFilters.Default predicate, however i am not sure whether the null check would be safe considering the predicate applies to other node types as well.