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.portlet;
016    
017    import static org.easymock.EasyMock.expect;
018    import static org.easymock.EasyMock.expectLastCall;
019    import static org.easymock.EasyMock.isA;
020    
021    import java.io.IOException;
022    
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.apache.tapestry.services.WebRequestServicer;
025    import org.apache.tapestry.web.WebRequest;
026    import org.apache.tapestry.web.WebResponse;
027    import org.testng.annotations.Test;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletException;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    /**
036     * Tests for {@link ActionRequestServicerToWebRequestServicerBridge} and
037     * {@link org.apache.tapestry.portlet.RenderRequestServicerToWebRequestServicerBridge}.
038     * 
039     * @author Howard M. Lewis Ship
040     * @since 4.0
041     */
042    @Test(sequential=true)
043    public class TestPortletServicerBridges extends BaseComponentTestCase
044    {
045        private class WebRequestServicerFixture implements WebRequestServicer
046        {
047            WebRequest _request;
048    
049            WebResponse _response;
050    
051            public void service(WebRequest request, WebResponse response) throws IOException
052            {
053                _request = request;
054                _response = response;
055            }
056    
057        }
058    
059        public void testActionBridgeSuccess() throws Exception
060        {
061            ActionRequest request = newMock(ActionRequest.class);
062            ActionResponse response = newMock(ActionResponse.class);
063    
064            PortletRequestGlobals prg = newMock(PortletRequestGlobals.class);
065            WebRequestServicerFixture wrs = new WebRequestServicerFixture();
066    
067            prg.store(request, response);
068            
069            request.removeAttribute("FOO");
070            expect(response.encodeURL("FOO")).andReturn(null);
071    
072            replay();
073    
074            ActionRequestServicerToWebRequestServicerBridge bridge = 
075                new ActionRequestServicerToWebRequestServicerBridge();
076            bridge.setPortletRequestGlobals(prg);
077            bridge.setWebRequestServicer(wrs);
078    
079            bridge.service(request, response);
080    
081            // Test that the WebXXX wrappers createde by the bridge and passed to the WebRequestServicer
082            // encapsulate the ActionRequest and ActionResponse
083    
084            wrs._request.setAttribute("FOO", null);
085            wrs._response.encodeURL("FOO");
086    
087            verify();
088        }
089    
090        public void testRenderBridgeSuccess() throws Exception
091        {
092            RenderRequest request = newMock(RenderRequest.class);
093            RenderResponse response = newMock(RenderResponse.class);
094    
095            PortletRequestGlobals prg = newMock(PortletRequestGlobals.class);
096            WebRequestServicerFixture wrs = new WebRequestServicerFixture();
097            
098            prg.store(request, response);
099    
100            request.removeAttribute("FOO");
101            response.reset();
102    
103            replay();
104    
105            RenderRequestServicerToWebRequestServicerBridge bridge = 
106                new RenderRequestServicerToWebRequestServicerBridge();
107            bridge.setPortletRequestGlobals(prg);
108            bridge.setWebRequestServicer(wrs);
109    
110            bridge.service(request, response);
111    
112            // Test that the WebXXX wrappers createde by the bridge and passed to the WebRequestServicer
113            // encapsulate the RenderRequest and RenderResponse
114    
115            wrs._request.setAttribute("FOO", null);
116    
117            // Prove that the *correct* wrapper type, RenderWebResponse, has been used.
118    
119            wrs._response.reset();
120    
121            verify();
122        }
123    
124        public void testActionBridgeFailure() throws Exception
125        {
126            ActionRequest request = newMock(ActionRequest.class);
127            ActionResponse response = newMock(ActionResponse.class);
128            PortletRequestGlobals prg = newMock(PortletRequestGlobals.class);
129            
130            WebRequestServicer servicer = newMock(WebRequestServicer.class);
131            
132            Throwable t = new RuntimeException("Failure.");
133    
134            prg.store(request, response);
135            servicer.service(isA(PortletWebRequest.class), isA(PortletWebResponse.class));
136            expectLastCall().andThrow(t);
137            
138            replay();
139    
140            ActionRequestServicerToWebRequestServicerBridge bridge = new ActionRequestServicerToWebRequestServicerBridge();
141            bridge.setPortletRequestGlobals(prg);
142            bridge.setWebRequestServicer(servicer);
143    
144            try
145            {
146    
147                bridge.service(request, response);
148                unreachable();
149            }
150            catch (PortletException ex)
151            {
152                // PortletException doesn't seem to copy the
153                // message?
154                // assertEquals("Failure.", ex.getMessage());
155                // Note: implemented by PortletException, not tied
156                // to JDK 1.4
157                assertSame(t, ex.getCause());
158            }
159    
160            verify();
161        }
162    
163        public void testRenderBridgeFailure() throws Exception
164        {
165            RenderRequest request = newMock(RenderRequest.class);
166            RenderResponse response = newMock(RenderResponse.class);
167            PortletRequestGlobals prg = newMock(PortletRequestGlobals.class);
168            
169            WebRequestServicer servicer = newMock(WebRequestServicer.class);
170            
171            Throwable t = new RuntimeException("Failure.");
172    
173            prg.store(request, response);
174            
175            servicer.service(isA(PortletWebRequest.class), isA(RenderWebResponse.class));
176            expectLastCall().andThrow(t);
177            
178            replay();
179            
180            RenderRequestServicerToWebRequestServicerBridge bridge = new RenderRequestServicerToWebRequestServicerBridge();
181            bridge.setPortletRequestGlobals(prg);
182            bridge.setWebRequestServicer(servicer);
183            
184            try
185            {
186    
187                bridge.service(request, response);
188                unreachable();
189            }
190            catch (PortletException ex)
191            {
192                // PortletException doesn't seem to copy the
193                // message?
194                // assertEquals("Failure.", ex.getMessage());
195                // Note: implemented by PortletException, not tied
196                // to JDK 1.4
197                assertSame(t, ex.getCause());
198            }
199    
200            verify();
201        }
202    }