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