View Javadoc

1   /*
2    * $Id: PortletResultTest.java 568895 2007-08-23 08:57:36Z rgielen $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.portlet.result;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletMode;
31  import javax.portlet.PortletRequestDispatcher;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import junit.textui.TestRunner;
36  
37  import org.apache.struts2.portlet.PortletActionConstants;
38  import org.jmock.Mock;
39  import org.jmock.cglib.MockObjectTestCase;
40  import org.jmock.core.Constraint;
41  
42  import com.opensymphony.xwork2.ActionContext;
43  import com.opensymphony.xwork2.ActionInvocation;
44  
45  /***
46   * PortletResultTest. Insert description.
47   *
48   */
49  public class PortletResultTest extends MockObjectTestCase implements PortletActionConstants {
50  
51      Mock mockInvocation = null;
52      Mock mockConfig = null;
53      Mock mockCtx = null;
54  
55      public void setUp() throws Exception {
56          super.setUp();
57          mockInvocation = mock(ActionInvocation.class);
58          mockConfig = mock(PortletConfig.class);
59          mockCtx = mock(PortletContext.class);
60  
61          mockConfig.stubs().method(ANYTHING);
62          mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
63  
64          Map paramMap = new HashMap();
65          Map sessionMap = new HashMap();
66  
67          Map context = new HashMap();
68          context.put(ActionContext.SESSION, sessionMap);
69          context.put(ActionContext.PARAMETERS, paramMap);
70          context.put(PortletActionConstants.PORTLET_CONFIG, mockConfig.proxy());
71  
72          ActionContext.setContext(new ActionContext(context));
73  
74          mockInvocation.stubs().method("getInvocationContext").will(returnValue(ActionContext.getContext()));
75  
76      }
77  
78      public void testDoExecute_render() {
79          Mock mockRequest = mock(RenderRequest.class);
80          Mock mockResponse = mock(RenderResponse.class);
81          Mock mockRd = mock(PortletRequestDispatcher.class);
82  
83          RenderRequest req = (RenderRequest)mockRequest.proxy();
84          RenderResponse res = (RenderResponse)mockResponse.proxy();
85          PortletRequestDispatcher rd = (PortletRequestDispatcher)mockRd.proxy();
86          PortletConfig cfg = (PortletConfig)mockConfig.proxy();
87          PortletContext ctx = (PortletContext)mockCtx.proxy();
88          ActionInvocation inv = (ActionInvocation)mockInvocation.proxy();
89  
90          Constraint[] params = new Constraint[]{same(req), same(res)};
91          mockRd.expects(once()).method("include").with(params);
92          mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
93          mockResponse.expects(once()).method("setContentType").with(eq("text/html"));
94          mockConfig.expects(once()).method("getPortletContext").will(returnValue(ctx));
95  
96          mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
97  
98          ActionContext ctxMap = ActionContext.getContext();
99          ctxMap.put(PortletActionConstants.RESPONSE, res);
100         ctxMap.put(PortletActionConstants.REQUEST, req);
101         ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
102         ctxMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
103 
104         PortletResult result = new PortletResult();
105         try {
106             result.doExecute("/WEB-INF/pages/testPage.jsp", inv);
107         }
108         catch(Exception e) {
109             e.printStackTrace();
110             fail("Error occured!");
111         }
112 
113     }
114 
115     public void testDoExecute_event_locationIsAction() {
116 
117         Mock mockRequest = mock(ActionRequest.class);
118         Mock mockResponse = mock(ActionResponse.class);
119 
120         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("testView")};
121         mockResponse.expects(once()).method("setRenderParameter").with(params);
122         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
123         mockResponse.expects(once()).method("setRenderParameter").with(params);
124         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
125         ActionContext ctx = ActionContext.getContext();
126 
127         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
128         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
129         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
130 
131         PortletResult result = new PortletResult();
132         try {
133             result.doExecute("testView.action", (ActionInvocation)mockInvocation.proxy());
134         }
135         catch(Exception e) {
136             e.printStackTrace();
137             fail("Error occured!");
138         }
139 
140     }
141 
142     public void testDoExecute_event_locationIsJsp() {
143         Mock mockRequest = mock(ActionRequest.class);
144         Mock mockResponse = mock(ActionResponse.class);
145 
146         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("renderDirect")};
147         mockResponse.expects(once()).method("setRenderParameter").with(params);
148         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
149         mockResponse.expects(once()).method("setRenderParameter").with(params);
150         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
151 
152         ActionContext ctx = ActionContext.getContext();
153 
154         Map session = new HashMap();
155 
156         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
157         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
158         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
159         ctx.put(ActionContext.SESSION, session);
160 
161         PortletResult result = new PortletResult();
162         try {
163             result.doExecute("/WEB-INF/pages/testJsp.jsp", (ActionInvocation)mockInvocation.proxy());
164         } catch(Exception e) {
165             e.printStackTrace();
166             fail("Error occured!");
167         }
168         assertEquals("/WEB-INF/pages/testJsp.jsp", session.get(RENDER_DIRECT_LOCATION));
169     }
170 
171     public void testDoExecute_event_locationHasQueryParams() {
172         Mock mockRequest = mock(ActionRequest.class);
173         Mock mockResponse = mock(ActionResponse.class);
174 
175         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("testView")};
176         mockResponse.expects(once()).method("setRenderParameter").with(params);
177         params = new Constraint[]{eq("testParam1"), eq("testValue1")};
178         mockResponse.expects(once()).method("setRenderParameter").with(params);
179         params = new Constraint[]{eq("testParam2"), eq("testValue2")};
180         mockResponse.expects(once()).method("setRenderParameter").with(params);
181         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
182         mockResponse.expects(once()).method("setRenderParameter").with(params);
183         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
184 
185         ActionContext ctx = ActionContext.getContext();
186 
187         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
188         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
189         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
190 
191         PortletResult result = new PortletResult();
192         try {
193             result.doExecute("testView.action?testParam1=testValue1&testParam2=testValue2", (ActionInvocation)mockInvocation.proxy());
194         }
195         catch(Exception e) {
196             e.printStackTrace();
197             fail("Error occured!");
198         }
199     }
200 
201     public void testTitleAndContentType() throws Exception {
202         Mock mockRequest = mock(RenderRequest.class);
203         Mock mockResponse = mock(RenderResponse.class);
204         Mock mockRd = mock(PortletRequestDispatcher.class);
205 
206         RenderRequest req = (RenderRequest)mockRequest.proxy();
207         RenderResponse res = (RenderResponse)mockResponse.proxy();
208         PortletRequestDispatcher rd = (PortletRequestDispatcher)mockRd.proxy();
209         PortletConfig cfg = (PortletConfig)mockConfig.proxy();
210         PortletContext ctx = (PortletContext)mockCtx.proxy();
211 
212         Constraint[] params = new Constraint[]{same(req), same(res)};
213         mockRd.expects(once()).method("include").with(params);
214         mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
215         mockConfig.expects(once()).method("getPortletContext").will(returnValue(ctx));
216 
217         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
218 
219         ActionContext ctxMap = ActionContext.getContext();
220         ctxMap.put(PortletActionConstants.RESPONSE, res);
221         ctxMap.put(PortletActionConstants.REQUEST, req);
222         ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
223         ctxMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
224 
225         mockResponse.expects(atLeastOnce()).method("setTitle").with(eq("testTitle"));
226         mockResponse.expects(atLeastOnce()).method("setContentType").with(eq("testContentType"));
227 
228         PortletResult result = new PortletResult();
229         result.setTitle("testTitle");
230         result.setContentType("testContentType");
231         result.doExecute("/WEB-INF/pages/testPage.jsp", (ActionInvocation)mockInvocation.proxy());
232     }
233 
234     public void tearDown() throws Exception {
235         super.tearDown();
236         ActionContext.setContext(null);
237     }
238 
239     public static void main(String[] args) {
240         TestRunner.run(PortletResultTest.class);
241     }
242 
243 }