    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.adapters.cas;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.springframework.context.ApplicationContext;
    21  import org.springframework.context.support.ClassPathXmlApplicationContext;
    22
    23  import org.springframework.mock.web.MockHttpServletRequest;
    24
    25  import javax.servlet.http.HttpServletRequest;
    26
    27
    28  /***
    29   * Tests {@link CasPasswordHandlerProxy}.
    30   *
    31   * @author Ben Alex
    32   * @version $Id: CasPasswordHandlerProxyTests.java,v 1.3 2005/11/17 00:56:09 benalex Exp $
    33   */
    34  public class CasPasswordHandlerProxyTests extends TestCase {
    35      //~ Constructors ===========================================================
    36
    37      public CasPasswordHandlerProxyTests() {
    38          super();
    39      }
    40
    41      public CasPasswordHandlerProxyTests(String arg0) {
    42          super(arg0);
    43      }
    44
    45      //~ Methods ================================================================
    46
    47      public final void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      public static void main(String[] args) {
    52          junit.textui.TestRunner.run(CasPasswordHandlerProxyTests.class);
    53      }
    54
    55      public void testDetectsIfHttpServletRequestNotPassed() {
    56          CasPasswordHandlerProxy proxy = new MockCasPasswordHandlerProxy(
    57                  "org/acegisecurity/adapters/cas/applicationContext-valid.xml");
    58
    59          try {
    60              proxy.authenticate(null, "x", "y");
    61              fail("Should have thrown IllegalArgumentException");
    62          } catch (IllegalArgumentException expected) {
    63              assertEquals("Can only process HttpServletRequest",
    64                  expected.getMessage());
    65          }
    66      }
    67
    68      public void testDetectsMissingDelegate() {
    69          CasPasswordHandlerProxy proxy = new MockCasPasswordHandlerProxy(
    70                  "org/acegisecurity/adapters/cas/applicationContext-invalid.xml");
    71
    72          try {
    73              proxy.authenticate(new MockHttpServletRequest(), "x", "y");
    74              fail("Should have thrown IllegalArgumentException");
    75          } catch (IllegalArgumentException expected) {
    76              assertEquals("Bean context must contain at least one bean of type CasPasswordHandler",
    77                  expected.getMessage());
    78          }
    79      }
    80
    81      public void testNormalOperation() {
    82          CasPasswordHandlerProxy proxy = new MockCasPasswordHandlerProxy(
    83                  "org/acegisecurity/adapters/cas/applicationContext-valid.xml");
    84          assertTrue(proxy.authenticate(new MockHttpServletRequest(), "marissa",
    85                  "koala"));
    86          assertFalse(proxy.authenticate(new MockHttpServletRequest(), "marissa",
    87                  "WRONG_PASSWORD"));
    88          assertFalse(proxy.authenticate(new MockHttpServletRequest(),
    89                  "INVALID_USER_NAME", "WRONG_PASSWORD"));
    90      }
    91
    92      //~ Inner Classes ==========================================================
    93
    94      /***
    95       * Mock object so that application context source can be specified.
    96       */
    97      private class MockCasPasswordHandlerProxy extends CasPasswordHandlerProxy {
    98          private ApplicationContext ctx;
    99
    100         public MockCasPasswordHandlerProxy(String appContextLocation) {
    101             ctx = new ClassPathXmlApplicationContext(appContextLocation);
    102         }
    103
    104         private MockCasPasswordHandlerProxy() {
    105             super();
    106         }
    107
    108         protected ApplicationContext getContext(HttpServletRequest httpRequest) {
    109             return ctx;
    110         }
    111     }
    112 }