View Javadoc

1   /*
2    * $Id$
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  
22  package org.apache.struts2.components;
23  
24  import java.io.StringWriter;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.portlet.PortletMode;
29  import javax.portlet.PortletURL;
30  
31  import org.apache.struts2.StrutsTestCase;
32  import org.apache.struts2.portlet.PortletActionConstants;
33  import org.apache.struts2.portlet.servlet.PortletServletRequest;
34  import org.apache.struts2.portlet.servlet.PortletServletResponse;
35  import org.springframework.mock.web.portlet.MockPortalContext;
36  import org.springframework.mock.web.portlet.MockPortletURL;
37  import org.springframework.mock.web.portlet.MockRenderRequest;
38  import org.springframework.mock.web.portlet.MockRenderResponse;
39  
40  import com.opensymphony.xwork2.ActionContext;
41  import com.opensymphony.xwork2.mock.MockActionInvocation;
42  import com.opensymphony.xwork2.mock.MockActionProxy;
43  import com.opensymphony.xwork2.util.ValueStack;
44  
45  public class PortletUrlRendererTest extends StrutsTestCase {
46  
47  	PortletUrlRenderer renderer;
48  
49  	public void setUp() throws Exception {
50  		super.setUp();
51  		renderer = new PortletUrlRenderer();
52  	}
53  
54  	/***
55  	 * Ensure that the namespace of the current executing action is used when no
56  	 * namespace is specified. (WW-1875)
57  	 */
58  	public void testShouldIncludeCurrentNamespaceIfNoNamespaceSpecifiedForRenderUrl()
59  			throws Exception {
60  		final MockPortletURL portletUrl = new MockPortletURL(
61  				new MockPortalContext(), "render");
62  		MockRenderRequest request = new MockRenderRequest();
63  		MockRenderResponse response = new MockRenderResponse() {
64  			@Override
65  			public PortletURL createRenderURL() {
66  				return portletUrl;
67  			}
68  		};
69  
70  		ActionContext ctx = ActionContext.getContext();
71  		ctx.put(PortletActionConstants.PHASE,
72  				PortletActionConstants.RENDER_PHASE);
73  		ctx.put(PortletActionConstants.REQUEST, request);
74  		ctx.put(PortletActionConstants.RESPONSE, response);
75  
76  		Map<PortletMode, String> modeMap = new HashMap<PortletMode, String>();
77  		modeMap.put(PortletMode.VIEW, "/view");
78  		ctx.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
79  		ValueStack stack = ctx.getValueStack();
80  		URL url = new URL(stack, new PortletServletRequest(request, null),
81  				new PortletServletResponse(response));
82  
83  		MockActionInvocation ai = new MockActionInvocation();
84  		MockActionProxy ap = new MockActionProxy();
85  		ap.setActionName("testAction");
86  		ap.setNamespace("/current_namespace");
87  		ai.setProxy(ap);
88  		ai.setStack(stack);
89  		ai.setAction(new Object());
90  		ctx.setActionInvocation(ai);
91  
92  		StringWriter renderOutput = new StringWriter();
93  		renderer.renderUrl(renderOutput, url);
94  
95  		String action = portletUrl
96  				.getParameter(PortletActionConstants.ACTION_PARAM);
97  		assertEquals("/view/current_namespace/testAction", action);
98  	}
99  
100 	/***
101 	 * Ensure that the namespace of the current executing action is used when no
102 	 * namespace is specified. (WW-1875)
103 	 */
104 	public void testShouldIncludeCurrentNamespaceIfNoNamespaceSpecifiedForRenderFormUrl()
105 			throws Exception {
106 		final MockPortletURL portletUrl = new MockPortletURL(
107 				new MockPortalContext(), "render");
108 		MockRenderRequest request = new MockRenderRequest();
109 		MockRenderResponse response = new MockRenderResponse() {
110 			@Override
111 			public PortletURL createActionURL() {
112 				return portletUrl;
113 			}
114 		};
115 
116 		ActionContext ctx = ActionContext.getContext();
117 		ctx.put(PortletActionConstants.PHASE,
118 				PortletActionConstants.RENDER_PHASE);
119 		ctx.put(PortletActionConstants.REQUEST, request);
120 		ctx.put(PortletActionConstants.RESPONSE, response);
121 
122 		Map<PortletMode, String> modeMap = new HashMap<PortletMode, String>();
123 		modeMap.put(PortletMode.VIEW, "/view");
124 		ctx.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
125 		ValueStack stack = ctx.getValueStack();
126 		Form form = new Form(stack, new PortletServletRequest(request, null),
127 				new PortletServletResponse(response));
128 
129 		MockActionInvocation ai = new MockActionInvocation();
130 		MockActionProxy ap = new MockActionProxy();
131 		ap.setActionName("testAction");
132 		ap.setNamespace("/current_namespace");
133 		ai.setProxy(ap);
134 		ai.setStack(stack);
135 		ai.setAction(new Object());
136 		ctx.setActionInvocation(ai);
137 
138 		StringWriter renderOutput = new StringWriter();
139 		renderer.renderFormUrl(form);
140 
141 		String action = portletUrl
142 				.getParameter(PortletActionConstants.ACTION_PARAM);
143 		assertEquals("/view/current_namespace/testAction", action);
144 	}
145 }