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

David Lee dlee at calldei.com
Tue Mar 8 06:33:07 PST 2011

Interesting. both approaches using assertion based testing remind me of
Michael's example can do things schematron cannot such as comparing the type
of the result, and testing non-xml output
but I suspect that will require a custom implementation (is source available

Possibly with the addition of XDM Serialization a schematron type approach
might work.
(http://xml.calldei.com/XDMSerialize) ... but then that would require a
schematron implementation that could read this format.

Alas this is the kind of chicken & egg problem with XDM today.  

David A. Lee
dlee at calldei.com

-----Original Message-----
From: talk-bounces at x-query.com [mailto:talk-bounces at x-query.com] On Behalf
Of Michael Kay
Sent: Tuesday, March 08, 2011 4:13 AM
To: talk at x-query.com
Subject: Re: [xquery-talk] XQuery-based Java Unit tests?

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 
<created by="O'Neil Delpratt, Saxonica" on="2010-09-28"/>
<environment ref="empty"/>
<test>format-integer(1500000, '#,###,000')</test>

<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>
<error code="XTDE0030"/>

<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>
<assert>count($result) = 1</assert>

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

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
> documents. It is a problem akin to validation, isn't it? But validating
> the fixed domain schemas is also not enough, as it is usually very
> 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
> framework which encapsulates the XQuery processor and reduces the coding
> 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
> 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
> so that instead of literal expressions also name references are supported,
> allowing for parameters to be passed on as external variables to the
> 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
> adhoc schemas instead of a raw list of XQuery expressions, and if so,
> 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

talk at x-query.com

More information about the talk mailing list