KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > record > TestClientPropertyPersistenceStrategy


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.record;
16
17 import java.util.Arrays JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.apache.tapestry.IPage;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.engine.ServiceEncoding;
25 import org.apache.tapestry.web.WebRequest;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for {@link org.apache.tapestry.record.ClientPropertyPersistenceStrategy}.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

34 public class TestClientPropertyPersistenceStrategy extends HiveMindTestCase
35 {
36     public void testInitialize()
37     {
38         MockControl requestc = newControl(WebRequest.class);
39         WebRequest request = (WebRequest) requestc.getMock();
40
41         request.getParameterNames();
42         requestc.setReturnValue(Arrays.asList(new Object JavaDoc[]
43         { "foo", "state:MyPage" }));
44
45         request.getParameterValue("state:MyPage");
46         requestc.setReturnValue("ENCODED");
47
48         MockControl encoderc = newControl(PersistentPropertyDataEncoder.class);
49         PersistentPropertyDataEncoder encoder = (PersistentPropertyDataEncoder) encoderc.getMock();
50
51         List JavaDoc changes = Collections.singletonList(new PropertyChangeImpl("foo", "bar", "baz"));
52
53         encoder.decodePageChanges("ENCODED");
54         encoderc.setReturnValue(changes);
55
56         replayControls();
57
58         ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy(encoder);
59         strategy.setRequest(request);
60
61         strategy.initializeService();
62
63         assertSame(changes, strategy.getStoredChanges("MyPage", null));
64
65         verifyControls();
66     }
67
68     public void testGetChangesUnknownPage()
69     {
70         ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy();
71
72         assertTrue(strategy.getStoredChanges("UnknownPage", null).isEmpty());
73     }
74
75     public void testStoreAndRetrieve()
76     {
77         PropertyChange pc = new PropertyChangeImpl("foo", "bar", "baz");
78
79         ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy();
80
81         strategy.store("MyPage", "foo", "bar", "baz");
82
83         assertEquals(Collections.singletonList(pc), strategy.getStoredChanges("MyPage", null));
84
85         strategy.discardStoredChanges("MyPage", null);
86
87         assertEquals(Collections.EMPTY_LIST, strategy.getStoredChanges("MyPage", null));
88     }
89
90     public void testAddParametersForPersistentProperties()
91     {
92         MockControl requestc = newControl(WebRequest.class);
93         WebRequest request = (WebRequest) requestc.getMock();
94
95         IRequestCycle cycle = (IRequestCycle) newMock(IRequestCycle.class);
96         ServiceEncoding encoding = (ServiceEncoding) newMock(ServiceEncoding.class);
97
98         request.getParameterNames();
99         requestc.setReturnValue(Arrays.asList(new Object JavaDoc[]
100         { "foo", "state:MyPage" }));
101
102         request.getParameterValue("state:MyPage");
103         requestc.setReturnValue("ENCODED");
104
105         encoding.setParameterValue("state:MyPage", "ENCODED");
106
107         replayControls();
108
109         ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy();
110         strategy.setRequest(request);
111         strategy.setScope(new AppClientPropertyPersistenceScope());
112
113         strategy.initializeService();
114         
115         strategy.addParametersForPersistentProperties(encoding, cycle);
116
117         verifyControls();
118
119     }
120     
121     public void testPageScope()
122     {
123         MockControl requestc = newControl(WebRequest.class);
124         WebRequest request = (WebRequest) requestc.getMock();
125
126         MockControl cyclec = newControl(IRequestCycle.class);
127         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
128         
129         MockControl pagec = newControl(IPage.class);
130         IPage page = (IPage) pagec.getMock();
131         
132         ServiceEncoding encoding = (ServiceEncoding) newMock(ServiceEncoding.class);
133
134         cycle.getPage();
135         cyclec.setReturnValue(page);
136         
137         cycle.getPage();
138         cyclec.setReturnValue(page);
139         
140         page.getPageName();
141         pagec.setReturnValue("MyPage");
142         
143         page.getPageName();
144         pagec.setReturnValue("MyPage");
145         
146         request.getParameterNames();
147         requestc.setReturnValue(Arrays.asList(new Object JavaDoc[]
148         { "foo", "state:MyPage", "state:OtherPage" }));
149
150         request.getParameterValue("state:MyPage");
151         requestc.setReturnValue("ENCODED");
152
153         request.getParameterValue("state:OtherPage");
154         requestc.setReturnValue("ENCODED");
155
156         encoding.setParameterValue("state:MyPage", "ENCODED");
157
158         replayControls();
159
160         ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy();
161         strategy.setRequest(request);
162         strategy.setScope(new PageClientPropertyPersistenceScope());
163
164         strategy.initializeService();
165         
166         strategy.addParametersForPersistentProperties(encoding, cycle);
167
168         verifyControls();
169
170     }
171 }
Popular Tags