permaculture playing cards*
The moose likes Testing and the fly likes How to test Struts Plugins Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to test Struts Plugins" Watch "How to test Struts Plugins" New topic
Author

How to test Struts Plugins

Jorge Blaz
Ranch Hand

Joined: Jan 31, 2004
Posts: 53
Hello,

I have done several plugins for struts (similar to TilesPlugin or ValidatorPlugin). What I don't know is which tool to choose in order to test the plugins.
I have used StrutsTestCase for Struts Actions, but don't know if I can test the plugins with StrutsTestCase as well. Ho about Cactus?
I've also done a handler Class (extending DefaultHandler) to read an xml with a SAX Parser. It's possible to test this Handler class also with JUnit?.

Thanks in advance
Jorge
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Jorge Blaz:
I have done several plugins for struts (similar to TilesPlugin or ValidatorPlugin). What I don't know is which tool to choose in order to test the plugins.
I have used StrutsTestCase for Struts Actions, but don't know if I can test the plugins with StrutsTestCase as well. Ho about Cactus?

I suppose that depends on what kind of an interface do these Struts plugins have?

Originally posted by Jorge Blaz:
I've also done a handler Class (extending DefaultHandler) to read an xml with a SAX Parser. It's possible to test this Handler class also with JUnit?

Yes, you can indeed test a SAX handler with JUnit -- just create an instance of the handler and start calling the SAX event methods (and then, after you're done, verify somehow that the handler did what it's expected to do).


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

Joined: Jan 31, 2004
Posts: 53
Originally posted by Lasse Koskela:

I suppose that depends on what kind of an interface do these Struts plugins have?.


I have a class that implements org.apache.struts.action.PlugIn, reads a path to an xml file (with set-property nested tag) in the init method, then parses this xml with SAX, build a map with the xml element values and stores the map in the application context.


Originally posted by Lasse Koskela:

Yes, you can indeed test a SAX handler with JUnit -- just create an instance of the handler and start calling the SAX event methods (and then, after you're done, verify somehow that the handler did what it's expected to do).


Thanks Lasse, I will try it :-)

Regards
Jorge
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Jorge Blaz:
I have a class that implements org.apache.struts.action.PlugIn, reads a path to an xml file (with set-property nested tag) in the init method, then parses this xml with SAX, build a map with the xml element values and stores the map in the application context.

If you extract the XML parsing and map building code into separate classes, what's left for the Plugin class to do is to read the XML file from the correct place, feed it to the SAX parser, and ask the SAX handler for the resulting map, and put the map to the application context.

I'd probably just let the test create a temporary file at runtime containing the simplest XML I can imagine, make sure that the application context doesn't have a map yet, call init() on the plugin instance, and verify that the application context got the map.

Another option would be to delegate everything to non-Struts-dependent classes, leaving the Struts PlugIn class simple enough to not warrant unit tests. I.e. something like:

...and then you could test the Plugin delegate class in isolation of the SAX handler, the actual XML document contents, and the ApplicationContext since you would be in a position to pass mock implementations of them to the delegate's init() method.

Just a thought. Might not be worth the effort.
Jorge Blaz
Ranch Hand

Joined: Jan 31, 2004
Posts: 53
Originally posted by Lasse Koskela:

...and then you could test the Plugin delegate class in isolation of the SAX handler, the actual XML document contents, and the ApplicationContext since you would be in a position to pass mock implementations of them to the delegate's init() method.

Just a thought. Might not be worth the effort.


I don't understand your suggestion here. I'm very new to testing. Is there a way to have mock Objects for the ActionServlet and ModuleConfig objects?. I guess Cactus could give you a Servlet or HttpRequest object, but ModuleConfig and ActionServlet are Struts dependant. I've take a look at StrutsTestCase Api and haven't found a way to get these objects either as mock objects or as container-supplied ones.
The same happens when testing the maps that are in the application context scope. I cannot simulate the ServletContext object.

By the way, I tested the Handler and it worked. I parsed it and then asserted that the maps had the right values readed from the xml file.

Thanks Lasse for your help :-)

Regards
Jorge
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to test Struts Plugins