001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.engine.state;
016    
017    import org.apache.tapestry.BaseComponentTestCase;
018    import org.apache.tapestry.SessionStoreOptimized;
019    import org.apache.tapestry.web.WebRequest;
020    import org.apache.tapestry.web.WebSession;
021    import static org.easymock.EasyMock.checkOrder;
022    import static org.easymock.EasyMock.expect;
023    import org.testng.annotations.Test;
024    
025    /**
026     * Tests for {@link org.apache.tapestry.engine.state.SessionScopeManager}.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    @Test(sequential = true)
032    public class TestSessionScopeManager extends BaseComponentTestCase
033    {
034        private WebRequest newRequest(boolean create, WebSession session)
035        {
036            WebRequest request = newMock(WebRequest.class);
037            
038            expect(request.getSession(create)).andReturn(session);
039    
040            return request;
041        }
042    
043        private WebRequest newRequest(WebSession session)
044        {
045            WebRequest request = newMock(WebRequest.class);
046    
047            expect(request.getSession(true)).andReturn(session);
048    
049            return request;
050        }
051    
052        private WebSession newSession(String key, Object value)
053        {
054            WebSession session = newSession();
055            checkOrder(session, false);
056            
057            trainGetAttribute(session, key, value);
058    
059            return session;
060        }
061    
062        private StateObjectFactory newFactory(Object stateObject)
063        {
064            StateObjectFactory factory = newMock(StateObjectFactory.class);
065    
066            expect(factory.createStateObject()).andReturn(stateObject);
067    
068            return factory;
069        }
070    
071        public void testExistsNoSession()
072        {
073            WebRequest request = newRequest(false, null);
074    
075            replay();
076    
077            SessionScopeManager m = new SessionScopeManager();
078            m.setRequest(request);
079    
080            assertEquals(false, m.exists("doesn't matter"));
081    
082            verify();
083        }
084    
085        public void testExistsMissing()
086        {
087            WebSession session = newSession("state:myapp:fred", null);
088            WebRequest request = newRequest(false, session);
089            
090            replay();
091    
092            SessionScopeManager m = new SessionScopeManager();
093            m.setRequest(request);
094            m.setApplicationId("myapp");
095    
096            assertEquals(false, m.exists("fred"));
097    
098            verify();
099        }
100    
101        public void testExists()
102        {
103            WebSession session = newSession("state:testapp:fred", "XXX");
104            WebRequest request = newRequest(false, session);
105    
106            replay();
107    
108            SessionScopeManager m = new SessionScopeManager();
109            m.setRequest(request);
110            m.setApplicationId("testapp");
111    
112            assertEquals(true, m.exists("fred"));
113    
114            verify();
115        }
116    
117        public void testGetExists()
118        {
119            Object stateObject = new Object();
120            WebSession session = newSession("state:testapp:fred", stateObject);
121            WebRequest request = newRequest(session);
122    
123            replay();
124    
125            SessionScopeManager m = new SessionScopeManager();
126            m.setRequest(request);
127            m.setApplicationId("testapp");
128    
129            assertSame(stateObject, m.get("fred", null));
130    
131            verify();
132        }
133    
134        public void testGetAndCreate()
135        {
136            Object stateObject = new Object();
137            
138            WebSession session = newSession();
139            
140            WebRequest request = newRequest(session);
141            
142            trainGetAttribute(session, "state:myapp:fred", null);
143    
144            StateObjectFactory factory = newFactory(stateObject);
145            
146            session.setAttribute("state:myapp:fred", stateObject);
147    
148            replay();
149    
150            SessionScopeManager m = new SessionScopeManager();
151            m.setRequest(request);
152            m.setApplicationId("myapp");
153    
154            assertSame(stateObject, m.get("fred", factory));
155    
156            verify();
157        }
158    
159        protected void trainGetAttribute(WebSession session, String name, Object attribute)
160        {
161            expect(session.getAttribute(name)).andReturn(attribute);
162        }
163    
164        public void testStore()
165        {
166            Object stateObject = new Object();
167    
168            WebSession session = newSession();
169            WebRequest request = newRequest(session);
170            
171            session.setAttribute("state:myapp:fred", stateObject);
172    
173            replay();
174    
175            SessionScopeManager m = new SessionScopeManager();
176            m.setRequest(request);
177            m.setApplicationId("myapp");
178    
179            m.store("fred", stateObject);
180    
181            verify();
182        }
183    
184        protected WebSession newSession()
185        {
186            return newMock(WebSession.class);
187        }
188    
189        protected SessionStoreOptimized newOptimized(boolean dirty)
190        {
191            SessionStoreOptimized optimized = newMock(SessionStoreOptimized.class);
192    
193            expect(optimized.isStoreToSessionNeeded()).andReturn(dirty);
194    
195            return optimized;
196        }
197    
198        public void testStoreOptimizedClean()
199        {
200            Object stateObject = newOptimized(false);
201    
202            SessionScopeManager m = new SessionScopeManager();
203    
204            replay();
205    
206            m.store("fred", stateObject);
207    
208            verify();
209        }
210    
211        public void testStoreOptimizedDirty()
212        {
213            Object stateObject = newOptimized(true);
214    
215            WebSession session = newSession();
216            WebRequest request = newRequest(session);
217            
218            session.setAttribute("state:myapp:fred", stateObject);
219    
220            replay();
221    
222            SessionScopeManager m = new SessionScopeManager();
223            m.setRequest(request);
224            m.setApplicationId("myapp");
225    
226            m.store("fred", stateObject);
227    
228            verify();
229        }
230    }