1
2
3
4
5
6
7
8
9
10
11
12
13
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 import java.util.List;
25 import java.util.Vector;
26
27
28 /***
29 * Tests {@link AnonymousAuthenticationToken}.
30 *
31 * @author Ben Alex
32 * @version $Id: AnonymousAuthenticationTokenTests.java,v 1.3 2005/11/17 00:56:27 benalex Exp $
33 */
34 public class AnonymousAuthenticationTokenTests extends TestCase {
35
36
37 public AnonymousAuthenticationTokenTests() {
38 super();
39 }
40
41 public AnonymousAuthenticationTokenTests(String arg0) {
42 super(arg0);
43 }
44
45
46
47 public final void setUp() throws Exception {
48 super.setUp();
49 }
50
51 public static void main(String[] args) {
52 junit.textui.TestRunner.run(AnonymousAuthenticationTokenTests.class);
53 }
54
55 public void testConstructorRejectsNulls() {
56 try {
57 new AnonymousAuthenticationToken(null, "Test",
58 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
59 "ROLE_TWO")});
60 fail("Should have thrown IllegalArgumentException");
61 } catch (IllegalArgumentException expected) {
62 assertTrue(true);
63 }
64
65 try {
66 new AnonymousAuthenticationToken("key", null,
67 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
68 "ROLE_TWO")});
69 fail("Should have thrown IllegalArgumentException");
70 } catch (IllegalArgumentException expected) {
71 assertTrue(true);
72 }
73
74 try {
75 new AnonymousAuthenticationToken("key", "Test", null);
76 fail("Should have thrown IllegalArgumentException");
77 } catch (IllegalArgumentException expected) {
78 assertTrue(true);
79 }
80
81 try {
82 new AnonymousAuthenticationToken("key", "Test",
83 new GrantedAuthority[] {null});
84 fail("Should have thrown IllegalArgumentException");
85 } catch (IllegalArgumentException expected) {
86 assertTrue(true);
87 }
88
89 try {
90 new AnonymousAuthenticationToken("key", "Test",
91 new GrantedAuthority[] {});
92 fail("Should have thrown IllegalArgumentException");
93 } catch (IllegalArgumentException expected) {
94 assertTrue(true);
95 }
96 }
97
98 public void testEqualsWhenEqual() {
99 List proxyList1 = new Vector();
100 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
101
102 AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
103 "Test",
104 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
105 "ROLE_TWO")});
106
107 AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key",
108 "Test",
109 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
110 "ROLE_TWO")});
111
112 assertEquals(token1, token2);
113 }
114
115 public void testGetters() {
116 AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key",
117 "Test",
118 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
119 "ROLE_TWO")});
120
121 assertEquals("key".hashCode(), token.getKeyHash());
122 assertEquals("Test", token.getPrincipal());
123 assertEquals("", token.getCredentials());
124 assertEquals("ROLE_ONE", token.getAuthorities()[0].getAuthority());
125 assertEquals("ROLE_TWO", token.getAuthorities()[1].getAuthority());
126 assertTrue(token.isAuthenticated());
127 }
128
129 public void testNoArgConstructor() {
130 try {
131 new AnonymousAuthenticationToken();
132 fail("Should have thrown IllegalArgumentException");
133 } catch (IllegalArgumentException expected) {
134 assertTrue(true);
135 }
136 }
137
138 public void testNotEqualsDueToAbstractParentEqualsCheck() {
139 AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
140 "Test",
141 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
142 "ROLE_TWO")});
143
144 AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key",
145 "DIFFERENT_PRINCIPAL",
146 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
147 "ROLE_TWO")});
148
149 assertFalse(token1.equals(token2));
150 }
151
152 public void testNotEqualsDueToDifferentAuthenticationClass() {
153 AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
154 "Test",
155 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
156 "ROLE_TWO")});
157
158 UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test",
159 "Password",
160 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
161 "ROLE_TWO")});
162
163 assertFalse(token1.equals(token2));
164 }
165
166 public void testNotEqualsDueToKey() {
167 AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
168 "Test",
169 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
170 "ROLE_TWO")});
171
172 AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("DIFFERENT_KEY",
173 "Test",
174 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
175 "ROLE_TWO")});
176
177 assertFalse(token1.equals(token2));
178 }
179
180 public void testSetAuthenticatedIgnored() {
181 AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key",
182 "Test",
183 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
184 "ROLE_TWO")});
185 assertTrue(token.isAuthenticated());
186 token.setAuthenticated(false);
187 assertTrue(!token.isAuthenticated());
188 }
189 }