KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > TestPortletServicerBridges


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.portlet;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.portlet.ActionRequest;
20 import javax.portlet.ActionResponse;
21 import javax.portlet.PortletException;
22 import javax.portlet.RenderRequest;
23 import javax.portlet.RenderResponse;
24
25 import org.apache.hivemind.test.AggregateArgumentsMatcher;
26 import org.apache.hivemind.test.ArgumentMatcher;
27 import org.apache.hivemind.test.HiveMindTestCase;
28 import org.apache.hivemind.test.TypeMatcher;
29 import org.apache.tapestry.services.WebRequestServicer;
30 import org.apache.tapestry.web.WebRequest;
31 import org.apache.tapestry.web.WebResponse;
32 import org.easymock.MockControl;
33
34 /**
35  * Tests for {@link ActionRequestServicerToWebRequestServicerBridge} and
36  * {@link org.apache.tapestry.portlet.RenderRequestServicerToWebRequestServicerBridge}.
37  *
38  * @author Howard M. Lewis Ship
39  * @since 4.0
40  */

41 public class TestPortletServicerBridges extends HiveMindTestCase
42 {
43     private class WebRequestServicerFixture implements WebRequestServicer
44     {
45         WebRequest _request;
46
47         WebResponse _response;
48
49         public void service(WebRequest request, WebResponse response) throws IOException JavaDoc
50         {
51             _request = request;
52             _response = response;
53         }
54
55     }
56
57     public void testActionBridgeSuccess() throws Exception JavaDoc
58     {
59         ActionRequest request = (ActionRequest) newMock(ActionRequest.class);
60
61         MockControl responsec = newControl(ActionResponse.class);
62         ActionResponse response = (ActionResponse) responsec.getMock();
63
64         PortletRequestGlobals prg = (PortletRequestGlobals) newMock(PortletRequestGlobals.class);
65         WebRequestServicerFixture wrs = new WebRequestServicerFixture();
66
67         prg.store(request, response);
68
69         request.removeAttribute("FOO");
70         response.encodeURL("FOO");
71         responsec.setReturnValue(null);
72
73         replayControls();
74
75         ActionRequestServicerToWebRequestServicerBridge bridge = new ActionRequestServicerToWebRequestServicerBridge();
76         bridge.setPortletRequestGlobals(prg);
77         bridge.setWebRequestServicer(wrs);
78
79         bridge.service(request, response);
80
81         // Test that the WebXXX wrappers createde by the bridge and passed to the WebRequestServicer
82
// encapsulate the ActionRequest and ActionResponse
83

84         wrs._request.setAttribute("FOO", null);
85         wrs._response.encodeURL("FOO");
86
87         verifyControls();
88     }
89
90     public void testRenderBridgeSuccess() throws Exception JavaDoc
91     {
92         RenderRequest request = (RenderRequest) newMock(RenderRequest.class);
93
94         MockControl responsec = newControl(RenderResponse.class);
95         RenderResponse response = (RenderResponse) responsec.getMock();
96
97         PortletRequestGlobals prg = (PortletRequestGlobals) newMock(PortletRequestGlobals.class);
98         WebRequestServicerFixture wrs = new WebRequestServicerFixture();
99
100         prg.store(request, response);
101
102         request.removeAttribute("FOO");
103         response.reset();
104
105         replayControls();
106
107         RenderRequestServicerToWebRequestServicerBridge bridge = new RenderRequestServicerToWebRequestServicerBridge();
108         bridge.setPortletRequestGlobals(prg);
109         bridge.setWebRequestServicer(wrs);
110
111         bridge.service(request, response);
112
113         // Test that the WebXXX wrappers createde by the bridge and passed to the WebRequestServicer
114
// encapsulate the RenderRequest and RenderResponse
115

116         wrs._request.setAttribute("FOO", null);
117
118         // Prove that the *correct* wrapper type, RenderWebResponse, has been used.
119

120         wrs._response.reset();
121
122         verifyControls();
123     }
124
125     public void testActionBridgeFailure() throws Exception JavaDoc
126     {
127         ActionRequest request = (ActionRequest) newMock(ActionRequest.class);
128         ActionResponse response = (ActionResponse) newMock(ActionResponse.class);
129         PortletRequestGlobals prg = (PortletRequestGlobals) newMock(PortletRequestGlobals.class);
130
131         MockControl control = newControl(WebRequestServicer.class);
132         WebRequestServicer servicer = (WebRequestServicer) control.getMock();
133
134         Throwable JavaDoc t = new RuntimeException JavaDoc("Failure.");
135
136         prg.store(request, response);
137         servicer.service(new PortletWebRequest(request), new PortletWebResponse(response));
138         control.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
139         { new TypeMatcher(), new TypeMatcher() }));
140         control.setThrowable(t);
141
142         replayControls();
143
144         ActionRequestServicerToWebRequestServicerBridge bridge = new ActionRequestServicerToWebRequestServicerBridge();
145         bridge.setPortletRequestGlobals(prg);
146         bridge.setWebRequestServicer(servicer);
147
148         try
149         {
150
151             bridge.service(request, response);
152             unreachable();
153         }
154         catch (PortletException ex)
155         {
156             // PortletException doesn't seem to copy the
157
// message?
158
// assertEquals("Failure.", ex.getMessage());
159
// Note: implemented by PortletException, not tied
160
// to JDK 1.4
161
assertSame(t, ex.getCause());
162         }
163
164         verifyControls();
165     }
166
167     public void testRenderBridgeFailure() throws Exception JavaDoc
168     {
169         RenderRequest request = (RenderRequest) newMock(RenderRequest.class);
170         RenderResponse response = (RenderResponse) newMock(RenderResponse.class);
171         PortletRequestGlobals prg = (PortletRequestGlobals) newMock(PortletRequestGlobals.class);
172
173         MockControl control = newControl(WebRequestServicer.class);
174         WebRequestServicer servicer = (WebRequestServicer) control.getMock();
175
176         Throwable JavaDoc t = new RuntimeException JavaDoc("Failure.");
177
178         prg.store(request, response);
179         servicer.service(new PortletWebRequest(request), new RenderWebResponse(response));
180         control.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
181         { new TypeMatcher(), new TypeMatcher() }));
182         control.setThrowable(t);
183
184         replayControls();
185
186         RenderRequestServicerToWebRequestServicerBridge bridge = new RenderRequestServicerToWebRequestServicerBridge();
187         bridge.setPortletRequestGlobals(prg);
188         bridge.setWebRequestServicer(servicer);
189
190         try
191         {
192
193             bridge.service(request, response);
194             unreachable();
195         }
196         catch (PortletException ex)
197         {
198             // PortletException doesn't seem to copy the
199
// message?
200
// assertEquals("Failure.", ex.getMessage());
201
// Note: implemented by PortletException, not tied
202
// to JDK 1.4
203
assertSame(t, ex.getCause());
204         }
205
206         verifyControls();
207     }
208 }
Popular Tags