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
25
26
27
28
29
30
31 public class AnonymousAuthenticationTokenTests extends TestCase {
32
33
34 public AnonymousAuthenticationTokenTests() {
35 super();
36 }
37
38 public AnonymousAuthenticationTokenTests(String arg0) {
39 super(arg0);
40 }
41
42
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 }