    1   /* Copyright 2004 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.intercept.web;
    17
    18  import junit.framework.TestCase;
    19
    20
    21
    22
    23  import java.io.IOException;
    24
    25  import javax.servlet.FilterChain;
    26  import javax.servlet.ServletException;
    27  import javax.servlet.ServletRequest;
    28  import javax.servlet.ServletResponse;
    29
    30  import org.springframework.mock.web.MockHttpServletRequest;
    31  import org.springframework.mock.web.MockHttpServletResponse;
    32
    33
    34
    35  /***
    36   * Tests {@link AbstractFilterInvocationDefinitionSource}.
    37   *
    38   * @author Ben Alex
    39   * @version $Id: AbstractFilterInvocationDefinitionSourceTests.java,v 1.3 2005/11/17 00:55:50 benalex Exp $
    40   */
    41  public class AbstractFilterInvocationDefinitionSourceTests extends TestCase {
    42      //~ Constructors ===========================================================
    43
    44      public AbstractFilterInvocationDefinitionSourceTests() {
    45          super();
    46      }
    47
    48      public AbstractFilterInvocationDefinitionSourceTests(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(AbstractFilterInvocationDefinitionSourceTests.class);
    60      }
    61
    62      public void testDoesNotSupportAnotherObject() {
    63          MockFilterInvocationDefinitionSource mfis = new MockFilterInvocationDefinitionSource(false,
    64                  true);
    65          assertFalse(mfis.supports(String.class));
    66      }
    67
    68      public void testGetAttributesForANonFilterInvocation() {
    69          MockFilterInvocationDefinitionSource mfis = new MockFilterInvocationDefinitionSource(false,
    70                  true);
    71
    72          try {
    73              mfis.getAttributes(new String());
    74              fail("Should have thrown IllegalArgumentException");
    75          } catch (IllegalArgumentException expected) {
    76              assertTrue(true);
    77          }
    78      }
    79
    80      public void testGetAttributesForANullObject() {
    81          MockFilterInvocationDefinitionSource mfis = new MockFilterInvocationDefinitionSource(false,
    82                  true);
    83
    84          try {
    85              mfis.getAttributes(null);
    86              fail("Should have thrown IllegalArgumentException");
    87          } catch (IllegalArgumentException expected) {
    88              assertTrue(true);
    89          }
    90      }
    91
    92      public void testGetAttributesForFilterInvocationSuccess() {
    93          MockFilterInvocationDefinitionSource mfis = new MockFilterInvocationDefinitionSource(false,
    94                  true);
    95
    96          try {
    97              mfis.getAttributes(new FilterInvocation(
    98                      new MockHttpServletRequest(null, null),
    99                      new MockHttpServletResponse(), new MockFilterChain()));
    100             fail("Should have thrown UnsupportedOperationException");
    101         } catch (UnsupportedOperationException expected) {
    102             assertTrue(true);
    103         }
    104     }
    105
    106     public void testSupportsFilterInvocation() {
    107         MockFilterInvocationDefinitionSource mfis = new MockFilterInvocationDefinitionSource(false,
    108                 true);
    109         assertTrue(mfis.supports(FilterInvocation.class));
    110     }
    111
    112     //~ Inner Classes ==========================================================
    113
    114     private class MockFilterChain implements FilterChain {
    115         public void doFilter(ServletRequest arg0, ServletResponse arg1)
    116             throws IOException, ServletException {
    117             throw new UnsupportedOperationException(
    118                 "mock method not implemented");
    119         }
    120     }
    121 }