Detect nulls within GrantedAuthority[] passed to constructor. This ensures end-user DAO implementations are creating the User correctly.

This commit is contained in:
Ben Alex 2004-04-18 11:23:01 +00:00
parent a0f809991d
commit f38ed01b29
2 changed files with 23 additions and 0 deletions

View File

@ -48,6 +48,8 @@ public class User {
* is enabled * is enabled
* *
* @throws IllegalArgumentException if a <code>null</code> value was passed * @throws IllegalArgumentException if a <code>null</code> value was passed
* either as a parameter or as an element in the
* <code>GrantedAuthority[]</code> array
*/ */
public User(String username, String password, boolean enabled, public User(String username, String password, boolean enabled,
GrantedAuthority[] authorities) throws IllegalArgumentException { GrantedAuthority[] authorities) throws IllegalArgumentException {
@ -56,6 +58,14 @@ public class User {
"Cannot pass null values to constructor"); "Cannot pass null values to constructor");
} }
for (int i = 0; i < authorities.length; i++) {
if (authorities[i] == null) {
throw new IllegalArgumentException("Granted authority element "
+ i
+ " is null - GrantedAuthority[] cannot contain any null elements");
}
}
this.username = username; this.username = username;
this.password = password; this.password = password;
this.enabled = enabled; this.enabled = enabled;

View File

@ -84,6 +84,19 @@ public class UserTests extends TestCase {
} }
} }
public void testNullWithinGrantedAuthorityElementIsRejected()
throws Exception {
try {
User user = new User(null, "koala", true,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
"ROLE_TWO"), null, new GrantedAuthorityImpl(
"ROLE_THREE")});
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
public void testUserGettersSetter() throws Exception { public void testUserGettersSetter() throws Exception {
User user = new User("marissa", "koala", true, User user = new User("marissa", "koala", true,
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl( new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(