KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > struts > StrutsSupportTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.struts;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25
26 import junit.framework.TestCase;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionServlet;
30 import org.apache.struts.config.ModuleConfig;
31 import org.easymock.MockControl;
32
33 import org.springframework.mock.web.MockHttpServletRequest;
34 import org.springframework.mock.web.MockHttpServletResponse;
35 import org.springframework.mock.web.MockServletContext;
36 import org.springframework.web.context.WebApplicationContext;
37 import org.springframework.web.context.support.StaticWebApplicationContext;
38
39 /**
40  * @author Juergen Hoeller
41  * @since 09.04.2004
42  */

43 public class StrutsSupportTests extends TestCase {
44
45     public void testActionSupportWithContextLoaderPlugIn() throws ServletException JavaDoc {
46         StaticWebApplicationContext wac = new StaticWebApplicationContext();
47         wac.addMessage("test", Locale.getDefault(), "testmessage");
48         final ServletContext JavaDoc servletContext = new MockServletContext();
49         wac.setServletContext(servletContext);
50         wac.refresh();
51         servletContext.setAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, wac);
52
53         ActionServlet actionServlet = new ActionServlet() {
54             public ServletContext JavaDoc getServletContext() {
55                 return servletContext;
56             }
57         };
58         ActionSupport action = new ActionSupport() {
59         };
60         action.setServlet(actionServlet);
61
62         assertEquals(wac, action.getWebApplicationContext());
63         assertEquals(servletContext, action.getServletContext());
64         assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));
65
66         action.setServlet(null);
67     }
68
69     public void testActionSupportWithRootContext() throws ServletException JavaDoc {
70         StaticWebApplicationContext wac = new StaticWebApplicationContext();
71         wac.addMessage("test", Locale.getDefault(), "testmessage");
72         final ServletContext JavaDoc servletContext = new MockServletContext();
73         wac.setServletContext(servletContext);
74         wac.refresh();
75         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
76
77         ActionServlet actionServlet = new ActionServlet() {
78             public ServletContext JavaDoc getServletContext() {
79                 return servletContext;
80             }
81         };
82         ActionSupport action = new ActionSupport() {
83         };
84         action.setServlet(actionServlet);
85
86         assertEquals(wac, action.getWebApplicationContext());
87         assertEquals(servletContext, action.getServletContext());
88         assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));
89
90         action.setServlet(null);
91     }
92
93     public void testDispatchActionSupportWithContextLoaderPlugIn() throws ServletException JavaDoc {
94         StaticWebApplicationContext wac = new StaticWebApplicationContext();
95         wac.addMessage("test", Locale.getDefault(), "testmessage");
96         final ServletContext JavaDoc servletContext = new MockServletContext();
97         wac.setServletContext(servletContext);
98         wac.refresh();
99         servletContext.setAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, wac);
100
101         ActionServlet actionServlet = new ActionServlet() {
102             public ServletContext JavaDoc getServletContext() {
103                 return servletContext;
104             }
105         };
106         DispatchActionSupport action = new DispatchActionSupport() {
107         };
108         action.setServlet(actionServlet);
109
110         assertEquals(wac, action.getWebApplicationContext());
111         assertEquals(servletContext, action.getServletContext());
112         assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));
113
114         action.setServlet(null);
115     }
116
117     public void testDispatchActionSupportWithRootContext() throws ServletException JavaDoc {
118         StaticWebApplicationContext wac = new StaticWebApplicationContext();
119         wac.addMessage("test", Locale.getDefault(), "testmessage");
120         final ServletContext JavaDoc servletContext = new MockServletContext();
121         wac.setServletContext(servletContext);
122         wac.refresh();
123         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
124
125         ActionServlet actionServlet = new ActionServlet() {
126             public ServletContext JavaDoc getServletContext() {
127                 return servletContext;
128             }
129         };
130         DispatchActionSupport action = new DispatchActionSupport() {
131         };
132         action.setServlet(actionServlet);
133
134         assertEquals(wac, action.getWebApplicationContext());
135         assertEquals(servletContext, action.getServletContext());
136         assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));
137
138         action.setServlet(null);
139     }
140
141     public void testLookupDispatchActionSupportWithContextLoaderPlugIn() throws ServletException JavaDoc {
142         StaticWebApplicationContext wac = new StaticWebApplicationContext();
143         wac.addMessage("test", Locale.getDefault(), "testmessage");
144         final ServletContext JavaDoc servletContext = new MockServletContext();
145         wac.setServletContext(servletContext);
146         wac.refresh();
147         servletContext.setAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, wac);
148
149         ActionServlet actionServlet = new ActionServlet() {
150             public ServletContext JavaDoc getServletContext() {
151                 return servletContext;
152             }
153         };
154         LookupDispatchActionSupport action = new LookupDispatchActionSupport() {
155             protected Map JavaDoc getKeyMethodMap() {
156                 return new HashMap JavaDoc();
157             }
158         };
159         action.setServlet(actionServlet);
160
161         assertEquals(wac, action.getWebApplicationContext());
162         assertEquals(servletContext, action.getServletContext());
163         assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));
164
165         action.setServlet(null);
166     }
167
168     public void testLookupDispatchActionSupportWithRootContext() throws ServletException JavaDoc {
169         StaticWebApplicationContext wac = new StaticWebApplicationContext();
170         wac.addMessage("test", Locale.getDefault(), "testmessage");
171         final ServletContext JavaDoc servletContext = new MockServletContext();
172         wac.setServletContext(servletContext);
173         wac.refresh();
174         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
175
176         ActionServlet actionServlet = new ActionServlet() {
177             public ServletContext JavaDoc getServletContext() {
178                 return servletContext;
179             }
180         };
181         LookupDispatchActionSupport action = new LookupDispatchActionSupport() {
182             protected Map JavaDoc getKeyMethodMap() {
183                 return new HashMap JavaDoc();
184             }
185         };
186         action.setServlet(actionServlet);
187
188         assertEquals(wac, action.getWebApplicationContext());
189         assertEquals(servletContext, action.getServletContext());
190         assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));
191
192         action.setServlet(null);
193     }
194
195     public void testDelegatingActionProxy() throws Exception JavaDoc {
196         final MockServletContext servletContext = new MockServletContext("/org/springframework/web/struts/");
197         ContextLoaderPlugIn plugin = new ContextLoaderPlugIn();
198         ActionServlet actionServlet = new ActionServlet() {
199             public String JavaDoc getServletName() {
200                 return "action";
201             }
202             public ServletContext JavaDoc getServletContext() {
203                 return servletContext;
204             }
205         };
206
207         MockControl moduleConfigControl = MockControl.createControl(ModuleConfig.class);
208         ModuleConfig moduleConfig = (ModuleConfig) moduleConfigControl.getMock();
209         moduleConfig.getPrefix();
210         moduleConfigControl.setDefaultReturnValue("");
211         moduleConfigControl.replay();
212
213         plugin.init(actionServlet, moduleConfig);
214         assertTrue(servletContext.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX) != null);
215
216         DelegatingActionProxy proxy = new DelegatingActionProxy();
217         proxy.setServlet(actionServlet);
218         ActionMapping mapping = new ActionMapping();
219         mapping.setPath("/test");
220         mapping.setModuleConfig(moduleConfig);
221         ActionForward forward = proxy.execute(
222                 mapping, null, new MockHttpServletRequest(servletContext), new MockHttpServletResponse());
223         assertEquals("/test", forward.getPath());
224
225         TestAction testAction = (TestAction) plugin.getWebApplicationContext().getBean("/test");
226         assertTrue(testAction.getServlet() != null);
227         proxy.setServlet(null);
228         plugin.destroy();
229         assertTrue(testAction.getServlet() == null);
230
231         moduleConfigControl.verify();
232     }
233
234     public void testDelegatingActionProxyWithModule() throws Exception JavaDoc {
235         final MockServletContext servletContext = new MockServletContext("/org/springframework/web/struts/WEB-INF");
236         ContextLoaderPlugIn plugin = new ContextLoaderPlugIn();
237         plugin.setContextConfigLocation("action-servlet.xml");
238         ActionServlet actionServlet = new ActionServlet() {
239             public String JavaDoc getServletName() {
240                 return "action";
241             }
242             public ServletContext JavaDoc getServletContext() {
243                 return servletContext;
244             }
245         };
246
247         MockControl moduleConfigControl = MockControl.createControl(ModuleConfig.class);
248         ModuleConfig moduleConfig = (ModuleConfig) moduleConfigControl.getMock();
249         moduleConfig.getPrefix();
250         moduleConfigControl.setDefaultReturnValue("/module");
251         moduleConfigControl.replay();
252
253         plugin.init(actionServlet, moduleConfig);
254         assertTrue(servletContext.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX) == null);
255         assertTrue(servletContext.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + "/module") != null);
256
257         DelegatingActionProxy proxy = new DelegatingActionProxy();
258         proxy.setServlet(actionServlet);
259         ActionMapping mapping = new ActionMapping();
260         mapping.setPath("/test2");
261         mapping.setModuleConfig(moduleConfig);
262         ActionForward forward = proxy.execute(
263                 mapping, null, new MockHttpServletRequest(servletContext), new MockHttpServletResponse());
264         assertEquals("/module/test2", forward.getPath());
265
266         TestAction testAction = (TestAction) plugin.getWebApplicationContext().getBean("/module/test2");
267         assertTrue(testAction.getServlet() != null);
268         proxy.setServlet(null);
269         plugin.destroy();
270         assertTrue(testAction.getServlet() == null);
271
272         moduleConfigControl.verify();
273     }
274
275     public void testDelegatingActionProxyWithModuleAndDefaultContext() throws Exception JavaDoc {
276         final MockServletContext servletContext = new MockServletContext("/org/springframework/web/struts/WEB-INF");
277         ContextLoaderPlugIn plugin = new ContextLoaderPlugIn();
278         plugin.setContextConfigLocation("action-servlet.xml");
279         ActionServlet actionServlet = new ActionServlet() {
280             public String JavaDoc getServletName() {
281                 return "action";
282             }
283             public ServletContext JavaDoc getServletContext() {
284                 return servletContext;
285             }
286         };
287
288         MockControl defaultModuleConfigControl = MockControl.createControl(ModuleConfig.class);
289         ModuleConfig defaultModuleConfig = (ModuleConfig) defaultModuleConfigControl.getMock();
290         defaultModuleConfig.getPrefix();
291         defaultModuleConfigControl.setDefaultReturnValue("");
292         defaultModuleConfigControl.replay();
293
294         MockControl moduleConfigControl = MockControl.createControl(ModuleConfig.class);
295         ModuleConfig moduleConfig = (ModuleConfig) moduleConfigControl.getMock();
296         moduleConfig.getPrefix();
297         moduleConfigControl.setDefaultReturnValue("/module");
298         moduleConfigControl.replay();
299
300         plugin.init(actionServlet, defaultModuleConfig);
301         assertTrue(servletContext.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX) != null);
302         assertTrue(servletContext.getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + "/module") == null);
303
304         DelegatingActionProxy proxy = new DelegatingActionProxy();
305         proxy.setServlet(actionServlet);
306         ActionMapping mapping = new ActionMapping();
307         mapping.setPath("/test2");
308         mapping.setModuleConfig(moduleConfig);
309         ActionForward forward = proxy.execute(
310                 mapping, null, new MockHttpServletRequest(servletContext), new MockHttpServletResponse());
311         assertEquals("/module/test2", forward.getPath());
312
313         TestAction testAction = (TestAction) plugin.getWebApplicationContext().getBean("/module/test2");
314         assertTrue(testAction.getServlet() != null);
315         proxy.setServlet(null);
316         plugin.destroy();
317         assertTrue(testAction.getServlet() == null);
318
319         moduleConfigControl.verify();
320     }
321
322 }
323
Popular Tags