    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.util;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.ConfigAttribute;
    21  import org.acegisecurity.ConfigAttributeDefinition;
    22  import org.acegisecurity.MockApplicationContext;
    23  import org.acegisecurity.MockFilterConfig;
    24  import org.acegisecurity.intercept.web.FilterInvocationDefinitionSource;
    25  import org.acegisecurity.intercept.web.MockFilterInvocationDefinitionSource;
    26  import org.acegisecurity.intercept.web.PathBasedFilterInvocationDefinitionMap;
    27
    28  import org.springframework.context.ApplicationContext;
    29  import org.springframework.context.support.ClassPathXmlApplicationContext;
    30  import org.springframework.mock.web.MockHttpServletRequest;
    31  import org.springframework.mock.web.MockHttpServletResponse;
    32
    33
    34  /***
    35   * Tests {@link FilterChainProxy}.
    36   *
    37   * @author Carlos Sanchez
    38   * @author Ben Alex
    39   * @version $Id: FilterChainProxyTests.java,v 1.7 2005/11/17 00:56:08 benalex Exp $
    40   */
    41  public class FilterChainProxyTests extends TestCase {
    42      //~ Constructors ===========================================================
    43
    44      // ===========================================================
    45      public FilterChainProxyTests() {
    46          super();
    47      }
    48
    49      public FilterChainProxyTests(String arg0) {
    50          super(arg0);
    51      }
    52
    53      //~ Methods ================================================================
    54
    55      // ================================================================
    56      public static void main(String[] args) {
    57          junit.textui.TestRunner.run(FilterChainProxyTests.class);
    58      }
    59
    60      public void testDetectsFilterInvocationDefinitionSourceThatDoesNotReturnAllConfigAttributes()
    61          throws Exception {
    62          FilterChainProxy filterChainProxy = new FilterChainProxy();
    63          filterChainProxy.setApplicationContext(MockApplicationContext
    64              .getContext());
    65          filterChainProxy.setFilterInvocationDefinitionSource(new MockFilterInvocationDefinitionSource(
    66                  false, false));
    67
    68          try {
    69              filterChainProxy.afterPropertiesSet();
    70              fail("Should have thrown IllegalArgumentException");
    71          } catch (IllegalArgumentException expected) {
    72              assertEquals("FilterChainProxy requires the FilterInvocationDefinitionSource to return a non-null response to getConfigAttributeDefinitions()",
    73                  expected.getMessage());
    74          }
    75      }
    76
    77      public void testDetectsIfConfigAttributeDoesNotReturnValueForGetAttributeMethod()
    78          throws Exception {
    79          FilterChainProxy filterChainProxy = new FilterChainProxy();
    80          filterChainProxy.setApplicationContext(MockApplicationContext
    81              .getContext());
    82
    83          ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
    84          cad.addConfigAttribute(new MockConfigAttribute());
    85
    86          PathBasedFilterInvocationDefinitionMap fids = new PathBasedFilterInvocationDefinitionMap();
    87          fids.addSecureUrl("/**", cad);
    88
    89          filterChainProxy.setFilterInvocationDefinitionSource(fids);
    90          filterChainProxy.afterPropertiesSet();
    91
    92          try {
    93              filterChainProxy.init(new MockFilterConfig());
    94              fail("Should have thrown IllegalArgumentException");
    95          } catch (IllegalArgumentException expected) {
    96              assertTrue(expected.getMessage().endsWith("returned null to the getAttribute() method, which is invalid when used with FilterChainProxy"));
    97          }
    98      }
    99
    100     public void testDetectsMissingFilterInvocationDefinitionSource()
    101         throws Exception {
    102         FilterChainProxy filterChainProxy = new FilterChainProxy();
    103         filterChainProxy.setApplicationContext(MockApplicationContext
    104             .getContext());
    105
    106         try {
    107             filterChainProxy.afterPropertiesSet();
    108             fail("Should have thrown IllegalArgumentException");
    109         } catch (IllegalArgumentException expected) {
    110             assertEquals("filterInvocationDefinitionSource must be specified",
    111                 expected.getMessage());
    112         }
    113     }
    114
    115     public void testGettersSetters() {
    116         FilterChainProxy filterChainProxy = new FilterChainProxy();
    117         FilterInvocationDefinitionSource fids = new MockFilterInvocationDefinitionSource(false,
    118                 false);
    119         filterChainProxy.setFilterInvocationDefinitionSource(fids);
    120         assertEquals(fids,
    121             filterChainProxy.getFilterInvocationDefinitionSource());
    122     }
    123
    124     public void testNormalOperation() throws Exception {
    125         ApplicationContext appCtx = new ClassPathXmlApplicationContext(
    126                 "org/acegisecurity/util/filtertest-valid.xml");
    127         FilterChainProxy filterChainProxy = (FilterChainProxy) appCtx.getBean("filterChain",
    128                 FilterChainProxy.class);
    129         MockFilter filter = (MockFilter) appCtx.getBean("mockFilter",
    130                 MockFilter.class);
    131         assertFalse(filter.isWasInitialized());
    132         assertFalse(filter.isWasDoFiltered());
    133         assertFalse(filter.isWasDestroyed());
    134
    135         filterChainProxy.init(new MockFilterConfig());
    136         assertTrue(filter.isWasInitialized());
    137         assertFalse(filter.isWasDoFiltered());
    138         assertFalse(filter.isWasDestroyed());
    139
    140         MockHttpServletRequest request = new MockHttpServletRequest();
    141         request.setServletPath("/foo/secure/super/somefile.html");
    142
    143         MockHttpServletResponse response = new MockHttpServletResponse();
    144         MockFilterChain chain = new MockFilterChain(true);
    145
    146         filterChainProxy.doFilter(request, response, chain);
    147         assertTrue(filter.isWasInitialized());
    148         assertTrue(filter.isWasDoFiltered());
    149         assertFalse(filter.isWasDestroyed());
    150
    151         request.setServletPath("/a/path/which/doesnt/match/any/filter.html");
    152         filterChainProxy.doFilter(request, response, chain);
    153
    154         filterChainProxy.destroy();
    155         assertTrue(filter.isWasInitialized());
    156         assertTrue(filter.isWasDoFiltered());
    157         assertTrue(filter.isWasDestroyed());
    158     }
    159
    160     //~ Inner Classes ==========================================================
    161
    162     private class MockConfigAttribute implements ConfigAttribute {
    163         public String getAttribute() {
    164             return null;
    165         }
    166     }
    167 }