aspose file tools*
The moose likes Testing and the fly likes Junit For testing SAX API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit For testing SAX API" Watch "Junit For testing SAX API" New topic
Author

Junit For testing SAX API

Ajit Kanada
Ranch Hand

Joined: Jan 23, 2001
Posts: 95
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ajit Kanada
Ranch Hand

Joined: Jan 23, 2001
Posts: 95
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ajit Kanada
Ranch Hand

Joined: Jan 23, 2001
Posts: 95
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jan 23, 2001
Posts: 95
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jan 23, 2001
Posts: 95
If u have sample code for above class explaining how to use can u post it here?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Junit For testing SAX API
 
Similar Threads
incontainer testing using junit
EJB 3.0 : Testing
Unit testing framework for testing servlets
Agile Java
Junit / httpunit questions.