View Javadoc

1   /*
2    * $Id: SessionMapTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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 junit.framework.TestCase;
35  
36  import com.mockobjects.constraint.Constraint;
37  import com.mockobjects.constraint.IsAnything;
38  import com.mockobjects.constraint.IsEqual;
39  import com.mockobjects.dynamic.Mock;
40  
41  
42  /***
43   */
44  public class SessionMapTest extends TestCase {
45  
46      private Mock requestMock;
47      private Mock sessionMock;
48  
49  
50      public void testClearInvalidatesTheSession() throws Exception {
51          List<String> attributeNames = new ArrayList<String>();
52          attributeNames.add("test");
53          attributeNames.add("anotherTest");
54          Enumeration attributeNamesEnum = Collections.enumeration(attributeNames);
55  
56          MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest) requestMock.proxy());
57          sessionMock.expect("setAttribute",
58                  new Constraint[] {
59                      new IsEqual("test"), new IsEqual("test value")
60                  });
61          sessionMock.expect("setAttribute",
62                  new Constraint[] {
63                      new IsEqual("anotherTest"), new IsEqual("another test value")
64                  });
65          sessionMock.expectAndReturn("getAttributeNames", attributeNamesEnum);
66          sessionMock.expect("removeAttribute",
67                  new Constraint[]{
68                      new IsEqual("test")
69                  });
70          sessionMock.expect("removeAttribute",
71                  new Constraint[]{
72                      new IsEqual("anotherTest")
73                  });
74          sessionMap.put("test", "test value");
75          sessionMap.put("anotherTest", "another test value");
76          sessionMap.clear();
77          assertNull(sessionMap.get("test"));
78          assertNull(sessionMap.get("anotherTest"));
79          sessionMock.verify();
80      }
81  
82      public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception {
83          Object value = new Object();
84          sessionMock.expectAndReturn("getAttribute", new Constraint[]{
85                  new IsEqual("KEY")
86          }, value);
87  
88          SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
89          assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY"));
90          sessionMock.verify();
91      }
92  
93      public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception {
94          Object value = new Object();
95          sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
96          sessionMock.expect("setAttribute", new Constraint[]{
97                  new IsEqual("KEY"), new IsEqual(value)
98          });
99  
100         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
101         sessionMap.put("KEY", value);
102         sessionMock.verify();
103     }
104 
105     public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception {
106         Object value = new Object();
107         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
108                 new IsEqual("KEY")
109         }, null);
110         sessionMock.expect("setAttribute", new Constraint[]{
111                 new IsEqual("KEY"), new IsEqual(value)
112         });
113 
114         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
115         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));
116         sessionMock.verify();
117     }
118 
119     public void testPuttingObjectInMapReturnsPreviousValueForKey() throws Exception {
120         Object originalValue = new Object();
121         Object value = new Object();
122         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
123                 new IsEqual("KEY")
124         }, null);
125         sessionMock.expect("setAttribute", new Constraint[]{
126                 new IsEqual("KEY"), new IsEqual(originalValue)
127         });
128         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
129                 new IsEqual("KEY")
130         }, originalValue);
131         sessionMock.expect("setAttribute", new Constraint[]{
132                 new IsEqual("KEY"), new IsEqual(value)
133         });
134 
135         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
136         sessionMap.put("KEY", originalValue);
137         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));
138         sessionMock.verify();
139     }
140 
141     public void testRemovePassThroughCallToRemoveAttribute() throws Exception {
142         Object value = new Object();
143         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
144                 new IsEqual("KEY")
145         }, value);
146         sessionMock.expect("removeAttribute", new Constraint[]{
147                 new IsEqual("KEY")
148         });
149 
150         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
151         assertEquals(value, sessionMap.remove("KEY"));
152         sessionMock.verify();
153     }
154 
155     protected void setUp() throws Exception {
156         sessionMock = new Mock(HttpSession.class);
157         requestMock = new Mock(HttpServletRequest.class);
158         requestMock.matchAndReturn("getSession", new Constraint[]{new IsEqual(Boolean.FALSE)}, sessionMock.proxy());
159     }
160 
161 
162     /***
163      * class that extends session map, making the values available in a local map -- useful
164      * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
165      * putting new data into the map.
166      */
167     private class MockSessionMap extends SessionMap {
168 
169         private static final long serialVersionUID = 8783604360786273764L;
170 
171         private Map map = new HashMap();
172 
173         public MockSessionMap(HttpServletRequest request) {
174             super(request);
175         }
176 
177         public Object get(Object key) {
178             return map.get(key);
179         }
180 
181         public Object put(Object key, Object value) {
182             Object originalValue = super.put(key, value);
183             map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
184 
185             return originalValue;
186         }
187 
188         public void clear() {
189             super.clear();
190             map.clear();
191         }
192     }
193 }