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.util.Collections;
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpSession;
025: import javax.servlet.http.HttpSessionContext;
026:
027: /**
028: * Mock session for testing purposes.
029: * Contains nothing but attribute values.
030: *
031: * @author Oliver Stuhr
032: */
033: public class TestSession implements HttpSession {
034:
035: // contains entries of type string (attribute name) -> object (attribute values)
036: private Map attributes;
037: private ServletContext context;
038:
039: /**
040: * Creates a new instance.
041: */
042: public TestSession() {
043: this (new HashMap());
044: }
045:
046: /**
047: * Creates a new instance.
048: *
049: * @param attributes A java.util.Map containing entries whose
050: * keys are strings and whose values are objects.
051: * It represents the mapping from attribute names
052: * to attribute values.
053: */
054: public TestSession(Map attributes) {
055: this (new TestServletContext(), attributes);
056: }
057:
058: /**
059: * Creates a new instance.
060: *
061: * @param context the servlet context we're in
062: */
063: public TestSession(ServletContext context) {
064: this (context, new HashMap());
065: }
066:
067: /**
068: * Creates a new instance.
069: *
070: * @param context the servlet context we're in
071: * @param attributes A java.util.Map containing entries whose
072: * keys are strings and whose values are objects.
073: * It represents the mapping from attribute names
074: * to attribute values.
075: */
076: public TestSession(ServletContext context, Map attributes) {
077: super ();
078:
079: this .context = context;
080: this .attributes = attributes;
081: }
082:
083: /* (non-Javadoc)
084: * @see javax.servlet.http.HttpSession#getCreationTime()
085: */
086: public long getCreationTime() {
087: throw new UnsupportedOperationException();
088: }
089:
090: /* (non-Javadoc)
091: * @see javax.servlet.http.HttpSession#getId()
092: */
093: public String getId() {
094: throw new UnsupportedOperationException();
095: }
096:
097: /* (non-Javadoc)
098: * @see javax.servlet.http.HttpSession#getLastAccessedTime()
099: */
100: public long getLastAccessedTime() {
101: throw new UnsupportedOperationException();
102: }
103:
104: /* (non-Javadoc)
105: * @see javax.servlet.http.HttpSession#getServletContext()
106: */
107: public ServletContext getServletContext() {
108: return context;
109: }
110:
111: /* (non-Javadoc)
112: * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
113: */
114: public void setMaxInactiveInterval(int interval) {
115: throw new UnsupportedOperationException();
116: }
117:
118: /* (non-Javadoc)
119: * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
120: */
121: public int getMaxInactiveInterval() {
122: throw new UnsupportedOperationException();
123: }
124:
125: /* (non-Javadoc)
126: * @see javax.servlet.http.HttpSession#getSessionContext()
127: */
128: public HttpSessionContext getSessionContext() {
129: throw new UnsupportedOperationException();
130: }
131:
132: /* (non-Javadoc)
133: * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
134: */
135: public Object getAttribute(String name) {
136: return attributes.get(name);
137: }
138:
139: /* (non-Javadoc)
140: * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
141: */
142: public Object getValue(String name) {
143: throw new UnsupportedOperationException();
144: }
145:
146: /* (non-Javadoc)
147: * @see javax.servlet.http.HttpSession#getAttributeNames()
148: */
149: public Enumeration getAttributeNames() {
150: return Collections.enumeration(attributes.keySet());
151: }
152:
153: /* (non-Javadoc)
154: * @see javax.servlet.http.HttpSession#getValueNames()
155: */
156: public String[] getValueNames() {
157: throw new UnsupportedOperationException();
158: }
159:
160: /* (non-Javadoc)
161: * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
162: */
163: public void setAttribute(String name, Object value) {
164: attributes.put(name, value);
165: }
166:
167: /* (non-Javadoc)
168: * @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)
169: */
170: public void putValue(String name, Object value) {
171: throw new UnsupportedOperationException();
172: }
173:
174: /* (non-Javadoc)
175: * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
176: */
177: public void removeAttribute(String name) {
178: attributes.remove(name);
179: }
180:
181: /* (non-Javadoc)
182: * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
183: */
184: public void removeValue(String name) {
185: throw new UnsupportedOperationException();
186: }
187:
188: /* (non-Javadoc)
189: * @see javax.servlet.http.HttpSession#invalidate()
190: */
191: public void invalidate() {
192: attributes = new HashMap();
193: }
194:
195: /* (non-Javadoc)
196: * @see javax.servlet.http.HttpSession#isNew()
197: */
198: public boolean isNew() {
199: throw new UnsupportedOperationException();
200: }
201:
202: public String toString() {
203: return getClass().getName() + " [attr " + attributes + "]";
204: }
205: }
|