001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base.test;
017:
018: import java.io.InputStream;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.util.Collections;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import javax.servlet.RequestDispatcher;
028: import javax.servlet.Servlet;
029: import javax.servlet.ServletContext;
030: import javax.servlet.ServletException;
031:
032: /**
033: * Mock servlet context for testing purposes.
034: * Contains nothing but attribute values.
035: *
036: * @author Christoph Beck
037: */
038: public class TestServletContext implements ServletContext {
039:
040: // contains entries of type string (attribute name) -> object (attribute values)
041: private Map attributes;
042:
043: /**
044: * Creates a new instance.
045: */
046: public TestServletContext() {
047: this (new HashMap());
048: }
049:
050: /**
051: * Creates a new instance.
052: *
053: * @param attributes A java.util.Map containing entries whose
054: * keys are strings and whose values are objects.
055: * It represents the mapping from attribute names
056: * to attribute values.
057: */
058: public TestServletContext(Map attributes) {
059: super ();
060:
061: this .attributes = attributes;
062: }
063:
064: /* (non-Javadoc)
065: * @see javax.servlet.ServletContext#getContext(java.lang.String)
066: */
067: public ServletContext getContext(String arg0) {
068: throw new UnsupportedOperationException();
069: }
070:
071: /* (non-Javadoc)
072: * @see javax.servlet.ServletContext#getMajorVersion()
073: */
074: public int getMajorVersion() {
075: throw new UnsupportedOperationException();
076: }
077:
078: /* (non-Javadoc)
079: * @see javax.servlet.ServletContext#getMinorVersion()
080: */
081: public int getMinorVersion() {
082: throw new UnsupportedOperationException();
083: }
084:
085: /* (non-Javadoc)
086: * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
087: */
088: public String getMimeType(String arg0) {
089: throw new UnsupportedOperationException();
090: }
091:
092: /* (non-Javadoc)
093: * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
094: */
095: public Set getResourcePaths(String arg0) {
096: throw new UnsupportedOperationException();
097: }
098:
099: /**
100: * Forward to {@link Class#getResource(java.lang.String)}
101: */
102: public URL getResource(String arg0) throws MalformedURLException {
103: return getClass().getResource(arg0);
104: }
105:
106: /**
107: * Forward to {@link Class#getResourceAsStream(java.lang.String)}
108: */
109: public InputStream getResourceAsStream(String arg0) {
110: return getClass().getResourceAsStream(arg0);
111: }
112:
113: /* (non-Javadoc)
114: * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
115: */
116: public RequestDispatcher getRequestDispatcher(String arg0) {
117: throw new UnsupportedOperationException();
118: }
119:
120: /* (non-Javadoc)
121: * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
122: */
123: public RequestDispatcher getNamedDispatcher(String arg0) {
124: throw new UnsupportedOperationException();
125: }
126:
127: /* (non-Javadoc)
128: * @see javax.servlet.ServletContext#getServlet(java.lang.String)
129: */
130: public Servlet getServlet(String arg0) throws ServletException {
131: throw new UnsupportedOperationException();
132: }
133:
134: /* (non-Javadoc)
135: * @see javax.servlet.ServletContext#getServlets()
136: */
137: public Enumeration getServlets() {
138: throw new UnsupportedOperationException();
139: }
140:
141: /* (non-Javadoc)
142: * @see javax.servlet.ServletContext#getServletNames()
143: */
144: public Enumeration getServletNames() {
145: throw new UnsupportedOperationException();
146: }
147:
148: /* (non-Javadoc)
149: * @see javax.servlet.ServletContext#log(java.lang.String)
150: */
151: public void log(String arg0) {
152: throw new UnsupportedOperationException();
153: }
154:
155: /* (non-Javadoc)
156: * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
157: */
158: public void log(Exception arg0, String arg1) {
159: throw new UnsupportedOperationException();
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
164: */
165: public void log(String arg0, Throwable arg1) {
166: throw new UnsupportedOperationException();
167: }
168:
169: /* (non-Javadoc)
170: * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
171: */
172: public String getRealPath(String arg0) {
173: throw new UnsupportedOperationException();
174: }
175:
176: /* (non-Javadoc)
177: * @see javax.servlet.ServletContext#getServerInfo()
178: */
179: public String getServerInfo() {
180: throw new UnsupportedOperationException();
181: }
182:
183: /* (non-Javadoc)
184: * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
185: */
186: public String getInitParameter(String arg0) {
187: throw new UnsupportedOperationException();
188: }
189:
190: /* (non-Javadoc)
191: * @see javax.servlet.ServletContext#getInitParameterNames()
192: */
193: public Enumeration getInitParameterNames() {
194: throw new UnsupportedOperationException();
195: }
196:
197: /* (non-Javadoc)
198: * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
199: */
200: public Object getAttribute(String arg0) {
201: return attributes.get(arg0);
202: }
203:
204: /* (non-Javadoc)
205: * @see javax.servlet.ServletContext#getAttributeNames()
206: */
207: public Enumeration getAttributeNames() {
208: return Collections.enumeration(attributes.keySet());
209: }
210:
211: /* (non-Javadoc)
212: * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
213: */
214: public void setAttribute(String arg0, Object arg1) {
215: attributes.put(arg0, arg1);
216: }
217:
218: /* (non-Javadoc)
219: * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
220: */
221: public void removeAttribute(String arg0) {
222: attributes.remove(arg0);
223: }
224:
225: /* (non-Javadoc)
226: * @see javax.servlet.ServletContext#getServletContextName()
227: */
228: public String getServletContextName() {
229: throw new UnsupportedOperationException();
230: }
231: }
|