KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > SessionMapTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.mockobjects.constraint.Constraint;
8 import com.mockobjects.constraint.IsAnything;
9 import com.mockobjects.constraint.IsEqual;
10 import com.mockobjects.dynamic.Mock;
11 import junit.framework.TestCase;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpSession JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18
19 /**
20  * @author Robert Dawson (robert@rojotek.com)
21  */

22 public class SessionMapTest extends TestCase {
23     //~ Instance fields ////////////////////////////////////////////////////////
24

25     private Mock requestMock;
26     private Mock sessionMock;
27
28     //~ Methods ////////////////////////////////////////////////////////////////
29

30     public void testClearInvalidatesTheSession() throws Exception JavaDoc {
31         MockSessionMap sessionMap = new MockSessionMap((HttpServletRequest JavaDoc) requestMock.proxy());
32         sessionMap.clear();
33         sessionMock.verify();
34     }
35
36     public void testGetOnSessionMapUsesWrappedSessionsGetAttribute() throws Exception JavaDoc {
37         Object JavaDoc value = new Object JavaDoc();
38         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
39             new IsEqual("KEY")
40         }, value);
41
42         SessionMap sessionMap = new SessionMap((HttpServletRequest JavaDoc) requestMock.proxy());
43         assertEquals("Expected the get using KEY to return the value object setup in the mockSession", value, sessionMap.get("KEY"));
44         sessionMock.verify();
45     }
46
47     public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute() throws Exception JavaDoc {
48         Object JavaDoc value = new Object JavaDoc();
49         sessionMock.expect("getAttribute", new Constraint[]{new IsAnything()});
50         sessionMock.expect("setAttribute", new Constraint[]{
51             new IsEqual("KEY"), new IsEqual(value)
52         });
53
54         SessionMap sessionMap = new SessionMap((HttpServletRequest JavaDoc) requestMock.proxy());
55         sessionMap.put("KEY", value);
56         sessionMock.verify();
57     }
58
59     public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey() throws Exception JavaDoc {
60         Object JavaDoc value = new Object JavaDoc();
61         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
62             new IsEqual("KEY")
63         }, null);
64         sessionMock.expect("setAttribute", new Constraint[]{
65             new IsEqual("KEY"), new IsEqual(value)
66         });
67
68         SessionMap sessionMap = new SessionMap((HttpServletRequest JavaDoc) requestMock.proxy());
69         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));
70         sessionMock.verify();
71     }
72
73     public void testPuttingObjectInMapReturnsPreviousValueForKey() throws Exception JavaDoc {
74         Object JavaDoc originalValue = new Object JavaDoc();
75         Object JavaDoc value = new Object JavaDoc();
76         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
77             new IsEqual("KEY")
78         }, null);
79         sessionMock.expect("setAttribute", new Constraint[]{
80             new IsEqual("KEY"), new IsEqual(originalValue)
81         });
82         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
83             new IsEqual("KEY")
84         }, originalValue);
85         sessionMock.expect("setAttribute", new Constraint[]{
86             new IsEqual("KEY"), new IsEqual(value)
87         });
88
89         SessionMap sessionMap = new SessionMap((HttpServletRequest JavaDoc) requestMock.proxy());
90         sessionMap.put("KEY", originalValue);
91         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));
92         sessionMock.verify();
93     }
94
95     public void testRemovePassThroughCallToRemoveAttribute() throws Exception JavaDoc {
96         Object JavaDoc value = new Object JavaDoc();
97         sessionMock.expectAndReturn("getAttribute", new Constraint[]{
98             new IsEqual("KEY")
99         }, value);
100         sessionMock.expect("removeAttribute", new Constraint[]{
101             new IsEqual("KEY")
102         });
103
104         SessionMap sessionMap = new SessionMap((HttpServletRequest JavaDoc) requestMock.proxy());
105         assertEquals(value, sessionMap.remove("KEY"));
106         sessionMock.verify();
107     }
108
109     protected void setUp() throws Exception JavaDoc {
110         sessionMock = new Mock(HttpSession JavaDoc.class);
111         requestMock = new Mock(HttpServletRequest JavaDoc.class);
112         requestMock.matchAndReturn("getSession", new Constraint[] { new IsEqual(Boolean.FALSE)}, sessionMock.proxy());
113     }
114
115     //~ Inner Classes //////////////////////////////////////////////////////////
116

117     /**
118      * class that extends session map, making the values available in a local map -- useful
119      * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
120      * putting new data into the map.
121      */

122     private class MockSessionMap extends SessionMap {
123         private Map JavaDoc map = new HashMap JavaDoc();
124
125         public MockSessionMap(HttpServletRequest JavaDoc request) {
126             super(request);
127         }
128
129         public Object JavaDoc get(Object JavaDoc key) {
130             return map.get(key);
131         }
132
133         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
134             Object JavaDoc originalValue = super.put(key, value);
135             map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
136

137             return originalValue;
138         }
139     }
140 }
141
Popular Tags