KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 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 import java.util.Vector JavaDoc;
24
25 import javax.servlet.ServletContext JavaDoc;
26
27 import org.apache.cactus.ServletTestCase;
28 import org.apache.cactus.server.ServletContextWrapper;
29
30 /**
31  * Tests that exercise the Cactus Servlet Config wrapper.
32  *
33  * @version $Id: TestServletConfig.java,v 1.5 2004/10/24 01:30:23 felipeal Exp $
34  */

35 public class TestServletConfig extends ServletTestCase
36 {
37     /**
38      * Verify that we can add parameters to the config list of parameters
39      * programatically, without having to define them in <code>web.xml</code>.
40      */

41     public void testSetConfigParameter()
42     {
43         config.setInitParameter("testparam", "test value");
44
45         assertEquals("test value", config.getInitParameter("testparam"));
46
47         boolean found = false;
48         Enumeration JavaDoc en = config.getInitParameterNames();
49
50         while (en.hasMoreElements())
51         {
52             String JavaDoc name = (String JavaDoc) en.nextElement();
53
54             if (name.equals("testparam"))
55             {
56                 found = true;
57
58                 break;
59             }
60         }
61
62         assertTrue("[testparam] not found in parameter names", found);
63     }
64
65     //-------------------------------------------------------------------------
66

67     /**
68      * Verify that calling <code>setInitParameter()</code> with a parameter
69      * already defined in <code>web.xml</code> will override it.
70      */

71     public void testSetConfigParameterOverrideWebXmlParameter()
72     {
73         // Note: "param1" is a parameter that must be defined on the Servlet
74
// redirector, with a value different than "testoverrideparam1".
75
assertTrue(
76             config.getOriginalConfig().getInitParameter("param1") != null);
77         assertTrue(
78             !config.getOriginalConfig().getInitParameter("param1").equals(
79             "testoverrideparam1"));
80
81         config.setInitParameter("param1", "testoverrideparam1");
82
83         Enumeration JavaDoc en = config.getInitParameterNames();
84         int count = 0;
85         
86         while (en.hasMoreElements())
87         {
88             String JavaDoc name = (String JavaDoc) en.nextElement();
89
90             if (name.equals("param1"))
91             {
92                 assertEquals("testoverrideparam1",
93                     config.getInitParameter(name));
94                 count++;
95             }
96         }
97
98         assertTrue("[param1] was found " + count + " times. Should have "
99             + "been found once.", count == 1);
100     }
101
102     //-------------------------------------------------------------------------
103

104     /**
105      * Verify that we can override the
106      * <code>ServletConfig.getServletName()</code> method.
107      */

108     public void testGetServletNameOverriden()
109     {
110         config.setServletName("MyServlet");
111         assertEquals("MyServlet", config.getServletName());
112         assertTrue(!config.getOriginalConfig().getServletName().equals(
113             config.getServletName()));
114     }
115
116     //-------------------------------------------------------------------------
117

118     /**
119      * Verify that if we don't override the servlet name we get the original
120      * name (i.e. Cactus is effectively transparent).
121      */

122     public void testGetServletNameNoOverride()
123     {
124         assertEquals(config.getOriginalConfig().getServletName(),
125             config.getServletName());
126     }
127
128     //-------------------------------------------------------------------------
129

130     /**
131      * Verify that calls to <code>ServletContext.log()</code> methods can
132      * be retrieved and asserted.
133      */

134     public void testGetLogs()
135     {
136         String JavaDoc message = "some test log";
137         ServletContext JavaDoc context = config.getServletContext();
138
139         context.log(message);
140
141         Vector JavaDoc logs = ((ServletContextWrapper) context).getLogs();
142
143         assertEquals("Found more than one log message", logs.size(), 1);
144         assertTrue("Cannot find expected log message : [" + message + "]",
145             logs.contains("some test log"));
146     }
147
148 }
149
Popular Tags