001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.userdetails;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.userdetails.User;
024: import org.acegisecurity.userdetails.UserDetails;
025:
026: /**
027: * Tests {@link User}.
028: *
029: * @author Ben Alex
030: * @version $Id: UserTests.java 1496 2006-05-23 13:38:33Z benalex $
031: */
032: public class UserTests extends TestCase {
033: //~ Constructors ===================================================================================================
034:
035: public UserTests() {
036: super ();
037: }
038:
039: public UserTests(String arg0) {
040: super (arg0);
041: }
042:
043: //~ Methods ========================================================================================================
044:
045: public static void main(String[] args) {
046: junit.textui.TestRunner.run(UserTests.class);
047: }
048:
049: public final void setUp() throws Exception {
050: super .setUp();
051: }
052:
053: public void testEquals() {
054: User user1 = new User("marissa", "koala", true, true, true,
055: true, new GrantedAuthority[] {
056: new GrantedAuthorityImpl("ROLE_ONE"),
057: new GrantedAuthorityImpl("ROLE_TWO") });
058:
059: assertFalse(user1.equals(null));
060: assertFalse(user1.equals("A STRING"));
061:
062: assertTrue(user1.equals(user1));
063:
064: assertTrue(user1.equals(new User("marissa", "koala", true,
065: true, true, true, new GrantedAuthority[] {
066: new GrantedAuthorityImpl("ROLE_ONE"),
067: new GrantedAuthorityImpl("ROLE_TWO") })));
068:
069: assertFalse(user1.equals(new User("DIFFERENT_USERNAME",
070: "koala", true, true, true, true,
071: new GrantedAuthority[] {
072: new GrantedAuthorityImpl("ROLE_ONE"),
073: new GrantedAuthorityImpl("ROLE_TWO") })));
074:
075: assertFalse(user1.equals(new User("marissa",
076: "DIFFERENT_PASSWORD", true, true, true, true,
077: new GrantedAuthority[] {
078: new GrantedAuthorityImpl("ROLE_ONE"),
079: new GrantedAuthorityImpl("ROLE_TWO") })));
080:
081: assertFalse(user1.equals(new User("marissa", "koala", false,
082: true, true, true, new GrantedAuthority[] {
083: new GrantedAuthorityImpl("ROLE_ONE"),
084: new GrantedAuthorityImpl("ROLE_TWO") })));
085:
086: assertFalse(user1.equals(new User("marissa", "koala", true,
087: false, true, true, new GrantedAuthority[] {
088: new GrantedAuthorityImpl("ROLE_ONE"),
089: new GrantedAuthorityImpl("ROLE_TWO") })));
090:
091: assertFalse(user1.equals(new User("marissa", "koala", true,
092: true, false, true, new GrantedAuthority[] {
093: new GrantedAuthorityImpl("ROLE_ONE"),
094: new GrantedAuthorityImpl("ROLE_TWO") })));
095:
096: assertFalse(user1.equals(new User("marissa", "koala", true,
097: true, true, false, new GrantedAuthority[] {
098: new GrantedAuthorityImpl("ROLE_ONE"),
099: new GrantedAuthorityImpl("ROLE_TWO") })));
100:
101: assertFalse(user1.equals(new User("marissa", "koala", true,
102: true, true, true,
103: new GrantedAuthority[] { new GrantedAuthorityImpl(
104: "ROLE_ONE") })));
105: }
106:
107: public void testNoArgConstructorDoesntExist() {
108: Class clazz = User.class;
109:
110: try {
111: clazz.getDeclaredConstructor((Class[]) null);
112: fail("Should have thrown NoSuchMethodException");
113: } catch (NoSuchMethodException expected) {
114: assertTrue(true);
115: }
116: }
117:
118: public void testNullValuesRejected() throws Exception {
119: try {
120: UserDetails user = new User(null, "koala", true, true,
121: true, true, new GrantedAuthority[] {
122: new GrantedAuthorityImpl("ROLE_ONE"),
123: new GrantedAuthorityImpl("ROLE_TWO") });
124: fail("Should have thrown IllegalArgumentException");
125: } catch (IllegalArgumentException expected) {
126: assertTrue(true);
127: }
128:
129: try {
130: UserDetails user = new User("marissa", null, true, true,
131: true, true, new GrantedAuthority[] {
132: new GrantedAuthorityImpl("ROLE_ONE"),
133: new GrantedAuthorityImpl("ROLE_TWO") });
134: fail("Should have thrown IllegalArgumentException");
135: } catch (IllegalArgumentException expected) {
136: assertTrue(true);
137: }
138:
139: try {
140: UserDetails user = new User("marissa", "koala", true, true,
141: true, true, null);
142: fail("Should have thrown IllegalArgumentException");
143: } catch (IllegalArgumentException expected) {
144: assertTrue(true);
145: }
146:
147: try {
148: UserDetails user = new User(
149: "marissa",
150: "koala",
151: true,
152: true,
153: true,
154: true,
155: new GrantedAuthority[] {
156: new GrantedAuthorityImpl("ROLE_ONE"), null });
157: fail("Should have thrown IllegalArgumentException");
158: } catch (IllegalArgumentException expected) {
159: assertTrue(true);
160: }
161: }
162:
163: public void testNullWithinGrantedAuthorityElementIsRejected()
164: throws Exception {
165: try {
166: UserDetails user = new User(null, "koala", true, true,
167: true, true, new GrantedAuthority[] {
168: new GrantedAuthorityImpl("ROLE_ONE"),
169: new GrantedAuthorityImpl("ROLE_TWO"), null,
170: new GrantedAuthorityImpl("ROLE_THREE") });
171: fail("Should have thrown IllegalArgumentException");
172: } catch (IllegalArgumentException expected) {
173: assertTrue(true);
174: }
175: }
176:
177: public void testUserGettersSetter() throws Exception {
178: UserDetails user = new User("marissa", "koala", true, true,
179: true, true, new GrantedAuthority[] {
180: new GrantedAuthorityImpl("ROLE_ONE"),
181: new GrantedAuthorityImpl("ROLE_TWO") });
182: assertEquals("marissa", user.getUsername());
183: assertEquals("koala", user.getPassword());
184: assertTrue(user.isEnabled());
185: assertEquals(new GrantedAuthorityImpl("ROLE_ONE"), user
186: .getAuthorities()[0]);
187: assertEquals(new GrantedAuthorityImpl("ROLE_TWO"), user
188: .getAuthorities()[1]);
189: assertTrue(user.toString().indexOf("marissa") != -1);
190: }
191:
192: public void testUserIsEnabled() throws Exception {
193: UserDetails user = new User("marissa", "koala", false, true,
194: true, true, new GrantedAuthority[] {
195: new GrantedAuthorityImpl("ROLE_ONE"),
196: new GrantedAuthorityImpl("ROLE_TWO") });
197: assertTrue(!user.isEnabled());
198: }
199: }
|