View Javadoc

1   /*
2    * $Id: SessionMapTest.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpSession;
29  
30  import junit.framework.TestCase;
31  
32  import com.mockobjects.constraint.Constraint;
33  import com.mockobjects.constraint.IsAnything;
34  import com.mockobjects.constraint.IsEqual;
35  import com.mockobjects.dynamic.Mock;
36  
37  
38  /***
39   */
40  public class SessionMapTest extends TestCase {
41  
42      private Mock requestMock;
43      private Mock sessionMock;
44  
45  
46      public void testClearInvalidatesTheSession() throws Exception {
47      	List<String> attributeNames = new ArrayList<String>();
48      	attributeNames.add("test");
49      	attributeNames.add("anotherTest");
50      	Enumeration attributeNamesEnum = Collections.enumeration(attributeNames);
51      	
52          MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest) requestMock.proxy());
53          sessionMock.expect("setAttribute", 
54          		new Constraint[] {
55          			new IsEqual("test"), new IsEqual("test value")
56          		});
57          sessionMock.expect("setAttribute", 
58          		new Constraint[] {
59          			new IsEqual("anotherTest"), new IsEqual("another test value")
60          		});
61          sessionMock.expectAndReturn("getAttributeNames", attributeNamesEnum);
62          sessionMock.expect("removeAttribute", 
63          		new Constraint[]{
64          			new IsEqual("test")
65          		});
66          sessionMock.expect("removeAttribute", 
67          		new Constraint[]{
68          			new IsEqual("anotherTest")
69          		});
70          sessionMap.put("test", "test value");
71          sessionMap.put("anotherTest", "another test value");
72          sessionMap.clear();
73          assertNull(sessionMap.get("test"));
74          assertNull(sessionMap.get("anotherTest"));
75          sessionMock.verify();
76      }
77  
78      public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception {
79          Object value = new Object();
80          sessionMock.expectAndReturn("getAttribute", new Constraint[]{
81                  new IsEqual("KEY")
82          }, value);
83  
84          SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
85          assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY"));
86          sessionMock.verify();
87      }
88  
89      public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception {
90          Object value = new Object();
91          sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
92          sessionMock.expect("setAttribute", new Constraint[]{
93                  new IsEqual("KEY"), new IsEqual(value)
94          });
95  
96          SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
97          sessionMap.put("KEY", value);
98          sessionMock.verify();
99      }
100 
101     public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception {
102         Object value = new Object();
103         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
104                 new IsEqual("KEY")
105         }, null);
106         sessionMock.expect("setAttribute", new Constraint[]{
107                 new IsEqual("KEY"), new IsEqual(value)
108         });
109 
110         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
111         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));
112         sessionMock.verify();
113     }
114 
115     public void testPuttingObjectInMapReturnsPreviousValueForKey() throws Exception {
116         Object originalValue = new Object();
117         Object value = new Object();
118         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
119                 new IsEqual("KEY")
120         }, null);
121         sessionMock.expect("setAttribute", new Constraint[]{
122                 new IsEqual("KEY"), new IsEqual(originalValue)
123         });
124         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
125                 new IsEqual("KEY")
126         }, originalValue);
127         sessionMock.expect("setAttribute", new Constraint[]{
128                 new IsEqual("KEY"), new IsEqual(value)
129         });
130 
131         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
132         sessionMap.put("KEY", originalValue);
133         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));
134         sessionMock.verify();
135     }
136 
137     public void testRemovePassThroughCallToRemoveAttribute() throws Exception {
138         Object value = new Object();
139         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
140                 new IsEqual("KEY")
141         }, value);
142         sessionMock.expect("removeAttribute", new Constraint[]{
143                 new IsEqual("KEY")
144         });
145 
146         SessionMap sessionMap = new SessionMap((HttpServletRequest) requestMock.proxy());
147         assertEquals(value, sessionMap.remove("KEY"));
148         sessionMock.verify();
149     }
150 
151     protected void setUp() throws Exception {
152         sessionMock = new Mock(HttpSession.class);
153         requestMock = new Mock(HttpServletRequest.class);
154         requestMock.matchAndReturn("getSession", new Constraint[]{new IsEqual(Boolean.FALSE)}, sessionMock.proxy());
155     }
156 
157 
158     /***
159      * class that extends session map, making the values available in a local map -- useful
160      * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
161      * putting new data into the map.
162      */
163     private class MockSessionMap extends SessionMap {
164     	
165 		private static final long serialVersionUID = 8783604360786273764L;
166 		
167 		private Map map = new HashMap();
168 
169         public MockSessionMap(HttpServletRequest request) {
170             super(request);
171         }
172 
173         public Object get(Object key) {
174             return map.get(key);
175         }
176 
177         public Object put(Object key, Object value) {
178             Object originalValue = super.put(key, value);
179             map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
180 
181             return originalValue;
182         }
183         
184         public void clear() {
185         	super.clear();
186         	map.clear();
187         }
188     }
189 }