View Javadoc

1   /*
2    * $Id: SessionMapTest.java 724839 2008-12-09 19:19:19Z nilsga $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dispatcher;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpSession;
33  
34  import org.springframework.mock.web.MockHttpServletRequest;
35  
36  import junit.framework.TestCase;
37  
38  import com.mockobjects.constraint.Constraint;
39  import com.mockobjects.constraint.IsAnything;
40  import com.mockobjects.constraint.IsEqual;
41  import com.mockobjects.dynamic.Mock;
42  
43  
44  /***
45   */
46  public class SessionMapTest extends TestCase {
47  
48      private Mock requestMock;
49      private Mock sessionMock;
50  
51  
52      public void testClearInvalidatesTheSession() throws Exception {
53          List<String> attributeNames = new ArrayList<String>();
54          attributeNames.add("test");
55          attributeNames.add("anotherTest");
56          Enumeration attributeNamesEnum = Collections.enumeration(attributeNames);
57  
58          MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest) requestMock.proxy());
59          sessionMock.expect("setAttribute",
60                  new Constraint[] {
61                      new IsEqual("test"), new IsEqual("test value")
62                  });
63          sessionMock.expect("setAttribute",
64                  new Constraint[] {
65                      new IsEqual("anotherTest"), new IsEqual("another test value")
66                  });
67          sessionMock.expectAndReturn("getAttributeNames", attributeNamesEnum);
68          sessionMock.expect("removeAttribute",
69                  new Constraint[]{
70                      new IsEqual("test")
71                  });
72          sessionMock.expect("removeAttribute",
73                  new Constraint[]{
74                      new IsEqual("anotherTest")
75                  });
76          sessionMap.put("test", "test value");
77          sessionMap.put("anotherTest", "another test value");
78          sessionMap.clear();
79          assertNull(sessionMap.get("test"));
80          assertNull(sessionMap.get("anotherTest"));
81          sessionMock.verify();
82      }
83  
84      public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception {
85          Object value = new Object();
86          sessionMock.expectAndReturn("getAttribute", new Constraint[]{
87                  new IsEqual("KEY")
88          }, value);
89  
90          SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
91          assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY"));
92          sessionMock.verify();
93      }
94  
95      public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception {
96          Object value = new Object();
97          sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
98          sessionMock.expect("setAttribute", new Constraint[]{
99                  new IsEqual("KEY"), new IsEqual(value)
100         });
101 
102         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
103         sessionMap.put("KEY", value);
104         sessionMock.verify();
105     }
106 
107     public void testGetObjectOnSessionMapUsesWrappedSessionsGetAttributeWithStringValue() throws Exception {
108         Object key = new Object();
109         Object value = new Object();
110         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
111                 new IsEqual(key.toString())
112         }, value);
113 
114         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
115         assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get(key));
116         sessionMock.verify();
117     }
118 
119     public void testPutObjectOnSessionMapUsesWrappedSessionsSetsAttributeWithStringValue() throws Exception {
120         Object key = new Object();
121         Object value = new Object();
122         sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
123         sessionMock.expect("setAttribute", new Constraint[]{
124                 new IsEqual(key.toString()), new IsEqual(value)
125         });
126 
127         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
128         sessionMap.put(key, value);
129         sessionMock.verify();
130     }
131     
132     public void testContainsKeyWillFindAnObjectPutOnSessionMap() throws Exception {
133     	
134     	MockHttpServletRequest request = new MockHttpServletRequest();
135     	
136         Object key = new Object();
137         Object value = new Object();
138         
139         SessionMap<Object, Object> sessionMap = new SessionMap<Object, Object>(request);
140         sessionMap.put(key, value);
141         assertTrue(sessionMap.containsKey(key));
142     }
143 
144     public void testContainsKeyWillReturnFalseIfObjectNotFoundOnSessionMap() throws Exception {
145     	
146     	MockHttpServletRequest request = new MockHttpServletRequest();
147     	
148         Object key = new Object();
149         Object someOtherKey = new Object();
150         Object value = new Object();
151         
152         SessionMap<Object, Object> sessionMap = new SessionMap<Object, Object>(request);
153         sessionMap.put(key, value);
154         
155         assertFalse(sessionMap.containsKey(someOtherKey));
156     }
157 
158     public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception {
159         Object value = new Object();
160         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
161                 new IsEqual("KEY")
162         }, null);
163         sessionMock.expect("setAttribute", new Constraint[]{
164                 new IsEqual("KEY"), new IsEqual(value)
165         });
166 
167         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
168         assertNull("should be null, as the contract for Map says that put returns the previous value in the map for the key", sessionMap.put("KEY", value));
169         sessionMock.verify();
170     }
171 
172     public void testPuttingObjectInMapReturnsPreviousValueForKey() throws Exception {
173         Object originalValue = new Object();
174         Object value = new Object();
175         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
176                 new IsEqual("KEY")
177         }, null);
178         sessionMock.expect("setAttribute", new Constraint[]{
179                 new IsEqual("KEY"), new IsEqual(originalValue)
180         });
181         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
182                 new IsEqual("KEY")
183         }, originalValue);
184         sessionMock.expect("setAttribute", new Constraint[]{
185                 new IsEqual("KEY"), new IsEqual(value)
186         });
187 
188         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
189         sessionMap.put("KEY", originalValue);
190         assertEquals("should be the OriginalValue, as the contract for Map says that put returns the previous value in the map for the key", originalValue, sessionMap.put("KEY", value));
191         sessionMock.verify();
192     }
193 
194     public void testRemovePassThroughCallToRemoveAttribute() throws Exception {
195         Object value = new Object();
196         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
197                 new IsEqual("KEY")
198         }, value);
199         sessionMock.expect("removeAttribute", new Constraint[]{
200                 new IsEqual("KEY")
201         });
202 
203         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
204         assertEquals(value, sessionMap.remove("KEY"));
205         sessionMock.verify();
206     }
207 
208     protected void setUp() throws Exception {
209         sessionMock = new Mock(HttpSession.class);
210         requestMock = new Mock(HttpServletRequest.class);
211         requestMock.matchAndReturn("getSession", new Constraint[]{new IsEqual(Boolean.FALSE)}, sessionMock.proxy());
212     }
213 
214 
215     /***
216      * class that extends session map, making the values available in a local map -- useful
217      * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
218      * putting new data into the map.
219      */
220     private class MockSessionMap extends SessionMap {
221 
222         private static final long serialVersionUID = 8783604360786273764L;
223 
224         private Map map = new HashMap();
225 
226         public MockSessionMap(HttpServletRequest request) {
227             super(request);
228         }
229 
230         public Object get(Object key) {
231             return map.get(key);
232         }
233 
234         public Object put(Object key, Object value) {
235             Object originalValue = super.put(key, value);
236             map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
237 
238             return originalValue;
239         }
240 
241         public void clear() {
242             super.clear();
243             map.clear();
244         }
245     }
246 }