    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.wrapper;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.MockFilterConfig;
    21
    22  import org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter;
    23  import org.acegisecurity.wrapper.SecurityContextHolderAwareRequestWrapper;
    24
    25  import java.io.IOException;
    26
    27  import javax.servlet.FilterChain;
    28  import javax.servlet.ServletException;
    29  import javax.servlet.ServletRequest;
    30  import javax.servlet.ServletResponse;
    31
    32  import org.springframework.mock.web.MockHttpServletRequest;
    33
    34
    35  /***
    36   * Tests {@link SecurityContextHolderAwareRequestFilter}.
    37   *
    38   * @author Ben Alex
    39   * @version $Id: SecurityContextHolderAwareRequestFilterTests.java,v 1.2 2005/11/17 00:55:51 benalex Exp $
    40   */
    41  public class SecurityContextHolderAwareRequestFilterTests extends TestCase {
    42      //~ Constructors ===========================================================
    43
    44      public SecurityContextHolderAwareRequestFilterTests() {
    45          super();
    46      }
    47
    48      public SecurityContextHolderAwareRequestFilterTests(String arg0) {
    49          super(arg0);
    50      }
    51
    52      //~ Methods ================================================================
    53
    54      public final void setUp() throws Exception {
    55          super.setUp();
    56      }
    57
    58      public static void main(String[] args) {
    59          junit.textui.TestRunner.run(SecurityContextHolderAwareRequestFilterTests.class);
    60      }
    61
    62      public void testCorrectOperation() throws Exception {
    63          SecurityContextHolderAwareRequestFilter filter = new SecurityContextHolderAwareRequestFilter();
    64          filter.init(new MockFilterConfig());
    65          filter.doFilter(new MockHttpServletRequest(null, null), null,
    66              new MockFilterChain(SecurityContextHolderAwareRequestWrapper.class));
    67
    68          // Now re-execute the filter, ensuring our replacement wrapper is still used
    69          filter.doFilter(new MockHttpServletRequest(null, null), null,
    70              new MockFilterChain(SecurityContextHolderAwareRequestWrapper.class));
    71
    72          filter.destroy();
    73      }
    74
    75      //~ Inner Classes ==========================================================
    76
    77      private class MockFilterChain implements FilterChain {
    78          private Class expectedServletRequest;
    79
    80          public MockFilterChain(Class expectedServletRequest) {
    81              this.expectedServletRequest = expectedServletRequest;
    82          }
    83
    84          private MockFilterChain() {
    85              super();
    86          }
    87
    88          public void doFilter(ServletRequest request, ServletResponse response)
    89              throws IOException, ServletException {
    90              if (request.getClass().isAssignableFrom(expectedServletRequest)) {
    91                  assertTrue(true);
    92              } else {
    93                  fail("Expected class to be of type " + expectedServletRequest
    94                      + " but was: " + request.getClass());
    95              }
    96          }
    97      }
    98  }