• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Junit For testing SAX API

 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I want to use JUnit for testing DOM and SAX APIs .Has anyone done this before?
Are there any readymade TestCases for the same?
Regards
Ajiy
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Diff class from XMLUnit is very useful in performing simple equality comparisons between two XML documents (might fit your DOM testing needs).
For testing your custom SAX handlers, I'd suggest simply creating an instance of your handler and calling the event methods on it (startDocument(), startElement(), characters(), endElement(), endDocument(), etc.) or just parsing a "test document" with your handler and verifying the outcome against what you expect it to do.
 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi lasse
Thanx for your reply.
Actually i want to test a SAX compliant parser built internally.
Also the DOM parser which based on Xerces.
How do u do this.
In effect i will be testing SAX and DOM API.
Do u have any information @ this?
Regards
Ajit Kanada
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you guys have written your own XML parser (i.e. a replacement for the likes of Xerces)? I guess you should just write a lot of sample XML files, feed them to your SAX parser implementation, and verify that SAX handler received the expected number of event method calls and in the right order.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:
So you guys have written your own XML parser (i.e. a replacement for the likes of Xerces)? I guess you should just write a lot of sample XML files, feed them to your SAX parser implementation, and verify that SAX handler received the expected number of event method calls and in the right order.

That would be more like acceptance tests, of course - which is probably even more important to have.
 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i want to test SAX API i need to use parse xml document using SAX parser and then see the series of events which are happeneing.How do i use JUnit for this?
Any code samples?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajit Kanada:
If i want to test SAX API i need to use parse xml document using SAX parser and then see the series of events which are happeneing.How do i use JUnit for this?
If you just want to "see" the events, write a "PrintingHandler extends DocumentHandler" and make each SAX method print out its signature.
That won't help with automated testing, however, so you might want to consider something like:

This kind of mock testing approach is used by libraries such as EasyMock and JMock (JMock also has an extension that lets you mock out concrete classes instead of just interfaces!).
 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for reply..i have some doubts.
When i say

should i use
class MyRecordingHandler extends DefaultHandler ?
Also when i am calling handler.startElement("child",...) methods i need to hardcode the element names.? Is that right approach?
[ April 21, 2004: Message edited by: Ajit Kanada ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should i use
class MyRecordingHandler extends DefaultHandler ?
Yes, unless you like "extends HandlerBase" better.
Also when i am calling handler.startElement("child",...) methods i need to hardcode the element names.? Is that right approach?
Well, I assume you're hardcoding the "input" (the XML document) as well so I don't see the problem here.
 
Ajit Kanada
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If u have sample code for above class explaining how to use can u post it here?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajit Kanada:
If u have sample code for above class explaining how to use can u post it here?
Which class? If you're talking about the SAX handler class that your class needs to extend, then I would recommend reading IBM's Understanding SAX. There's also a SAX tutorial from Sun Microsystems, and here is the simplest sample code for parsing an XML file with a SAX handler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic