View Javadoc

1   /*
2    * $Id: PortletResultTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.portlet.result;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.ActionResponse;
25  import javax.portlet.PortletConfig;
26  import javax.portlet.PortletContext;
27  import javax.portlet.PortletMode;
28  import javax.portlet.PortletRequestDispatcher;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  import junit.textui.TestRunner;
33  
34  import org.apache.struts2.portlet.PortletActionConstants;
35  import org.jmock.Mock;
36  import org.jmock.cglib.MockObjectTestCase;
37  import org.jmock.core.Constraint;
38  
39  import com.opensymphony.xwork2.ActionContext;
40  import com.opensymphony.xwork2.ActionInvocation;
41  
42  /***
43   * PortletResultTest. Insert description.
44   * 
45   */
46  public class PortletResultTest extends MockObjectTestCase {
47      
48      Mock mockInvocation = null;
49      Mock mockConfig = null;
50      Mock mockCtx = null;
51      
52      public void setUp() throws Exception {
53          super.setUp();
54          mockInvocation = mock(ActionInvocation.class);
55          mockConfig = mock(PortletConfig.class);
56          mockCtx = mock(PortletContext.class);
57          
58          mockConfig.stubs().method(ANYTHING);
59          mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
60          
61          Map paramMap = new HashMap();
62          Map sessionMap = new HashMap();
63          
64          Map context = new HashMap();
65          context.put(ActionContext.SESSION, sessionMap);
66          context.put(ActionContext.PARAMETERS, paramMap);
67          context.put(PortletActionConstants.PORTLET_CONFIG, mockConfig.proxy());
68          
69          ActionContext.setContext(new ActionContext(context));
70          
71          mockInvocation.stubs().method("getInvocationContext").will(returnValue(ActionContext.getContext()));
72          
73      }
74      
75      public void testDoExecute_render() {
76          Mock mockRequest = mock(RenderRequest.class);
77          Mock mockResponse = mock(RenderResponse.class);
78          Mock mockRd = mock(PortletRequestDispatcher.class);
79          Mock mockPrep = mock(PortletRequestDispatcher.class);
80          
81          RenderRequest req = (RenderRequest)mockRequest.proxy();
82          RenderResponse res = (RenderResponse)mockResponse.proxy();
83          PortletRequestDispatcher rd = (PortletRequestDispatcher)mockRd.proxy();
84          PortletConfig cfg = (PortletConfig)mockConfig.proxy();
85          PortletContext ctx = (PortletContext)mockCtx.proxy();
86          ActionInvocation inv = (ActionInvocation)mockInvocation.proxy();
87          
88          Constraint[] params = new Constraint[]{same(req), same(res)};
89          mockRd.expects(once()).method("include").with(params);
90          mockPrep.expects(once()).method("include").with(params);
91          mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
92          mockCtx.expects(once()).method("getNamedDispatcher").with(eq("preparator")).will(returnValue(mockPrep.proxy()));
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("location"), eq("/WEB-INF/pages/testJsp.jsp")};
149         mockResponse.expects(once()).method("setRenderParameter").with(params);
150         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
151         mockResponse.expects(once()).method("setRenderParameter").with(params);
152         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
153         
154         ActionContext ctx = ActionContext.getContext();
155         
156         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
157         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
158         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
159         
160         PortletResult result = new PortletResult();
161         try {
162             result.doExecute("/WEB-INF/pages/testJsp.jsp", (ActionInvocation)mockInvocation.proxy());
163         }
164         catch(Exception e) {
165             e.printStackTrace();
166             fail("Error occured!");
167         }
168     }
169     
170     public void testDoExecute_event_locationHasQueryParams() {
171         Mock mockRequest = mock(ActionRequest.class);
172         Mock mockResponse = mock(ActionResponse.class);
173         
174         Constraint[] params = new Constraint[]{eq(PortletActionConstants.ACTION_PARAM), eq("testView")};
175         mockResponse.expects(once()).method("setRenderParameter").with(params);
176         params = new Constraint[]{eq("testParam1"), eq("testValue1")};
177         mockResponse.expects(once()).method("setRenderParameter").with(params);
178         params = new Constraint[]{eq("testParam2"), eq("testValue2")};
179         mockResponse.expects(once()).method("setRenderParameter").with(params);
180         params = new Constraint[]{eq(PortletActionConstants.MODE_PARAM), eq(PortletMode.VIEW.toString())};
181         mockResponse.expects(once()).method("setRenderParameter").with(params);
182         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
183         
184         ActionContext ctx = ActionContext.getContext();
185         
186         ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
187         ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
188         ctx.put(PortletActionConstants.PHASE, PortletActionConstants.EVENT_PHASE);
189         
190         PortletResult result = new PortletResult();
191         try {
192             result.doExecute("testView.action?testParam1=testValue1&testParam2=testValue2", (ActionInvocation)mockInvocation.proxy());
193         }
194         catch(Exception e) {
195             e.printStackTrace();
196             fail("Error occured!");
197         }
198     }
199     
200     public void testTitleAndContentType() throws Exception {
201         Mock mockRequest = mock(RenderRequest.class);
202         Mock mockResponse = mock(RenderResponse.class);
203         Mock mockRd = mock(PortletRequestDispatcher.class);
204         Mock mockPrep = 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         mockPrep.expects(once()).method("include").with(params);
215         mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
216         mockCtx.expects(once()).method("getNamedDispatcher").with(eq("preparator")).will(returnValue(mockPrep.proxy()));
217         mockConfig.expects(once()).method("getPortletContext").will(returnValue(ctx));
218         
219         mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
220         
221         ActionContext ctxMap = ActionContext.getContext();
222         ctxMap.put(PortletActionConstants.RESPONSE, res);
223         ctxMap.put(PortletActionConstants.REQUEST, req);
224         ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
225         ctxMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
226         
227         mockResponse.expects(atLeastOnce()).method("setTitle").with(eq("testTitle"));
228         mockResponse.expects(atLeastOnce()).method("setContentType").with(eq("testContentType"));
229         
230         PortletResult result = new PortletResult();
231         result.setTitle("testTitle");
232         result.setContentType("testContentType");
233         result.doExecute("/WEB-INF/pages/testPage.jsp", (ActionInvocation)mockInvocation.proxy());
234     }
235     
236     public void tearDown() throws Exception {
237         super.tearDown();
238         ActionContext.setContext(null);
239     }
240     
241     public static void main(String[] args) {
242         TestRunner.run(PortletResultTest.class);
243     }
244     
245 }