Clover coverage report - Acegi Security System for Spring - 1.0.0-RC1
Coverage timestamp: Mon Dec 5 2005 09:05:15 EST
file stats: LOC: 273   Methods: 14
NCLOC: 122   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
User.java 88.9% 91.7% 85.7% 90%
coverage coverage
 1    /* Copyright 2004, 2005 Acegi Technology Pty Limited
 2    *
 3    * Licensed under the Apache License, Version 2.0 (the "License");
 4    * you may not use this file except in compliance with the License.
 5    * You may obtain a copy of the License at
 6    *
 7    * http://www.apache.org/licenses/LICENSE-2.0
 8    *
 9    * Unless required by applicable law or agreed to in writing, software
 10    * distributed under the License is distributed on an "AS IS" BASIS,
 11    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    * See the License for the specific language governing permissions and
 13    * limitations under the License.
 14    */
 15   
 16    package org.acegisecurity.userdetails;
 17   
 18    import org.acegisecurity.GrantedAuthority;
 19    import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
 20    import org.springframework.util.Assert;
 21   
 22    /**
 23    * Models core user information retieved by an {@link UserDetailsService}.
 24    *
 25    * <P>
 26    * Implemented with value object semantics (immutable after construction, like a
 27    * <code>String</code>). Developers may use this class directly, subclass it,
 28    * or write their own {@link UserDetails} implementation from scratch.
 29    * </p>
 30    */
 31    public class User implements UserDetails {
 32    // ~ Instance fields
 33    // ========================================================
 34   
 35    private String password;
 36   
 37    private String username;
 38   
 39    private GrantedAuthority[] authorities;
 40   
 41    private boolean accountNonExpired;
 42   
 43    private boolean accountNonLocked;
 44   
 45    private boolean credentialsNonExpired;
 46   
 47    private boolean enabled;
 48   
 49    // ~ Constructors
 50    // ===========================================================
 51   
 52  1 protected User() {
 53  1 throw new IllegalArgumentException("Cannot use default constructor");
 54    }
 55   
 56    /**
 57    * Construct the <code>User</code> with the details required by {@link
 58    * DaoAuthenticationProvider}.
 59    *
 60    * @param username
 61    * the username presented to the
 62    * <code>DaoAuthenticationProvider</code>
 63    * @param password
 64    * the password that should be presented to the
 65    * <code>DaoAuthenticationProvider</code>
 66    * @param enabled
 67    * set to <code>true</code> if the user is enabled
 68    * @param authorities
 69    * the authorities that should be granted to the caller if they
 70    * presented the correct username and password and the user is
 71    * enabled
 72    *
 73    * @throws IllegalArgumentException
 74    * if a <code>null</code> value was passed either as a
 75    * parameter or as an element in the
 76    * <code>GrantedAuthority[]</code> array
 77    *
 78    * @deprecated use new constructor with extended properties (this
 79    * constructor will be removed from release 1.0.0)
 80    */
 81  0 public User(String username, String password, boolean enabled,
 82    GrantedAuthority[] authorities) throws IllegalArgumentException {
 83  0 this(username, password, enabled, true, true, authorities);
 84    }
 85   
 86    /**
 87    * Construct the <code>User</code> with the details required by {@link
 88    * DaoAuthenticationProvider}.
 89    *
 90    * @param username
 91    * the username presented to the
 92    * <code>DaoAuthenticationProvider</code>
 93    * @param password
 94    * the password that should be presented to the
 95    * <code>DaoAuthenticationProvider</code>
 96    * @param enabled
 97    * set to <code>true</code> if the user is enabled
 98    * @param accountNonExpired
 99    * set to <code>true</code> if the account has not expired
 100    * @param credentialsNonExpired
 101    * set to <code>true</code> if the credentials have not expired
 102    * @param authorities
 103    * the authorities that should be granted to the caller if they
 104    * presented the correct username and password and the user is
 105    * enabled
 106    *
 107    * @throws IllegalArgumentException
 108    * if a <code>null</code> value was passed either as a
 109    * parameter or as an element in the
 110    * <code>GrantedAuthority[]</code> array
 111    *
 112    * @deprecated use new constructor with extended properties (this
 113    * constructor will be removed from release 1.0.0)
 114    */
 115  0 public User(String username, String password, boolean enabled,
 116    boolean accountNonExpired, boolean credentialsNonExpired,
 117    GrantedAuthority[] authorities) throws IllegalArgumentException {
 118  0 this(username, password, enabled, accountNonExpired,
 119    credentialsNonExpired, true, authorities);
 120    }
 121   
 122    /**
 123    * Construct the <code>User</code> with the details required by {@link
 124    * DaoAuthenticationProvider}.
 125    *
 126    * @param username
 127    * the username presented to the
 128    * <code>DaoAuthenticationProvider</code>
 129    * @param password
 130    * the password that should be presented to the
 131    * <code>DaoAuthenticationProvider</code>
 132    * @param enabled
 133    * set to <code>true</code> if the user is enabled
 134    * @param accountNonExpired
 135    * set to <code>true</code> if the account has not expired
 136    * @param credentialsNonExpired
 137    * set to <code>true</code> if the credentials have not expired
 138    * @param accountNonLocked
 139    * set to <code>true</code> if the account is not locked
 140    * @param authorities
 141    * the authorities that should be granted to the caller if they
 142    * presented the correct username and password and the user is
 143    * enabled
 144    *
 145    * @throws IllegalArgumentException
 146    * if a <code>null</code> value was passed either as a
 147    * parameter or as an element in the
 148    * <code>GrantedAuthority[]</code> array
 149    */
 150  265 public User(String username, String password, boolean enabled,
 151    boolean accountNonExpired, boolean credentialsNonExpired,
 152    boolean accountNonLocked, GrantedAuthority[] authorities)
 153    throws IllegalArgumentException {
 154  265 if (((username == null) || "".equals(username)) || (password == null)) {
 155  3 throw new IllegalArgumentException(
 156    "Cannot pass null or empty values to constructor");
 157    }
 158   
 159  262 this.username = username;
 160  262 this.password = password;
 161  262 this.enabled = enabled;
 162  262 this.accountNonExpired = accountNonExpired;
 163  262 this.credentialsNonExpired = credentialsNonExpired;
 164  262 this.accountNonLocked = accountNonLocked;
 165  262 setAuthorities(authorities);
 166    }
 167   
 168    // ~ Methods
 169    // ================================================================
 170   
 171  36 public boolean equals(Object rhs) {
 172  36 if (!(rhs instanceof User) || (rhs == null)) {
 173  22 return false;
 174    }
 175   
 176  14 User user = (User) rhs;
 177   
 178    // We rely on constructor to guarantee any User has non-null and >0
 179    // authorities
 180  14 if (user.getAuthorities().length != this.getAuthorities().length) {
 181  1 return false;
 182    }
 183   
 184  13 for (int i = 0; i < this.getAuthorities().length; i++) {
 185  23 if (!this.getAuthorities()[i].equals(user.getAuthorities()[i])) {
 186  0 return false;
 187    }
 188    }
 189   
 190    // We rely on constructor to guarantee non-null username and password
 191  13 return (this.getPassword().equals(user.getPassword())
 192    && this.getUsername().equals(user.getUsername())
 193    && (this.isAccountNonExpired() == user.isAccountNonExpired())
 194    && (this.isAccountNonLocked() == user.isAccountNonLocked())
 195    && (this.isCredentialsNonExpired() == user
 196    .isCredentialsNonExpired()) && (this.isEnabled() == user
 197    .isEnabled()));
 198    }
 199   
 200  867 public GrantedAuthority[] getAuthorities() {
 201  867 return authorities;
 202    }
 203   
 204  84 public String getPassword() {
 205  84 return password;
 206    }
 207   
 208  250 public String getUsername() {
 209  250 return username;
 210    }
 211   
 212  44 public boolean isAccountNonExpired() {
 213  44 return accountNonExpired;
 214    }
 215   
 216  43 public boolean isAccountNonLocked() {
 217  43 return this.accountNonLocked;
 218    }
 219   
 220  34 public boolean isCredentialsNonExpired() {
 221  34 return credentialsNonExpired;
 222    }
 223   
 224  52 public boolean isEnabled() {
 225  52 return enabled;
 226    }
 227   
 228  262 protected void setAuthorities(GrantedAuthority[] authorities) {
 229  262 Assert
 230    .notNull(authorities,
 231    "Cannot pass a null GrantedAuthority array");
 232   
 233  261 for (int i = 0; i < authorities.length; i++) {
 234  382 Assert
 235    .notNull(
 236    authorities[i],
 237    "Granted authority element "
 238    + i
 239    + " is null - GrantedAuthority[] cannot contain any null elements");
 240    }
 241   
 242  260 this.authorities = authorities;
 243    }
 244   
 245  155 public String toString() {
 246  155 StringBuffer sb = new StringBuffer();
 247  155 sb.append(super.toString() + ": ");
 248  155 sb.append("Username: " + this.username + "; ");
 249  155 sb.append("Password: [PROTECTED]; ");
 250  155 sb.append("Enabled: " + this.enabled + "; ");
 251  155 sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
 252  155 sb
 253    .append("credentialsNonExpired: " + this.credentialsNonExpired
 254    + "; ");
 255  155 sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
 256   
 257  155 if (this.getAuthorities() != null) {
 258  155 sb.append("Granted Authorities: ");
 259   
 260  155 for (int i = 0; i < this.getAuthorities().length; i++) {
 261  208 if (i > 0) {
 262  53 sb.append(", ");
 263    }
 264   
 265  208 sb.append(this.getAuthorities()[i].toString());
 266    }
 267    } else {
 268  0 sb.append("Not granted any authorities");
 269    }
 270   
 271  155 return sb.toString();
 272    }
 273    }