001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet.result;
006:
007: import java.io.IOException;
008: import java.util.HashMap;
009: import java.util.Map;
010:
011: import javax.portlet.ActionRequest;
012: import javax.portlet.ActionResponse;
013: import javax.portlet.PortletConfig;
014: import javax.portlet.PortletContext;
015: import javax.portlet.PortletMode;
016: import javax.portlet.PortletRequestDispatcher;
017: import javax.portlet.RenderRequest;
018: import javax.portlet.RenderResponse;
019:
020: import junit.textui.TestRunner;
021:
022: import org.jmock.Mock;
023: import org.jmock.cglib.MockObjectTestCase;
024: import org.jmock.core.Constraint;
025:
026: import com.opensymphony.webwork.portlet.PortletActionConstants;
027: import com.opensymphony.xwork.ActionContext;
028: import com.opensymphony.xwork.ActionInvocation;
029:
030: /**
031: * PortletResultTest. Insert description.
032: *
033: * @author Nils-Helge Garli
034: * @version $Revision: 2519 $ $Date: 2006-03-21 21:37:27 +0100 (Tue, 21 Mar 2006) $
035: */
036: public class PortletResultTest extends MockObjectTestCase {
037:
038: Mock mockInvocation = null;
039: Mock mockConfig = null;
040: Mock mockCtx = null;
041:
042: public void setUp() throws Exception {
043: super .setUp();
044: mockInvocation = mock(ActionInvocation.class);
045: mockConfig = mock(PortletConfig.class);
046: mockCtx = mock(PortletContext.class);
047:
048: mockConfig.stubs().method(ANYTHING);
049: mockConfig.stubs().method("getPortletContext").will(
050: returnValue(mockCtx.proxy()));
051:
052: Map paramMap = new HashMap();
053: Map sessionMap = new HashMap();
054:
055: Map context = new HashMap();
056: context.put(ActionContext.SESSION, sessionMap);
057: context.put(ActionContext.PARAMETERS, paramMap);
058: context.put(PortletActionConstants.PORTLET_CONFIG, mockConfig
059: .proxy());
060:
061: ActionContext.setContext(new ActionContext(context));
062:
063: mockInvocation.stubs().method("getInvocationContext").will(
064: returnValue(ActionContext.getContext()));
065:
066: }
067:
068: public void testDoExecute_render() {
069: Mock mockRequest = mock(RenderRequest.class);
070: Mock mockResponse = mock(RenderResponse.class);
071: Mock mockRd = mock(PortletRequestDispatcher.class);
072: Mock mockPrep = mock(PortletRequestDispatcher.class);
073:
074: RenderRequest req = (RenderRequest) mockRequest.proxy();
075: RenderResponse res = (RenderResponse) mockResponse.proxy();
076: PortletRequestDispatcher rd = (PortletRequestDispatcher) mockRd
077: .proxy();
078: PortletConfig cfg = (PortletConfig) mockConfig.proxy();
079: PortletContext ctx = (PortletContext) mockCtx.proxy();
080: ActionInvocation inv = (ActionInvocation) mockInvocation
081: .proxy();
082:
083: Constraint[] params = new Constraint[] { same(req), same(res) };
084: mockRd.expects(once()).method("include").with(params);
085: mockPrep.expects(once()).method("include").with(params);
086: mockCtx.expects(once()).method("getRequestDispatcher").with(
087: eq("/WEB-INF/pages/testPage.jsp"))
088: .will(returnValue(rd));
089: mockCtx.expects(once()).method("getNamedDispatcher").with(
090: eq("preparator")).will(returnValue(mockPrep.proxy()));
091: mockResponse.expects(once()).method("setContentType").with(
092: eq("text/html"));
093: mockConfig.expects(once()).method("getPortletContext").will(
094: returnValue(ctx));
095:
096: mockRequest.stubs().method("getPortletMode").will(
097: returnValue(PortletMode.VIEW));
098:
099: ActionContext ctxMap = ActionContext.getContext();
100: ctxMap.put(PortletActionConstants.RESPONSE, res);
101: ctxMap.put(PortletActionConstants.REQUEST, req);
102: ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
103: ctxMap.put(PortletActionConstants.PHASE,
104: PortletActionConstants.RENDER_PHASE);
105:
106: PortletResult result = new PortletResult();
107: try {
108: result.doExecute("/WEB-INF/pages/testPage.jsp", inv);
109: } catch (Exception e) {
110: e.printStackTrace();
111: fail("Error occured!");
112: }
113:
114: }
115:
116: public void testDoExecute_event_locationIsAction() {
117:
118: Mock mockRequest = mock(ActionRequest.class);
119: Mock mockResponse = mock(ActionResponse.class);
120:
121: Constraint[] params = new Constraint[] {
122: eq(PortletActionConstants.ACTION_PARAM), eq("testView") };
123: mockResponse.expects(once()).method("setRenderParameter").with(
124: params);
125: params = new Constraint[] {
126: eq(PortletActionConstants.MODE_PARAM),
127: eq(PortletMode.VIEW.toString()) };
128: mockResponse.expects(once()).method("setRenderParameter").with(
129: params);
130: mockRequest.stubs().method("getPortletMode").will(
131: returnValue(PortletMode.VIEW));
132: ActionContext ctx = ActionContext.getContext();
133:
134: ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
135: ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
136: ctx.put(PortletActionConstants.PHASE,
137: PortletActionConstants.EVENT_PHASE);
138:
139: PortletResult result = new PortletResult();
140: try {
141: result.doExecute("testView.action",
142: (ActionInvocation) mockInvocation.proxy());
143: } catch (Exception e) {
144: e.printStackTrace();
145: fail("Error occured!");
146: }
147:
148: }
149:
150: public void testDoExecute_event_locationIsJsp() {
151: Mock mockRequest = mock(ActionRequest.class);
152: Mock mockResponse = mock(ActionResponse.class);
153:
154: Constraint[] params = new Constraint[] {
155: eq(PortletActionConstants.ACTION_PARAM),
156: eq("renderDirect") };
157: mockResponse.expects(once()).method("setRenderParameter").with(
158: params);
159: params = new Constraint[] { eq("location"),
160: eq("/WEB-INF/pages/testJsp.jsp") };
161: mockResponse.expects(once()).method("setRenderParameter").with(
162: params);
163: params = new Constraint[] {
164: eq(PortletActionConstants.MODE_PARAM),
165: eq(PortletMode.VIEW.toString()) };
166: mockResponse.expects(once()).method("setRenderParameter").with(
167: params);
168: mockRequest.stubs().method("getPortletMode").will(
169: returnValue(PortletMode.VIEW));
170:
171: ActionContext ctx = ActionContext.getContext();
172:
173: ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
174: ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
175: ctx.put(PortletActionConstants.PHASE,
176: PortletActionConstants.EVENT_PHASE);
177:
178: PortletResult result = new PortletResult();
179: try {
180: result.doExecute("/WEB-INF/pages/testJsp.jsp",
181: (ActionInvocation) mockInvocation.proxy());
182: } catch (Exception e) {
183: e.printStackTrace();
184: fail("Error occured!");
185: }
186: }
187:
188: public void testDoExecute_event_locationHasQueryParams() {
189: Mock mockRequest = mock(ActionRequest.class);
190: Mock mockResponse = mock(ActionResponse.class);
191:
192: Constraint[] params = new Constraint[] {
193: eq(PortletActionConstants.ACTION_PARAM), eq("testView") };
194: mockResponse.expects(once()).method("setRenderParameter").with(
195: params);
196: params = new Constraint[] { eq("testParam1"), eq("testValue1") };
197: mockResponse.expects(once()).method("setRenderParameter").with(
198: params);
199: params = new Constraint[] { eq("testParam2"), eq("testValue2") };
200: mockResponse.expects(once()).method("setRenderParameter").with(
201: params);
202: params = new Constraint[] {
203: eq(PortletActionConstants.MODE_PARAM),
204: eq(PortletMode.VIEW.toString()) };
205: mockResponse.expects(once()).method("setRenderParameter").with(
206: params);
207: mockRequest.stubs().method("getPortletMode").will(
208: returnValue(PortletMode.VIEW));
209:
210: ActionContext ctx = ActionContext.getContext();
211:
212: ctx.put(PortletActionConstants.REQUEST, mockRequest.proxy());
213: ctx.put(PortletActionConstants.RESPONSE, mockResponse.proxy());
214: ctx.put(PortletActionConstants.PHASE,
215: PortletActionConstants.EVENT_PHASE);
216:
217: PortletResult result = new PortletResult();
218: try {
219: result
220: .doExecute(
221: "testView.action?testParam1=testValue1&testParam2=testValue2",
222: (ActionInvocation) mockInvocation.proxy());
223: } catch (Exception e) {
224: e.printStackTrace();
225: fail("Error occured!");
226: }
227: }
228:
229: public void testTitleAndContentType() throws Exception {
230: Mock mockRequest = mock(RenderRequest.class);
231: Mock mockResponse = mock(RenderResponse.class);
232: Mock mockRd = mock(PortletRequestDispatcher.class);
233: Mock mockPrep = mock(PortletRequestDispatcher.class);
234:
235: RenderRequest req = (RenderRequest) mockRequest.proxy();
236: RenderResponse res = (RenderResponse) mockResponse.proxy();
237: PortletRequestDispatcher rd = (PortletRequestDispatcher) mockRd
238: .proxy();
239: PortletConfig cfg = (PortletConfig) mockConfig.proxy();
240: PortletContext ctx = (PortletContext) mockCtx.proxy();
241: ActionInvocation inv = (ActionInvocation) mockInvocation
242: .proxy();
243:
244: Constraint[] params = new Constraint[] { same(req), same(res) };
245: mockRd.expects(once()).method("include").with(params);
246: mockPrep.expects(once()).method("include").with(params);
247: mockCtx.expects(once()).method("getRequestDispatcher").with(
248: eq("/WEB-INF/pages/testPage.jsp"))
249: .will(returnValue(rd));
250: mockCtx.expects(once()).method("getNamedDispatcher").with(
251: eq("preparator")).will(returnValue(mockPrep.proxy()));
252: mockConfig.expects(once()).method("getPortletContext").will(
253: returnValue(ctx));
254:
255: mockRequest.stubs().method("getPortletMode").will(
256: returnValue(PortletMode.VIEW));
257:
258: ActionContext ctxMap = ActionContext.getContext();
259: ctxMap.put(PortletActionConstants.RESPONSE, res);
260: ctxMap.put(PortletActionConstants.REQUEST, req);
261: ctxMap.put(PortletActionConstants.PORTLET_CONFIG, cfg);
262: ctxMap.put(PortletActionConstants.PHASE,
263: PortletActionConstants.RENDER_PHASE);
264:
265: mockResponse.expects(atLeastOnce()).method("setTitle").with(
266: eq("testTitle"));
267: mockResponse.expects(atLeastOnce()).method("setContentType")
268: .with(eq("testContentType"));
269:
270: PortletResult result = new PortletResult();
271: result.setTitle("testTitle");
272: result.setContentType("testContentType");
273: result.doExecute("/WEB-INF/pages/testPage.jsp",
274: (ActionInvocation) mockInvocation.proxy());
275: }
276:
277: public void tearDown() throws Exception {
278: super .tearDown();
279: ActionContext.setContext(null);
280: }
281:
282: public static void main(String[] args) {
283: TestRunner.run(PortletResultTest.class);
284: }
285:
286: }
|