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