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