Re: Getting Initialization Parameters from web.xml

Kazuhito SUGURI <[email protected]>
Newsgroups gmane.comp.jakarta.cactus.user
Message-ID <[email protected]>
Hi Bryan,

In article <[email protected]>,
Thu, 08 Jun 2006 11:56:21 -0600,
Bryan Cornies <[email protected]> wrote: 
bryan> I am trying to test a servlet which contains an init() method which
bryan> grabs a handful of initialization parameters from the web.xml file.  It
bryan> looks like:
[snip]
bryan> I understand that, from reading the documention, I need to call init on
bryan> the servlet and pass it the ServletConfigWrapper member variable.  Thus,
bryan> my test method looks like:
[snip]
bryan> The problem is, unless I explicitly call setInitializationParameter() in
bryan> testDoPost(), which just redundently sets the same parameters that are
bryan> already specified in web.xml, the servlet cannot find them when it calls
bryan> config.getInitParameter().  It works fine when I send a POST to the
bryan> servlet through a jsp.

What I can point out are as follows:

1. A MyServlet instance which is instantiated in the testXXX method
   is not the one instantiated as a servlet by the servlet container.
   As the responsibility to set ServletConfig to a servlet is
   in the servlet container, the responsibility to set ServetConfig(Wrapper)
   to the class under test is in your testXXX method.

2. ServletContext available from testXXX method is the one for
   ServletRedirector defined in web.xml, not the one for your MyServlet.
   Please recall a fact that ServletRedirector is mapped as a servlet
   and your test-case is called from the servlet.
   ServletContextWrapper is provided to help emulate ServletContext
   for a class under test.

3. I think what you need to test against your MyServlet class are
   behavior of init(config) and post-init conditions when
   - config consists of just expected parameters
   - config missing some of mandatory parameers
   - config missing some of optional parameters (test its default works)
   - config contains no parameters
   - parameter(s) has wrong or inconsistent value(s)
   - etc.

   This means that you have to have multiple init-paremter sets
   for a class under test.
   I'm not sure it's reasonable to define all such parameter sets
   in web.xml.

4. As a servlet-class can be mapped to multiple servlet-names,
   init-parameters cannot be determined from the class-name.

   You can provide a utility class to set init-parameters
   to ServletConfigWrapper.
   The utility class should have method looks like
      void setInitParamByName(ServletConfigWrapper theWrapper, String theName);

   If you already have such helper, it would be nice if you could add
   the code(s) in the wiki:
	http://wiki.apache.org/jakarta-cactus/UsefulCode


bryan> I feel like I'm missing a key concept somewhere.  I've spent the last
bryan> couple of days trying anything I could think of with no luck.  Any
bryan> assistance would be greatly appreciated!

From stand point of test-first practice, you shoud not map MyServlet
as a servlet in web.xml until MyServlet passes all unit-tests. :-)

Regards,
----
Kazuhito SUGURI
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.