001: // Copyright 2007 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.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018: import static org.easymock.EasyMock.eq;
019: import static org.easymock.EasyMock.isA;
020:
021: import java.util.List;
022:
023: import org.apache.tapestry.Link;
024: import org.apache.tapestry.internal.util.Holder;
025: import org.apache.tapestry.services.PersistentFieldChange;
026: import org.apache.tapestry.services.Request;
027: import org.apache.tapestry.test.TapestryTestCase;
028: import org.easymock.EasyMock;
029: import org.easymock.IAnswer;
030: import org.testng.annotations.Test;
031:
032: public class ClientPersistentFieldStorageImplTest extends
033: TapestryTestCase {
034: @Test
035: public void no_client_data_in_request() {
036: Request request = mockRequest(null);
037: Link link = mockLink();
038:
039: replay();
040:
041: ClientPersistentFieldStorage storage = new ClientPersistentFieldStorageImpl(
042: request);
043:
044: // Should do nothing.
045:
046: storage.updateLink(link);
047:
048: verify();
049: }
050:
051: @SuppressWarnings("unchecked")
052: @Test
053: public void store_and_restore_a_change() {
054: Request request = mockRequest(null);
055: Link link = mockLink();
056: final Holder<String> holder = Holder.create();
057:
058: String pageName = "Foo";
059: String componentId = "bar.baz";
060: String fieldName = "biff";
061: Object value = 99;
062:
063: // Use an IAnswer to capture the value.
064:
065: link.addParameter(
066: eq(ClientPersistentFieldStorageImpl.PARAMETER_NAME),
067: isA(String.class));
068: getMocksControl().andAnswer(new IAnswer<Void>() {
069: public Void answer() throws Throwable {
070: String base64 = (String) EasyMock.getCurrentArguments()[1];
071:
072: holder.put(base64);
073:
074: return null;
075: }
076: });
077:
078: replay();
079:
080: ClientPersistentFieldStorage storage1 = new ClientPersistentFieldStorageImpl(
081: request);
082:
083: storage1.postChange(pageName, componentId, fieldName, value);
084:
085: List<PersistentFieldChange> changes1 = newList(storage1
086: .gatherFieldChanges(pageName));
087:
088: storage1.updateLink(link);
089:
090: verify();
091:
092: System.out.println(holder.get());
093:
094: assertEquals(changes1.size(), 1);
095: PersistentFieldChange change1 = changes1.get(0);
096:
097: assertEquals(change1.getComponentId(), componentId);
098: assertEquals(change1.getFieldName(), fieldName);
099: assertEquals(change1.getValue(), value);
100:
101: // Now more training ...
102:
103: train_getParameter(request,
104: ClientPersistentFieldStorageImpl.PARAMETER_NAME, holder
105: .get());
106:
107: replay();
108:
109: ClientPersistentFieldStorage storage2 = new ClientPersistentFieldStorageImpl(
110: request);
111:
112: List<PersistentFieldChange> changes2 = newList(storage2
113: .gatherFieldChanges(pageName));
114:
115: verify();
116:
117: assertEquals(changes2.size(), 1);
118: PersistentFieldChange change2 = changes2.get(0);
119:
120: assertEquals(change2.getComponentId(), componentId);
121: assertEquals(change2.getFieldName(), fieldName);
122: assertEquals(change2.getValue(), value);
123:
124: assertNotSame(change1, change2);
125: }
126:
127: @Test
128: public void multiple_changes() {
129: Request request = mockRequest(null);
130: Link link = mockLink();
131:
132: String pageName = "Foo";
133: String componentId = "bar.baz";
134:
135: link.addParameter(
136: eq(ClientPersistentFieldStorageImpl.PARAMETER_NAME),
137: isA(String.class));
138:
139: replay();
140:
141: ClientPersistentFieldStorage storage = new ClientPersistentFieldStorageImpl(
142: request);
143:
144: for (int k = 0; k < 3; k++) {
145: for (int i = 0; i < 20; i++) {
146: // Force some cache collisions ...
147:
148: storage.postChange(pageName, componentId, "field" + i,
149: i * k);
150: }
151: }
152:
153: storage.postChange(pageName, null, "field", "foo");
154: storage.postChange(pageName, null, "field", "bar");
155:
156: storage.updateLink(link);
157:
158: verify();
159: }
160:
161: @Test
162: public void null_value_is_a_remove() {
163: Request request = mockRequest(null);
164: Link link = mockLink();
165:
166: String pageName = "Foo";
167: String componentId = "bar.baz";
168: String fieldName = "woops";
169:
170: replay();
171:
172: ClientPersistentFieldStorage storage = new ClientPersistentFieldStorageImpl(
173: request);
174:
175: storage.postChange(pageName, componentId, fieldName, 99);
176: storage.postChange(pageName, componentId, fieldName, null);
177:
178: storage.updateLink(link);
179:
180: assertTrue(storage.gatherFieldChanges(pageName).isEmpty());
181:
182: verify();
183: }
184:
185: @Test
186: public void value_not_serializable() {
187: Request request = mockRequest(null);
188:
189: Object badBody = new Object() {
190: @Override
191: public String toString() {
192: return "<BadBoy>";
193: }
194: };
195:
196: replay();
197:
198: ClientPersistentFieldStorage storage = new ClientPersistentFieldStorageImpl(
199: request);
200:
201: try {
202: storage.postChange("Foo", "bar.baz", "woops", badBody);
203: unreachable();
204: } catch (IllegalArgumentException ex) {
205: assertEquals(
206: ex.getMessage(),
207: "State persisted on the client must be serializable, but <BadBoy> does not implement the Serializable interface.");
208: }
209:
210: verify();
211: }
212:
213: @Test
214: public void corrupt_client_data() {
215: // A cut-n-paste from some previous output, with the full value significantly truncated.
216: Request request = mockRequest("H4sIAAAAAAAAAEWQsUoDQRCGJxdDTEwRU2hlZ71pBQ");
217:
218: replay();
219:
220: ClientPersistentFieldStorage storage = new ClientPersistentFieldStorageImpl(
221: request);
222:
223: try {
224: storage.postChange("Foo", "bar.baz", "woops", 99);
225: unreachable();
226: } catch (RuntimeException ex) {
227: assertEquals(ex.getMessage(), ServicesMessages
228: .corruptClientState());
229: assertNotNull(ex.getCause());
230: }
231:
232: verify();
233: }
234:
235: protected final Request mockRequest(String clientData) {
236: Request request = mockRequest();
237:
238: train_getParameter(request,
239: ClientPersistentFieldStorageImpl.PARAMETER_NAME,
240: clientData);
241:
242: return request;
243: }
244:
245: }
|