001: /*
002: * $Id: MockHttpSession.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 javax.servlet.ServletContext;
024: import javax.servlet.http.HttpSession;
025: import javax.servlet.http.HttpSessionContext;
026:
027: import java.util.Enumeration;
028: import java.util.HashMap;
029:
030: /**
031: * <p>Mock <strong>HttpSession</strong> object for low-level unit tests of
032: * Struts controller components. Coarser grained tests should be implemented
033: * in terms of the Cactus framework, instead of the mock object classes.</p>
034: *
035: * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
036: * create unit tests is provided, plus additional methods to configure this
037: * object as necessary. Methods for unsupported operations will throw
038: * <code>UnsupportedOperationException</code>.</p>
039: *
040: * <p><strong>WARNING</strong> - Because unit tests operate in a single
041: * threaded environment, no synchronization is performed.</p>
042: *
043: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
044: * $
045: */
046: public class MockHttpSession implements HttpSession {
047: // ----------------------------------------------------- Instance Variables
048:
049: /**
050: * <p> The set of session attributes. </p>
051: */
052: protected HashMap attributes = new HashMap();
053:
054: /**
055: * <p> The ServletContext with which we are associated. </p>
056: */
057: protected ServletContext servletContext = null;
058:
059: // ----------------------------------------------------------- Constructors
060: public MockHttpSession() {
061: super ();
062: }
063:
064: public MockHttpSession(ServletContext servletContext) {
065: super ();
066: setServletContext(servletContext);
067: }
068:
069: // --------------------------------------------------------- Public Methods
070: public void setServletContext(ServletContext servletContext) {
071: this .servletContext = servletContext;
072: }
073:
074: // ---------------------------------------------------- HttpSession Methods
075: public Object getAttribute(String name) {
076: return (attributes.get(name));
077: }
078:
079: public Enumeration getAttributeNames() {
080: return (new MockEnumeration(attributes.keySet().iterator()));
081: }
082:
083: public long getCreationTime() {
084: throw new UnsupportedOperationException();
085: }
086:
087: public String getId() {
088: throw new UnsupportedOperationException();
089: }
090:
091: public long getLastAccessedTime() {
092: throw new UnsupportedOperationException();
093: }
094:
095: public int getMaxInactiveInterval() {
096: throw new UnsupportedOperationException();
097: }
098:
099: public ServletContext getServletContext() {
100: return (this .servletContext);
101: }
102:
103: public HttpSessionContext getSessionContext() {
104: throw new UnsupportedOperationException();
105: }
106:
107: public Object getValue(String name) {
108: throw new UnsupportedOperationException();
109: }
110:
111: public String[] getValueNames() {
112: throw new UnsupportedOperationException();
113: }
114:
115: public void invalidate() {
116: throw new UnsupportedOperationException();
117: }
118:
119: public boolean isNew() {
120: throw new UnsupportedOperationException();
121: }
122:
123: public void putValue(String name, Object value) {
124: throw new UnsupportedOperationException();
125: }
126:
127: public void removeAttribute(String name) {
128: attributes.remove(name);
129: }
130:
131: public void removeValue(String name) {
132: throw new UnsupportedOperationException();
133: }
134:
135: public void setAttribute(String name, Object value) {
136: if (value == null) {
137: attributes.remove(name);
138: } else {
139: attributes.put(name, value);
140: }
141: }
142:
143: public void setMaxInactiveInterval(int interval) {
144: throw new UnsupportedOperationException();
145: }
146: }
|