001: /*
002: * $Id: FilterDispatcherTest.java 484741 2006-12-08 20:22:37Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.io.IOException;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.servlet.FilterConfig;
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.struts2.StrutsConstants;
034: import org.apache.struts2.StrutsTestCase;
035: import org.apache.struts2.dispatcher.mapper.ActionMapper;
036: import org.apache.struts2.dispatcher.mapper.ActionMapping;
037: import org.apache.struts2.util.ObjectFactoryDestroyable;
038: import org.springframework.mock.web.MockFilterConfig;
039: import org.springframework.mock.web.MockHttpServletRequest;
040: import org.springframework.mock.web.MockHttpServletResponse;
041: import org.springframework.mock.web.MockServletContext;
042:
043: import com.mockobjects.servlet.MockFilterChain;
044: import com.opensymphony.xwork2.ObjectFactory;
045: import com.opensymphony.xwork2.config.ConfigurationManager;
046: import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
047: import com.opensymphony.xwork2.inject.Container;
048: import com.opensymphony.xwork2.inject.ContainerBuilder;
049: import com.opensymphony.xwork2.inject.Context;
050: import com.opensymphony.xwork2.inject.Factory;
051:
052: /**
053: * FilterDispatcher TestCase.
054: *
055: * @version $Date: 2006-12-08 15:22:37 -0500 (Fri, 08 Dec 2006) $ $Id: FilterDispatcherTest.java 484741 2006-12-08 20:22:37Z mrdon $
056: */
057: public class FilterDispatcherTest extends StrutsTestCase {
058:
059: public void testParsePackages() throws Exception {
060:
061: FilterDispatcher filterDispatcher = new FilterDispatcher();
062: String[] result1 = filterDispatcher
063: .parse("foo.bar.package1 foo.bar.package2 foo.bar.package3");
064: String[] result2 = filterDispatcher
065: .parse("foo.bar.package1\tfoo.bar.package2\tfoo.bar.package3");
066: String[] result3 = filterDispatcher
067: .parse("foo.bar.package1,foo.bar.package2,foo.bar.package3");
068: String[] result4 = filterDispatcher
069: .parse("foo.bar.package1 foo.bar.package2 \t foo.bar.package3 , foo.bar.package4");
070:
071: assertEquals(result1[0], "foo/bar/package1/");
072: assertEquals(result1[1], "foo/bar/package2/");
073: assertEquals(result1[2], "foo/bar/package3/");
074:
075: assertEquals(result2[0], "foo/bar/package1/");
076: assertEquals(result2[1], "foo/bar/package2/");
077: assertEquals(result2[2], "foo/bar/package3/");
078:
079: assertEquals(result3[0], "foo/bar/package1/");
080: assertEquals(result3[1], "foo/bar/package2/");
081: assertEquals(result3[2], "foo/bar/package3/");
082:
083: assertEquals(result4[0], "foo/bar/package1/");
084: assertEquals(result4[1], "foo/bar/package2/");
085: assertEquals(result4[2], "foo/bar/package3/");
086: assertEquals(result4[3], "foo/bar/package4/");
087: }
088:
089: public void testObjectFactoryDestroy() throws Exception {
090:
091: final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
092: FilterDispatcher filterDispatcher = new FilterDispatcher() {
093: @Override
094: protected Dispatcher createDispatcher(FilterConfig cfg) {
095: return new Dispatcher(cfg.getServletContext(),
096: new HashMap()) {
097: Container cont = new ContainerBuilder().factory(
098: ObjectFactory.class, new Factory() {
099: public Object create(Context context)
100: throws Exception {
101: return destroyedObjectFactory;
102: }
103: }).create(false);
104:
105: @Override
106: public Container getContainer() {
107: return cont;
108: }
109: };
110: }
111: };
112: filterDispatcher.init(new MockFilterConfig(
113: (ServletContext) null));
114:
115: assertFalse(destroyedObjectFactory.destroyed);
116: filterDispatcher.destroy();
117: assertTrue(destroyedObjectFactory.destroyed);
118: }
119:
120: public void testIfActionMapperIsNullDontServiceAction()
121: throws Exception {
122: MockServletContext servletContext = new MockServletContext();
123: MockFilterConfig filterConfig = new MockFilterConfig(
124: servletContext);
125: MockHttpServletRequest req = new MockHttpServletRequest(
126: servletContext);
127: MockHttpServletResponse res = new MockHttpServletResponse();
128: MockFilterChain chain = new MockFilterChain();
129: final NoOpDispatcher _dispatcher = new NoOpDispatcher(
130: servletContext);
131: ConfigurationManager confManager = new ConfigurationManager();
132: confManager.setConfiguration(new DefaultConfiguration());
133: _dispatcher.setConfigurationManager(confManager);
134: Dispatcher.setInstance(_dispatcher);
135:
136: ObjectFactory.setObjectFactory(new InnerObjectFactory());
137:
138: FilterDispatcher filter = new FilterDispatcher() {
139: protected Dispatcher createDispatcher() {
140: return _dispatcher;
141: }
142: };
143: filter.setActionMapper(null);
144: filter.init(filterConfig);
145: filter.doFilter(req, res, chain);
146:
147: assertFalse(_dispatcher.serviceRequest);
148: }
149:
150: public void testCharacterEncodingSetBeforeRequestWrappingAndActionService()
151: throws Exception {
152: MockServletContext servletContext = new MockServletContext();
153: MockFilterConfig filterConfig = new MockFilterConfig(
154: servletContext);
155: MockHttpServletRequest req = new MockHttpServletRequest(
156: servletContext);
157: MockHttpServletResponse res = new MockHttpServletResponse();
158: MockFilterChain chain = new MockFilterChain();
159: final InnerDispatcher _dispatcher = new InnerDispatcher(
160: servletContext);
161: Dispatcher.setInstance(null);
162:
163: DefaultConfiguration conf = new DefaultConfiguration() {
164: @Override
165: public Container getContainer() {
166: return new ContainerBuilder().create(false);
167: }
168: };
169:
170: ConfigurationManager confManager = new ConfigurationManager();
171: confManager.setConfiguration(conf);
172: _dispatcher.setConfigurationManager(confManager);
173:
174: ObjectFactory.setObjectFactory(new InnerObjectFactory());
175:
176: _dispatcher.setDefaultEncoding("UTF-16_DUMMY");
177:
178: FilterDispatcher filter = new FilterDispatcher() {
179: protected Dispatcher createDispatcher(
180: FilterConfig filterConfig) {
181: return _dispatcher;
182: }
183: };
184: filter.setActionMapper(new InnerActionMapper());
185: filter.init(filterConfig);
186: filter.doFilter(req, res, chain);
187:
188: assertTrue(_dispatcher.wrappedRequest);
189: assertTrue(_dispatcher.serviceRequest);
190: }
191:
192: // === inner class ========
193: public static class InnerObjectFactory extends ObjectFactory {
194:
195: }
196:
197: public static class NoOpDispatcher extends Dispatcher {
198: protected boolean wrappedRequest = false;
199: protected boolean serviceRequest = false;
200:
201: public NoOpDispatcher(ServletContext servletContext) {
202: super (servletContext, new HashMap());
203: }
204:
205: @Override
206: public HttpServletRequest wrapRequest(
207: HttpServletRequest request,
208: ServletContext servletContext) throws IOException {
209: wrappedRequest = true;
210: return request;
211: }
212:
213: public void serviceAction(HttpServletRequest request,
214: HttpServletResponse response, ServletContext context,
215: ActionMapping mapping) throws ServletException {
216: serviceRequest = true;
217: }
218: }
219:
220: public static class InnerDispatcher extends Dispatcher {
221:
222: protected boolean wrappedRequest = false;
223: protected boolean serviceRequest = false;
224:
225: public InnerDispatcher(ServletContext servletContext) {
226: super (servletContext, new HashMap());
227: }
228:
229: @Override
230: public HttpServletRequest wrapRequest(
231: HttpServletRequest request,
232: ServletContext servletContext) throws IOException {
233: wrappedRequest = true;
234: // if we set the chracter encoding AFTER we do wrap request, we will get
235: // a failing test
236: assertNotNull(request.getCharacterEncoding());
237: assertEquals(request.getCharacterEncoding(), "UTF-16_DUMMY");
238:
239: return request;
240: }
241:
242: public void serviceAction(HttpServletRequest request,
243: HttpServletResponse response, ServletContext context,
244: ActionMapping mapping) throws ServletException {
245: serviceRequest = true;
246: // if we set the chracter encoding AFTER we do wrap request, we will get
247: // a failing test
248: assertNotNull(request.getCharacterEncoding());
249: assertEquals(request.getCharacterEncoding(), "UTF-16_DUMMY");
250: }
251: }
252:
253: public static class InnerActionMapper implements ActionMapper {
254:
255: public ActionMapping getMapping(HttpServletRequest request,
256: ConfigurationManager config) {
257: return new ActionMapping();
258: }
259:
260: public String getUriFromActionMapping(ActionMapping mapping) {
261: return null;
262: }
263: }
264:
265: public static class NullActionMapper implements ActionMapper {
266: public ActionMapping getMapping(HttpServletRequest request,
267: ConfigurationManager config) {
268: return null;
269: }
270:
271: public String getUriFromActionMapping(ActionMapping mapping) {
272: return null;
273: }
274: }
275:
276: public static class InnerDestroyableObjectFactory extends
277: ObjectFactory implements ObjectFactoryDestroyable {
278: public boolean destroyed = false;
279:
280: public void destroy() {
281: destroyed = true;
282: }
283: }
284:
285: }
|