001: /*
002: * ========================================================================
003: *
004: * Copyright 2004 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:
024: import org.apache.cactus.ServletTestCase;
025: import org.apache.cactus.server.ServletContextWrapper;
026:
027: /**
028: * Tests that exercise the Cactus Servlet Context wrapper.
029: *
030: * @version $Id: TestServletContext.java 239054 2004-10-24 01:30:23Z felipeal $
031: */
032: public class TestServletContext extends ServletTestCase {
033: /**
034: * The Cactus servlet context wrapper.
035: */
036: private ServletContextWrapper context;
037:
038: /**
039: * Common initialization steps for all tests.
040: */
041: public void setUp() {
042: context = (ServletContextWrapper) config.getServletContext();
043: }
044:
045: /**
046: * Verify that we can add parameters to the context list of parameters
047: * programatically, without having to define them in <code>web.xml</code>.
048: */
049: public void testSetContextInitParameterUsingApi() {
050: context.setInitParameter("testparam", "test value");
051:
052: assertEquals("test value", context
053: .getInitParameter("testparam"));
054:
055: boolean found = false;
056: Enumeration en = context.getInitParameterNames();
057:
058: while (en.hasMoreElements()) {
059: String name = (String) en.nextElement();
060:
061: if (name.equals("testparam")) {
062: found = true;
063:
064: break;
065: }
066: }
067:
068: assertTrue("[testparam] not found in parameter names", found);
069: }
070:
071: //-------------------------------------------------------------------------
072:
073: /**
074: * Verify that calling <code>setInitParameter()</code> with a parameter
075: * already defined in <code>web.xml</code> will override it.
076: */
077: public void testSetContextInitParameterOverrideWebXmlParameter() {
078: // Note: "param1" is a parameter that must be already defined in
079: // web.xml (in the context-param element), with a value different
080: // than "testoverrideparam1".
081: assertTrue(
082: "'param' context-param should been defined in web.xml",
083: context.getOriginalContext().getInitParameter("param") != null);
084: assertTrue(!context.getOriginalContext().getInitParameter(
085: "param").equals("testoverrideparam"));
086:
087: context.setInitParameter("param", "testoverrideparam");
088:
089: Enumeration en = context.getInitParameterNames();
090: int count = 0;
091:
092: while (en.hasMoreElements()) {
093: String name = (String) en.nextElement();
094:
095: if (name.equals("param")) {
096: assertEquals("testoverrideparam", context
097: .getInitParameter(name));
098: count++;
099: }
100: }
101:
102: assertTrue("[param] was found " + count
103: + " times. Should have " + "been found once.",
104: count == 1);
105: }
106:
107: }
|