001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet.unit;
021:
022: import java.util.Enumeration;
023: import java.util.Vector;
024:
025: import javax.servlet.ServletContext;
026:
027: import org.apache.cactus.ServletTestCase;
028: import org.apache.cactus.server.ServletContextWrapper;
029:
030: /**
031: * Tests that exercise the Cactus Servlet Config wrapper.
032: *
033: * @version $Id: TestServletConfig.java 239054 2004-10-24 01:30:23Z felipeal $
034: */
035: public class TestServletConfig extends ServletTestCase {
036: /**
037: * Verify that we can add parameters to the config list of parameters
038: * programatically, without having to define them in <code>web.xml</code>.
039: */
040: public void testSetConfigParameter() {
041: config.setInitParameter("testparam", "test value");
042:
043: assertEquals("test value", config.getInitParameter("testparam"));
044:
045: boolean found = false;
046: Enumeration en = config.getInitParameterNames();
047:
048: while (en.hasMoreElements()) {
049: String name = (String) en.nextElement();
050:
051: if (name.equals("testparam")) {
052: found = true;
053:
054: break;
055: }
056: }
057:
058: assertTrue("[testparam] not found in parameter names", found);
059: }
060:
061: //-------------------------------------------------------------------------
062:
063: /**
064: * Verify that calling <code>setInitParameter()</code> with a parameter
065: * already defined in <code>web.xml</code> will override it.
066: */
067: public void testSetConfigParameterOverrideWebXmlParameter() {
068: // Note: "param1" is a parameter that must be defined on the Servlet
069: // redirector, with a value different than "testoverrideparam1".
070: assertTrue(config.getOriginalConfig()
071: .getInitParameter("param1") != null);
072: assertTrue(!config.getOriginalConfig().getInitParameter(
073: "param1").equals("testoverrideparam1"));
074:
075: config.setInitParameter("param1", "testoverrideparam1");
076:
077: Enumeration en = config.getInitParameterNames();
078: int count = 0;
079:
080: while (en.hasMoreElements()) {
081: String name = (String) en.nextElement();
082:
083: if (name.equals("param1")) {
084: assertEquals("testoverrideparam1", config
085: .getInitParameter(name));
086: count++;
087: }
088: }
089:
090: assertTrue("[param1] was found " + count
091: + " times. Should have " + "been found once.",
092: count == 1);
093: }
094:
095: //-------------------------------------------------------------------------
096:
097: /**
098: * Verify that we can override the
099: * <code>ServletConfig.getServletName()</code> method.
100: */
101: public void testGetServletNameOverriden() {
102: config.setServletName("MyServlet");
103: assertEquals("MyServlet", config.getServletName());
104: assertTrue(!config.getOriginalConfig().getServletName().equals(
105: config.getServletName()));
106: }
107:
108: //-------------------------------------------------------------------------
109:
110: /**
111: * Verify that if we don't override the servlet name we get the original
112: * name (i.e. Cactus is effectively transparent).
113: */
114: public void testGetServletNameNoOverride() {
115: assertEquals(config.getOriginalConfig().getServletName(),
116: config.getServletName());
117: }
118:
119: //-------------------------------------------------------------------------
120:
121: /**
122: * Verify that calls to <code>ServletContext.log()</code> methods can
123: * be retrieved and asserted.
124: */
125: public void testGetLogs() {
126: String message = "some test log";
127: ServletContext context = config.getServletContext();
128:
129: context.log(message);
130:
131: Vector logs = ((ServletContextWrapper) context).getLogs();
132:
133: assertEquals("Found more than one log message", logs.size(), 1);
134: assertTrue("Cannot find expected log message : [" + message
135: + "]", logs.contains("some test log"));
136: }
137:
138: }
|