001: /*
002: * $Id: MockServletContext.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.mock;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import javax.servlet.RequestDispatcher;
027: import javax.servlet.Servlet;
028: import javax.servlet.ServletContext;
029:
030: import java.io.InputStream;
031:
032: import java.net.URL;
033:
034: import java.util.Enumeration;
035: import java.util.HashMap;
036: import java.util.Set;
037:
038: /**
039: * <p>Mock <strong>ServletContext</strong> object for low-level unit tests of
040: * Struts controller components. Coarser grained tests should be implemented
041: * in terms of the Cactus framework, instead of the mock object classes.</p>
042: *
043: * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
044: * create unit tests is provided, plus additional methods to configure this
045: * object as necessary. Methods for unsupported operations will throw
046: * <code>UnsupportedOperationException</code>.</p>
047: *
048: * <p><strong>WARNING</strong> - Because unit tests operate in a single
049: * threaded environment, no synchronization is performed.</p>
050: *
051: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
052: * $
053: */
054: public class MockServletContext implements ServletContext {
055: // ----------------------------------------------------- Instance Variables
056:
057: /**
058: * <p> The set of servlet context attributes. </p>
059: */
060: protected HashMap attributes = new HashMap();
061:
062: /**
063: * <p> Default destination for <code>LOG()</code> output. </p>
064: */
065: protected Log log = LogFactory.getLog(MockServletContext.class);
066:
067: /**
068: * <p> The set of context initialization parameters. </p>
069: */
070: protected HashMap parameters = new HashMap();
071:
072: // --------------------------------------------------------- Public Methods
073: public void addInitParameter(String name, String value) {
074: parameters.put(name, value);
075: }
076:
077: public void setLog(Log log) {
078: this .log = log;
079: }
080:
081: // ------------------------------------------------- ServletContext Methods
082: public Object getAttribute(String name) {
083: return (attributes.get(name));
084: }
085:
086: public Enumeration getAttributeNames() {
087: return (new MockEnumeration(attributes.keySet().iterator()));
088: }
089:
090: public ServletContext getContext(String uripath) {
091: throw new UnsupportedOperationException();
092: }
093:
094: public String getInitParameter(String name) {
095: return ((String) parameters.get(name));
096: }
097:
098: public Enumeration getInitParameterNames() {
099: return (new MockEnumeration(parameters.keySet().iterator()));
100: }
101:
102: public int getMajorVersion() {
103: return (2);
104: }
105:
106: public String getMimeType(String file) {
107: throw new UnsupportedOperationException();
108: }
109:
110: public int getMinorVersion() {
111: return (3);
112: }
113:
114: public RequestDispatcher getNamedDispatcher(String name) {
115: throw new UnsupportedOperationException();
116: }
117:
118: public String getRealPath(String path) {
119: throw new UnsupportedOperationException();
120: }
121:
122: public RequestDispatcher getRequestDispatcher(String path) {
123: throw new UnsupportedOperationException();
124: }
125:
126: public URL getResource(String path) {
127: return this .getClass().getResource(path);
128:
129: //throw new UnsupportedOperationException();
130: }
131:
132: public InputStream getResourceAsStream(String path) {
133: return this .getClass().getResourceAsStream(path);
134:
135: //throw new UnsupportedOperationException();
136: }
137:
138: public Set getResourcePaths(String path) {
139: throw new UnsupportedOperationException();
140: }
141:
142: public String getServerInfo() {
143: return ("MockServletContext/$Version$");
144: }
145:
146: public Servlet getServlet(String name) {
147: throw new UnsupportedOperationException();
148: }
149:
150: public String getServletContextName() {
151: return (getServerInfo());
152: }
153:
154: public Enumeration getServletNames() {
155: throw new UnsupportedOperationException();
156: }
157:
158: public Enumeration getServlets() {
159: throw new UnsupportedOperationException();
160: }
161:
162: public void log(Exception exception, String message) {
163: log(message, exception);
164: }
165:
166: public void log(String message) {
167: log.info(message);
168: }
169:
170: public void log(String message, Throwable throwable) {
171: log.error(message, throwable);
172: }
173:
174: public void removeAttribute(String name) {
175: attributes.remove(name);
176: }
177:
178: public void setAttribute(String name, Object value) {
179: if (value == null) {
180: attributes.remove(name);
181: } else {
182: attributes.put(name, value);
183: }
184: }
185: }
|