View Javadoc

    1   /*
    2    * $Id: FilterTest.java 484717 2006-12-08 19:57:59Z 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
    26  import javax.servlet.FilterConfig;
    27  import javax.servlet.ServletContext;
    28  import javax.servlet.ServletException;
    29  import javax.servlet.ServletRequest;
    30  import javax.servlet.ServletResponse;
    31  import javax.servlet.http.HttpServletRequest;
    32  import javax.servlet.http.HttpServletResponse;
    33
    34  import junit.framework.TestCase;
    35
    36  import org.apache.struts2.dispatcher.mapper.ActionMapper;
    37  import org.apache.struts2.dispatcher.mapper.ActionMapping;
    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.inject.Container;
    47
    48
    49  /***
    50   *
    51   * @version $Date: 2006-12-08 14:57:59 -0500 (Fri, 08 Dec 2006) $ $Id: FilterTest.java 484717 2006-12-08 19:57:59Z mrdon $
    52   */
    53  public class FilterTest extends TestCase {
    54
    55      protected MockFilterConfig filterConfig;
    56      protected MockHttpServletRequest request;
    57      protected MockHttpServletResponse response;
    58      protected MockFilterChain filterChain;
    59      protected MockFilterChain filterChain2;
    60      protected MockServletContext servletContext;
    61
    62      protected InnerDispatcher _dispatcher1;
    63      protected InnerDispatcher _dispatcher2;
    64      protected ActionContextCleanUp cleanUp;
    65      protected FilterDispatcher filterDispatcher;
    66
    67      protected int cleanUpFilterCreateDispatcherCount = 0; // number of times clean up filter create a dispatcher
    68      protected int filterDispatcherCreateDispatcherCount = 0; // number of times FilterDispatcher create a dispatcher
    69
    70
    71      @Override
    72      protected void tearDown() throws Exception {
    73          filterConfig = null;
    74          request = null;
    75          response = null;
    76          filterChain = null;
    77          filterChain2 = null;
    78          servletContext = null;
    79          _dispatcher1 = null;
    80          _dispatcher2 = null;
    81          cleanUp = null;
    82          filterDispatcher = null;
    83      }
    84
    85      @Override
    86      protected void setUp() throws Exception {
    87          Dispatcher.setInstance(null);
    88
    89          filterConfig = new MockFilterConfig();
    90          request = new MockHttpServletRequest();
    91          response = new MockHttpServletResponse();
    92          servletContext = new MockServletContext();
    93
    94          _dispatcher1 = new InnerDispatcher(servletContext){
    95              @Override
    96              public String toString() {
    97                  return "dispatcher1";
    98              }
    99          };
    100         _dispatcher2 = new InnerDispatcher(servletContext){
    101             @Override
    102             public String toString() {
    103                 return "dispatcher2";
    104             }
    105         };
    106         filterChain = new MockFilterChain() {
    107             @Override
    108             public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
    109                 filterDispatcher.doFilter(req, res, filterChain2);
    110             }
    111         };
    112         filterChain2 = new MockFilterChain() {
    113             @Override
    114             public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
    115             }
    116         };
    117
    118
    119         cleanUp = new ActionContextCleanUp();
    120
    121         filterDispatcher = new FilterDispatcher() {
    122             @Override
    123             protected Dispatcher createDispatcher(FilterConfig filterConfig) {
    124                 filterDispatcherCreateDispatcherCount++;
    125                 return _dispatcher2;
    126             }
    127
    128             @Override
    129             public String toString() {
    130                 return "filterDispatcher";
    131             }
    132         };
    133     }
    134
    135     public void testUsingFilterDispatcherOnly() throws Exception {
    136         assertEquals(cleanUpFilterCreateDispatcherCount, 0);
    137         assertEquals(filterDispatcherCreateDispatcherCount, 0);
    138         assertFalse(_dispatcher1.init);
    139         assertFalse(_dispatcher1.prepare);
    140         assertFalse(_dispatcher1.wrapRequest);
    141         assertFalse(_dispatcher1.service);
    142         assertFalse(_dispatcher2.init);
    143         assertFalse(_dispatcher2.prepare);
    144         assertFalse(_dispatcher2.wrapRequest);
    145         assertFalse(_dispatcher2.service);
    146
    147         filterDispatcher.init(filterConfig);
    148         FilterDispatcher.setActionMapper(new FilterTest.InnerMapper());
    149         filterDispatcher.doFilter(request, response, filterChain2);
    150         filterDispatcher.destroy();
    151
    152         // we are using FilterDispatcher only, so cleanUp filter's Dispatcher should not be created.
    153         assertEquals(cleanUpFilterCreateDispatcherCount, 0);
    154         assertEquals(filterDispatcherCreateDispatcherCount, 1);
    155         assertFalse(_dispatcher1.init);
    156         assertFalse(_dispatcher1.prepare);
    157         assertFalse(_dispatcher1.wrapRequest);
    158         assertFalse(_dispatcher1.service);
    159         assertTrue(_dispatcher2.init);
    160         assertTrue(_dispatcher2.prepare);
    161         assertTrue(_dispatcher2.wrapRequest);
    162         assertTrue(_dispatcher2.service);
    163         assertTrue(Dispatcher.getInstance() == null);
    164     }
    165
    166
    167     public void testUsingFilterDispatcherOnly_Multiple() throws Exception {
    168
    169         filterDispatcher.setActionMapper(new FilterTest.InnerMapper());
    170
    171         assertEquals(cleanUpFilterCreateDispatcherCount, 0);
    172         assertEquals(filterDispatcherCreateDispatcherCount, 0);
    173         assertFalse(_dispatcher1.prepare);
    174         assertFalse(_dispatcher1.wrapRequest);
    175         assertFalse(_dispatcher1.service);
    176         assertFalse(_dispatcher1.cleanUp);
    177         assertFalse(_dispatcher2.prepare);
    178         assertFalse(_dispatcher2.wrapRequest);
    179         assertFalse(_dispatcher2.service);
    180         assertFalse(_dispatcher2.cleanUp);
    181
    182         filterDispatcher.init(filterConfig);
    183         FilterDispatcher.setActionMapper(new FilterTest.InnerMapper());
    184         filterDispatcher.doFilter(request, response, filterChain2);
    185         filterDispatcher.doFilter(request, response, filterChain2);
    186         filterDispatcher.destroy();
    187
    188         assertEquals(cleanUpFilterCreateDispatcherCount, 0);
    189         // We should create dispatcher once, although filter.doFilter(...) is called  many times.
    190         assertEquals(filterDispatcherCreateDispatcherCount, 1);
    191         assertFalse(_dispatcher1.prepare);
    192         assertFalse(_dispatcher1.wrapRequest);
    193         assertFalse(_dispatcher1.service);
    194         assertFalse(_dispatcher1.cleanUp);
    195         assertTrue(_dispatcher2.prepare);
    196         assertTrue(_dispatcher2.wrapRequest);
    197         assertTrue(_dispatcher2.service);
    198         assertTrue(_dispatcher2.cleanUp);
    199         assertTrue(Dispatcher.getInstance() == null);
    200
    201     }
    202
    203
    204
    205     /*public void testUsingCleanUpAndFilterDispatcher() throws Exception {
    206         ObjectFactory oldObjecFactory = ObjectFactory.getObjectFactory();
    207         try {
    208             ObjectFactory.setObjectFactory(new InnerObjectFactory());
    209             filterDispatcher.setActionMapper(new FilterTest.InnerMapper());
    210
    211             assertEquals(cleanUpFilterCreateDispatcherCount, 0);
    212             assertEquals(filterDispatcherCreateDispatcherCount, 0);
    213             assertFalse(_dispatcher1.prepare);
    214             assertFalse(_dispatcher1.wrapRequest);
    215             assertFalse(_dispatcher1.service);
    216             assertFalse(_dispatcher2.prepare);
    217             assertFalse(_dispatcher2.wrapRequest);
    218             assertFalse(_dispatcher2.service);
    219
    220             cleanUp.init(filterConfig);
    221             filterDispatcher.init(filterConfig);
    222             cleanUp.doFilter(request, response, filterChain);
    223             filterDispatcher.destroy();
    224             cleanUp.destroy();
    225
    226             assertEquals(1, cleanUpFilterCreateDispatcherCount);
    227             assertEquals(1, filterDispatcherCreateDispatcherCount);
    228             assertTrue(_dispatcher1.prepare);
    229             assertTrue(_dispatcher1.wrapRequest);
    230             assertTrue(_dispatcher1.service);
    231             assertFalse(_dispatcher2.prepare);
    232             assertFalse(_dispatcher2.wrapRequest);
    233             assertFalse(_dispatcher2.service);
    234             assertTrue(Dispatcher.getInstance() == null);
    235         }
    236         finally {
    237             ObjectFactory.setObjectFactory(oldObjecFactory);
    238         }
    239     }
    240
    241
    242     public void testUsingCleanUpAndFilterDispatcher_Multiple() throws Exception {
    243         ObjectFactory oldObjecFactory = ObjectFactory.getObjectFactory();
    244         try {
    245             ObjectFactory.setObjectFactory(new InnerObjectFactory());
    246             filterDispatcher.setActionMapper(new FilterTest.InnerMapper());
    247
    248             assertEquals(cleanUpFilterCreateDispatcherCount, 0);
    249             assertEquals(filterDispatcherCreateDispatcherCount, 0);
    250             assertFalse(_dispatcher1.prepare);
    251             assertFalse(_dispatcher1.wrapRequest);
    252             assertFalse(_dispatcher1.service);
    253             assertFalse(_dispatcher2.prepare);
    254             assertFalse(_dispatcher2.wrapRequest);
    255             assertFalse(_dispatcher2.service);
    256
    257             cleanUp.init(filterConfig);
    258             filterDispatcher.init(filterConfig);
    259             cleanUp.doFilter(request, response, filterChain);
    260             cleanUp.doFilter(request, response, filterChain);
    261             filterDispatcher.destroy();
    262             cleanUp.destroy();
    263
    264             assertEquals(cleanUpFilterCreateDispatcherCount, 1);
    265             assertEquals(filterDispatcherCreateDispatcherCount, 1);
    266             assertTrue(_dispatcher1.prepare);
    267             assertTrue(_dispatcher1.wrapRequest);
    268             assertTrue(_dispatcher1.service);
    269             assertFalse(_dispatcher2.prepare);
    270             assertFalse(_dispatcher2.wrapRequest);
    271             assertFalse(_dispatcher2.service);
    272             assertTrue(Dispatcher.getInstance() == null);
    273         }
    274         finally {
    275             ObjectFactory.setObjectFactory(oldObjecFactory);
    276         }
    277     }
    278     */
    279
    280
    281     class InnerDispatcher extends Dispatcher {
    282     	public boolean init = false;
    283         public boolean prepare = false;
    284         public boolean wrapRequest = false;
    285         public boolean service = false;
    286         public boolean cleanUp = false;
    287
    288         public InnerDispatcher(ServletContext servletContext) {
    289             super(servletContext, new HashMap<String, String>());
    290         }
    291
    292         @Override
    293         public void init() {
    294         	init= true;
    295         }
    296
    297         @Override
    298         public Container getContainer() {
    299             return null;
    300         }
    301
    302         @Override
    303         public void prepare(HttpServletRequest request, HttpServletResponse response) {
    304             prepare = true;
    305         }
    306
    307         @Override
    308         public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
    309             wrapRequest = true;
    310             return request;
    311         }
    312
    313         @Override
    314         public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
    315             service = true;
    316         }
    317
    318         @Override
    319         public void cleanup() {
    320         	cleanUp = true;
    321         }
    322     }
    323
    324     class NullInnerMapper implements ActionMapper {
    325         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    326             return null;
    327         }
    328
    329         public String getUriFromActionMapping(ActionMapping mapping) {
    330             return null;
    331         }
    332     }
    333
    334     public static class InnerMapper implements ActionMapper {
    335
    336         public InnerMapper() {}
    337
    338         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    339             return new ActionMapping();
    340         }
    341
    342         public String getUriFromActionMapping(ActionMapping mapping) {
    343             return "";
    344         }
    345     }
    346
    347     class InnerObjectFactory extends ObjectFactory {
    348         public InnerObjectFactory() {
    349             super();
    350         }
    351     }
    352 }
    353