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.adapters;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.GrantedAuthority;
21  import org.acegisecurity.GrantedAuthorityImpl;
22  
23  
24  /**
25   * Tests {@link AbstractAdapterAuthenticationToken}.
26   *
27   * @author Ben Alex
28   * @version $Id: AbstractAdapterAuthenticationTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
29   */
30  public class AbstractAdapterAuthenticationTokenTests extends TestCase {
31      //~ Constructors ===================================================================================================
32  
33      public AbstractAdapterAuthenticationTokenTests() {
34          super();
35      }
36  
37      public AbstractAdapterAuthenticationTokenTests(String arg0) {
38          super(arg0);
39      }
40  
41      //~ Methods ========================================================================================================
42  
43      public static void main(String[] args) {
44          junit.textui.TestRunner.run(AbstractAdapterAuthenticationTokenTests.class);
45      }
46  
47      public final void setUp() throws Exception {
48          super.setUp();
49      }
50  
51      public void testGetters() throws Exception {
52          MockDecisionManagerImpl token = new MockDecisionManagerImpl("my_password", "Test", "Password",
53                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
54          assertEquals("Test", token.getPrincipal());
55          assertEquals("Password", token.getCredentials());
56          assertEquals("my_password".hashCode(), token.getKeyHash());
57      }
58  
59      public void testIsUserInRole() throws Exception {
60          MockDecisionManagerImpl token = new MockDecisionManagerImpl("my_password", "Test", "Password",
61                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
62          assertTrue(token.isUserInRole("ROLE_ONE"));
63          assertTrue(token.isUserInRole("ROLE_TWO"));
64          assertTrue(!token.isUserInRole(""));
65          assertTrue(!token.isUserInRole("ROLE_ONE "));
66          assertTrue(!token.isUserInRole("role_one"));
67          assertTrue(!token.isUserInRole("ROLE_XXXX"));
68      }
69  
70      public void testObjectsEquals() throws Exception {
71          MockDecisionManagerImpl token1 = new MockDecisionManagerImpl("my_password", "Test", "Password",
72                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
73          MockDecisionManagerImpl token2 = new MockDecisionManagerImpl("my_password", "Test", "Password",
74                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
75          assertEquals(token1, token2);
76  
77          MockDecisionManagerImpl token3 = new MockDecisionManagerImpl("my_password", "Test", "Password_Changed",
78                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
79          assertTrue(!token1.equals(token3));
80  
81          MockDecisionManagerImpl token4 = new MockDecisionManagerImpl("my_password", "Test_Changed", "Password",
82                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
83          assertTrue(!token1.equals(token4));
84  
85          MockDecisionManagerImpl token5 = new MockDecisionManagerImpl("password_changed", "Test", "Password",
86                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
87          assertTrue(!token1.equals(token5));
88  
89          MockDecisionManagerImpl token6 = new MockDecisionManagerImpl("my_password", "Test", "Password",
90                  new GrantedAuthority[] {
91                      new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO_CHANGED")
92                  });
93          assertTrue(!token1.equals(token6));
94  
95          MockDecisionManagerImpl token7 = new MockDecisionManagerImpl("my_password", "Test", "Password",
96                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")});
97          assertTrue(!token1.equals(token7));
98  
99          assertTrue(!token1.equals(new Integer(100)));
100     }
101 
102     public void testSetAuthenticatedAlwaysReturnsTrue()
103         throws Exception {
104         MockDecisionManagerImpl token = new MockDecisionManagerImpl("my_password", "Test", "Password",
105                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
106         assertTrue(token.isAuthenticated());
107         token.setAuthenticated(false);
108         assertTrue(token.isAuthenticated());
109     }
110 
111     //~ Inner Classes ==================================================================================================
112 
113     private class MockDecisionManagerImpl extends AbstractAdapterAuthenticationToken {
114         private String password;
115         private String username;
116 
117         public MockDecisionManagerImpl(String key, String username, String password, GrantedAuthority[] authorities) {
118             super(key, authorities);
119             this.username = username;
120             this.password = password;
121         }
122 
123         public Object getCredentials() {
124             return this.password;
125         }
126 
127         public Object getPrincipal() {
128             return this.username;
129         }
130     }
131 }