1   /* Copyright 2004 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,v 1.3 2005/11/17 00:55:48 benalex Exp $
29   */
30  public class AbstractAuthenticationTokenTests extends TestCase {
31      //~ Constructors ===========================================================
32  
33      public AbstractAuthenticationTokenTests() {
34          super();
35      }
36  
37      public AbstractAuthenticationTokenTests(String arg0) {
38          super(arg0);
39      }
40  
41      //~ Methods ================================================================
42  
43      public final void setUp() throws Exception {
44          super.setUp();
45      }
46  
47      public static void main(String[] args) {
48          junit.textui.TestRunner.run(AbstractAuthenticationTokenTests.class);
49      }
50  
51      public void testGetters() throws Exception {
52          MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
53                  "Password",
54                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
55                          "ROLE_TWO")});
56          assertEquals("Test", token.getPrincipal());
57          assertEquals("Password", token.getCredentials());
58          assertEquals("Test", token.getName());
59      }
60  
61      public void testObjectsEquals() throws Exception {
62          MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
63                  "Password",
64                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
65                          "ROLE_TWO")});
66          MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test",
67                  "Password",
68                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
69                          "ROLE_TWO")});
70          assertEquals(token1, token2);
71  
72          MockAuthenticationImpl token3 = new MockAuthenticationImpl("Test",
73                  "Password_Changed",
74                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
75                          "ROLE_TWO")});
76          assertTrue(!token1.equals(token3));
77  
78          MockAuthenticationImpl token4 = new MockAuthenticationImpl("Test_Changed",
79                  "Password",
80                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
81                          "ROLE_TWO")});
82          assertTrue(!token1.equals(token4));
83  
84          MockAuthenticationImpl token5 = new MockAuthenticationImpl("Test",
85                  "Password",
86                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
87                          "ROLE_TWO_CHANGED")});
88          assertTrue(!token1.equals(token5));
89  
90          MockAuthenticationImpl token6 = new MockAuthenticationImpl("Test",
91                  "Password",
92                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")});
93          assertTrue(!token1.equals(token6));
94  
95          MockAuthenticationImpl token7 = new MockAuthenticationImpl("Test",
96                  "Password", null);
97          assertTrue(!token1.equals(token7));
98          assertTrue(!token7.equals(token1));
99  
100         assertTrue(!token1.equals(new Integer(100)));
101     }
102 
103     public void testSetAuthenticated() throws Exception {
104         MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
105                 "Password",
106                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
107                         "ROLE_TWO")});
108         assertTrue(!token.isAuthenticated());
109         token.setAuthenticated(true);
110         assertTrue(token.isAuthenticated());
111     }
112 
113     public void testToStringWithAuthorities() {
114         MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
115                 "Password",
116                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
117                         "ROLE_TWO")});
118         assertTrue(token.toString().lastIndexOf("ROLE_TWO") != -1);
119     }
120 
121     public void testToStringWithNullAuthorities() {
122         MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
123                 "Password", null);
124         assertTrue(token.toString().lastIndexOf("Not granted any authorities") != -1);
125     }
126 
127     //~ Inner Classes ==========================================================
128 
129     private class MockAuthenticationImpl extends AbstractAuthenticationToken {
130         private Object credentials;
131         private Object principal;
132         private GrantedAuthority[] authorities;
133         private boolean authenticated = false;
134 
135         public MockAuthenticationImpl(Object principal, Object credentials,
136             GrantedAuthority[] authorities) {
137             this.principal = principal;
138             this.credentials = credentials;
139             this.authorities = authorities;
140         }
141 
142         private MockAuthenticationImpl() {
143             super();
144         }
145 
146         public void setAuthenticated(boolean isAuthenticated) {
147             this.authenticated = isAuthenticated;
148         }
149 
150         public boolean isAuthenticated() {
151             return this.authenticated;
152         }
153 
154         public GrantedAuthority[] getAuthorities() {
155             return this.authorities;
156         }
157 
158         public Object getCredentials() {
159             return this.credentials;
160         }
161 
162         public Object getPrincipal() {
163             return this.principal;
164         }
165     }
166 }