1
2
3
4
5
6
7
8
9
10
11
12
13
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,v 1.2 2005/11/17 00:55:50 benalex Exp $
29 */
30 public class AbstractAdapterAuthenticationTokenTests extends TestCase {
31
32
33 public AbstractAdapterAuthenticationTokenTests() {
34 super();
35 }
36
37 public AbstractAdapterAuthenticationTokenTests(String arg0) {
38 super(arg0);
39 }
40
41
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(AbstractAdapterAuthenticationTokenTests.class);
49 }
50
51 public void testGetters() throws Exception {
52 MockDecisionManagerImpl token = new MockDecisionManagerImpl("my_password",
53 "Test", "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("my_password".hashCode(), token.getKeyHash());
59 }
60
61 public void testIsUserInRole() throws Exception {
62 MockDecisionManagerImpl token = new MockDecisionManagerImpl("my_password",
63 "Test", "Password",
64 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
65 "ROLE_TWO")});
66 assertTrue(token.isUserInRole("ROLE_ONE"));
67 assertTrue(token.isUserInRole("ROLE_TWO"));
68 assertTrue(!token.isUserInRole(""));
69 assertTrue(!token.isUserInRole("ROLE_ONE "));
70 assertTrue(!token.isUserInRole("role_one"));
71 assertTrue(!token.isUserInRole("ROLE_XXXX"));
72 }
73
74 public void testNoArgsConstructor() {
75 try {
76 new MockDecisionManagerImpl();
77 fail("Should have thrown IllegalArgumentException");
78 } catch (IllegalArgumentException expected) {
79 assertTrue(true);
80 }
81 }
82
83 public void testObjectsEquals() throws Exception {
84 MockDecisionManagerImpl token1 = new MockDecisionManagerImpl("my_password",
85 "Test", "Password",
86 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
87 "ROLE_TWO")});
88 MockDecisionManagerImpl token2 = new MockDecisionManagerImpl("my_password",
89 "Test", "Password",
90 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
91 "ROLE_TWO")});
92 assertEquals(token1, token2);
93
94 MockDecisionManagerImpl token3 = new MockDecisionManagerImpl("my_password",
95 "Test", "Password_Changed",
96 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
97 "ROLE_TWO")});
98 assertTrue(!token1.equals(token3));
99
100 MockDecisionManagerImpl token4 = new MockDecisionManagerImpl("my_password",
101 "Test_Changed", "Password",
102 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
103 "ROLE_TWO")});
104 assertTrue(!token1.equals(token4));
105
106 MockDecisionManagerImpl token5 = new MockDecisionManagerImpl("password_changed",
107 "Test", "Password",
108 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
109 "ROLE_TWO")});
110 assertTrue(!token1.equals(token5));
111
112 MockDecisionManagerImpl token6 = new MockDecisionManagerImpl("my_password",
113 "Test", "Password",
114 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
115 "ROLE_TWO_CHANGED")});
116 assertTrue(!token1.equals(token6));
117
118 MockDecisionManagerImpl token7 = new MockDecisionManagerImpl("my_password",
119 "Test", "Password",
120 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")});
121 assertTrue(!token1.equals(token7));
122
123 assertTrue(!token1.equals(new Integer(100)));
124 }
125
126 public void testSetAuthenticatedAlwaysReturnsTrue()
127 throws Exception {
128 MockDecisionManagerImpl token = new MockDecisionManagerImpl("my_password",
129 "Test", "Password",
130 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
131 "ROLE_TWO")});
132 assertTrue(token.isAuthenticated());
133 token.setAuthenticated(false);
134 assertTrue(token.isAuthenticated());
135 }
136
137
138
139 private class MockDecisionManagerImpl
140 extends AbstractAdapterAuthenticationToken {
141 private String password;
142 private String username;
143
144 public MockDecisionManagerImpl(String key, String username,
145 String password, GrantedAuthority[] authorities) {
146 super(key, authorities);
147 this.username = username;
148 this.password = password;
149 }
150
151 private MockDecisionManagerImpl() {
152 throw new IllegalArgumentException();
153 }
154
155 public Object getCredentials() {
156 return this.password;
157 }
158
159 public Object getPrincipal() {
160 return this.username;
161 }
162 }
163 }