001 // Copyright 2004, 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.services.impl; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.hivemind.ErrorLog; 019 import org.apache.tapestry.BaseComponentTestCase; 020 import org.apache.tapestry.IEngine; 021 import org.apache.tapestry.IRequestCycle; 022 import org.apache.tapestry.engine.IEngineService; 023 import org.apache.tapestry.engine.ILink; 024 import org.apache.tapestry.engine.ServiceEncoder; 025 import org.apache.tapestry.engine.ServiceEncoding; 026 import org.apache.tapestry.engine.encoders.PageServiceEncoder; 027 import org.apache.tapestry.record.PropertyPersistenceStrategy; 028 import org.apache.tapestry.record.PropertyPersistenceStrategySource; 029 import org.apache.tapestry.services.LinkFactory; 030 import org.apache.tapestry.services.ServiceConstants; 031 import org.apache.tapestry.util.io.DataSqueezerUtil; 032 import org.apache.tapestry.web.WebRequest; 033 import static org.easymock.EasyMock.checkOrder; 034 import static org.easymock.EasyMock.expect; 035 import org.testng.annotations.Test; 036 037 import java.util.*; 038 039 /** 040 * Tests for {@link org.apache.tapestry.services.impl.LinkFactoryImpl}. 041 * 042 * @author Howard M. Lewis Ship 043 * @since 4.0 044 */ 045 @Test 046 public class LinkFactoryTest extends BaseComponentTestCase 047 { 048 private ErrorLog newErrorLog() 049 { 050 return newMock(ErrorLog.class); 051 } 052 053 private static class NoopEncoder implements ServiceEncoder 054 { 055 public void decode(ServiceEncoding encoding) 056 { 057 // 058 } 059 060 public void encode(ServiceEncoding encoding) 061 { 062 // 063 } 064 } 065 066 private static class MockSource implements PropertyPersistenceStrategySource 067 { 068 069 public PropertyPersistenceStrategy getStrategy(String name) 070 { 071 return null; 072 } 073 074 public Collection getAllStoredChanges(String pageName) 075 { 076 return null; 077 } 078 079 public void discardAllStoredChanged(String pageName) 080 { 081 } 082 083 public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post) 084 { 085 encoding.setParameterValue("foo", "bar"); 086 } 087 088 } 089 090 private ServiceEncoderContribution newContribution(String id, ServiceEncoder encoder) 091 { 092 ServiceEncoderContribution result = new ServiceEncoderContribution(); 093 094 result.setId(id); 095 result.setEncoder(encoder); 096 097 return result; 098 } 099 100 private IEngine newEngine() 101 { 102 return newMock(IEngine.class); 103 } 104 105 private void trainGetOutputEncoding(IEngine engine, String outputEncoding) 106 { 107 expect(engine.getOutputEncoding()).andReturn(outputEncoding); 108 } 109 110 protected WebRequest newRequest(String contextPath) 111 { 112 WebRequest request = newRequest(); 113 checkOrder(request, false); 114 115 expect(request.getContextPath()).andReturn(contextPath).anyTimes(); 116 117 return request; 118 } 119 120 public void test_No_Encoders() 121 { 122 ErrorLog log = newErrorLog(); 123 WebRequest request = newRequest("/context"); 124 IEngine engine = newEngine(); 125 IRequestCycle cycle = newCycle(); 126 IEngineService service = newService("myservice"); 127 128 trainGetEngine(cycle, engine); 129 trainGetOutputEncoding(engine, "utf-8"); 130 131 replay(); 132 133 LinkFactoryImpl lf = new LinkFactoryImpl(); 134 135 lf.setContributions(Collections.EMPTY_LIST); 136 lf.setErrorLog(log); 137 lf.setServletPath("/app"); 138 lf.setRequest(request); 139 lf.setRequestCycle(cycle); 140 141 lf.initializeService(); 142 143 Map parameters = new HashMap(); 144 145 ILink link = lf.constructLink(service, false, parameters, false); 146 147 assertEquals("/context/app?service=myservice", link.getURL()); 148 149 verify(); 150 } 151 152 private IEngineService newService(String name) 153 { 154 IEngineService service = newMock(IEngineService.class); 155 156 expect(service.getName()).andReturn(name); 157 158 return service; 159 } 160 161 public void test_Stateful_Request() 162 { 163 ErrorLog log = newErrorLog(); 164 WebRequest request = newRequest("/context"); 165 IEngine engine = newEngine(); 166 IEngineService service = newService("myservice"); 167 IRequestCycle cycle = newCycle(); 168 169 trainGetEngine(cycle, engine); 170 trainGetOutputEncoding(engine, "utf-8"); 171 trainEncodeURL(cycle, "/context/app?foo=bar&service=myservice", "/context/app?foo=bar&service=myservice&jsessionid=124"); 172 173 replay(); 174 175 LinkFactoryImpl lf = new LinkFactoryImpl(); 176 177 lf.setContributions(Collections.EMPTY_LIST); 178 lf.setErrorLog(log); 179 lf.setServletPath("/app"); 180 lf.setRequest(request); 181 lf.setPersistenceStrategySource(new MockSource()); 182 lf.setRequestCycle(cycle); 183 184 lf.initializeService(); 185 186 Map parameters = new HashMap(); 187 188 ILink link = lf.constructLink(service, false, parameters, true); 189 190 assertEquals("/context/app?foo=bar&service=myservice&jsessionid=124", link.getURL()); 191 192 verify(); 193 } 194 195 public void test_Noop_Encoders() 196 { 197 WebRequest request = newRequest("/context"); 198 IRequestCycle cycle = newCycle(); 199 ErrorLog log = newErrorLog(); 200 IEngine engine = newEngine(); 201 IEngineService service = newService("myservice"); 202 203 trainGetEngine(cycle, engine); 204 trainGetOutputEncoding(engine, "utf-8"); 205 206 replay(); 207 208 List l = new ArrayList(); 209 l.add(newContribution("fred", new NoopEncoder())); 210 l.add(newContribution("barney", new NoopEncoder())); 211 212 LinkFactoryImpl lf = new LinkFactoryImpl(); 213 214 lf.setContributions(l); 215 lf.setErrorLog(log); 216 lf.setServletPath("/app"); 217 lf.setRequest(request); 218 lf.setRequestCycle(cycle); 219 220 lf.initializeService(); 221 222 Map parameters = new HashMap(); 223 224 ILink link = lf.constructLink(service, false, parameters, false); 225 226 assertEquals("/context/app?service=myservice", link.getURL()); 227 228 verify(); 229 } 230 231 public void test_Active_Encoder() 232 { 233 WebRequest request = newRequest("/context"); 234 IRequestCycle cycle = newCycle(); 235 ErrorLog log = newErrorLog(); 236 IEngineService service = newService("page"); 237 IEngine engine = newEngine(); 238 239 trainGetEngine(cycle, engine); 240 trainGetOutputEncoding(engine, "utf-8"); 241 242 replay(); 243 244 PageServiceEncoder e = new PageServiceEncoder(); 245 e.setServiceName("page"); 246 e.setExtension("html"); 247 248 List l = Collections.singletonList(newContribution("encoder", e)); 249 250 LinkFactoryImpl lf = new LinkFactoryImpl(); 251 252 lf.setContributions(l); 253 lf.setErrorLog(log); 254 lf.setServletPath("/app"); 255 lf.setRequest(request); 256 lf.setRequestCycle(cycle); 257 258 lf.initializeService(); 259 260 Map parameters = new HashMap(); 261 parameters.put(ServiceConstants.PAGE, "Barney"); 262 263 ILink link = lf.constructLink(service, false, parameters, false); 264 265 assertEquals("/context/Barney.html", link.getURL()); 266 267 verify(); 268 } 269 270 public void test_Service_Name_Is_Null() 271 { 272 IEngineService service = newService(null); 273 274 Map parameters = new HashMap(); 275 276 replay(); 277 278 LinkFactory lf = new LinkFactoryImpl(); 279 280 try 281 { 282 lf.constructLink(service, false, parameters, true); 283 unreachable(); 284 } 285 catch (ApplicationRuntimeException ex) 286 { 287 assertEquals(ImplMessages.serviceNameIsNull(), ex.getMessage()); 288 } 289 290 verify(); 291 } 292 293 public void test_With_Service_Parameters() 294 { 295 WebRequest request = newRequest("/context"); 296 IRequestCycle cycle = newCycle(); 297 ErrorLog log = newErrorLog(); 298 IEngineService service = newService("external"); 299 IEngine engine = newEngine(); 300 301 trainGetEngine(cycle, engine); 302 trainGetOutputEncoding(engine, "utf-8"); 303 304 replay(); 305 306 PageServiceEncoder e = new PageServiceEncoder(); 307 e.setServiceName("external"); 308 e.setExtension("ext"); 309 310 List l = Collections.singletonList(newContribution("encoder", e)); 311 312 LinkFactoryImpl lf = new LinkFactoryImpl(); 313 314 lf.setContributions(l); 315 lf.setErrorLog(log); 316 lf.setServletPath("/app"); 317 lf.setDataSqueezer(DataSqueezerUtil.createUnitTestSqueezer()); 318 lf.setRequest(request); 319 lf.setRequestCycle(cycle); 320 321 lf.initializeService(); 322 323 Map parameters = new HashMap(); 324 parameters.put(ServiceConstants.PAGE, "Barney"); 325 parameters.put(ServiceConstants.PARAMETER, new Object[] 326 { Boolean.TRUE }); 327 328 ILink link = lf.constructLink(service, false, parameters, false); 329 330 assertEquals("/context/Barney.ext?sp=T", link.getURL()); 331 332 verify(); 333 } 334 }