瀏覽代碼

Enhancements to detect errors and faciltiate easier testing.

Ben Alex 21 年之前
父節點
當前提交
adb1971873
共有 1 個文件被更改,包括 36 次插入3 次删除
  1. 36 3
      core/src/main/java/org/acegisecurity/userdetails/memory/UserMap.java

+ 36 - 3
core/src/main/java/org/acegisecurity/userdetails/memory/UserMap.java

@@ -16,6 +16,7 @@
 package net.sf.acegisecurity.providers.dao.memory;
 
 import net.sf.acegisecurity.providers.dao.User;
+import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -42,16 +43,48 @@ public class UserMap {
 
     //~ Methods ================================================================
 
-    public User getUser(String username) {
-        return (User) this.userMap.get(username.toLowerCase());
+    /**
+     * Locates the specified user by performing a case insensitive search by
+     * username.
+     *
+     * @param username to find
+     *
+     * @return the located user
+     *
+     * @throws UsernameNotFoundException if the user could not be found
+     */
+    public User getUser(String username) throws UsernameNotFoundException {
+        User result = (User) this.userMap.get(username.toLowerCase());
+
+        if (result == null) {
+            throw new UsernameNotFoundException("Could not find user: "
+                + username);
+        }
+
+        return result;
+    }
+
+    /**
+     * Indicates the size of the user map.
+     *
+     * @return the number of users in the map
+     */
+    public int getUserCount() {
+        return this.userMap.size();
     }
 
     /**
      * Adds a user to the in-memory map.
      *
      * @param user the user to be stored
+     *
+     * @throws IllegalArgumentException if a null User was passed
      */
-    public void addUser(User user) {
+    public void addUser(User user) throws IllegalArgumentException {
+        if (user == null) {
+            throw new IllegalArgumentException("Must be a valid User");
+        }
+
         logger.info("Adding user [" + user + "]");
         this.userMap.put(user.getUsername().toLowerCase(), user);
     }