View Javadoc

1   /*
2    * $Id: Jsr168DispatcherTest.java 568895 2007-08-23 08:57:36Z rgielen $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.portlet.dispatcher;
22  
23  import com.opensymphony.xwork2.Action;
24  import com.opensymphony.xwork2.ActionInvocation;
25  import com.opensymphony.xwork2.ActionProxy;
26  import com.opensymphony.xwork2.ActionProxyFactory;
27  import com.opensymphony.xwork2.util.ValueStack;
28  import com.opensymphony.xwork2.util.ValueStackFactory;
29  import junit.textui.TestRunner;
30  import org.apache.struts2.StrutsConstants;
31  import org.apache.struts2.portlet.PortletActionConstants;
32  import org.jmock.Mock;
33  import org.jmock.cglib.MockObjectTestCase;
34  import org.jmock.core.Constraint;
35  
36  import javax.portlet.*;
37  import java.util.*;
38  
39  /***
40   * Jsr168DispatcherTest. Insert description.
41   *
42   */
43  public class Jsr168DispatcherTest extends MockObjectTestCase implements PortletActionConstants {
44  
45      Jsr168Dispatcher dispatcher = null;
46      Mock mockConfig = null;
47      Mock mockCtx = null;
48      Mock mockRequest = null;
49      Mock mockSession = null;
50      Mock mockActionFactory = null;
51      Mock mockActionProxy = null;
52      Mock mockAction = null;
53      Mock mockInvocation = null;
54  
55      public void setUp() {
56          dispatcher = new Jsr168Dispatcher();
57      }
58  
59      private void initPortletConfig(final Map initParams, final Map attributes) {
60          mockConfig = mock(PortletConfig.class);
61          mockCtx = mock(PortletContext.class);
62          mockConfig.stubs().method(ANYTHING);
63          mockCtx.stubs().method(ANYTHING);
64          setupStub(initParams, mockConfig, "getInitParameter");
65          mockCtx.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(attributes.keySet())));
66          setupStub(attributes, mockCtx, "getAttribute");
67          mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
68          mockCtx.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
69          setupStub(initParams, mockCtx, "getInitParameter");
70          mockConfig.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
71          setupStub(initParams, mockConfig, "getInitParameter");
72          mockConfig.stubs().method("getResourceBundle").will(returnValue(new ListResourceBundle() {
73              protected Object[][] getContents() {
74                  return new String[][]{{"javax.portlet.title", "MyTitle"}};
75              }
76          }));
77      }
78  
79      private void setupActionFactory(String namespace, String actionName, String result, ValueStack stack) {
80          if(mockActionFactory == null) {
81              mockActionFactory = mock(ActionProxyFactory.class);
82          }
83          mockAction = mock(Action.class);
84          mockActionProxy = mock(ActionProxy.class);
85          mockInvocation = mock(ActionInvocation.class);
86  
87          mockActionFactory.expects(once()).method("createActionProxy").with(new Constraint[]{eq(namespace), eq(actionName), isA(Map.class)}).will(returnValue(mockActionProxy.proxy()));
88          mockActionProxy.stubs().method("getAction").will(returnValue(mockAction.proxy()));
89          mockActionProxy.expects(once()).method("execute").will(returnValue(result));
90          mockActionProxy.expects(once()).method("getInvocation").will(returnValue(mockInvocation.proxy()));
91          mockActionProxy.expects(once()).method("setMethod");
92          mockInvocation.stubs().method("getStack").will(returnValue(stack));
93  
94      }
95  
96      public void testRender_ok() {
97          final Mock mockResponse = mock(RenderResponse.class);
98          mockResponse.stubs().method(ANYTHING);
99  
100         PortletMode mode = PortletMode.VIEW;
101 
102         Map requestParams = new HashMap();
103         requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
104         requestParams.put(EVENT_ACTION, new String[]{"true"});
105         requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
106 
107         Map sessionMap = new HashMap();
108 
109 
110 
111         Map initParams = new HashMap();
112         initParams.put("viewNamespace", "/view");
113         initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
114 
115         initPortletConfig(initParams, new HashMap());
116         initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), PortletMode.VIEW, WindowState.NORMAL, false, null);
117         setupActionFactory("/view", "testAction", "success", ValueStackFactory.getFactory().createValueStack());
118 
119         mockInvocation.expects(once()).method("getStack").will(
120                 returnValue(null));
121         //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
122         try {
123             dispatcher
124                     .setActionProxyFactory((ActionProxyFactory) mockActionFactory
125                             .proxy());
126             dispatcher.init((PortletConfig) mockConfig.proxy());
127             dispatcher.render((RenderRequest) mockRequest.proxy(),
128                     (RenderResponse) mockResponse.proxy());
129         } catch (Exception e) {
130             e.printStackTrace();
131             fail("Error occured");
132         }
133     }
134 
135     public void testProcessAction_ok() {
136         final Mock mockResponse = mock(ActionResponse.class);
137 
138         PortletMode mode = PortletMode.VIEW;
139         Map initParams = new HashMap();
140         initParams.put("viewNamespace", "/view");
141 
142         Map requestParams = new HashMap();
143         requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
144         requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
145 
146         initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
147         initPortletConfig(initParams, new HashMap());
148         initRequest(requestParams, new HashMap(), new HashMap(), new HashMap(), PortletMode.VIEW, WindowState.NORMAL, true, null);
149         setupActionFactory("/view", "testAction", "success", ValueStackFactory.getFactory().createValueStack());
150         //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
151         try {
152             dispatcher
153                     .setActionProxyFactory((ActionProxyFactory) mockActionFactory
154                             .proxy());
155             dispatcher.init((PortletConfig) mockConfig.proxy());
156             dispatcher.processAction((ActionRequest) mockRequest.proxy(),
157                     (ActionResponse) mockResponse.proxy());
158         } catch (Exception e) {
159             e.printStackTrace();
160             fail("Error occured");
161         }
162     }
163 
164     /***
165      * Initialize the mock request (and as a result, the mock session)
166      * @param requestParams The request parameters
167      * @param requestAttributes The request attributes
168      * @param sessionParams The session attributes
169      * @param renderParams The render parameters. Will only be set if <code>isEvent</code> is <code>true</code>
170      * @param mode The portlet mode
171      * @param state The portlet window state
172      * @param isEvent <code>true</code> when the request is an ActionRequest.
173      * @param locale The locale. If <code>null</code>, the request will return <code>Locale.getDefault()</code>
174      */
175     private void initRequest(Map requestParams, Map requestAttributes, Map sessionParams, Map renderParams, PortletMode mode, WindowState state, boolean isEvent, Locale locale) {
176         mockRequest = isEvent ? mock(ActionRequest.class) : mock(RenderRequest.class);
177         mockSession = mock(PortletSession.class);
178         mockSession.stubs().method(ANYTHING);
179         mockRequest.stubs().method(ANYTHING);
180         setupStub(sessionParams, mockSession, "getAttribute");
181         mockSession.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(sessionParams.keySet())));
182         setupParamStub(requestParams, mockRequest, "getParameter");
183         setupStub(requestAttributes, mockRequest, "getAttribute");
184         mockRequest.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(requestAttributes.keySet())));
185         mockRequest.stubs().method("getParameterMap").will(returnValue(requestParams));
186         mockRequest.stubs().method("getParameterNames").will(returnValue(Collections.enumeration(requestParams.keySet())));
187         mockRequest.stubs().method("getPortletSession").will(returnValue(mockSession.proxy()));
188         if(locale != null) {
189             mockRequest.stubs().method("getLocale").will(returnValue(locale));
190         }
191         else {
192             mockRequest.stubs().method("getLocale").will(returnValue(Locale.getDefault()));
193         }
194         mockRequest.stubs().method("getPortletMode").will(returnValue(mode));
195         mockRequest.stubs().method("getWindowState").will(returnValue(state));
196     }
197 
198     /***
199      * @param requestParams
200      * @param mockRequest2
201      * @param string
202      */
203     private void setupParamStub(Map requestParams, Mock mockRequest, String method) {
204         Map newMap = new HashMap();
205         Iterator it = requestParams.keySet().iterator();
206         while(it.hasNext()) {
207             Object key = it.next();
208             String[] val = (String[])requestParams.get(key);
209             newMap.put(key, val[0]);
210         }
211         setupStub(newMap, mockRequest, method);
212 
213     }
214 
215     /***
216      * Set up stubs for the mock.
217      * @param map The map containing the <code>key</code> and <code>values</code>. The key is the
218      * expected parameter to <code>method</code>, and value is the value that should be returned from
219      * the stub.
220      * @param mock The mock to initialize.
221      * @param method The name of the method to stub.
222      */
223     private void setupStub(Map map, Mock mock, String method) {
224         Iterator it = map.keySet().iterator();
225         while(it.hasNext()) {
226             Object key = it.next();
227             Object val = map.get(key);
228             mock.stubs().method(method).with(eq(key)).will(returnValue(val));
229         }
230     }
231 
232     public void testModeChangeUsingPortletWidgets() {
233         final Mock mockResponse = mock(RenderResponse.class);
234         mockResponse.stubs().method(ANYTHING);
235         PortletMode mode = PortletMode.EDIT;
236 
237         Map requestParams = new HashMap();
238         requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
239         requestParams.put(EVENT_ACTION, new String[]{"false"});
240         requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{PortletMode.VIEW.toString()});
241 
242         Map sessionMap = new HashMap();
243 
244         Map initParams = new HashMap();
245         initParams.put("viewNamespace", "/view");
246         initParams.put("editNamespace", "/edit");
247 
248         initPortletConfig(initParams, new HashMap());
249         initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), mode, WindowState.NORMAL, false, null);
250         setupActionFactory("/edit", "default", "success", ValueStackFactory.getFactory().createValueStack());
251 
252         mockInvocation.expects(once()).method("getStack").will(
253                 returnValue(null));
254         //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
255         try {
256             dispatcher
257                     .setActionProxyFactory((ActionProxyFactory) mockActionFactory
258                             .proxy());
259             dispatcher.init((PortletConfig) mockConfig.proxy());
260             dispatcher.render((RenderRequest) mockRequest.proxy(),
261                     (RenderResponse) mockResponse.proxy());
262         } catch (Exception e) {
263             e.printStackTrace();
264             fail("Error occured");
265         }
266     }
267 
268     public static void main(String[] args) {
269         TestRunner.run(Jsr168DispatcherTest.class);
270     }
271 
272 }