You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Bryan Cornies <br...@sjrb.ca> on 2006/06/08 19:56:21 UTC

Getting Initialization Parameters from web.xml

Hi,

I am trying to test a servlet which contains an init() method which
grabs a handful of initialization parameters from the web.xml file.  It
looks like:

public void init() throws ServletException 
{
	// Initialize servlet 
	ServletConfig config = getServletConfig();
      
	// Grab parameter values from web.xml
	param1 = config.getInitParameter("param1");
	param2 = config.getInitParameter("param2");
	
	etc...
}

I understand that, from reading the documention, I need to call init on
the servlet and pass it the ServletConfigWrapper member variable.  Thus,
my test method looks like:

public void testDoPost()
{
	MyServlet servlet = new MyServlet();
	servlet.init(config);
      servlet.doPost(request, response);
}

The problem is, unless I explicitly call setInitializationParameter() in
testDoPost(), which just redundently sets the same parameters that are
already specified in web.xml, the servlet cannot find them when it calls
config.getInitParameter().  It works fine when I send a POST to the
servlet through a jsp.

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

Cheers,
Bryan

Re: Getting Initialization Parameters from web.xml

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Bryan,

In article <B4...@PRDCG4EXVW01-1.OSS.PRD>,
Thu, 08 Jun 2006 11:56:21 -0600,
Bryan Cornies <br...@sjrb.ca> 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!