[xquery-talk] XQuery-based Java Unit tests?

Michael Kay mike at saxonica.com
Tue Mar 8 09:12:32 PST 2011


The XQuery Test Suite published by W3C relies on serializing the query 
result, canonicalizing it, and comparing with reference results. This 
isn't really very satisfactory, because (a) often more than one result 
is acceptable, (b) information is lost during serialization, such as the 
type annotations. So we've been working on a new test suite that relies 
on assertions against the query result, expressed as XPath expressions. 
Here are some example of tests, enough I hope to give the flavour:

<test-case name="format-integer-022">
<description>format-integer - picture grouping, with hash symbols at the 
end</description>
<created by="O'Neil Delpratt, Saxonica" on="2010-09-28"/>
<environment ref="empty"/>
<test>format-integer(1500000, '#,###,000')</test>
<result>
<assert-eq>'1,500,000'</assert-eq>
</result>
</test-case>

<test-case name="format-integer-023">
<description>format-integer - picture grouping</description>
<created by="O'Neil Delpratt, Saxonica" on="2010-09-28"/>
<environment ref="empty"/>
<test>format-integer(1500000, '0,000,')</test>
<result>
<error code="XTDE0030"/>
</result>
</test-case>

<test-case name="format-integer-036">
<description>format-integer - empty sequence</description>
<created by="Michael Kay, Saxonica" on="2010-09-28"/>
<environment ref="empty"/>
<test>format-integer((), 'Ww')</test>
<result>
<all-of>
<assert-eq>''</assert-eq>
<assert-type>xs:string</assert-type>
<assert>count($result) = 1</assert>
</all-of>
</result>
</test-case>

The "environment" element gives information about source documents, 
schemas, parameter values, etc; it's defined by reference so many tests 
can share the same named environment. Assertions about the result can be 
combined using <all-of> and <any-of>, and as well as those shown there 
is <assert-empty/>, <assert-true/>, <assert-false/>, <assert-string-value/>.

Your situation, of testing XML documents, is essentially the same 
problem except that the query is always "." and the variability is in 
the source document it applies to....

Michael Kay
Saxonica



On 08/03/2011 08:20, Hans-Juergen Rennau wrote:
> Hello People,
>
> imagine a Java based system producing XML documents. How to perform unit
> testing?
>
> I think the conventional approach of comparing single object fields (or for that
> matter, items) with expected values is not sufficient when dealing with complex
> documents. It is a problem akin to validation, isn't it? But validating against
> the fixed domain schemas is also not enough, as it is usually very specific,
> case-dependent contents that must be checked.
>
>
> So I consider working with XQuery expressions whose Effective Boolean Values are
> checked. I imagine that one can easily build a little extension of the testing
> framework which encapsulates the XQuery processor and reduces the coding effort
> to the habitual level. So we might be able to write something like
>
> assertEBV(doc, namespaceContext, new String[] {
>     "23 eq count(//item)",
>     "empty(//item[@quantity ge 10 and not(@discount)])",
>     ...,
> });
>
> where "doc" might be, for example, a DOM node, an XML text string, an input
> stream, a reader or even a JAXB object which is then marshalled behind the
> scenes into XML.
>
> Then one might also think about creating a library of reused XQuery expressions,
> so that instead of literal expressions also name references are supported,
> allowing for parameters to be passed on as external variables to the XQuery
> expression, like so:
>
> assertEBV(doc, namespaceContext, new String[] {
>     "23 eq count(//item)",
>     "empty(//item[@quantity ge 10 and not(@discount)])",
>     "%basicAddressValidator",
>     "%discountChecker(foo=bar)"
> });
>
> My questions:
> a) Are there already tools available which support a similar approach?
> b) Do you have suggestions how to refine the basic idea?
> c) Or are there better alternatives - would you, for example, recommend to use
> adhoc schemas instead of a raw list of XQuery expressions, and if so, which
> schema language?
>
> Any answers, thoughts or comments appreciated!
>
> With kind regards,
> -- Hans-Juergen
>
>
>
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk
>



More information about the talk mailing list