문제

I have an application that generates XML documents using the JDOM2 library. In my unit/integration tests, I would need to compare the generated documents against handmade sample documents for equality.

With standard org.w3c.dom.Document objects, I would do that via XMLUnit. How to do that with JDOM2?

도움이 되었습니까?

해결책

Doing a deep-equals with JDOM is not natively supported in the JDOM API. You will need to build your own. This is a good potential feature for JDOM 2.1.... hmmm. Perhaps I will add something like that (but it will need to be relatively complicated to get right).

Deep equals on two JDOM documents is complicated. You will need to compare namespaces, attributes, comments, processing instructions, etc. Often (some of) these differences are not important - like comments, or white-space differences: perhaps one side has the two text members Text("Hello ") and Text("World!"), and the other may have a single text member Text("Hello World!"). Are they the same?

My suggestion is to use some of the convenience features of JDOM2 (like the getDescendants() iterator) and then do your own logic to compare the two iterators one against the other.

I will consider a native JDOM API mechanism with some sort of interface for callbacks so that a deep compare is possible (with probably something that organizes a Comparable result (negative, 0, or positive) for less-than, equal, or greater-than.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top