KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > TestRequestCycle


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.engine;
16
17 import org.apache.hivemind.ErrorHandler;
18 import org.apache.hivemind.test.HiveMindTestCase;
19 import org.apache.tapestry.IEngine;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.pageload.PageSource;
22 import org.apache.tapestry.record.PropertyPersistenceStrategySource;
23 import org.apache.tapestry.request.RequestContext;
24 import org.apache.tapestry.services.AbsoluteURLBuilder;
25 import org.apache.tapestry.services.Infrastructure;
26 import org.apache.tapestry.util.QueryParameterMap;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.engine.RequestCycle}. Mostly just tests changes for 4.0
31  * (3.0 code is still mostly tested via the mock integration tests).
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36 public class TestRequestCycle extends HiveMindTestCase
37 {
38     private IEngine newEngine()
39     {
40         return (IEngine) newMock(IEngine.class);
41     }
42
43     private IMonitor newMonitor()
44     {
45         return (IMonitor) newMock(IMonitor.class);
46     }
47
48     private PropertyPersistenceStrategySource newStrategySource()
49     {
50         return (PropertyPersistenceStrategySource) newMock(PropertyPersistenceStrategySource.class);
51     }
52
53     private PageSource newPageSource()
54     {
55         return (PageSource) newMock(PageSource.class);
56     }
57
58     private Infrastructure newInfrastructure()
59     {
60         return newInfrastructure(newPageSource());
61     }
62
63     private ErrorHandler newErrorHandler()
64     {
65         return (ErrorHandler) newMock(ErrorHandler.class);
66     }
67
68     private AbsoluteURLBuilder newBuilder()
69     {
70         return (AbsoluteURLBuilder) newMock(AbsoluteURLBuilder.class);
71     }
72
73     private Infrastructure newInfrastructure(PageSource source)
74     {
75         MockControl control = newControl(Infrastructure.class);
76         Infrastructure infrastructure = (Infrastructure) control.getMock();
77
78         infrastructure.getPageSource();
79         control.setReturnValue(source);
80
81         return infrastructure;
82     }
83
84     private IEngineService newService()
85     {
86         return (IEngineService) newMock(IEngineService.class);
87     }
88
89     private RequestCycleEnvironment newEnvironment(RequestContext context)
90     {
91         return new RequestCycleEnvironment(newErrorHandler(), newInfrastructure(), context,
92                 newStrategySource(), newBuilder());
93     }
94
95     public void testGetters()
96     {
97         RequestContext context = new RequestContext(null, null);
98         Infrastructure infrastructure = newInfrastructure();
99         RequestCycleEnvironment env = new RequestCycleEnvironment(newErrorHandler(),
100                 infrastructure, context, newStrategySource(), newBuilder());
101         IEngine engine = newEngine();
102         IEngineService service = newService();
103         IMonitor monitor = newMonitor();
104
105         replayControls();
106
107         IRequestCycle cycle = new RequestCycle(engine, new QueryParameterMap(), service, monitor,
108                 env);
109
110         assertSame(infrastructure, cycle.getInfrastructure());
111         assertSame(context, cycle.getRequestContext());
112         assertSame(service, cycle.getService());
113         assertSame(engine, cycle.getEngine());
114         assertSame(monitor, cycle.getMonitor());
115
116         verifyControls();
117     }
118
119     public void testForgetPage()
120     {
121         RequestContext context = new RequestContext(null, null);
122         Infrastructure infrastructure = newInfrastructure();
123         PropertyPersistenceStrategySource source = newStrategySource();
124         RequestCycleEnvironment env = new RequestCycleEnvironment(newErrorHandler(),
125                 infrastructure, context, source, newBuilder());
126         IEngine engine = newEngine();
127         IEngineService service = newService();
128         IMonitor monitor = newMonitor();
129
130         replayControls();
131
132         IRequestCycle cycle = new RequestCycle(engine, new QueryParameterMap(), service, monitor,
133                 env);
134         
135         cycle.getEngine();
136         
137         verifyControls();
138
139         source.discardAllStoredChanged("MyPage", cycle);
140
141         replayControls();
142
143         cycle.forgetPage("MyPage");
144
145         verifyControls();
146         
147         source.discardAllStoredChanged("MyPage", cycle);
148
149         replayControls();
150
151         cycle.discardPage("MyPage");
152
153         verifyControls();
154     }
155 }
Popular Tags