1   package servletunit;
2   
3   import javax.servlet.ServletContext;
4   import javax.servlet.http.HttpSession;
5   import javax.servlet.http.HttpSessionContext;
6   import java.util.Enumeration;
7   import java.util.Hashtable;
8   
9   //  StrutsTestCase - a JUnit extension for testing Struts actions
10  //  within the context of the ActionServlet.
11  //  Copyright (C) 2002 Deryl Seale
12  //
13  //  This library is free software; you can redistribute it and/or
14  //  modify it under the terms of the Apache Software License as
15  //  published by the Apache Software Foundation; either version 1.1
16  //  of the License, or (at your option) any later version.
17  //
18  //  This library is distributed in the hope that it will be useful,
19  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  //  Apache Software Foundation Licens for more details.
22  //
23  //  You may view the full text here: http://www.apache.org/LICENSE.txt
24  
25  /**
26   * This class simulates an HttpSession object. You can actually work with sessions. The simulation is done using a static
27   * session object inside HttpServletRequest.
28   */
29  public class HttpSessionSimulator implements HttpSession
30  {
31      private Hashtable values;
32      private boolean valid = true;
33      private ServletContext context;
34  
35      public HttpSessionSimulator(ServletContext context)
36      {
37          this.context = context;
38          values = new Hashtable();
39      }
40  
41      public Object getAttribute(String s) throws IllegalStateException
42      {
43          checkValid();
44          return values.get(s);
45      }
46  
47      public Enumeration getAttributeNames() throws IllegalStateException
48      {
49          checkValid();
50          return values.keys();
51      }
52  
53      public long getCreationTime() throws IllegalStateException
54      {
55          checkValid();
56          return -1;
57      }
58  
59      public String getId()
60      {
61          return "-9999";
62      }
63  
64      public long getLastAccessedTime()
65      {
66          return -1;
67      }
68  
69      public int getMaxInactiveInterval() throws IllegalStateException
70      {
71          checkValid();
72          return -1;
73      }
74  
75      /**
76       * This method is not supported.
77       */
78      public HttpSessionContext getSessionContext()
79      {
80          throw new UnsupportedOperationException("getSessionContext not supported!");
81      }
82  
83      public Object getValue(String s) throws IllegalStateException
84      {
85          checkValid();
86          return values.get(s);
87      }
88  
89      public String[] getValueNames() throws IllegalStateException
90      {
91          checkValid();
92          return (String[]) values.keySet().toArray();
93      }
94  
95      public void invalidate() throws IllegalStateException
96      {
97          checkValid();
98          this.valid = false;
99      }
100 
101     public boolean isNew() throws IllegalStateException
102     {
103         checkValid();
104         return false;
105     }
106 
107     public void putValue(String s, Object obj) throws IllegalStateException
108     {
109         checkValid();
110         values.put(s,obj);
111     }
112 
113     public void removeAttribute(String s) throws IllegalStateException
114     {
115         checkValid();
116         values.remove(s);
117     }
118 
119     public void removeValue(String s) throws IllegalStateException
120     {
121         checkValid();
122         values.remove(s);
123     }
124 
125     public void setAttribute(String s, Object obj) throws IllegalStateException
126     {
127         checkValid();
128         if (obj == null)
129             removeValue(s);
130         else
131             values.put(s,obj);
132     }
133 
134     public void setMaxInactiveInterval(int i)
135     {
136     }
137 
138     public ServletContext getServletContext() {
139         return this.context;
140     }
141 
142     private void checkValid() throws IllegalStateException {
143         if (!valid)
144             throw new IllegalStateException("session has been invalidated!");
145     }
146 
147     protected boolean isValid() {
148         return valid;
149     }
150 }