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.anonymous;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.GrantedAuthority;
21  import org.acegisecurity.GrantedAuthorityImpl;
22  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
23  
24  
25  /**
26   * Tests {@link AnonymousAuthenticationToken}.
27   *
28   * @author Ben Alex
29   * @version $Id: AnonymousAuthenticationTokenTests.java 1877 2007-05-25 05:33:06Z benalex $
30   */
31  public class AnonymousAuthenticationTokenTests extends TestCase {
32      //~ Constructors ===================================================================================================
33  
34      public AnonymousAuthenticationTokenTests() {
35          super();
36      }
37  
38      public AnonymousAuthenticationTokenTests(String arg0) {
39          super(arg0);
40      }
41  
42      //~ Methods ========================================================================================================
43  
44      public static void main(String[] args) {
45          junit.textui.TestRunner.run(AnonymousAuthenticationTokenTests.class);
46      }
47  
48      public final void setUp() throws Exception {
49          super.setUp();
50      }
51  
52      public void testConstructorRejectsNulls() {
53          try {
54              new AnonymousAuthenticationToken(null, "Test",
55                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
56              fail("Should have thrown IllegalArgumentException");
57          } catch (IllegalArgumentException expected) {
58              assertTrue(true);
59          }
60  
61          try {
62              new AnonymousAuthenticationToken("key", null,
63                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
64              fail("Should have thrown IllegalArgumentException");
65          } catch (IllegalArgumentException expected) {
66              assertTrue(true);
67          }
68  
69          try {
70              new AnonymousAuthenticationToken("key", "Test", null);
71              fail("Should have thrown IllegalArgumentException");
72          } catch (IllegalArgumentException expected) {
73              assertTrue(true);
74          }
75  
76          try {
77              new AnonymousAuthenticationToken("key", "Test", new GrantedAuthority[] {null});
78              fail("Should have thrown IllegalArgumentException");
79          } catch (IllegalArgumentException expected) {
80              assertTrue(true);
81          }
82  
83          try {
84              new AnonymousAuthenticationToken("key", "Test", new GrantedAuthority[] {});
85              fail("Should have thrown IllegalArgumentException");
86          } catch (IllegalArgumentException expected) {
87              assertTrue(true);
88          }
89      }
90  
91      public void testEqualsWhenEqual() {
92          AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test",
93                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
94  
95          AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key", "Test",
96                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
97  
98          assertEquals(token1, token2);
99      }
100 
101     public void testGetters() {
102         AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key", "Test",
103                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
104 
105         assertEquals("key".hashCode(), token.getKeyHash());
106         assertEquals("Test", token.getPrincipal());
107         assertEquals("", token.getCredentials());
108         assertEquals("ROLE_ONE", token.getAuthorities()[0].getAuthority());
109         assertEquals("ROLE_TWO", token.getAuthorities()[1].getAuthority());
110         assertTrue(token.isAuthenticated());
111     }
112 
113     public void testNoArgConstructorDoesntExist() {
114         Class clazz = AnonymousAuthenticationToken.class;
115 
116         try {
117             clazz.getDeclaredConstructor((Class[]) null);
118             fail("Should have thrown NoSuchMethodException");
119         } catch (NoSuchMethodException expected) {
120             assertTrue(true);
121         }
122     }
123 
124     public void testNotEqualsDueToAbstractParentEqualsCheck() {
125         AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test",
126                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
127 
128         AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key", "DIFFERENT_PRINCIPAL",
129                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
130 
131         assertFalse(token1.equals(token2));
132     }
133 
134     public void testNotEqualsDueToDifferentAuthenticationClass() {
135         AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test",
136                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
137 
138         UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password",
139                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
140 
141         assertFalse(token1.equals(token2));
142     }
143 
144     public void testNotEqualsDueToKey() {
145         AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test",
146                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
147 
148         AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("DIFFERENT_KEY", "Test",
149                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
150 
151         assertFalse(token1.equals(token2));
152     }
153 
154     public void testSetAuthenticatedIgnored() {
155         AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key", "Test",
156                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
157         assertTrue(token.isAuthenticated());
158         token.setAuthenticated(false);
159         assertTrue(!token.isAuthenticated());
160     }
161 }