01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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: * Tests {@link AbstractAuthenticationEvent} and its subclasses.
28: *
29: * @author Ben Alex
30: * @version $Id: AuthenticationEventTests.java 1496 2006-05-23 13:38:33Z benalex $
31: */
32: public class AuthenticationEventTests extends TestCase {
33: //~ Methods ========================================================================================================
34:
35: private Authentication getAuthentication() {
36: UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
37: "Principal", "Credentials");
38: authentication.setDetails("127.0.0.1");
39:
40: return authentication;
41: }
42:
43: public static void main(String[] args) {
44: junit.textui.TestRunner.run(AuthenticationEventTests.class);
45: }
46:
47: public final void setUp() throws Exception {
48: super .setUp();
49: }
50:
51: public void testAbstractAuthenticationEvent() {
52: Authentication auth = getAuthentication();
53: AbstractAuthenticationEvent event = new AuthenticationSuccessEvent(
54: auth);
55: assertEquals(auth, event.getAuthentication());
56: }
57:
58: public void testAbstractAuthenticationFailureEvent() {
59: Authentication auth = getAuthentication();
60: AuthenticationException exception = new DisabledException(
61: "TEST");
62: AbstractAuthenticationFailureEvent event = new AuthenticationFailureDisabledEvent(
63: auth, exception);
64: assertEquals(auth, event.getAuthentication());
65: assertEquals(exception, event.getException());
66: }
67:
68: public void testRejectsNullAuthentication() {
69: AuthenticationException exception = new DisabledException(
70: "TEST");
71:
72: try {
73: AuthenticationFailureDisabledEvent event = new AuthenticationFailureDisabledEvent(
74: null, exception);
75: fail("Should have thrown IllegalArgumentException");
76: } catch (IllegalArgumentException expected) {
77: assertTrue(true);
78: }
79: }
80:
81: public void testRejectsNullAuthenticationException() {
82: try {
83: new AuthenticationFailureDisabledEvent(getAuthentication(),
84: null);
85: fail("Should have thrown IllegalArgumentException");
86: } catch (IllegalArgumentException expected) {
87: assertTrue(true);
88: }
89: }
90: }
|