    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.event.authentication;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.Authentication;
    21  import org.acegisecurity.AuthenticationException;
    22  import org.acegisecurity.DisabledException;
    23  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
    24
    25
    26  /***
    27   * Tests {@link AbstractAuthenticationEvent} and its subclasses.
    28   *
    29   * @author Ben Alex
    30   * @version $Id: AuthenticationEventTests.java,v 1.2 2005/11/17 00:56:47 benalex Exp $
    31   */
    32  public class AuthenticationEventTests extends TestCase {
    33      //~ Methods ================================================================
    34
    35      public final void setUp() throws Exception {
    36          super.setUp();
    37      }
    38
    39      public static void main(String[] args) {
    40          junit.textui.TestRunner.run(AuthenticationEventTests.class);
    41      }
    42
    43      public void testAbstractAuthenticationEvent() {
    44          Authentication auth = getAuthentication();
    45          AbstractAuthenticationEvent event = new AuthenticationSuccessEvent(auth);
    46          assertEquals(auth, event.getAuthentication());
    47      }
    48
    49      public void testAbstractAuthenticationFailureEvent() {
    50          Authentication auth = getAuthentication();
    51          AuthenticationException exception = new DisabledException("TEST");
    52          AbstractAuthenticationFailureEvent event = new AuthenticationFailureDisabledEvent(auth,
    53                  exception);
    54          assertEquals(auth, event.getAuthentication());
    55          assertEquals(exception, event.getException());
    56      }
    57
    58      public void testRejectsNullAuthentication() {
    59          AuthenticationException exception = new DisabledException("TEST");
    60
    61          try {
    62              AuthenticationFailureDisabledEvent event = new AuthenticationFailureDisabledEvent(null,
    63                      exception);
    64              fail("Should have thrown IllegalArgumentException");
    65          } catch (IllegalArgumentException expected) {
    66              assertTrue(true);
    67          }
    68      }
    69
    70      public void testRejectsNullAuthenticationException() {
    71          try {
    72              new AuthenticationFailureDisabledEvent(getAuthentication(), null);
    73              fail("Should have thrown IllegalArgumentException");
    74          } catch (IllegalArgumentException expected) {
    75              assertTrue(true);
    76          }
    77      }
    78
    79      private Authentication getAuthentication() {
    80          UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("Principal",
    81                  "Credentials");
    82          authentication.setDetails("127.0.0.1");
    83
    84          return authentication;
    85      }
    86  }