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;
17:
18: import junit.framework.TestCase;
19:
20: import org.acegisecurity.providers.TestingAuthenticationToken;
21:
22: /**
23: * Tests {@link AbstractAuthenticationManager}.
24: *
25: * @author Luke Taylor
26: * @version $Id: AbstractAuthenticationManagerTests.java 1496 2006-05-23 13:38:33Z benalex $
27: */
28: public class AbstractAuthenticationManagerTests extends TestCase {
29: //~ Constructors ===================================================================================================
30:
31: public AbstractAuthenticationManagerTests() {
32: super ();
33: }
34:
35: public AbstractAuthenticationManagerTests(String arg0) {
36: super (arg0);
37: }
38:
39: //~ Methods ========================================================================================================
40:
41: /**
42: * Creates an AuthenticationManager which will return a token with the given details object set on it.
43: *
44: * @param resultDetails DOCUMENT ME!
45: *
46: * @return DOCUMENT ME!
47: */
48: private AuthenticationManager createAuthenticationManager(
49: final Object resultDetails) {
50: return new AbstractAuthenticationManager() {
51: protected Authentication doAuthentication(
52: Authentication authentication)
53: throws AuthenticationException {
54: TestingAuthenticationToken token = createAuthenticationToken();
55: token.setDetails(resultDetails);
56:
57: return token;
58: }
59: };
60: }
61:
62: private TestingAuthenticationToken createAuthenticationToken() {
63: return new TestingAuthenticationToken("name", "password",
64: new GrantedAuthorityImpl[0]);
65: }
66:
67: public void testDetailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() {
68: Object requestDetails = new String("(Request Details)");
69: Object resultDetails = new String("(Result Details)");
70: AuthenticationManager authMgr = createAuthenticationManager(resultDetails);
71:
72: TestingAuthenticationToken request = createAuthenticationToken();
73: request.setDetails(requestDetails);
74:
75: Authentication result = authMgr.authenticate(request);
76: assertEquals(resultDetails, result.getDetails());
77: }
78:
79: public void testDetailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
80: AuthenticationManager authMgr = createAuthenticationManager(null);
81: Object details = new Object();
82:
83: TestingAuthenticationToken request = createAuthenticationToken();
84: request.setDetails(details);
85:
86: Authentication result = authMgr.authenticate(request);
87: assertEquals(details, result.getDetails());
88: }
89: }
|