You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2004/02/28 10:12:45 UTC

cvs commit: jakarta-cactus/framework/src/java/share/org/apache/cactus/server AbstractServletContextWrapper.java

vmassol     2004/02/28 01:12:45

  Modified:    documentation/docs/xdocs/integration/ant
                        task_webxmlmerge.xml
               integration/ant/src/test/org/apache/cactus/integration/ant/deployment
                        TestWebXml.java TestWebXmlMerger.java
               samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit
                        TestShareAll.java TestServletConfig.java
               integration/ant/src/java/org/apache/cactus/integration/ant/deployment
                        WebXmlMerger.java WebXml.java
               samples/servlet/src/webapp/j2ee12/WEB-INF cactus-web.xml
               documentation/docs/xdocs/writing howto_testcase_servlet.xml
               documentation/docs/xdocs changes.xml
               samples/servlet/src/webapp/j2ee13/WEB-INF cactus-web.xml
               framework/src/java/share/org/apache/cactus/server
                        AbstractServletContextWrapper.java
  Added:       samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit
                        TestServletContext.java
  Log:
  Added new <code>ServletContextWrapper.setInitParameters()</code> which allows to programatically define Context init parameters (as if they had been entered in <code>web.xml</code> using the <code>&lt;context-param&gt;</code> element.
  
  Revision  Changes    Path
  1.6       +5 -0      jakarta-cactus/documentation/docs/xdocs/integration/ant/task_webxmlmerge.xml
  
  Index: task_webxmlmerge.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/integration/ant/task_webxmlmerge.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- task_webxmlmerge.xml	10 Jun 2003 09:20:32 -0000	1.5
  +++ task_webxmlmerge.xml	28 Feb 2004 09:12:45 -0000	1.6
  @@ -6,6 +6,7 @@
       <title>WebXmlMerge Ant Task</title>
       <authors>
         <author name="Christopher Lenz" email="cmlenz@apache.org"/>
  +      <author name="Vincent Massol" email="vmassol@apache.org"/>
       </authors>
     </properties>
   
  @@ -25,6 +26,10 @@
         This task currently merges only a subset of the definitions in a 
         descriptor, based on the most common usage scenarios:
         <ul>
  +        <li>
  +          <emphasis>Context parameters</emphasis>: Any &lt;context-param&gt;
  +          element get inserted into the resulting descriptor verbatim.
  +        </li>
           <li>
             <emphasis>Servlets and Filters</emphasis>: Both the actual definition
             of the servlet/filter as well as the mappings to URL patterns are
  
  
  
  1.7       +100 -3    jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXml.java
  
  Index: TestWebXml.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXml.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestWebXml.java	14 Feb 2004 11:50:51 -0000	1.6
  +++ TestWebXml.java	28 Feb 2004 09:12:45 -0000	1.7
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2003 The Apache Software Foundation.
  + * Copyright 2003-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -43,6 +43,7 @@
    * Unit tests for {@link WebXml}.
    *
    * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
  + * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
    *
    * @version $Id$
    */
  @@ -422,6 +423,23 @@
           assertEquals("/f1mapping", filterMappings.next());
           assertTrue(!filterMappings.hasNext());
       }
  +
  +    /**
  +     * Tests whether a single context-param is correctly inserted into an empty
  +     * descriptor.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testAddContextParamToEmptyDocument() throws Exception
  +    {
  +        String xml = "<web-app></web-app>";
  +        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
  +        WebXml webXml = new WebXml(doc);
  +        Element contextParamElement = 
  +            createContextParamElement(doc, "param", "value");
  +        webXml.addContextParam(contextParamElement);
  +        assertTrue(webXml.hasContextParam("param"));
  +    }
       
       /**
        * Tests whether a single filter is correctly inserted into an empty
  @@ -440,6 +458,30 @@
       }
   
       /**
  +     * Tests whether a single context param is correctly inserted into a 
  +     * descriptor that already contains an other context param definition.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testAddContextParamToDocumentWithAnotherContextParam() 
  +        throws Exception
  +    {
  +        String xml = "<web-app>"
  +            + "  <context-param>"
  +            + "    <param-name>param1</param-name>"
  +            + "    <param-value>value1</param-value>"
  +            + "  </context-param>"
  +            + "</web-app>";
  +        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
  +        WebXml webXml = new WebXml(doc);
  +        Element contextParamElement = 
  +            createContextParamElement(doc, "param2", "value2");
  +        webXml.addContextParam(contextParamElement);
  +        assertTrue(webXml.hasContextParam("param1"));
  +        assertTrue(webXml.hasContextParam("param2"));
  +    }
  +    
  +    /**
        * Tests whether a single filter is correctly inserted into a descriptor 
        * that already contains an other filter definition.
        * 
  @@ -462,6 +504,37 @@
       }
   
       /**
  +     * Tests whether trying to add a context param to a descriptor that already
  +     * contains a context param definition with the same name results in an
  +     * exception.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testAddContextParamToDocumentWithTheSameContextParam() 
  +        throws Exception
  +    {
  +        String xml = "<web-app>"
  +            + "  <context-param>"
  +            + "    <param-name>param</param-name>"
  +            + "    <param-value>value</param-value>"
  +            + "  </context-param>"
  +            + "</web-app>";
  +        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
  +        WebXml webXml = new WebXml(doc);
  +        Element contextParamElement = 
  +            createContextParamElement(doc, "param", "value");
  +        try
  +        {
  +            webXml.addContextParam(contextParamElement);
  +            fail("Expected IllegalStateException");
  +        }
  +        catch (IllegalStateException ise)
  +        {
  +            // expected
  +        }
  +    }
  +
  +    /**
        * Tests whether trying to add a filter to a descriptor that already
        * contains a filter definition with the same name results in a exception.
        * 
  @@ -488,7 +561,7 @@
               // expected
           }
       }
  -
  +    
       /**
        * Tests whether a single initialization parameter can be added to a filter
        * definition.
  @@ -1434,6 +1507,30 @@
   
       // Private Methods ---------------------------------------------------------
   
  +    /**
  +     * Create a <code>context-param</code> element containing the specified 
  +     * text in the child elements.
  +     * 
  +     * @param theDocument The DOM document
  +     * @param theParamName The parameter name
  +     * @param theParamValue The parameter value
  +     * @return The created element
  +     */
  +    public Element createContextParamElement(Document theDocument,
  +        String theParamName, String theParamValue)
  +    {
  +        Element contextParamElement = 
  +            theDocument.createElement("context-param");
  +        Element paramNameElement = theDocument.createElement("param-name");
  +        paramNameElement.appendChild(theDocument.createTextNode(theParamName));
  +        contextParamElement.appendChild(paramNameElement);
  +        Element paramValueElement = theDocument.createElement("param-value");
  +        paramValueElement.appendChild(
  +            theDocument.createTextNode(theParamValue));
  +        contextParamElement.appendChild(paramValueElement);
  +        return contextParamElement;
  +    }
  +    
       /**
        * Create a <code>filter</code> element containing the specified text in 
        * the child elements.
  
  
  
  1.7       +91 -8     jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXmlMerger.java
  
  Index: TestWebXmlMerger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXmlMerger.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestWebXmlMerger.java	14 Feb 2004 11:50:51 -0000	1.6
  +++ TestWebXmlMerger.java	28 Feb 2004 09:12:45 -0000	1.7
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2003 The Apache Software Foundation.
  + * Copyright 2003-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -37,6 +37,7 @@
    * TODO: we need more tests for the security sections and the various references
    * 
    * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
  + * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
    *
    * @version $Id$
    */
  @@ -91,6 +92,32 @@
       }
   
       /**
  +     * Tests whether a single context param is correctly merged into an empty
  +     * descriptor.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testMergeOneContextParamIntoEmptyDocument() throws Exception
  +    {
  +        String srcXml = "<web-app></web-app>";
  +        Document srcDoc =
  +            builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
  +        WebXml srcWebXml = new WebXml(srcDoc);
  +        String mergeXml = "<web-app>"
  +            + "  <context-param>"
  +            + "    <param-name>param</param-name>"
  +            + "    <param-value>value</param-value>"
  +            + "  </context-param>"
  +            + "</web-app>";
  +        Document mergeDoc =
  +            builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
  +        WebXml mergeWebXml = new WebXml(mergeDoc);
  +        WebXmlMerger merger = new WebXmlMerger(srcWebXml);
  +        merger.mergeContextParams(mergeWebXml);
  +        assertTrue(srcWebXml.hasContextParam("param"));
  +    }
  +    
  +    /**
        * Tests whether a single filter is correctly merged into a descriptor that
        * already contains another filter.
        * 
  @@ -124,6 +151,39 @@
       }
   
       /**
  +     * Tests whether a single context param is correctly merged into a 
  +     * descriptor that already contains another context param.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testMergeOneContextParamIntoDocumentWithAnotherContextParam()
  +        throws Exception
  +    {
  +        String srcXml = "<web-app>"
  +            + "  <context-param>"
  +            + "    <param-name>param1</param-name>"
  +            + "    <param-value>value1</param-value>"
  +            + "  </context-param>"
  +            + "</web-app>";
  +        Document srcDoc =
  +            builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
  +        WebXml srcWebXml = new WebXml(srcDoc);
  +        String mergeXml = "<web-app>"
  +            + "  <context-param>"
  +            + "    <param-name>param2</param-name>"
  +            + "    <param-value>value2</param-value>"
  +            + "  </context-param>"
  +            + "</web-app>";
  +        Document mergeDoc =
  +            builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
  +        WebXml mergeWebXml = new WebXml(mergeDoc);
  +        WebXmlMerger merger = new WebXmlMerger(srcWebXml);
  +        merger.mergeContextParams(mergeWebXml);
  +        assertTrue(srcWebXml.hasContextParam("param1"));
  +        assertTrue(srcWebXml.hasContextParam("param2"));
  +    }
  +
  +    /**
        * Tests whether a single filter in the merge descriptor is ignored because
        * a filter with the same name already exists in the source descriptor. 
        * 
  @@ -141,12 +201,7 @@
           Document srcDoc =
               builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
           WebXml srcWebXml = new WebXml(srcDoc);
  -        String mergeXml = "<web-app>"
  -            + "  <filter>"
  -            + "    <filter-name>f1</filter-name>"
  -            + "    <filter-class>fclass1</filter-class>"
  -            + "  </filter>"
  -            + "</web-app>";
  +        String mergeXml = srcXml;
           Document mergeDoc =
               builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
           WebXml mergeWebXml = new WebXml(mergeDoc);
  @@ -155,6 +210,34 @@
           assertTrue(srcWebXml.hasFilter("f1"));
       }
   
  +    /**
  +     * Tests whether a single context param in the merge descriptor is ignored 
  +     * because a context param with the same name already exists in the source 
  +     * descriptor. 
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testMergeOneContextParamIntoDocumentWithSameContextParam()
  +        throws Exception
  +    {
  +        String srcXml = "<web-app>"
  +            + "  <context-param>"
  +            + "    <param-name>param</param-name>"
  +            + "    <param-value>value</param-value>"
  +            + "  </context-param>"
  +            + "</web-app>";
  +        Document srcDoc =
  +            builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
  +        WebXml srcWebXml = new WebXml(srcDoc);
  +        String mergeXml = srcXml;
  +        Document mergeDoc =
  +            builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
  +        WebXml mergeWebXml = new WebXml(mergeDoc);
  +        WebXmlMerger merger = new WebXmlMerger(srcWebXml);
  +        merger.mergeContextParams(mergeWebXml);
  +        assertTrue(srcWebXml.hasContextParam("param"));
  +    }
  +    
       /**
        * Tests whether a filter initialization parameter is merged into the
        * descriptor.
  
  
  
  1.3       +2 -1      jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestShareAll.java
  
  Index: TestShareAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestShareAll.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestShareAll.java	14 Feb 2004 11:52:31 -0000	1.2
  +++ TestShareAll.java	28 Feb 2004 09:12:45 -0000	1.3
  @@ -64,6 +64,7 @@
           suite.addTestSuite(TestHttpHeaders.class);
           suite.addTestSuite(TestHttpRequest.class);
           suite.addTestSuite(TestServletConfig.class);
  +        suite.addTestSuite(TestServletContext.class);
           suite.addTest(TestJUnitTestCaseWrapper.suite());
           
           // JspTestCase related tests
  
  
  
  1.3       +2 -2      jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestServletConfig.java
  
  Index: TestServletConfig.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestServletConfig.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestServletConfig.java	14 Feb 2004 11:52:31 -0000	1.2
  +++ TestServletConfig.java	28 Feb 2004 09:12:45 -0000	1.3
  @@ -28,7 +28,7 @@
   import org.apache.cactus.server.ServletContextWrapper;
   
   /**
  - * Tests that exercise the Servlet Config.
  + * Tests that exercise the Cactus Servlet Config wrapper.
    *
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
    *
  
  
  
  1.1                  jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/servlet/unit/TestServletContext.java
  
  Index: TestServletContext.java
  ===================================================================
  /* 
   * ========================================================================
   * 
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
   * ========================================================================
   */
  package org.apache.cactus.sample.servlet.unit;
  
  import java.util.Enumeration;
  
  import org.apache.cactus.ServletTestCase;
  import org.apache.cactus.server.ServletContextWrapper;
  
  /**
   * Tests that exercise the Cactus Servlet Context wrapper.
   *
   * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
   *
   * @version $Id: TestServletContext.java,v 1.1 2004/02/28 09:12:45 vmassol Exp $
   */
  public class TestServletContext extends ServletTestCase
  {
      /**
       * The Cactus servlet context wrapper. 
       */
      private ServletContextWrapper context;
  
      /**
       * Common initialization steps for all tests.
       */
      public void setUp()
      {
          context = (ServletContextWrapper) config.getServletContext();
      }
      
      /**
       * Verify that we can add parameters to the context list of parameters
       * programatically, without having to define them in <code>web.xml</code>.
       */
      public void testSetContextInitParameterUsingApi()
      {
          context.setInitParameter("testparam", "test value");
  
          assertEquals("test value", context.getInitParameter("testparam"));
  
          boolean found = false;
          Enumeration enum = context.getInitParameterNames();
  
          while (enum.hasMoreElements())
          {
              String name = (String) enum.nextElement();
  
              if (name.equals("testparam"))
              {
                  found = true;
  
                  break;
              }
          }
  
          assertTrue("[testparam] not found in parameter names", found);
      }
  
      //-------------------------------------------------------------------------
  
      /**
       * Verify that calling <code>setInitParameter()</code> with a parameter
       * already defined in <code>web.xml</code> will override it.
       */
      public void testSetContextInitParameterOverrideWebXmlParameter()
      {
          // Note: "param1" is a parameter that must be already defined in
          // web.xml (in the context-param element), with a value different
          // than "testoverrideparam1".
          assertTrue("'param' context-param should been defined in web.xml",
              context.getOriginalContext().getInitParameter("param") != null);
          assertTrue(
              !context.getOriginalContext().getInitParameter("param").equals(
              "testoverrideparam"));
  
          context.setInitParameter("param", "testoverrideparam");
  
          Enumeration enum = context.getInitParameterNames();
          int count = 0;
          
          while (enum.hasMoreElements())
          {
              String name = (String) enum.nextElement();
  
              if (name.equals("param"))
              {
                  assertEquals("testoverrideparam",
                      context.getInitParameter(name));
                  count++;
              }
          }
  
          assertTrue("[param] was found " + count + " times. Should have "
              + "been found once.", count == 1);
      }
  
  }
  
  
  
  1.6       +29 -2     jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlMerger.java
  
  Index: WebXmlMerger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlMerger.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- WebXmlMerger.java	14 Feb 2004 11:50:50 -0000	1.5
  +++ WebXmlMerger.java	28 Feb 2004 09:12:45 -0000	1.6
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2003 The Apache Software Foundation.
  + * Copyright 2003-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -29,6 +29,7 @@
    * Helper class that can merge two web deployment descriptors.
    *
    * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
  + * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
    *
    * @since Cactus 1.5
    * @version $Id$
  @@ -70,6 +71,7 @@
       public final void merge(WebXml theMergeWebXml)
       {
           checkServletVersions(theMergeWebXml);
  +        mergeContextParams(theMergeWebXml);
           if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0)
           {
               mergeFilters(theMergeWebXml);
  @@ -123,6 +125,31 @@
           }
       }
   
  +    /**
  +     * Merges the context-param definitions from the specified descriptor into 
  +     * the original descriptor.
  +     * 
  +     * @param theWebXml The descriptor that contains the context-params 
  +     *        definitions that are to be merged into the original descriptor
  +     */
  +    protected final void mergeContextParams(WebXml theWebXml)
  +    {
  +        Iterator contextParams = theWebXml.getElements(WebXmlTag.CONTEXT_PARAM);
  +        int count = 0;
  +        while (contextParams.hasNext())
  +        {
  +            String paramName = theWebXml.getContextParamName(
  +                (Element) contextParams.next());
  +            if (!webXml.hasContextParam(paramName))
  +            {
  +                webXml.addContextParam(theWebXml.getContextParam(paramName));
  +            }
  +            count++;            
  +        }
  +        this.log.trace("Merged " + count + " context-param definition"
  +            + (count != 1 ? "s " : " ") + "into the descriptor");
  +    }
  +    
       /**
        * Merges the servlet definitions from the specified descriptor into the 
        * original descriptor.
  
  
  
  1.11      +86 -2     jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXml.java
  
  Index: WebXml.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXml.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- WebXml.java	14 Feb 2004 11:50:50 -0000	1.10
  +++ WebXml.java	28 Feb 2004 09:12:45 -0000	1.11
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2003 The Apache Software Foundation.
  + * Copyright 2003-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -153,6 +153,40 @@
               createNestedText(WebXmlTag.FILTER_CLASS, theFilterClass));
           addElement(WebXmlTag.FILTER, filterElement);
       }
  +
  +    /**
  +     * Adds a new context-param element to the descriptor.
  +     * 
  +     * @param theContextParam The element representing the context-param 
  +     *        definition
  +     */
  +    public final void addContextParam(Element theContextParam)
  +    {
  +        checkElement(theContextParam, WebXmlTag.CONTEXT_PARAM);
  +
  +        String paramName = 
  +            getNestedText(theContextParam, WebXmlTag.PARAM_NAME);
  +        if (paramName == null)
  +        {
  +            throw new IllegalArgumentException(
  +                "Not a valid context-param name element");
  +        }
  +
  +        String paramValue = 
  +            getNestedText(theContextParam, WebXmlTag.PARAM_VALUE);
  +        if (paramValue == null)
  +        {
  +            throw new IllegalArgumentException(
  +                "Not a valid context-param value element");
  +        }
  +
  +        if (hasContextParam(paramName))
  +        {
  +            throw new IllegalStateException("Context param '" + paramName
  +                + "' already defined");
  +        }
  +        addElement(WebXmlTag.CONTEXT_PARAM, theContextParam);
  +    }
       
       /**
        * Adds a new servlet filter to the descriptor.
  @@ -243,6 +277,43 @@
           }
           return null;
       }
  +
  +    /**
  +     * Returns the element that contains the definition of a specific context
  +     * param, or <code>null</code> if a context param of the specified name 
  +     * is not defined in the descriptor.
  +     * 
  +     * @param theParamName The context param name
  +     * @return The DOM element representing the context param definition
  +     */
  +    public final Element getContextParam(String theParamName)
  +    {
  +        if (theParamName == null)
  +        {
  +            throw new NullPointerException();
  +        }
  +        Iterator contextParamElements = getElements(WebXmlTag.CONTEXT_PARAM);
  +        while (contextParamElements.hasNext())
  +        {
  +            Element contextParamElement = (Element) contextParamElements.next();
  +            if (theParamName.equals(getNestedText(
  +                    contextParamElement, WebXmlTag.PARAM_NAME)))
  +            {
  +                return contextParamElement;
  +            }
  +        }
  +        return null;
  +    }
  +
  +    /**
  +     * @param theContextParam the context param element from which to extract 
  +     *        the name
  +     * @return the name of the passed context param element
  +     */
  +    public final String getContextParamName(Element theContextParam)
  +    {
  +        return getNestedText(theContextParam, WebXmlTag.PARAM_NAME);
  +    }
       
       /**
        * Returns a list of names of filters that are mapped to the specified
  @@ -354,6 +425,19 @@
               }
           }
           return filterNames.iterator();
  +    }
  +
  +    /**
  +     * Returns whether a context param by the specified name is defined in the 
  +     * deployment descriptor.
  +     * 
  +     * @param theParamName The name of the context param
  +     * @return <code>true</code> if the context param is defined,
  +     *         <code>false</code> otherwise
  +     */
  +    public final boolean hasContextParam(String theParamName)
  +    {
  +        return (getContextParam(theParamName) != null);
       }
       
       /**
  
  
  
  1.3       +5 -0      jakarta-cactus/samples/servlet/src/webapp/j2ee12/WEB-INF/cactus-web.xml
  
  Index: cactus-web.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/samples/servlet/src/webapp/j2ee12/WEB-INF/cactus-web.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- cactus-web.xml	10 Jun 2003 14:07:52 -0000	1.2
  +++ cactus-web.xml	28 Feb 2004 09:12:45 -0000	1.3
  @@ -5,6 +5,11 @@
   
   <web-app>
   
  +    <context-param>
  +      <param-name>param</param-name>
  +      <param-value>value used for testing</param-value>
  +    </context-param>
  +
       <servlet>
           <servlet-name>ServletRedirector</servlet-name>
           <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
  
  
  
  1.7       +5 -0      jakarta-cactus/documentation/docs/xdocs/writing/howto_testcase_servlet.xml
  
  Index: howto_testcase_servlet.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/writing/howto_testcase_servlet.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- howto_testcase_servlet.xml	2 Nov 2003 23:41:43 -0000	1.6
  +++ howto_testcase_servlet.xml	28 Feb 2004 09:12:45 -0000	1.7
  @@ -258,6 +258,11 @@
                 by calls to <code>ServletContext.log()</code> methods. This is
                 a helper method that makes it easy to assert what is logged.
               </li>
  +            <li>
  +              <code>setInitParameter()</code>: sets an initialisation
  +              parameter (as if it has been defined in the <code>web.xml</code>
  +              file, using the <code>&lt;context-param&gt;</code> element).
  +            </li>
             </ul>
   
           </section>
  
  
  
  1.167     +11 -1     jakarta-cactus/documentation/docs/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/changes.xml,v
  retrieving revision 1.166
  retrieving revision 1.167
  diff -u -r1.166 -r1.167
  --- changes.xml	27 Feb 2004 12:34:07 -0000	1.166
  +++ changes.xml	28 Feb 2004 09:12:45 -0000	1.167
  @@ -68,6 +68,16 @@
         </devs>
   
         <release version="1.6dev" date="in CVS">
  +        <action dev="VMA" type="add">
  +          Added new <code>ServletContextWrapper.setInitParameters()</code>
  +          which allows to programatically define Context init parameters (as
  +          if they had been entered in <code>web.xml</code> using the
  +          <code>&lt;context-param&gt;</code> element.
  +        </action>
  +        <action dev="VMA" type="add">
  +          Added support in the Ant integration <code>webxmlmerge</code> task 
  +          for merging <code>&lt;context-param&gt;</code> elements.
  +        </action>
           <action dev="VMA" type="update">
             Updated the version of Commons HttpClient in the Cactus distribution 
             to 2.0 final.
  @@ -161,7 +171,7 @@
           </action>
           <action dev="VMA" type="fix" due-to="Joe Germuska" due-to-email="Joe@Germuska.com">
             Make the <code>&lt;cactus&gt;</code> task work on Mac OSX by not 
  -          including the <code>tools.jar</code> file (on Max OSX all classes
  +          including the <code>tools.jar</code> file (on Mac OSX all classes
             are found in <code>classes.jar</code>).
           </action>
           <action dev="VMA" type="fix" due-to="Kazuhito Suguri" due-to-email="suguri.kazuhito@lab.ntt.co.jp">
  
  
  
  1.3       +5 -0      jakarta-cactus/samples/servlet/src/webapp/j2ee13/WEB-INF/cactus-web.xml
  
  Index: cactus-web.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/samples/servlet/src/webapp/j2ee13/WEB-INF/cactus-web.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- cactus-web.xml	10 Jun 2003 14:07:52 -0000	1.2
  +++ cactus-web.xml	28 Feb 2004 09:12:45 -0000	1.3
  @@ -5,6 +5,11 @@
   
   <web-app>
   
  +    <context-param>
  +      <param-name>param</param-name>
  +      <param-value>value used for testing</param-value>
  +    </context-param>
  +    
       <servlet>
           <servlet-name>ServletRedirector</servlet-name>
           <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
  
  
  
  1.11      +79 -8     jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractServletContextWrapper.java
  
  Index: AbstractServletContextWrapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractServletContextWrapper.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- AbstractServletContextWrapper.java	14 Feb 2004 11:49:46 -0000	1.10
  +++ AbstractServletContextWrapper.java	28 Feb 2004 09:12:45 -0000	1.11
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2001-2003 The Apache Software Foundation.
  + * Copyright 2001-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -25,6 +25,7 @@
   import java.net.URL;
   
   import java.util.Enumeration;
  +import java.util.Hashtable;
   import java.util.Vector;
   
   import javax.servlet.RequestDispatcher;
  @@ -54,11 +55,16 @@
       protected ServletContext originalContext;
   
       /**
  +     * List of parameters set using the <code>setInitParameter()</code> method.
  +     */
  +    protected Hashtable initParameters;
  +    
  +    /**
        * The logs resulting from calling the <code>log()</code> methods
        */
       private Vector logs = new Vector();
   
  -    // Interface methods ---------------------------------------------------
  +    // Constructors  -------------------------------------------------------
   
       /**
        * @param theOriginalContext the original servlet context object
  @@ -66,11 +72,33 @@
       public AbstractServletContextWrapper(ServletContext theOriginalContext)
       {
           this.originalContext = theOriginalContext;
  +        this.initParameters = new Hashtable();
       }
   
       // New methods ---------------------------------------------------------
   
       /**
  +     * @return the original unmodified config object
  +     * @since 1.6
  +     */
  +    public ServletContext getOriginalContext()
  +    {
  +        return this.originalContext;
  +    }
  +    
  +    /**
  +     * Sets a parameter as if it were set in the <code>web.xml</code> file
  +     * (using the &lt;context-param&gt; element).
  +     *
  +     * @param theName the parameter's name
  +     * @param theValue the parameter's value
  +     */
  +    public void setInitParameter(String theName, String theValue)
  +    {
  +        this.initParameters.put(theName, theValue);
  +    }
  +    
  +    /**
        * Returns all the text logs that have been generated using the
        * <code>log()</code> methods so that it is possible to easily assert the
        * content of the logs. This method does not return the exceptions or
  @@ -83,6 +111,8 @@
       {
           return this.logs;
       }
  +    
  +    // Overridden methods --------------------------------------------------
   
       /**
        * @see ServletContext#setAttribute(String, Object)
  @@ -295,21 +325,62 @@
       }
   
       /**
  -     * @see ServletContext#getInitParameterNames()
  +     * @return the union of the parameters defined in the Redirector
  +     *         <code>web.xml</code> file and the one set using the
  +     *         <code>setInitParameter()</code> method.
        */
       public Enumeration getInitParameterNames()
       {
  -        return this.originalContext.getInitParameterNames();
  +        Vector names = new Vector();
  +
  +        // Add parameters that were added using setInitParameter()
  +        Enumeration enum = this.initParameters.keys();
  +
  +        while (enum.hasMoreElements())
  +        {
  +            String value = (String) enum.nextElement();
  +
  +            names.add(value);
  +        }
  +
  +        // Add parameters from web.xml
  +        enum = this.originalContext.getInitParameterNames();
  +
  +        while (enum.hasMoreElements())
  +        {
  +            String value = (String) enum.nextElement();
  +
  +            // Do not add parameters that have been overriden by calling
  +            // the setInitParameter() method.
  +            if (!names.contains(value))
  +            {
  +                names.add(value);
  +            }
  +        }
  +
  +        return names.elements();
       }
   
       /**
  -     * @see ServletContext#getInitParameter(String)
  +     * @param theName the name of the parameter's value to return
  +     * @return the value of the parameter, looking for it first in the list of
  +     *         parameters set using the <code>setInitParameter()</code> method
  +     *         and then in those set in <code>web.xml</code>.
        */
       public String getInitParameter(String theName)
       {
  -        return this.originalContext.getInitParameter(theName);
  -    }
  +        // Look first in the list of parameters set using the
  +        // setInitParameter() method.
  +        String value = (String) this.initParameters.get(theName);
  +
  +        if (value == null)
  +        {
  +            value = this.originalContext.getInitParameter(theName);
  +        }
   
  +        return value;
  +    }
  +    
       /**
        * @param theUripath a String specifying the context path of another web
        *        application in the container
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org