001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet.dispatcher;
006:
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.ListResourceBundle;
011: import java.util.Locale;
012: import java.util.Map;
013:
014: import javax.portlet.ActionRequest;
015: import javax.portlet.ActionResponse;
016: import javax.portlet.PortletConfig;
017: import javax.portlet.PortletContext;
018: import javax.portlet.PortletMode;
019: import javax.portlet.PortletSession;
020: import javax.portlet.RenderRequest;
021: import javax.portlet.RenderResponse;
022: import javax.portlet.WindowState;
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletContextEvent;
025:
026: import junit.textui.TestRunner;
027:
028: import org.jmock.Mock;
029: import org.jmock.cglib.MockObjectTestCase;
030: import org.jmock.core.Constraint;
031:
032: import com.opensymphony.webwork.portlet.PortletActionConstants;
033: import com.opensymphony.webwork.portlet.context.ServletContextHolderListener;
034: import com.opensymphony.xwork.Action;
035: import com.opensymphony.xwork.ActionInvocation;
036: import com.opensymphony.xwork.ActionProxy;
037: import com.opensymphony.xwork.ActionProxyFactory;
038: import com.opensymphony.xwork.util.OgnlValueStack;
039:
040: /**
041: * Jsr168DispatcherTest. Insert description.
042: *
043: * @author Nils-Helge Garli
044: * @version $Revision: 2395 $ $Date: 2006-03-13 08:24:07 +0100 (Mon, 13 Mar 2006) $
045: */
046: public class Jsr168DispatcherTest extends MockObjectTestCase implements
047: PortletActionConstants {
048:
049: Jsr168Dispatcher dispatcher = null;
050: Mock mockConfig = null;
051: Mock mockCtx = null;
052: Mock mockRequest = null;
053: Mock mockSession = null;
054: Mock mockActionFactory = null;
055: Mock mockActionProxy = null;
056: Mock mockAction = null;
057: Mock mockInvocation = null;
058:
059: public void setUp() {
060: dispatcher = new Jsr168Dispatcher();
061: }
062:
063: private void initPortletConfig(final Map initParams,
064: final Map attributes) {
065: mockConfig = mock(PortletConfig.class);
066: mockCtx = mock(PortletContext.class);
067: mockConfig.stubs().method(ANYTHING);
068: setupStub(initParams, mockConfig, "getInitParameter");
069: mockCtx.stubs().method("getAttributeNames").will(
070: returnValue(Collections
071: .enumeration(attributes.keySet())));
072: setupStub(attributes, mockCtx, "getAttribute");
073: mockConfig.stubs().method("getPortletContext").will(
074: returnValue(mockCtx.proxy()));
075: mockCtx.stubs().method("getInitParameterNames").will(
076: returnValue(Collections
077: .enumeration(initParams.keySet())));
078: setupStub(initParams, mockCtx, "getInitParameter");
079:
080: mockConfig.stubs().method("getResourceBundle").will(
081: returnValue(new ListResourceBundle() {
082: protected Object[][] getContents() {
083: return new String[][] { {
084: "javax.portlet.title", "MyTitle" } };
085: }
086: }));
087: }
088:
089: private void setupActionFactory(String namespace,
090: String actionName, String result, OgnlValueStack stack) {
091: if (mockActionFactory == null) {
092: mockActionFactory = mock(ActionProxyFactory.class);
093: }
094: mockAction = mock(Action.class);
095: mockActionProxy = mock(ActionProxy.class);
096: mockInvocation = mock(ActionInvocation.class);
097:
098: mockActionFactory.expects(once()).method("createActionProxy")
099: .with(
100: new Constraint[] { eq(namespace),
101: eq(actionName), isA(Map.class) }).will(
102: returnValue(mockActionProxy.proxy()));
103: mockActionProxy.stubs().method("getAction").will(
104: returnValue(mockAction.proxy()));
105: mockActionProxy.expects(once()).method("execute").will(
106: returnValue(result));
107: mockActionProxy.expects(once()).method("getInvocation").will(
108: returnValue(mockInvocation.proxy()));
109: mockInvocation.stubs().method("getStack").will(
110: returnValue(stack));
111:
112: }
113:
114: public void testRender_ok() {
115: final Mock mockResponse = mock(RenderResponse.class);
116: mockResponse.stubs().method(ANYTHING);
117: final Mock servletContext = mock(ServletContext.class);
118: servletContext.stubs().method(ANYTHING);
119: ServletContextEvent event = new ServletContextEvent(
120: (ServletContext) servletContext.proxy());
121: new ServletContextHolderListener().contextInitialized(event);
122: String actionName = "testAction";
123: PortletMode mode = PortletMode.VIEW;
124:
125: Map requestParams = new HashMap();
126: requestParams.put(PortletActionConstants.ACTION_PARAM,
127: new String[] { "/view/testAction" });
128: requestParams.put(EVENT_ACTION, new String[] { "true" });
129: requestParams.put(PortletActionConstants.MODE_PARAM,
130: new String[] { mode.toString() });
131:
132: Map sessionMap = new HashMap();
133:
134: Map initParams = new HashMap();
135: initParams.put("viewNamespace", "/view");
136:
137: initPortletConfig(initParams, new HashMap());
138: initRequest(requestParams, new HashMap(), sessionMap,
139: new HashMap(), PortletMode.VIEW, WindowState.NORMAL,
140: false, null);
141: setupActionFactory("/view", "testAction", "success",
142: new OgnlValueStack());
143:
144: mockInvocation.expects(once()).method("getStack").will(
145: returnValue(null));
146: //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
147: try {
148: dispatcher
149: .setActionProxyFactory((ActionProxyFactory) mockActionFactory
150: .proxy());
151: dispatcher.init((PortletConfig) mockConfig.proxy());
152: dispatcher.render((RenderRequest) mockRequest.proxy(),
153: (RenderResponse) mockResponse.proxy());
154: } catch (Exception e) {
155: e.printStackTrace();
156: fail("Error occured");
157: }
158: }
159:
160: public void testProcessAction_ok() {
161: final Mock mockResponse = mock(ActionResponse.class);
162:
163: String actionName = "testAction";
164: PortletMode mode = PortletMode.VIEW;
165: Map initParams = new HashMap();
166: initParams.put("viewNamespace", "/view");
167:
168: Map requestParams = new HashMap();
169: requestParams.put(PortletActionConstants.ACTION_PARAM,
170: new String[] { "/view/testAction" });
171: requestParams.put(PortletActionConstants.MODE_PARAM,
172: new String[] { mode.toString() });
173:
174: initPortletConfig(initParams, new HashMap());
175: initRequest(requestParams, new HashMap(), new HashMap(),
176: new HashMap(), PortletMode.VIEW, WindowState.NORMAL,
177: true, null);
178: setupActionFactory("/view", "testAction", "success",
179: new OgnlValueStack());
180: Constraint[] paramConstraints = new Constraint[] {
181: eq(PortletActionConstants.EVENT_ACTION),
182: same(mockActionProxy.proxy()) };
183:
184: mockSession.expects(once()).method("setAttribute").with(
185: paramConstraints);
186:
187: mockResponse.expects(once()).method("setRenderParameter").with(
188: new Constraint[] {
189: eq(PortletActionConstants.EVENT_ACTION),
190: eq("true") });
191:
192: //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
193: try {
194: dispatcher
195: .setActionProxyFactory((ActionProxyFactory) mockActionFactory
196: .proxy());
197: dispatcher.init((PortletConfig) mockConfig.proxy());
198: dispatcher.processAction((ActionRequest) mockRequest
199: .proxy(), (ActionResponse) mockResponse.proxy());
200: } catch (Exception e) {
201: e.printStackTrace();
202: fail("Error occured");
203: }
204: }
205:
206: /**
207: * Initialize the mock request (and as a result, the mock session)
208: * @param requestParams The request parameters
209: * @param requestAttributes The request attributes
210: * @param sessionParams The session attributes
211: * @param renderParams The render parameters. Will only be set if <code>isEvent</code> is <code>true</code>
212: * @param mode The portlet mode
213: * @param state The portlet window state
214: * @param isEvent <code>true</code> when the request is an ActionRequest.
215: * @param locale The locale. If <code>null</code>, the request will return <code>Locale.getDefault()</code>
216: */
217: private void initRequest(Map requestParams, Map requestAttributes,
218: Map sessionParams, Map renderParams, PortletMode mode,
219: WindowState state, boolean isEvent, Locale locale) {
220: mockRequest = isEvent ? mock(ActionRequest.class)
221: : mock(RenderRequest.class);
222: mockSession = mock(PortletSession.class);
223: mockSession.stubs().method(ANYTHING);
224: mockRequest.stubs().method(ANYTHING);
225: setupStub(sessionParams, mockSession, "getAttribute");
226: mockSession.stubs().method("getAttributeNames").will(
227: returnValue(Collections.enumeration(sessionParams
228: .keySet())));
229: setupParamStub(requestParams, mockRequest, "getParameter");
230: setupStub(requestAttributes, mockRequest, "getAttribute");
231: mockRequest.stubs().method("getAttributeNames").will(
232: returnValue(Collections.enumeration(requestAttributes
233: .keySet())));
234: mockRequest.stubs().method("getParameterMap").will(
235: returnValue(requestParams));
236: mockRequest.stubs().method("getParameterNames").will(
237: returnValue(Collections.enumeration(requestParams
238: .keySet())));
239: mockRequest.stubs().method("getPortletSession").will(
240: returnValue(mockSession.proxy()));
241: if (locale != null) {
242: mockRequest.stubs().method("getLocale").will(
243: returnValue(locale));
244: } else {
245: mockRequest.stubs().method("getLocale").will(
246: returnValue(Locale.getDefault()));
247: }
248: mockRequest.stubs().method("getPortletMode").will(
249: returnValue(mode));
250: mockRequest.stubs().method("getWindowState").will(
251: returnValue(state));
252: }
253:
254: /**
255: * @param requestParams
256: * @param mockRequest2
257: * @param string
258: */
259: private void setupParamStub(Map requestParams, Mock mockRequest,
260: String method) {
261: Map newMap = new HashMap();
262: Iterator it = requestParams.keySet().iterator();
263: while (it.hasNext()) {
264: Object key = it.next();
265: String[] val = (String[]) requestParams.get(key);
266: newMap.put(key, val[0]);
267: }
268: setupStub(newMap, mockRequest, method);
269:
270: }
271:
272: /**
273: * Set up stubs for the mock.
274: * @param map The map containing the <code>key</code> and <code>values</code>. The key is the
275: * expected parameter to <code>method</code>, and value is the value that should be returned from
276: * the stub.
277: * @param mock The mock to initialize.
278: * @param method The name of the method to stub.
279: */
280: private void setupStub(Map map, Mock mock, String method) {
281: Iterator it = map.keySet().iterator();
282: while (it.hasNext()) {
283: Object key = it.next();
284: Object val = map.get(key);
285: mock.stubs().method(method).with(eq(key)).will(
286: returnValue(val));
287: }
288: }
289:
290: public void testModeChangeUsingPortletWidgets() {
291: final Mock mockResponse = mock(RenderResponse.class);
292: mockResponse.stubs().method(ANYTHING);
293: String actionName = "testAction";
294: PortletMode mode = PortletMode.EDIT;
295:
296: Map requestParams = new HashMap();
297: requestParams.put(PortletActionConstants.ACTION_PARAM,
298: new String[] { "/view/testAction" });
299: requestParams.put(EVENT_ACTION, new String[] { "false" });
300: requestParams.put(PortletActionConstants.MODE_PARAM,
301: new String[] { PortletMode.VIEW.toString() });
302:
303: Map sessionMap = new HashMap();
304:
305: Map initParams = new HashMap();
306: initParams.put("viewNamespace", "/view");
307: initParams.put("editNamespace", "/edit");
308:
309: initPortletConfig(initParams, new HashMap());
310: initRequest(requestParams, new HashMap(), sessionMap,
311: new HashMap(), mode, WindowState.NORMAL, false, null);
312: setupActionFactory("/edit", "default", "success",
313: new OgnlValueStack());
314:
315: mockInvocation.expects(once()).method("getStack").will(
316: returnValue(null));
317: //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
318: try {
319: dispatcher
320: .setActionProxyFactory((ActionProxyFactory) mockActionFactory
321: .proxy());
322: dispatcher.init((PortletConfig) mockConfig.proxy());
323: dispatcher.render((RenderRequest) mockRequest.proxy(),
324: (RenderResponse) mockResponse.proxy());
325: } catch (Exception e) {
326: e.printStackTrace();
327: fail("Error occured");
328: }
329: }
330:
331: public static void main(String[] args) {
332: TestRunner.run(Jsr168DispatcherTest.class);
333: }
334:
335: }
|