View Javadoc

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