1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.userdetails;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22 import org.acegisecurity.userdetails.User;
23 import org.acegisecurity.userdetails.UserDetails;
24
25
26 /***
27 * Tests {@link User}.
28 *
29 * @author Ben Alex
30 * @version $Id: UserTests.java,v 1.1 2005/11/29 13:10:07 benalex Exp $
31 */
32 public class UserTests extends TestCase {
33
34
35 public UserTests() {
36 super();
37 }
38
39 public UserTests(String arg0) {
40 super(arg0);
41 }
42
43
44
45 public final void setUp() throws Exception {
46 super.setUp();
47 }
48
49 public static void main(String[] args) {
50 junit.textui.TestRunner.run(UserTests.class);
51 }
52
53 public void testEquals() {
54 User user1 = new User("marissa", "koala", true, true, true, true,
55 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
56 "ROLE_TWO")});
57
58 assertFalse(user1.equals(null));
59 assertFalse(user1.equals("A STRING"));
60
61 assertTrue(user1.equals(user1));
62
63 assertTrue(user1.equals(
64 new User("marissa", "koala", true, true, true, true,
65 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
66 "ROLE_TWO")})));
67
68 assertFalse(user1.equals(
69 new User("DIFFERENT_USERNAME", "koala", true, true, true, true,
70 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
71 "ROLE_TWO")})));
72
73 assertFalse(user1.equals(
74 new User("marissa", "DIFFERENT_PASSWORD", true, true, true,
75 true,
76 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
77 "ROLE_TWO")})));
78
79 assertFalse(user1.equals(
80 new User("marissa", "koala", false, true, true, true,
81 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
82 "ROLE_TWO")})));
83
84 assertFalse(user1.equals(
85 new User("marissa", "koala", true, false, true, true,
86 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
87 "ROLE_TWO")})));
88
89 assertFalse(user1.equals(
90 new User("marissa", "koala", true, true, false, true,
91 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
92 "ROLE_TWO")})));
93
94 assertFalse(user1.equals(
95 new User("marissa", "koala", true, true, true, false,
96 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
97 "ROLE_TWO")})));
98
99 assertFalse(user1.equals(
100 new User("marissa", "koala", true, true, true, true,
101 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")})));
102 }
103
104 public void testNoArgConstructor() {
105 try {
106 new User();
107 fail("Should have thrown IllegalArgumentException");
108 } catch (IllegalArgumentException expected) {
109 assertTrue(true);
110 }
111 }
112
113 public void testNullValuesRejected() throws Exception {
114 try {
115 UserDetails user = new User(null, "koala", true, true, true, true,
116 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
117 "ROLE_TWO")});
118 fail("Should have thrown IllegalArgumentException");
119 } catch (IllegalArgumentException expected) {
120 assertTrue(true);
121 }
122
123 try {
124 UserDetails user = new User("marissa", null, true, true, true,
125 true,
126 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
127 "ROLE_TWO")});
128 fail("Should have thrown IllegalArgumentException");
129 } catch (IllegalArgumentException expected) {
130 assertTrue(true);
131 }
132
133 try {
134 UserDetails user = new User("marissa", "koala", true, true, true,
135 true, null);
136 fail("Should have thrown IllegalArgumentException");
137 } catch (IllegalArgumentException expected) {
138 assertTrue(true);
139 }
140
141 try {
142 UserDetails user = new User("marissa", "koala", true, true, true,
143 true,
144 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), null});
145 fail("Should have thrown IllegalArgumentException");
146 } catch (IllegalArgumentException expected) {
147 assertTrue(true);
148 }
149 }
150
151 public void testNullWithinGrantedAuthorityElementIsRejected()
152 throws Exception {
153 try {
154 UserDetails user = new User(null, "koala", true, true, true, true,
155 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
156 "ROLE_TWO"), null, new GrantedAuthorityImpl(
157 "ROLE_THREE")});
158 fail("Should have thrown IllegalArgumentException");
159 } catch (IllegalArgumentException expected) {
160 assertTrue(true);
161 }
162 }
163
164 public void testUserGettersSetter() throws Exception {
165 UserDetails user = new User("marissa", "koala", true, true, true, true,
166 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
167 "ROLE_TWO")});
168 assertEquals("marissa", user.getUsername());
169 assertEquals("koala", user.getPassword());
170 assertTrue(user.isEnabled());
171 assertEquals(new GrantedAuthorityImpl("ROLE_ONE"),
172 user.getAuthorities()[0]);
173 assertEquals(new GrantedAuthorityImpl("ROLE_TWO"),
174 user.getAuthorities()[1]);
175 assertTrue(user.toString().indexOf("marissa") != -1);
176 }
177
178 public void testUserIsEnabled() throws Exception {
179 UserDetails user = new User("marissa", "koala", false, true, true,
180 true,
181 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
182 "ROLE_TWO")});
183 assertTrue(!user.isEnabled());
184 }
185 }