001: package servletunit;
002:
003: import javax.servlet.ServletContext;
004: import javax.servlet.http.HttpSession;
005: import javax.servlet.http.HttpSessionContext;
006: import java.util.Enumeration;
007: import java.util.Hashtable;
008:
009: // StrutsTestCase - a JUnit extension for testing Struts actions
010: // within the context of the ActionServlet.
011: // Copyright (C) 2002 Deryl Seale
012: //
013: // This library is free software; you can redistribute it and/or
014: // modify it under the terms of the Apache Software License as
015: // published by the Apache Software Foundation; either version 1.1
016: // of the License, or (at your option) any later version.
017: //
018: // This library is distributed in the hope that it will be useful,
019: // but WITHOUT ANY WARRANTY; without even the implied warranty of
020: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: // Apache Software Foundation Licens for more details.
022: //
023: // You may view the full text here: http://www.apache.org/LICENSE.txt
024:
025: /**
026: * This class simulates an HttpSession object. You can actually work with sessions. The simulation is done using a static
027: * session object inside HttpServletRequest.
028: */
029: public class HttpSessionSimulator implements HttpSession {
030: private Hashtable values;
031: private boolean valid = true;
032: private ServletContext context;
033:
034: public HttpSessionSimulator(ServletContext context) {
035: this .context = context;
036: values = new Hashtable();
037: }
038:
039: public Object getAttribute(String s) throws IllegalStateException {
040: checkValid();
041: return values.get(s);
042: }
043:
044: public Enumeration getAttributeNames() throws IllegalStateException {
045: checkValid();
046: return values.keys();
047: }
048:
049: public long getCreationTime() throws IllegalStateException {
050: checkValid();
051: return -1;
052: }
053:
054: public String getId() {
055: return "-9999";
056: }
057:
058: public long getLastAccessedTime() {
059: return -1;
060: }
061:
062: public int getMaxInactiveInterval() throws IllegalStateException {
063: checkValid();
064: return -1;
065: }
066:
067: /**
068: * This method is not supported.
069: */
070: public HttpSessionContext getSessionContext() {
071: throw new UnsupportedOperationException(
072: "getSessionContext not supported!");
073: }
074:
075: public Object getValue(String s) throws IllegalStateException {
076: checkValid();
077: return values.get(s);
078: }
079:
080: public String[] getValueNames() throws IllegalStateException {
081: checkValid();
082: return (String[]) values.keySet().toArray();
083: }
084:
085: public void invalidate() throws IllegalStateException {
086: checkValid();
087: this .valid = false;
088: }
089:
090: public boolean isNew() throws IllegalStateException {
091: checkValid();
092: return false;
093: }
094:
095: public void putValue(String s, Object obj)
096: throws IllegalStateException {
097: checkValid();
098: values.put(s, obj);
099: }
100:
101: public void removeAttribute(String s) throws IllegalStateException {
102: checkValid();
103: values.remove(s);
104: }
105:
106: public void removeValue(String s) throws IllegalStateException {
107: checkValid();
108: values.remove(s);
109: }
110:
111: public void setAttribute(String s, Object obj)
112: throws IllegalStateException {
113: checkValid();
114: if (obj == null)
115: removeValue(s);
116: else
117: values.put(s, obj);
118: }
119:
120: public void setMaxInactiveInterval(int i) {
121: }
122:
123: public ServletContext getServletContext() {
124: return this .context;
125: }
126:
127: private void checkValid() throws IllegalStateException {
128: if (!valid)
129: throw new IllegalStateException(
130: "session has been invalidated!");
131: }
132:
133: protected boolean isValid() {
134: return valid;
135: }
136: }
|