James,

> From: James Strachan [mailto:[EMAIL PROTECTED]]
> 
> From: "Wannheden, Knut" <[EMAIL PROTECTED]>
> >
> > Would it be possible to write a different 
> <test:assertEquals/> tag that
> has
> > two nested elements as its body, whose output is then 
> compared?  Something
> > like:
> >
> > <test:assertEquals>
> >  <foo:bar/>
> >  <foo:baz/>
> > </test:assertEquals>
> 
> I'm sure it would be possible. Fancy submitting a patch :-)?
> 

If it would be of interest, sure!

> 
> > Would a custom XMLOuput class have to be implemented for this?
> 
> Not necessarily. You could just use a SAX ContentHandler & 
> LexicalHandler
> that could convert the SAX events into, say, a DOM tree of 
> some kind and
> perform the comparison that way.
> 
> e.g. here's how you could implement it - the following code 
> is based on the
> <x:parse> tag source code
> 
> import org.dom4j.Document;
> import org.dom4j.io.SAXContentHandler;
> 
> ...
> 
> SAXContentHandler handler = new SAXContentHandler();
> XMLOutput newOutput = new XMLOutput(handler);
> handler.startDocument();
> handler.startElement("", "dummy", "dummy", new AttributesImpl());
> 
> // pipe this tags body into the SAX handler
> invokeBody(newOutput);
> 
> handler.endElement("", "dummy", "dummy");
> handler.endDocument();
> 
> // now extract the Document created
> Document document = handler.getDocument();
> 
> 
> Note that there's an added complication in the example you 
> gave, that an XML
> document must have 1 root element. Hence the use of the 
> <dummy> startElement
> and endElement calls above.
> 

That sounds like a good idea.  Shouldn't be too hard to realize.  It's just
the equality testing code of two Documents (or Nodes) that remains...  Maybe
something's floating around already.

> I guess another approach to this could be to parse XML using 
> <x:parse> and
> then have a Document assertion. e.g.
> 
> <x:parse var="doc1">
>   <foo:bar/>
> </x:parse>
> 
> 
> <x:parse var="doc2">
>   <foo:baz/>
> </x:parse>
> 
> <test:assertDocumentsEqual expected="${doc1}" actual="${doc2}"/>
> 

Yes, I suppose this would be a way to do it, too.  But I think it'd be slick
to skip the indirection over variables and the additional
<assertDocumentsEqual/>.  There could be this additional
<assertOutputsEqual/> or <assertEquals/> could be reworked to either work
with the attributes actual and expected or its content.  I guess the message
could still be the body text, in which case the content would be mixed, like
so:

<test:assertEquals>
 <foo:bar/>                 <!-- execution results in expected -->
 <foo:baz/>                 <!-- execution results in actual -->
 Bar and Baz are not equal. <!-- message -->
</test:assertEquals>

But in case there is no output from either nested tag, I guess I would need
separate XMLOutputs afterall, no?

Cheers,

--
knut

Reply via email to