View Javadoc

1   /*
2    * $Id: FilterDispatcherTest.java 449367 2006-09-24 06:49:04Z 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.dispatcher;
19  
20  import java.io.IOException;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts2.StrutsConstants;
30  import org.apache.struts2.StrutsTestCase;
31  import org.apache.struts2.config.Settings;
32  import org.apache.struts2.dispatcher.mapper.ActionMapper;
33  import org.apache.struts2.dispatcher.mapper.ActionMapping;
34  import org.apache.struts2.util.ObjectFactoryDestroyable;
35  import org.apache.struts2.util.ObjectFactoryInitializable;
36  import org.apache.struts2.util.ObjectFactoryLifecycle;
37  import org.springframework.mock.web.MockFilterConfig;
38  import org.springframework.mock.web.MockHttpServletRequest;
39  import org.springframework.mock.web.MockHttpServletResponse;
40  import org.springframework.mock.web.MockServletContext;
41  
42  import com.mockobjects.servlet.MockFilterChain;
43  import com.opensymphony.xwork2.ObjectFactory;
44  import com.opensymphony.xwork2.config.ConfigurationManager;
45  import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
46  
47  /***
48   * FilterDispatcher TestCase.
49   *
50   */
51  public class FilterDispatcherTest extends StrutsTestCase {
52  
53  
54      public void testParsePackages() throws Exception {
55          FilterDispatcher filterDispatcher = new FilterDispatcher();
56          String[] result1 = filterDispatcher.parse("foo.bar.package1 foo.bar.package2 foo.bar.package3");
57          String[] result2 = filterDispatcher.parse("foo.bar.package1\tfoo.bar.package2\tfoo.bar.package3");
58          String[] result3 = filterDispatcher.parse("foo.bar.package1,foo.bar.package2,foo.bar.package3");
59          String[] result4 = filterDispatcher.parse("foo.bar.package1    foo.bar.package2  \t foo.bar.package3   , foo.bar.package4");
60  
61          assertEquals(result1[0], "foo/bar/package1/");
62          assertEquals(result1[1], "foo/bar/package2/");
63          assertEquals(result1[2], "foo/bar/package3/");
64  
65          assertEquals(result2[0], "foo/bar/package1/");
66          assertEquals(result2[1], "foo/bar/package2/");
67          assertEquals(result2[2], "foo/bar/package3/");
68  
69          assertEquals(result3[0], "foo/bar/package1/");
70          assertEquals(result3[1], "foo/bar/package2/");
71          assertEquals(result3[2], "foo/bar/package3/");
72  
73          assertEquals(result4[0], "foo/bar/package1/");
74          assertEquals(result4[1], "foo/bar/package2/");
75          assertEquals(result4[2], "foo/bar/package3/");
76          assertEquals(result4[3], "foo/bar/package4/");
77      }
78  
79      public void testObjectFactoryDestroy() throws Exception {
80  
81          FilterDispatcher filterDispatcher = new FilterDispatcher();
82          filterDispatcher.init(new MockFilterConfig((ServletContext) null));
83          InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
84          ObjectFactory.setObjectFactory(destroyedObjectFactory);
85  
86          assertFalse(destroyedObjectFactory.destroyed);
87          filterDispatcher.destroy();
88          assertTrue(destroyedObjectFactory.destroyed);
89      }
90  
91  
92      public void testObjectFactoryInitializable() throws Exception {
93  
94          Map configMap = new HashMap();
95          configMap.put(StrutsConstants.STRUTS_OBJECTFACTORY, "org.apache.struts2.dispatcher.FilterDispatcherTest$InnerInitializableObjectFactory");
96          configMap.put(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD, "false");
97          Settings.setInstance(new InnerConfiguration(configMap));
98  
99          MockServletContext servletContext = new MockServletContext();
100         MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
101 
102 
103         FilterDispatcher filterDispatcher = new FilterDispatcher();
104         filterDispatcher.init(filterConfig);
105 
106         assertTrue(ObjectFactory.getObjectFactory() instanceof InnerInitializableObjectFactory);
107         assertTrue(((InnerInitializableObjectFactory) ObjectFactory.getObjectFactory()).initializable);
108     }
109 
110     public void testObjectFactoryLifecycle() throws Exception {
111 
112         Map configMap = new HashMap();
113         configMap.put(StrutsConstants.STRUTS_OBJECTFACTORY, "org.apache.struts2.dispatcher.FilterDispatcherTest$InnerInitailizableDestroyableObjectFactory");
114         configMap.put(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD, "false");
115         Settings.setInstance(new InnerConfiguration(configMap));
116 
117         MockServletContext servletContext = new MockServletContext();
118         MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
119 
120 
121         FilterDispatcher filterDispatcher = new FilterDispatcher();
122         filterDispatcher.init(filterConfig);
123 
124         assertTrue(ObjectFactory.getObjectFactory() instanceof InnerInitailizableDestroyableObjectFactory);
125         assertTrue(((InnerInitailizableDestroyableObjectFactory) ObjectFactory.getObjectFactory()).initializable);
126 
127         assertFalse(((InnerInitailizableDestroyableObjectFactory) ObjectFactory.getObjectFactory()).destroyable);
128         filterDispatcher.destroy();
129         assertTrue(((InnerInitailizableDestroyableObjectFactory) ObjectFactory.getObjectFactory()).destroyable);
130     }
131     
132     public void testIfActionMapperIsNullDontServiceAction() throws Exception {
133     	try {
134     		MockServletContext servletContext = new MockServletContext();
135     		MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
136     		MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
137     		MockHttpServletResponse res = new MockHttpServletResponse();
138     		MockFilterChain chain = new MockFilterChain();
139     		final NoOpDispatcher dispatcher = new NoOpDispatcher(servletContext);
140     		Dispatcher.setInstance(null);
141 
142     		ConfigurationManager confManager = new ConfigurationManager();
143     		confManager.setConfiguration(new DefaultConfiguration());
144     		dispatcher.setConfigurationManager(confManager);
145     		
146     		
147     		ObjectFactory.setObjectFactory(new InnerObjectFactory());
148     		
149     		Map settings = new HashMap();
150     		settings.put(StrutsConstants.STRUTS_MAPPER_CLASS, "org.apache.struts2.dispatcher.FilterDispatcherTest$NullActionMapper");
151     		Settings.setInstance(new InnerConfiguration(settings));
152     	
153     		FilterDispatcher filter = new FilterDispatcher() {
154     			protected Dispatcher createDispatcher() {
155     				return dispatcher;
156     			}
157     		};
158     		filter.init(filterConfig);
159     		filter.doFilter(req, res, chain);
160     	
161     		assertFalse(dispatcher.serviceRequest);
162     	}
163     	finally {
164     		Settings.reset();
165     	}
166     }
167     
168     public void testCharacterEncodingSetBeforeRequestWrappingAndActionService() throws Exception { 
169     	try {
170     		MockServletContext servletContext = new MockServletContext();
171     		MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
172     		MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
173     		MockHttpServletResponse res = new MockHttpServletResponse();
174     		MockFilterChain chain = new MockFilterChain();
175     		final InnerDispatcher dispatcher = new InnerDispatcher(servletContext);
176     		Dispatcher.setInstance(null);
177 
178     		ConfigurationManager confManager = new ConfigurationManager();
179     		confManager.setConfiguration(new DefaultConfiguration());
180     		dispatcher.setConfigurationManager(confManager);
181     		
182     		
183     		ObjectFactory.setObjectFactory(new InnerObjectFactory());
184     		
185     		Map settings = new HashMap();
186     		settings.put(StrutsConstants.STRUTS_I18N_ENCODING, "UTF-16_DUMMY");
187     		settings.put(StrutsConstants.STRUTS_MAPPER_CLASS, "org.apache.struts2.dispatcher.FilterDispatcherTest$InnerActionMapper");
188     		Settings.setInstance(new InnerConfiguration(settings));
189     	
190     		FilterDispatcher filter = new FilterDispatcher() {
191     			protected Dispatcher createDispatcher() {
192     				return dispatcher;
193     			}
194     		};
195     		filter.init(filterConfig);
196     		filter.doFilter(req, res, chain);
197     	
198     		assertTrue(dispatcher.wrappedRequest);
199     		assertTrue(dispatcher.serviceRequest);
200     	}
201     	finally {
202     		Settings.reset();
203     	}
204     }
205 
206 
207     // === inner class ========
208     public static class InnerObjectFactory extends ObjectFactory {
209     	
210     }
211     
212     public static class NoOpDispatcher extends Dispatcher {
213     	protected boolean wrappedRequest = false;
214     	protected boolean serviceRequest = false;
215 
216 		public NoOpDispatcher(ServletContext servletContext) {
217 			super(servletContext);
218 		}
219 
220 		public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
221 			wrappedRequest = true;
222 			return request;
223 		}
224 		
225 		public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
226 			serviceRequest = true;
227 		}
228     }
229     
230     public static class InnerDispatcher extends Dispatcher {
231     	
232     	protected boolean wrappedRequest = false;
233     	protected boolean serviceRequest = false;
234 
235 		public InnerDispatcher(ServletContext servletContext) {
236 			super(servletContext);
237 		}
238 
239 		public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
240 			wrappedRequest = true;
241 			// if we set the chracter encoding AFTER we do wrap request, we will get
242 			// a failing test
243 			assertNotNull(request.getCharacterEncoding());
244 			assertEquals(request.getCharacterEncoding(), "UTF-16_DUMMY");
245 			
246 			return request;
247 		}
248 		
249 		public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
250 			serviceRequest = true;
251 			// if we set the chracter encoding AFTER we do wrap request, we will get
252 			// a failing test
253 			assertNotNull(request.getCharacterEncoding());
254 			assertEquals(request.getCharacterEncoding(), "UTF-16_DUMMY");
255 		}
256     }
257     
258     public static class InnerActionMapper implements ActionMapper {
259 
260 		public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager config) {
261 			return new ActionMapping();
262 		}
263 
264 		public String getUriFromActionMapping(ActionMapping mapping) {
265 			return null;
266 		}
267     }
268     
269     public static class NullActionMapper implements ActionMapper {
270     	public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager config) {
271 			return null;
272 		}
273 
274 		public String getUriFromActionMapping(ActionMapping mapping) {
275 			return null;
276 		}
277     }
278     
279     
280     public static class InnerConfiguration extends Settings {
281         Map<String,String> m;
282 
283         public InnerConfiguration(Map configMap) {
284             m = configMap;
285         }
286 
287         public boolean isSetImpl(String name) {
288             if (!m.containsKey(name))
289                 return super.isSetImpl(name);
290             else
291                 return true;
292         }
293 
294         public String getImpl(String aName) throws IllegalArgumentException {
295             if (!m.containsKey(aName))
296                 return super.getImpl(aName);
297             else
298                 return m.get(aName);
299         }
300     }
301 
302     public static class InnerDestroyableObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
303         public boolean destroyed = false;
304 
305         public void destroy() {
306             destroyed = true;
307         }
308     }
309 
310     public static class InnerInitializableObjectFactory extends ObjectFactory implements ObjectFactoryInitializable {
311         public boolean initializable = false;
312 
313         public void init(ServletContext servletContext) {
314             initializable = true;
315         }
316     }
317 
318     public static class InnerInitailizableDestroyableObjectFactory extends ObjectFactory implements ObjectFactoryLifecycle {
319         public boolean initializable = false;
320         public boolean destroyable = false;
321 
322         public void init(ServletContext servletContext) {
323             initializable = true;
324         }
325 
326         public void destroy() {
327             destroyable = true;
328         }
329     }
330 
331 
332 }