KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestServletContext


1 /*
2  * ========================================================================
3  *
4  * Copyright 2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet.unit;
21
22 import java.util.Enumeration JavaDoc;
23
24 import org.apache.cactus.ServletTestCase;
25 import org.apache.cactus.server.ServletContextWrapper;
26
27 /**
28  * Tests that exercise the Cactus Servlet Context wrapper.
29  *
30  * @version $Id: TestServletContext.java,v 1.3 2004/10/24 01:30:23 felipeal Exp $
31  */

32 public class TestServletContext extends ServletTestCase
33 {
34     /**
35      * The Cactus servlet context wrapper.
36      */

37     private ServletContextWrapper context;
38
39     /**
40      * Common initialization steps for all tests.
41      */

42     public void setUp()
43     {
44         context = (ServletContextWrapper) config.getServletContext();
45     }
46     
47     /**
48      * Verify that we can add parameters to the context list of parameters
49      * programatically, without having to define them in <code>web.xml</code>.
50      */

51     public void testSetContextInitParameterUsingApi()
52     {
53         context.setInitParameter("testparam", "test value");
54
55         assertEquals("test value", context.getInitParameter("testparam"));
56
57         boolean found = false;
58         Enumeration JavaDoc en = context.getInitParameterNames();
59
60         while (en.hasMoreElements())
61         {
62             String JavaDoc name = (String JavaDoc) en.nextElement();
63
64             if (name.equals("testparam"))
65             {
66                 found = true;
67
68                 break;
69             }
70         }
71
72         assertTrue("[testparam] not found in parameter names", found);
73     }
74
75     //-------------------------------------------------------------------------
76

77     /**
78      * Verify that calling <code>setInitParameter()</code> with a parameter
79      * already defined in <code>web.xml</code> will override it.
80      */

81     public void testSetContextInitParameterOverrideWebXmlParameter()
82     {
83         // Note: "param1" is a parameter that must be already defined in
84
// web.xml (in the context-param element), with a value different
85
// than "testoverrideparam1".
86
assertTrue("'param' context-param should been defined in web.xml",
87             context.getOriginalContext().getInitParameter("param") != null);
88         assertTrue(
89             !context.getOriginalContext().getInitParameter("param").equals(
90             "testoverrideparam"));
91
92         context.setInitParameter("param", "testoverrideparam");
93
94         Enumeration JavaDoc en = context.getInitParameterNames();
95         int count = 0;
96         
97         while (en.hasMoreElements())
98         {
99             String JavaDoc name = (String JavaDoc) en.nextElement();
100
101             if (name.equals("param"))
102             {
103                 assertEquals("testoverrideparam",
104                     context.getInitParameter(name));
105                 count++;
106             }
107         }
108
109         assertTrue("[param] was found " + count + " times. Should have "
110             + "been found once.", count == 1);
111     }
112
113 }
114
Popular Tags