View Javadoc

1   /*
2    * $Id: FilterDispatcherTest.java 653472 2008-05-05 13:10:05Z musachy $
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.dispatcher;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.FilterConfig;
29  import javax.servlet.ServletContext;
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.struts2.StrutsConstants;
35  import org.apache.struts2.StrutsTestCase;
36  import org.apache.struts2.dispatcher.mapper.ActionMapper;
37  import org.apache.struts2.dispatcher.mapper.ActionMapping;
38  import org.apache.struts2.util.ObjectFactoryDestroyable;
39  import org.springframework.mock.web.MockFilterConfig;
40  import org.springframework.mock.web.MockHttpServletRequest;
41  import org.springframework.mock.web.MockHttpServletResponse;
42  import org.springframework.mock.web.MockServletContext;
43  
44  import com.mockobjects.servlet.MockFilterChain;
45  import com.opensymphony.xwork2.ObjectFactory;
46  import com.opensymphony.xwork2.config.ConfigurationManager;
47  import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
48  import com.opensymphony.xwork2.inject.Container;
49  import com.opensymphony.xwork2.inject.ContainerBuilder;
50  import com.opensymphony.xwork2.inject.Context;
51  import com.opensymphony.xwork2.inject.Factory;
52  
53  /***
54   * FilterDispatcher TestCase.
55   *
56   * @version $Date: 2008-05-05 13:10:05 +0000 (Mon, 05 May 2008) $ $Id: FilterDispatcherTest.java 653472 2008-05-05 13:10:05Z musachy $
57   */
58  public class FilterDispatcherTest extends StrutsTestCase {
59  
60  
61      public void testParsePackages() throws Exception {
62  
63          DefaultStaticContentLoader filterDispatcher = new DefaultStaticContentLoader();
64          String[] result1 = filterDispatcher.parse("foo.bar.package1 foo.bar.package2 foo.bar.package3");
65          String[] result2 = filterDispatcher.parse("foo.bar.package1\tfoo.bar.package2\tfoo.bar.package3");
66          String[] result3 = filterDispatcher.parse("foo.bar.package1,foo.bar.package2,foo.bar.package3");
67          String[] result4 = filterDispatcher.parse("foo.bar.package1    foo.bar.package2  \t foo.bar.package3   , foo.bar.package4");
68  
69          assertEquals(result1[0], "foo/bar/package1/");
70          assertEquals(result1[1], "foo/bar/package2/");
71          assertEquals(result1[2], "foo/bar/package3/");
72  
73          assertEquals(result2[0], "foo/bar/package1/");
74          assertEquals(result2[1], "foo/bar/package2/");
75          assertEquals(result2[2], "foo/bar/package3/");
76  
77          assertEquals(result3[0], "foo/bar/package1/");
78          assertEquals(result3[1], "foo/bar/package2/");
79          assertEquals(result3[2], "foo/bar/package3/");
80  
81          assertEquals(result4[0], "foo/bar/package1/");
82          assertEquals(result4[1], "foo/bar/package2/");
83          assertEquals(result4[2], "foo/bar/package3/");
84          assertEquals(result4[3], "foo/bar/package4/");
85      }
86  
87      
88  
89      public void testIfActionMapperIsNullDontServiceAction() throws Exception {
90          MockServletContext servletContext = new MockServletContext();
91          MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
92          MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
93          MockHttpServletResponse res = new MockHttpServletResponse();
94          MockFilterChain chain = new MockFilterChain();
95          final NoOpDispatcher _dispatcher = new NoOpDispatcher(servletContext);
96          ConfigurationManager confManager = new ConfigurationManager();
97          confManager.setConfiguration(new DefaultConfiguration());
98          _dispatcher.setConfigurationManager(confManager);
99          Dispatcher.setInstance(_dispatcher);
100 
101         
102 
103 
104         FilterDispatcher filter = new FilterDispatcher() {
105             protected Dispatcher createDispatcher() {
106                 return _dispatcher;
107             }
108         };
109         filter.init(filterConfig);
110         filter.setActionMapper(null);
111         filter.doFilter(req, res, chain);
112 
113         assertFalse(_dispatcher.serviceRequest);
114     }
115     
116     public void testCharacterEncodingSetBeforeRequestWrappingAndActionService() throws Exception {
117         MockServletContext servletContext = new MockServletContext();
118         MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
119         MockHttpServletRequest req = new MockHttpServletRequest(servletContext);
120         MockHttpServletResponse res = new MockHttpServletResponse();
121         MockFilterChain chain = new MockFilterChain();
122         final InnerDispatcher _dispatcher = new InnerDispatcher(servletContext);
123         Dispatcher.setInstance(null);
124 
125         _dispatcher.setDefaultEncoding("UTF-16_DUMMY");
126 
127         FilterDispatcher filter = new FilterDispatcher() {
128             protected Dispatcher createDispatcher(FilterConfig filterConfig) {
129                 return _dispatcher;
130             }
131         };
132         filter.setActionMapper(new InnerActionMapper());
133         filter.init(filterConfig);
134         _dispatcher.setDefaultEncoding("UTF-16_DUMMY");
135         filter.doFilter(req, res, chain);
136 
137         assertTrue(_dispatcher.wrappedRequest);
138         assertTrue(_dispatcher.serviceRequest);
139     }
140 
141     // === inner class ========
142     public static class InnerObjectFactory extends ObjectFactory {
143 
144     }
145 
146     public static class NoOpDispatcher extends Dispatcher {
147         protected boolean wrappedRequest = false;
148         protected boolean serviceRequest = false;
149 
150         public NoOpDispatcher(ServletContext servletContext) {
151             super(servletContext, new HashMap());
152         }
153 
154         @Override
155         public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
156             wrappedRequest = true;
157             return request;
158         }
159 
160         public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
161             serviceRequest = true;
162         }
163     }
164 
165     public static class InnerDispatcher extends Dispatcher {
166 
167         protected boolean wrappedRequest = false;
168         protected boolean serviceRequest = false;
169 
170         public InnerDispatcher(ServletContext servletContext) {
171             super(servletContext, new HashMap());
172         }
173 
174         @Override
175         public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
176             wrappedRequest = true;
177             // if we set the chracter encoding AFTER we do wrap request, we will get
178             // a failing test
179             assertNotNull(request.getCharacterEncoding());
180             assertEquals("UTF-16_DUMMY", request.getCharacterEncoding());
181 
182             return request;
183         }
184 
185         public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
186             serviceRequest = true;
187             // if we set the chracter encoding AFTER we do wrap request, we will get
188             // a failing test
189             assertNotNull(request.getCharacterEncoding());
190             assertEquals("UTF-16_DUMMY", request.getCharacterEncoding());
191         }
192     }
193 
194     public static class InnerActionMapper implements ActionMapper {
195 
196         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager config) {
197             return new ActionMapping();
198         }
199 
200         public ActionMapping getMappingFromActionName(String actionName) {
201             return null;  //To change body of implemented methods use File | Settings | File Templates.
202         }
203 
204         public String getUriFromActionMapping(ActionMapping mapping) {
205             return null;
206         }
207     }
208 
209     public static class NullActionMapper implements ActionMapper {
210         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager config) {
211             return null;
212         }
213 
214         public ActionMapping getMappingFromActionName(String actionName) {
215             return null;  //To change body of implemented methods use File | Settings | File Templates.
216         }
217 
218         public String getUriFromActionMapping(ActionMapping mapping) {
219             return null;
220         }
221     }
222 
223 
224     public static class InnerDestroyableObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
225         public boolean destroyed = false;
226 
227         public void destroy() {
228             destroyed = true;
229         }
230     }
231 
232 }