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.record;
016    
017    import static org.easymock.EasyMock.checkOrder;
018    import static org.easymock.EasyMock.expect;
019    
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.List;
023    
024    import org.apache.tapestry.BaseComponentTestCase;
025    import org.apache.tapestry.engine.ServiceEncoding;
026    import org.apache.tapestry.web.WebRequest;
027    import org.apache.tapestry.web.WebSession;
028    import org.testng.annotations.Test;
029    
030    /**
031     * Tests for {@link SessionPropertyPersistenceStrategy}.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class SessionPropertyPersistenceStrategyTest extends BaseComponentTestCase
038    {
039        private ServiceEncoding newEncoding()
040        {
041            return newMock(ServiceEncoding.class);
042        }
043    
044        private WebRequest newRequest(boolean create, WebSession session)
045        {
046            WebRequest request = newMock(WebRequest.class);
047    
048            expect(request.getSession(create)).andReturn(session);
049    
050            return request;
051        }
052    
053        private WebSession newSession()
054        {
055            return newMock(WebSession.class);
056        }
057    
058        private WebSession newSession(String attributeName, boolean remove)
059        {
060            WebSession session = newSession();
061            checkOrder(session, false);
062            
063            trainGetAttributeNames(session, Collections.singletonList(attributeName));
064    
065            if (remove)
066                session.setAttribute(attributeName, null);
067    
068            return session;
069        }
070    
071        private WebSession newSession(String attributeName, Object value)
072        {
073            WebSession session = newMock(WebSession.class);
074            checkOrder(session, false);
075            
076            expect(session.getAttributeNames()).andReturn(Collections.singletonList(attributeName));
077            
078            if (value != null)
079                trainGetAttribute(session, attributeName, value);
080    
081            return session;
082        }
083    
084        public void testAddParametersDoesNothing()
085        {
086            ServiceEncoding encoding = newEncoding();
087    
088            replay();
089    
090            SessionPropertyPersistenceStrategy strategy = new SessionPropertyPersistenceStrategy();
091    
092            strategy.addParametersForPersistentProperties(encoding, false);
093    
094            verify();
095        }
096    
097        public void testClearPageProperty()
098        {
099            WebSession session = newSession();
100            WebRequest request = newRequest(true, session);
101    
102            session.setAttribute("session,myapp,Help,bar", null);
103    
104            replay();
105    
106            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
107    
108            s.setApplicationId("myapp");
109            s.setRequest(request);
110    
111            s.store("Help", null, "bar", null);
112    
113            verify();
114        }
115    
116        public void testDiscardChangesNoMatch()
117        {
118            WebSession session = newSession("session,myapp,Home,foo", false);
119            
120            WebRequest request = newRequest();
121            expect(request.getSession(false)).andReturn(session);
122            
123            replay();
124    
125            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
126            s.setRequest(request);
127            s.setApplicationId("myapp");
128    
129            s.discardStoredChanges("Foo");
130            verify();
131        }
132    
133        public void testDiscardChangesNoSession()
134        {
135            WebRequest request = newRequest(false, null);
136    
137            replay();
138    
139            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
140            s.setRequest(request);
141    
142            s.discardStoredChanges("Foo");
143    
144            verify();
145        }
146    
147        public void testDiscardChangesWithMatch()
148        {
149            WebSession session = newSession("session,myapp,Home,foo", true);
150            
151            WebRequest request = newRequest();
152            expect(request.getSession(false)).andReturn(session);
153            
154            replay();
155    
156            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
157            s.setRequest(request);
158            s.setApplicationId("myapp");
159    
160            s.discardStoredChanges("Home");
161    
162            verify();
163        }
164    
165        public void testGetStoreChangesNoMatch()
166        {
167            WebSession session = newSession("session,myapp,Home,foo,bar", null);
168            WebRequest request = newRequest();
169            expect(request.getSession(false)).andReturn(session);
170    
171            replay();
172    
173            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
174            s.setRequest(request);
175            s.setApplicationId("myapp");
176    
177            Collection actual = s.getStoredChanges("Help");
178    
179            assertTrue(actual.isEmpty());
180    
181            verify();
182        }
183    
184        public void testGetStoredChangesNoSession()
185        {
186            WebRequest request = newRequest(false, null);
187    
188            replay();
189    
190            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
191            s.setRequest(request);
192    
193            assertTrue(s.getStoredChanges("Foo").isEmpty());
194    
195            verify();
196        }
197    
198        public void testGetStoredComponentProperty()
199        {
200            Object value = new Object();
201            WebSession session = newSession("session,myapp,Help,zap.biff,bar", value);
202            WebRequest request = newRequest(false, session);
203    
204            replay();
205    
206            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
207            s.setRequest(request);
208            s.setApplicationId("myapp");
209    
210            Collection actual = s.getStoredChanges("Help");
211    
212            assertEquals(1, actual.size());
213    
214            PropertyChange pc = (PropertyChange) actual.iterator().next();
215    
216            assertEquals("zap.biff", pc.getComponentPath());
217            assertEquals("bar", pc.getPropertyName());
218            assertSame(value, pc.getNewValue());
219    
220            verify();
221        }
222    
223        public void testGetStoredPageProperty()
224        {
225            Object value = new Object();
226            WebSession session = newSession("session,myapp,Help,bar", value);
227            WebRequest request = newRequest(false, session);
228    
229            replay();
230    
231            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
232            s.setRequest(request);
233            s.setApplicationId("myapp");
234    
235            Collection actual = s.getStoredChanges("Help");
236    
237            assertEquals(1, actual.size());
238    
239            PropertyChange pc = (PropertyChange) actual.iterator().next();
240    
241            assertNull(pc.getComponentPath());
242            assertEquals("bar", pc.getPropertyName());
243            assertSame(value, pc.getNewValue());
244    
245            verify();
246        }
247    
248        public void testStoreComponentProperty()
249        {
250            WebSession session = newSession();
251            WebRequest request = newRequest(true, session);
252    
253            Object value = new Object();
254    
255            session.setAttribute("session,gloop,Nerf,zip.zap,spaz", value);
256    
257            replay();
258    
259            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
260    
261            s.setApplicationId("gloop");
262            s.setRequest(request);
263    
264            s.store("Nerf", "zip.zap", "spaz", value);
265    
266            verify();
267        }
268    
269        public void testStorePageProperty()
270        {
271            WebSession session = newSession();
272            WebRequest request = newRequest(true, session);
273    
274            Object value = new Object();
275    
276            session.setAttribute("session,myapp,Home,foo", value);
277    
278            replay();
279    
280            SessionPropertyPersistenceStrategy s = new SessionPropertyPersistenceStrategy();
281    
282            s.setApplicationId("myapp");
283            s.setRequest(request);
284    
285            s.store("Home", null, "foo", value);
286    
287            verify();
288        }
289    
290        private void trainGetAttribute(WebSession session, String attributeName, Object value)
291        {
292            expect(session.getAttribute(attributeName)).andReturn(value);
293        }
294    
295        private void trainGetAttributeNames(WebSession session, List names)
296        {
297            expect(session.getAttributeNames()).andReturn(names);
298        }
299    }