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.expect; 018 019 import java.util.Arrays; 020 import java.util.Collections; 021 import java.util.List; 022 023 import org.apache.tapestry.BaseComponentTestCase; 024 import org.apache.tapestry.IPage; 025 import org.apache.tapestry.IRequestCycle; 026 import org.apache.tapestry.engine.ServiceEncoding; 027 import org.apache.tapestry.web.WebRequest; 028 import org.testng.annotations.Test; 029 030 /** 031 * Tests for {@link org.apache.tapestry.record.ClientPropertyPersistenceStrategy}. 032 * 033 * @author Howard M. Lewis Ship 034 * @since 4.0 035 */ 036 @Test 037 public class ClientPropertyPersistenceStrategyTest extends BaseComponentTestCase 038 { 039 private PersistentPropertyDataEncoder newEncoder() 040 { 041 PersistentPropertyDataEncoderImpl encoder = new PersistentPropertyDataEncoderImpl(); 042 encoder.setClassResolver(getClassResolver()); 043 044 return encoder; 045 } 046 047 private ClientPropertyPersistenceScope newScope() 048 { 049 return newMock(ClientPropertyPersistenceScope.class); 050 } 051 052 public void testAddParametersForPersistentProperties() 053 { 054 WebRequest request = newRequest(); 055 056 ServiceEncoding encoding = newMock(ServiceEncoding.class); 057 058 trainGetParameterNames(request, new String[] 059 { "bar", "appstate:MyPage" }); 060 061 trainGetParameterValue(request, "appstate:MyPage", "ENCODED"); 062 063 encoding.setParameterValue("appstate:MyPage", "ENCODED"); 064 065 replay(); 066 067 ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy(); 068 strategy.setRequest(request); 069 strategy.setScope(new AppClientPropertyPersistenceScope()); 070 strategy.setEncoder(newEncoder()); 071 072 strategy.initializeService(); 073 074 strategy.addParametersForPersistentProperties(encoding, false); 075 076 verify(); 077 } 078 079 public void testGetChangesUnknownPage() 080 { 081 ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy(); 082 083 assertTrue(strategy.getStoredChanges("UnknownPage").isEmpty()); 084 } 085 086 public void testInitialize() 087 { 088 WebRequest request = newRequest(); 089 ClientPropertyPersistenceScope scope = newScope(); 090 PersistentPropertyDataEncoder encoder = newMock(PersistentPropertyDataEncoder.class); 091 092 trainGetParameterNames(request, new String[] 093 { "foo", "state:MyPage" }); 094 095 trainIsParameterForScope(scope, "foo", false); 096 trainIsParameterForScope(scope, "state:MyPage", true); 097 098 trainExtractPageName(scope, "state:MyPage", "MyPage"); 099 100 trainGetParameterValue(request, "state:MyPage", "ENCODED"); 101 102 List changes = Collections.singletonList(new PropertyChangeImpl("foo", "bar", "baz")); 103 104 trainDecodePageChanges(encoder, "ENCODED", changes); 105 106 replay(); 107 108 ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy(); 109 strategy.setRequest(request); 110 strategy.setScope(scope); 111 strategy.setEncoder(encoder); 112 113 strategy.initializeService(); 114 115 assertSame(changes, strategy.getStoredChanges("MyPage")); 116 117 verify(); 118 } 119 120 public void testPageScope() 121 { 122 WebRequest request = newRequest(); 123 IRequestCycle cycle = newCycle(); 124 IPage page = newPage(); 125 126 ServiceEncoding encoding = newMock(ServiceEncoding.class); 127 128 trainGetParameterNames(request, new String[] { "foo", "state:MyPage", "state:OtherPage" }); 129 130 trainGetParameterValue(request, "state:MyPage", "ENCODED1"); 131 trainGetParameterValue(request, "state:OtherPage", "ENCODED2"); 132 133 trainGetPage(cycle, page); 134 trainGetPageName(page, "MyPage"); 135 136 encoding.setParameterValue("state:MyPage", "ENCODED1"); 137 138 trainGetPage(cycle, page); 139 trainGetPageName(page, "MyPage"); 140 141 142 replay(); 143 144 PageClientPropertyPersistenceScope scope = new PageClientPropertyPersistenceScope(); 145 scope.setRequestCycle(cycle); 146 147 ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy(); 148 strategy.setRequest(request); 149 strategy.setScope(scope); 150 strategy.setEncoder(newEncoder()); 151 152 strategy.initializeService(); 153 154 strategy.addParametersForPersistentProperties(encoding, false); 155 156 verify(); 157 158 } 159 160 public void testStoreAndRetrieve() 161 { 162 PropertyChange pc = new PropertyChangeImpl("foo", "bar", "baz"); 163 164 ClientPropertyPersistenceStrategy strategy = new ClientPropertyPersistenceStrategy(); 165 strategy.setEncoder(newEncoder()); 166 167 strategy.store("MyPage", "foo", "bar", "baz"); 168 169 assertEquals(Collections.singletonList(pc), strategy.getStoredChanges("MyPage")); 170 171 strategy.discardStoredChanges("MyPage"); 172 173 assertEquals(Collections.EMPTY_LIST, strategy.getStoredChanges("MyPage")); 174 } 175 176 private void trainDecodePageChanges(PersistentPropertyDataEncoder encoder, String encoded, 177 List changes) 178 { 179 expect(encoder.decodePageChanges(encoded)).andReturn(changes); 180 } 181 182 private void trainExtractPageName(ClientPropertyPersistenceScope scope, String parameterName, 183 String pageName) 184 { 185 expect(scope.extractPageName(parameterName)).andReturn(pageName); 186 } 187 188 private void trainGetPage(IRequestCycle cycle, IPage page) 189 { 190 expect(cycle.getPage()).andReturn(page); 191 } 192 193 private void trainGetParameterNames(WebRequest request, String[] names) 194 { 195 expect(request.getParameterNames()).andReturn(Arrays.asList(names)); 196 } 197 198 private void trainGetParameterValue(WebRequest request, String parameterName, String value) 199 { 200 expect(request.getParameterValue(parameterName)).andReturn(value); 201 } 202 203 private void trainIsParameterForScope(ClientPropertyPersistenceScope scope, 204 String parameterName, boolean result) 205 { 206 expect(scope.isParameterForScope(parameterName)).andReturn(result); 207 } 208 }