xml - How to use JUnit to test the result of SAX parser? -
I have created an XML parser to parse some XML configuration files. The result of parser comes back in a callback listener, looks like this:
interface on parasite listener {ending at zero (result result); }
); Parser.parse ("conf.xml", new on purse realtistener () {end on zero (result result) {// get results object here}});
Now I want to write a JUnit code to test whether MyParser is working well or not. But when I put the above code into a test method, the examination method always ended with was implemented. I tried to wait for after calling pars () , and notify () in < Code> Parsing () is parsing, but it was always waiting for my guess that JUnit thread is blocked, but SAX XMLReader needs to send event to that thread, so it is not through will get. So I can not block JUnit thread but it ends before my parsing .. What can I do to test the parsing result?
MyParser roughly looks like this:
public class MyParser {OnParserResultListener listener; Public Zero Pars (InputStream XML, Operators Result Listener Listener) {this.listener = Listener; SAXParserFactory factory = SAXParserFactory.newInstance (); SAXParser parser; Try {Pars = Factory NASAspex (); XMLReader xmlReader = parser.getXMLReader (); // Handle XML parsing event in a custom baseholder xmlReader.setContentHandler (new bacillandler ()); Input source = new input source (xml); XmlReader.parse (is); } {Exception e} {}}} class basehandler default handler {public zero start element (string u, string l, string queue, attribute a) {.... // handle xml tag} public zero and element (string r, String l, string queue) throws SAXException {.... // Read XML value, to save on a result object Return the result object through the callback method of // OnParserResultListener.Lifeield (Results); } .... // other methods}
You need locking mechanism Phoneback Hold the callback to hold the thread, while the results are being distributed.
You can never remove this callback, some of the following should do such things:
@Test (timeout = 5000) receives public zero ResultFromParser () {Final Semaphore Lachch = New Semaphore (1); New thread (new runnable) {public run from zero} {MyPars parser = new MyPasser (); parser. PRSE ("conf.xml", new onpars realist listener) results on zero (result result) {/ Latch .release ();}});}}). Start (); Latch.acquire (); }
Comments
Post a Comment