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.engine; 016 017 import static org.easymock.EasyMock.checkOrder; 018 import static org.easymock.EasyMock.expect; 019 020 import org.apache.hivemind.ErrorHandler; 021 import org.apache.tapestry.BaseComponentTestCase; 022 import org.apache.tapestry.IEngine; 023 import org.apache.tapestry.IRequestCycle; 024 import org.apache.tapestry.RedirectException; 025 import org.apache.tapestry.pageload.PageSource; 026 import org.apache.tapestry.record.PropertyPersistenceStrategySource; 027 import org.apache.tapestry.services.AbsoluteURLBuilder; 028 import org.apache.tapestry.services.Infrastructure; 029 import org.apache.tapestry.services.ServiceMap; 030 import org.apache.tapestry.util.QueryParameterMap; 031 import org.testng.annotations.Test; 032 033 /** 034 * Tests for {@link org.apache.tapestry.engine.RequestCycle}. Mostly just tests changes for 4.0 035 * (3.0 code is still mostly tested via the mock integration tests). 036 * 037 * @author Howard M. Lewis Ship 038 * @since 4.0 039 */ 040 @Test 041 public class RequestCycleTest extends BaseComponentTestCase 042 { 043 private IEngine newEngine() 044 { 045 return newMock(IEngine.class); 046 } 047 048 private PropertyPersistenceStrategySource newStrategySource() 049 { 050 return newMock(PropertyPersistenceStrategySource.class); 051 } 052 053 private ErrorHandler newErrorHandler() 054 { 055 return newMock(ErrorHandler.class); 056 } 057 058 private AbsoluteURLBuilder newBuilder() 059 { 060 return newMock(AbsoluteURLBuilder.class); 061 } 062 063 private IEngineService newService() 064 { 065 return newMock(IEngineService.class); 066 } 067 068 public void testGetters() 069 { 070 Infrastructure infrastructure = newMock(Infrastructure.class); 071 PageSource pageSource = new PageSource(); 072 073 expect(infrastructure.getPageSource()).andReturn(pageSource); 074 075 IEngineService service = newService(); 076 ServiceMap map = newServiceMap("fred", service); 077 078 expect(infrastructure.getServiceMap()).andReturn(map); 079 080 RequestCycleEnvironment env = new RequestCycleEnvironment(newErrorHandler(), 081 infrastructure, newStrategySource(), newBuilder()); 082 083 IEngine engine = newEngine(); 084 085 replay(); 086 087 IRequestCycle cycle = new RequestCycle(engine, new QueryParameterMap(), "fred", env); 088 089 assertSame(infrastructure, cycle.getInfrastructure()); 090 assertSame(service, cycle.getService()); 091 assertSame(engine, cycle.getEngine()); 092 093 verify(); 094 } 095 096 private ServiceMap newServiceMap(String serviceName, IEngineService service) 097 { 098 ServiceMap map = newMock(ServiceMap.class); 099 checkOrder(map, false); 100 101 expect(map.getService(serviceName)).andReturn(service); 102 103 return map; 104 } 105 106 public void testForgetPage() 107 { 108 Infrastructure infrastructure = newMock(Infrastructure.class); 109 PageSource pageSource = new PageSource(); 110 111 expect(infrastructure.getPageSource()).andReturn(pageSource); 112 113 PropertyPersistenceStrategySource source = newStrategySource(); 114 RequestCycleEnvironment env = new RequestCycleEnvironment(newErrorHandler(), 115 infrastructure, source, newBuilder()); 116 IEngine engine = newEngine(); 117 118 replay(); 119 120 IRequestCycle cycle = new RequestCycle(engine, new QueryParameterMap(), null, env); 121 122 cycle.getEngine(); 123 124 verify(); 125 126 source.discardAllStoredChanged("MyPage"); 127 128 replay(); 129 130 cycle.forgetPage("MyPage"); 131 132 verify(); 133 } 134 135 public void testSendRedirect() 136 { 137 IRequestCycle cycle = new RequestCycle(); 138 139 try 140 { 141 cycle.sendRedirect("http://foo/bar"); 142 unreachable(); 143 } 144 catch (RedirectException ex) 145 { 146 assertEquals("http://foo/bar", ex.getRedirectLocation()); 147 } 148 } 149 }