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.providers;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.GrantedAuthority;
21  import org.acegisecurity.GrantedAuthorityImpl;
22  
23  
24  /**
25   * Tests {@link AbstractAuthenticationToken}.
26   *
27   * @author Ben Alex
28   * @version $Id: AbstractAuthenticationTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
29   */
30  public class AbstractAuthenticationTokenTests extends TestCase {
31      //~ Instance fields ================================================================================================
32  
33      private GrantedAuthority[] authorities = null;
34  
35      //~ Constructors ===================================================================================================
36  
37      public AbstractAuthenticationTokenTests() {
38          super();
39      }
40  
41      public AbstractAuthenticationTokenTests(String arg0) {
42          super(arg0);
43      }
44  
45      //~ Methods ========================================================================================================
46  
47      public static void main(String[] args) {
48          junit.textui.TestRunner.run(AbstractAuthenticationTokenTests.class);
49      }
50  
51      public final void setUp() throws Exception {
52          super.setUp();
53  
54          authorities = new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")};
55      }
56  
57      public void testAuthoritiesAreImmutable() {
58          MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", authorities);
59          GrantedAuthority[] gotAuthorities = token.getAuthorities();
60          assertNotSame(authorities, gotAuthorities);
61  
62          gotAuthorities[0] = new GrantedAuthorityImpl("ROLE_SUPER_USER");
63  
64          // reget them and check nothing has changed
65          gotAuthorities = token.getAuthorities();
66          assertEquals(2, gotAuthorities.length);
67          assertEquals(gotAuthorities[0], authorities[0]);
68          assertEquals(gotAuthorities[1], authorities[1]);
69          assertFalse(gotAuthorities[0].equals("ROLE_SUPER_USER"));
70          assertFalse(gotAuthorities[1].equals("ROLE_SUPER_USER"));
71      }
72  
73      public void testGetters() throws Exception {
74          MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", authorities);
75          assertEquals("Test", token.getPrincipal());
76          assertEquals("Password", token.getCredentials());
77          assertEquals("Test", token.getName());
78      }
79  
80      public void testHashCode() throws Exception {
81          MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test", "Password", authorities);
82          MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test", "Password", authorities);
83          MockAuthenticationImpl token3 = new MockAuthenticationImpl(null, null, new GrantedAuthority[] {});
84          assertEquals(token1.hashCode(), token2.hashCode());
85          assertTrue(token1.hashCode() != token3.hashCode());
86  
87          token2.setAuthenticated(true);
88  
89          assertTrue(token1.hashCode() != token2.hashCode());
90      }
91  
92      public void testObjectsEquals() throws Exception {
93          MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test", "Password", authorities);
94          MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test", "Password", authorities);
95          assertEquals(token1, token2);
96  
97          MockAuthenticationImpl token3 = new MockAuthenticationImpl("Test", "Password_Changed", authorities);
98          assertTrue(!token1.equals(token3));
99  
100         MockAuthenticationImpl token4 = new MockAuthenticationImpl("Test_Changed", "Password", authorities);
101         assertTrue(!token1.equals(token4));
102 
103         MockAuthenticationImpl token5 = new MockAuthenticationImpl("Test", "Password",
104                 new GrantedAuthority[] {
105                     new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO_CHANGED")
106                 });
107         assertTrue(!token1.equals(token5));
108 
109         MockAuthenticationImpl token6 = new MockAuthenticationImpl("Test", "Password",
110                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")});
111         assertTrue(!token1.equals(token6));
112 
113         MockAuthenticationImpl token7 = new MockAuthenticationImpl("Test", "Password", null);
114         assertTrue(!token1.equals(token7));
115         assertTrue(!token7.equals(token1));
116 
117         assertTrue(!token1.equals(new Integer(100)));
118     }
119 
120     public void testSetAuthenticated() throws Exception {
121         MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", authorities);
122         assertTrue(!token.isAuthenticated());
123         token.setAuthenticated(true);
124         assertTrue(token.isAuthenticated());
125     }
126 
127     public void testToStringWithAuthorities() {
128         MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", authorities);
129         assertTrue(token.toString().lastIndexOf("ROLE_TWO") != -1);
130     }
131 
132     public void testToStringWithNullAuthorities() {
133         MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", null);
134         assertTrue(token.toString().lastIndexOf("Not granted any authorities") != -1);
135     }
136 
137     //~ Inner Classes ==================================================================================================
138 
139     private class MockAuthenticationImpl extends AbstractAuthenticationToken {
140         private Object credentials;
141         private Object principal;
142 
143         public MockAuthenticationImpl(Object principal, Object credentials, GrantedAuthority[] authorities) {
144             super(authorities);
145             this.principal = principal;
146             this.credentials = credentials;
147         }
148 
149         private MockAuthenticationImpl() {
150             super(null);
151         }
152 
153         public Object getCredentials() {
154             return this.credentials;
155         }
156 
157         public Object getPrincipal() {
158             return this.principal;
159         }
160     }
161 }