KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > state > TestSessionScopeManager


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.engine.state;
16
17 import org.apache.hivemind.test.HiveMindTestCase;
18 import org.apache.tapestry.web.WebRequest;
19 import org.apache.tapestry.web.WebSession;
20 import org.easymock.MockControl;
21
22 /**
23  * Tests for {@link org.apache.tapestry.engine.state.SessionScopeManager}.
24  *
25  * @author Howard M. Lewis Ship
26  * @since 4.0
27  */

28 public class TestSessionScopeManager extends HiveMindTestCase
29 {
30     private WebRequest newRequest(boolean create, WebSession session)
31     {
32         MockControl control = newControl(WebRequest.class);
33         WebRequest request = (WebRequest) control.getMock();
34
35         request.getSession(create);
36         control.setReturnValue(session);
37
38         return request;
39     }
40
41     private WebRequest newRequest(WebSession session)
42     {
43         MockControl control = newControl(WebRequest.class);
44         WebRequest request = (WebRequest) control.getMock();
45
46         request.getSession(true);
47         control.setReturnValue(session);
48
49         return request;
50     }
51
52     private WebSession newSession(String JavaDoc key, Object JavaDoc value)
53     {
54         MockControl control = newControl(WebSession.class);
55         WebSession session = (WebSession) control.getMock();
56
57         session.getAttribute(key);
58         control.setReturnValue(value);
59
60         return session;
61     }
62
63     private StateObjectFactory newFactory(Object JavaDoc stateObject)
64     {
65         MockControl control = newControl(StateObjectFactory.class);
66         StateObjectFactory factory = (StateObjectFactory) control.getMock();
67
68         factory.createStateObject();
69         control.setReturnValue(stateObject);
70
71         return factory;
72     }
73
74     public void testExistsNoSession()
75     {
76         WebRequest request = newRequest(false, null);
77
78         replayControls();
79
80         SessionScopeManager m = new SessionScopeManager();
81         m.setRequest(request);
82
83         assertEquals(false, m.exists("doesn't matter"));
84
85         verifyControls();
86     }
87
88     public void testExistsMissing()
89     {
90         WebSession session = newSession("state:myapp:fred", null);
91         WebRequest request = newRequest(false, session);
92
93         replayControls();
94
95         SessionScopeManager m = new SessionScopeManager();
96         m.setRequest(request);
97         m.setApplicationId("myapp");
98
99         assertEquals(false, m.exists("fred"));
100
101         verifyControls();
102     }
103
104     public void testExists()
105     {
106         WebSession session = newSession("state:testapp:fred", "XXX");
107         WebRequest request = newRequest(false, session);
108
109         replayControls();
110
111         SessionScopeManager m = new SessionScopeManager();
112         m.setRequest(request);
113         m.setApplicationId("testapp");
114
115         assertEquals(true, m.exists("fred"));
116
117         verifyControls();
118     }
119
120     public void testGetExists()
121     {
122         Object JavaDoc stateObject = new Object JavaDoc();
123         WebSession session = newSession("state:testapp:fred", stateObject);
124         WebRequest request = newRequest(session);
125
126         replayControls();
127
128         SessionScopeManager m = new SessionScopeManager();
129         m.setRequest(request);
130         m.setApplicationId("testapp");
131
132         assertSame(stateObject, m.get("fred", null));
133
134         verifyControls();
135     }
136
137     public void testGetAndCreate()
138     {
139         Object JavaDoc stateObject = new Object JavaDoc();
140         StateObjectFactory factory = newFactory(stateObject);
141
142         MockControl control = newControl(WebSession.class);
143         WebSession session = (WebSession) control.getMock();
144
145         session.getAttribute("state:myapp:fred");
146         control.setReturnValue(null);
147
148         session.setAttribute("state:myapp:fred", stateObject);
149
150         WebRequest request = newRequest(session);
151
152         replayControls();
153
154         SessionScopeManager m = new SessionScopeManager();
155         m.setRequest(request);
156         m.setApplicationId("myapp");
157
158         assertSame(stateObject, m.get("fred", factory));
159
160         verifyControls();
161     }
162
163     public void testStore()
164     {
165         Object JavaDoc stateObject = new Object JavaDoc();
166
167         MockControl control = newControl(WebSession.class);
168         WebSession session = (WebSession) control.getMock();
169
170         session.setAttribute("state:myapp:fred", stateObject);
171
172         WebRequest request = newRequest(session);
173
174         replayControls();
175
176         SessionScopeManager m = new SessionScopeManager();
177         m.setRequest(request);
178         m.setApplicationId("myapp");
179
180         m.store("fred", stateObject);
181
182         verifyControls();
183     }
184 }
Popular Tags