开发者

JUnit Test if XML Document is sorted on arbitrary column

Given an xml document like

<root>
  <row>
    <a>2</a>
    <b>1</b>
    <c>8</c>
  </row>
  <row>
    <b>5</b>
    <a>2</a>
    <c>8</c>
  </row>
  <row>
    <a>2</a>
    <c>8</c>
    <b>6</b>
  </row>
</root>

Is ther开发者_运维问答e an easy way to assert that the XML document is sorted on element B in XMLUnit

Edit: I have an odd problem with a piece of software that I can not change where the value of the XML tags in any given node needs to be in a specific order. I'd like it for my test harness to enforce this set of rules before any other validation.


Use the value of the following simple XPath 1.0 expression:

not(//b[. > following::b])


You can build an assertion around an XPath selection.

Probably the simplest way would be to use the XPath expression /root/row/b, request the NODESET result type, then copy the text from each result node into a List structure. You can assert that the text in that list is equal to an "expected" list created with the Arrays.asList() method (I'm assuming you're using Java).


If you can execute XQuery, then the XQuery assertion:

every $i in (1 to count ($x/row) - 1)
satisfies 
   let $j :=  $i + 1   
   return number($x/row[$i]/b) <= number($x/row[$j]/b)

where $x is the document, is true if the rows are in ascending order of b, false otherwise

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜