View Javadoc

1   /*
2    * $Id: PortletUrlTagTest.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp;
19  
20  import java.lang.reflect.Field;
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import javax.portlet.PortletMode;
27  import javax.portlet.PortletRequest;
28  import javax.portlet.PortletResponse;
29  import javax.portlet.PortletURL;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  import javax.portlet.WindowState;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.jsp.PageContext;
36  
37  import junit.textui.TestRunner;
38  
39  import org.apache.struts2.config.Settings;
40  import org.apache.struts2.dispatcher.Dispatcher;
41  import org.apache.struts2.portlet.PortletActionConstants;
42  import org.apache.struts2.portlet.util.PortletUrlHelper;
43  import org.jmock.Mock;
44  import org.jmock.cglib.MockObjectTestCase;
45  import org.jmock.core.Constraint;
46  
47  import com.mockobjects.servlet.MockJspWriter;
48  import com.opensymphony.xwork2.ActionContext;
49  import com.opensymphony.xwork2.util.ValueStack;
50  import com.opensymphony.xwork2.util.ValueStackFactory;
51  
52  /***
53   */
54  public class PortletUrlTagTest extends MockObjectTestCase {
55  
56  	URLTag tag = new URLTag();
57  
58  	Mock mockHttpReq = null;
59  
60  	Mock mockHttpRes = null;
61  
62  	Mock mockPortletReq = null;
63  
64  	Mock mockPortletRes = null;
65  
66  	Mock mockPageCtx = null;
67  
68  	Mock mockPortletUrl = null;
69  	
70  	MockJspWriter mockJspWriter = null;
71  
72  	ValueStack stack = null;
73  
74  	public static void main(String[] args) {
75  		TestRunner.run(PortletUrlTagTest.class);
76  	}
77  
78  	public void setUp() throws Exception {
79  		super.setUp();
80  		
81  		Settings.reset();
82          Dispatcher.setInstance(new Dispatcher(null));
83          
84          mockPortletApiAvailable();
85  		
86  		stack = ValueStackFactory.getFactory().createValueStack();
87  
88  		
89  		mockHttpReq = mock(HttpServletRequest.class);
90  		mockHttpRes = mock(HttpServletResponse.class);
91  		mockPortletReq = mock(RenderRequest.class);
92  		mockPortletRes = mock(RenderResponse.class);
93  		mockPageCtx = mock(PageContext.class);
94  		mockPortletUrl = mock(PortletURL.class);
95  		mockJspWriter = new MockJspWriter();
96  
97  		mockPageCtx.stubs().method("getRequest").will(
98  				returnValue((HttpServletRequest) mockHttpReq.proxy()));
99  		mockPageCtx.stubs().method("getResponse").will(
100 				returnValue((HttpServletResponse) mockHttpRes.proxy()));
101 		mockPageCtx.stubs().method("getOut").will(returnValue(mockJspWriter));
102 		
103 		mockHttpReq.stubs().method("getScheme").will(returnValue("http"));
104 		mockHttpReq.stubs().method("getAttribute").with(
105 				eq("struts.valueStack")).will(returnValue(stack));
106 		mockHttpReq.stubs().method("getAttribute").with(
107 				eq("javax.portlet.response")).will(
108 				returnValue((PortletResponse) mockPortletRes.proxy()));
109 		mockHttpReq.stubs().method("getAttribute").with(
110 				eq("javax.portlet.request")).will(
111 				returnValue((PortletRequest) mockPortletReq.proxy()));
112 		
113 		mockPortletReq.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
114 		mockPortletReq.stubs().method("getWindowState").will(returnValue(WindowState.NORMAL));
115 		mockPortletReq.stubs().method("getContextPath").will(returnValue("/contextPath"));
116 
117 		tag.setPageContext((PageContext) mockPageCtx.proxy());
118 		
119 		Map modeMap = new HashMap();
120 		modeMap.put(PortletMode.VIEW, "/view");
121 		modeMap.put(PortletMode.HELP, "/help");
122 		modeMap.put(PortletMode.EDIT, "/edit");
123 		Map sessionMap = new HashMap();
124 		Map contextMap = new HashMap();
125 		contextMap.put(ActionContext.SESSION, sessionMap);
126 		contextMap.put(PortletActionConstants.REQUEST, mockPortletReq.proxy());
127 		contextMap.put(PortletActionConstants.RESPONSE, mockPortletRes.proxy());
128 		contextMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
129 		contextMap.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
130 		ActionContext ctx = new ActionContext(contextMap);
131 		ctx.setValueStack(stack);
132 		ActionContext.setContext(ctx);
133     }
134 
135 	/***
136      * 
137      */
138     private void mockPortletApiAvailable() {
139         try {
140             Field field = Dispatcher.class.getDeclaredField("portletSupportActive");
141             field.setAccessible(true);
142             field.set(null, Boolean.TRUE);
143         }
144         catch(Exception e) {
145             
146         }
147         
148     }
149 
150     public void testEnsureParamsAreStringArrays() {
151 		Map params = new HashMap();
152 		params.put("param1", "Test1");
153 		params.put("param2", new String[] { "Test2" });
154 
155 		Map result = PortletUrlHelper.ensureParamsAreStringArrays(params);
156 		assertEquals(2, result.size());
157 		assertTrue(result.get("param1") instanceof String[]);
158 	}
159 
160 	public void testSetWindowState() throws Exception {
161 
162 	    PortletMode mode = PortletMode.VIEW;
163 	    
164 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
165 
166 		mockPortletRes.expects(once()).method("createRenderURL").will(
167 				returnValue((PortletURL) mockPortletUrl.proxy()));
168 		
169 		Map paramMap = new HashMap();
170 		paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
171 		paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
172 		
173 		mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
174 		mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.MAXIMIZED));
175 		mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
176 		
177 		tag.setAction("testAction");
178 		tag.setWindowState("maximized");
179 		tag.doStartTag();
180 		tag.doEndTag();
181 
182 	}
183 	
184 	public void testSetPortletMode() throws Exception  {
185 	    
186 	    PortletMode mode = PortletMode.HELP;
187 	    
188 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
189 
190 		mockPortletRes.expects(once()).method("createRenderURL").will(
191 				returnValue((PortletURL) mockPortletUrl.proxy()));
192 		
193 		Map paramMap = new HashMap();
194 		paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/help/testAction"});
195 		paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
196 		
197 		mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
198 		mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.HELP));
199 		mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
200 		
201 		tag.setAction("testAction");
202 		tag.setPortletMode("help");
203 		tag.doStartTag();
204 		tag.doEndTag();
205 	}
206 	
207 	public void testUrlWithQueryParams() throws Exception {
208 	    
209 	    PortletMode mode = PortletMode.VIEW;
210 	    
211 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
212 
213 		mockPortletRes.expects(once()).method("createRenderURL").will(
214 				returnValue((PortletURL) mockPortletUrl.proxy()));
215 		
216 		Map paramMap = new HashMap();
217 		paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
218 		paramMap.put("testParam1", new String[]{"testValue1"});
219 		paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
220 		
221 		mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
222 		mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
223 		mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
224 		
225 		tag.setAction("testAction?testParam1=testValue1");
226 		tag.doStartTag();
227 		tag.doEndTag();
228 	}
229 	
230 	public void testActionUrl() throws Exception {
231 	    
232 	    PortletMode mode = PortletMode.VIEW;
233 	    
234 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
235 
236 		mockPortletRes.expects(once()).method("createActionURL").will(
237 				returnValue((PortletURL) mockPortletUrl.proxy()));
238 		
239 		Map paramMap = new HashMap();
240 		paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
241 		paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
242 		
243 		mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
244 		mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
245 		mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
246 		
247 		tag.setAction("testAction");
248 		tag.setPortletUrlType("action");
249 		tag.doStartTag();
250 		tag.doEndTag();
251 	}
252 	
253 	public void testResourceUrl() throws Exception {
254 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
255 		mockPortletRes.expects(once()).method("encodeURL").will(returnValue("/contextPath/image.gif"));
256 		mockJspWriter.setExpectedData("/contextPath/image.gif");
257 		tag.setValue("image.gif");
258 		tag.doStartTag();
259 		tag.doEndTag();
260 		mockJspWriter.verify();
261 	}
262 	
263 	public void testResourceUrlWithNestedParam() throws Exception {
264 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
265 		mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1")).will(returnValue("/contextPath/image.gif?testParam1=testValue1"));
266 		mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1");
267 		
268 		ParamTag paramTag = new ParamTag();
269 		paramTag.setPageContext((PageContext)mockPageCtx.proxy());
270 		paramTag.setParent(tag);
271 		paramTag.setName("testParam1");
272 		paramTag.setValue("'testValue1'");
273 		tag.setValue("image.gif");
274 		tag.doStartTag();
275 		paramTag.doStartTag();
276 		paramTag.doEndTag();
277 		tag.doEndTag();
278 		mockJspWriter.verify();
279 	}
280 	
281 	public void testResourceUrlWithTwoNestedParam() throws Exception {
282 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
283 		mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2")).will(returnValue("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2"));
284 		mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2");
285 		
286 		ParamTag paramTag = new ParamTag();
287 		paramTag.setPageContext((PageContext)mockPageCtx.proxy());
288 		paramTag.setParent(tag);
289 		paramTag.setName("testParam1");
290 		paramTag.setValue("'testValue1'");
291 		ParamTag paramTag2 = new ParamTag();
292 		paramTag2.setPageContext((PageContext)mockPageCtx.proxy());
293 		paramTag2.setParent(tag);
294 		paramTag2.setName("testParam2");
295 		paramTag2.setValue("'testValue2'");
296 		tag.setValue("image.gif");
297 		tag.doStartTag();
298 		paramTag.doStartTag();
299 		paramTag.doEndTag();
300 		paramTag2.doStartTag();
301 		paramTag2.doEndTag();
302 		tag.doEndTag();
303 		mockJspWriter.verify();
304 	}
305 
306 	private static class ParamMapConstraint implements Constraint {
307 
308 		private Map myExpectedMap = null;
309 		private Map myActualMap = null;
310 		
311 		public ParamMapConstraint(Map expectedMap) {
312 			if(expectedMap == null) {
313 				throw new IllegalArgumentException("Use an isNull constraint instead!");
314 			}
315 			myExpectedMap = expectedMap;
316 		}
317 
318 		/* (non-Javadoc)
319 		 * @see org.jmock.core.Constraint#eval(java.lang.Object)
320 		 */
321 		public boolean eval(Object val) {
322 			myActualMap = (Map)val;
323 			boolean result = false;
324 			if(val != null) {
325 				if(myExpectedMap.size() == myActualMap.size()) {
326 					Iterator keys = myExpectedMap.keySet().iterator();
327 					boolean allSame = true;
328 					while(keys.hasNext()) {
329 						Object key = keys.next();
330 						if(!myActualMap.containsKey(key)) {
331 							allSame = false;
332 							break;
333 						}
334 						else {
335 							String[] expected = (String[])myExpectedMap.get(key);
336 							String[] actual = (String[])myActualMap.get(key);
337 							if(!Arrays.equals(expected, actual)) {
338 								allSame = false;
339 								break;
340 							}
341 						}
342 					}
343 					result = allSame;
344 				}
345 			}
346 			return result;
347 		}
348 
349 		/* (non-Javadoc)
350 		 * @see org.jmock.core.SelfDescribing#describeTo(java.lang.StringBuffer)
351 		 */
352 		public StringBuffer describeTo(StringBuffer sb) {
353 			return sb.append(myExpectedMap);
354 		}
355 		
356 		
357 
358 	}
359 
360 }