001: /*
002: * $Id: PortletResultTest.java 568895 2007-08-23 08:57:36Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.result;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletConfig;
029: import javax.portlet.PortletContext;
030: import javax.portlet.PortletMode;
031: import javax.portlet.PortletRequestDispatcher;
032: import javax.portlet.RenderRequest;
033: import javax.portlet.RenderResponse;
034:
035: import junit.textui.TestRunner;
036:
037: import org.apache.struts2.portlet.PortletActionConstants;
038: import org.jmock.Mock;
039: import org.jmock.cglib.MockObjectTestCase;
040: import org.jmock.core.Constraint;
041:
042: import com.opensymphony.xwork2.ActionContext;
043: import com.opensymphony.xwork2.ActionInvocation;
044:
045: /**
046: * PortletResultTest. Insert description.
047: *
048: */
049: public class PortletResultTest extends MockObjectTestCase implements
050: PortletActionConstants {
051:
052: Mock mockInvocation = null;
053: Mock mockConfig = null;
054: Mock mockCtx = null;
055:
056: public void setUp() throws Exception {
057: super .setUp();
058: mockInvocation = mock(ActionInvocation.class);
059: mockConfig = mock(PortletConfig.class);
060: mockCtx = mock(PortletContext.class);
061:
062: mockConfig.stubs().method(ANYTHING);
063: mockConfig.stubs().method("getPortletContext").will(
064: returnValue(mockCtx.proxy()));
065:
066: Map paramMap = new HashMap();
067: Map sessionMap = new HashMap();
068:
069: Map context = new HashMap();
070: context.put(ActionContext.SESSION, sessionMap);
071: context.put(ActionContext.PARAMETERS, paramMap);
072: context.put(PortletActionConstants.PORTLET_CONFIG, mockConfig
073: .proxy());
074:
075: ActionContext.setContext(new ActionContext(context));
076:
077: mockInvocation.stubs().method("getInvocationContext").will(
078: returnValue(ActionContext.getContext()));
079:
080: }
081:
082: public void testDoExecute_render() {
083: Mock mockRequest = mock(RenderRequest.class);
084: Mock mockResponse = mock(RenderResponse.class);
085: Mock mockRd = mock(PortletRequestDispatcher.class);
086:
087: RenderRequest req = (RenderRequest) mockRequest.proxy();
088: RenderResponse res = (RenderResponse) mockResponse.proxy();
089: PortletRequestDispatcher rd = (PortletRequestDispatcher) mockRd
090: .proxy();
091: PortletConfig cfg = (PortletConfig) mockConfig.proxy();
092: PortletContext ctx = (PortletContext) mockCtx.proxy();
093: ActionInvocation inv = (ActionInvocation) mockInvocation
094: .proxy();
095:
096: Constraint[] params = new Constraint[] { same(req), same(res) };
097: mockRd.expects(once()).method("include").with(params);
098: mockCtx.expects(once()).method("getRequestDispatcher").with(
099: eq("/WEB-INF/pages/testPage.jsp"))
100: .will(returnValue(rd));
101: mockResponse.expects(once()).method("setContentType").with(
102: eq("text/html"));
103: mockConfig.expects(once()).method("getPortletContext").will(
104: returnValue(ctx));
105:
106: mockRequest.stubs().method("getPortletMode").will(
107: returnValue(PortletMode.VIEW));
108:
109: ActionContext ctxMap = ActionContext.getContext();
110: ctxMap.put(PortletActionConstants.RESPONSE, res);
111: ctxMap.put(PortletActionConstants.REQUEST, req);
112: ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
113: ctxMap.put(PortletActionConstants.PHASE,
114: PortletActionConstants.RENDER_PHASE);
115:
116: PortletResult result = new PortletResult();
117: try {
118: result.doExecute("/WEB-INF/pages/testPage.jsp", inv);
119: } catch (Exception e) {
120: e.printStackTrace();
121: fail("Error occured!");
122: }
123:
124: }
125:
126: public void testDoExecute_event_locationIsAction() {
127:
128: Mock mockRequest = mock(ActionRequest.class);
129: Mock mockResponse = mock(ActionResponse.class);
130:
131: Constraint[] params = new Constraint[] {
132: eq(PortletActionConstants.ACTION_PARAM), eq("testView") };
133: mockResponse.expects(once()).method("setRenderParameter").with(
134: params);
135: params = new Constraint[] {
136: eq(PortletActionConstants.MODE_PARAM),
137: eq(PortletMode.VIEW.toString()) };
138: mockResponse.expects(once()).method("setRenderParameter").with(
139: params);
140: mockRequest.stubs().method("getPortletMode").will(
141: returnValue(PortletMode.VIEW));
142: ActionContext ctx = ActionContext.getContext();
143:
144: ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
145: ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
146: ctx.put(PortletActionConstants.PHASE,
147: PortletActionConstants.EVENT_PHASE);
148:
149: PortletResult result = new PortletResult();
150: try {
151: result.doExecute("testView.action",
152: (ActionInvocation) mockInvocation.proxy());
153: } catch (Exception e) {
154: e.printStackTrace();
155: fail("Error occured!");
156: }
157:
158: }
159:
160: public void testDoExecute_event_locationIsJsp() {
161: Mock mockRequest = mock(ActionRequest.class);
162: Mock mockResponse = mock(ActionResponse.class);
163:
164: Constraint[] params = new Constraint[] {
165: eq(PortletActionConstants.ACTION_PARAM),
166: eq("renderDirect") };
167: mockResponse.expects(once()).method("setRenderParameter").with(
168: params);
169: params = new Constraint[] {
170: eq(PortletActionConstants.MODE_PARAM),
171: eq(PortletMode.VIEW.toString()) };
172: mockResponse.expects(once()).method("setRenderParameter").with(
173: params);
174: mockRequest.stubs().method("getPortletMode").will(
175: returnValue(PortletMode.VIEW));
176:
177: ActionContext ctx = ActionContext.getContext();
178:
179: Map session = new HashMap();
180:
181: ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
182: ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
183: ctx.put(PortletActionConstants.PHASE,
184: PortletActionConstants.EVENT_PHASE);
185: ctx.put(ActionContext.SESSION, session);
186:
187: PortletResult result = new PortletResult();
188: try {
189: result.doExecute("/WEB-INF/pages/testJsp.jsp",
190: (ActionInvocation) mockInvocation.proxy());
191: } catch (Exception e) {
192: e.printStackTrace();
193: fail("Error occured!");
194: }
195: assertEquals("/WEB-INF/pages/testJsp.jsp", session
196: .get(RENDER_DIRECT_LOCATION));
197: }
198:
199: public void testDoExecute_event_locationHasQueryParams() {
200: Mock mockRequest = mock(ActionRequest.class);
201: Mock mockResponse = mock(ActionResponse.class);
202:
203: Constraint[] params = new Constraint[] {
204: eq(PortletActionConstants.ACTION_PARAM), eq("testView") };
205: mockResponse.expects(once()).method("setRenderParameter").with(
206: params);
207: params = new Constraint[] { eq("testParam1"), eq("testValue1") };
208: mockResponse.expects(once()).method("setRenderParameter").with(
209: params);
210: params = new Constraint[] { eq("testParam2"), eq("testValue2") };
211: mockResponse.expects(once()).method("setRenderParameter").with(
212: params);
213: params = new Constraint[] {
214: eq(PortletActionConstants.MODE_PARAM),
215: eq(PortletMode.VIEW.toString()) };
216: mockResponse.expects(once()).method("setRenderParameter").with(
217: params);
218: mockRequest.stubs().method("getPortletMode").will(
219: returnValue(PortletMode.VIEW));
220:
221: ActionContext ctx = ActionContext.getContext();
222:
223: ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
224: ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
225: ctx.put(PortletActionConstants.PHASE,
226: PortletActionConstants.EVENT_PHASE);
227:
228: PortletResult result = new PortletResult();
229: try {
230: result
231: .doExecute(
232: "testView.action?testParam1=testValue1&testParam2=testValue2",
233: (ActionInvocation) mockInvocation.proxy());
234: } catch (Exception e) {
235: e.printStackTrace();
236: fail("Error occured!");
237: }
238: }
239:
240: public void testTitleAndContentType() throws Exception {
241: Mock mockRequest = mock(RenderRequest.class);
242: Mock mockResponse = mock(RenderResponse.class);
243: Mock mockRd = mock(PortletRequestDispatcher.class);
244:
245: RenderRequest req = (RenderRequest) mockRequest.proxy();
246: RenderResponse res = (RenderResponse) mockResponse.proxy();
247: PortletRequestDispatcher rd = (PortletRequestDispatcher) mockRd
248: .proxy();
249: PortletConfig cfg = (PortletConfig) mockConfig.proxy();
250: PortletContext ctx = (PortletContext) mockCtx.proxy();
251:
252: Constraint[] params = new Constraint[] { same(req), same(res) };
253: mockRd.expects(once()).method("include").with(params);
254: mockCtx.expects(once()).method("getRequestDispatcher").with(
255: eq("/WEB-INF/pages/testPage.jsp"))
256: .will(returnValue(rd));
257: mockConfig.expects(once()).method("getPortletContext").will(
258: returnValue(ctx));
259:
260: mockRequest.stubs().method("getPortletMode").will(
261: returnValue(PortletMode.VIEW));
262:
263: ActionContext ctxMap = ActionContext.getContext();
264: ctxMap.put(PortletActionConstants.RESPONSE, res);
265: ctxMap.put(PortletActionConstants.REQUEST, req);
266: ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
267: ctxMap.put(PortletActionConstants.PHASE,
268: PortletActionConstants.RENDER_PHASE);
269:
270: mockResponse.expects(atLeastOnce()).method("setTitle").with(
271: eq("testTitle"));
272: mockResponse.expects(atLeastOnce()).method("setContentType")
273: .with(eq("testContentType"));
274:
275: PortletResult result = new PortletResult();
276: result.setTitle("testTitle");
277: result.setContentType("testContentType");
278: result.doExecute("/WEB-INF/pages/testPage.jsp",
279: (ActionInvocation) mockInvocation.proxy());
280: }
281:
282: public void tearDown() throws Exception {
283: super .tearDown();
284: ActionContext.setContext(null);
285: }
286:
287: public static void main(String[] args) {
288: TestRunner.run(PortletResultTest.class);
289: }
290:
291: }
|