/***********************************************************************
 * Copyright (c) 2000-2006 The Apache Software Foundation.             *
 * All rights reserved.                                                *
 * ------------------------------------------------------------------- *
 * Licensed under the Apache License, Version 2.0 (the "License"); you *
 * may not use this file except in compliance with the License. You    *
 * may obtain a copy of the License at:                                *
 *                                                                     *
 *     http://www.apache.org/licenses/LICENSE-2.0                      *
 *                                                                     *
 * Unless required by applicable law or agreed to in writing, software *
 * distributed under the License is distributed on an "AS IS" BASIS,   *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
 * implied.  See the License for the specific language governing       *
 * permissions and limitations under the License.                      *
 ***********************************************************************/
package org.apache.james.userrepository;

import org.apache.james.security.DigestUtil;
import org.apache.james.services.User;
import org.apache.james.services.UsersRepository;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class MockUsersRepository implements UsersRepository {

    private final HashMap m_users = new HashMap();

    /**
     * force the repository to hold implementations of JamesUser interface, instead of User
     * JamesUser is _not_ required as of the UsersRepository interface, so the necessarity forcing it
     * is due to code using UsersRepository while at the same time expecting it to hold JamesUsers
     * (like in RemoteManagerHandler) 
     */
    private boolean m_forceUseJamesUser = false;

    public void setForceUseJamesUser() {
        m_forceUseJamesUser = true;
    }

    public boolean addUser(User user) {

        if (m_forceUseJamesUser && user instanceof DefaultUser ) {
            DefaultUser aUser = (DefaultUser)user;
            user = new DefaultJamesUser(aUser.getUserName(),
                                             aUser.getHashedPassword(),
                                             aUser.getHashAlgorithm());
        }

        String key = user.getUserName();
        if (m_users.containsKey(key)) return false;
        m_users.put(key, user);
        return true;
    }

    public void addUser(String name, Object attributes) {
        try {
            String passwordHash = DigestUtil.digestString(((String) attributes), "SHA");

            User user;

            if (m_forceUseJamesUser) {
                user = new DefaultJamesUser(name, passwordHash, "SHA");
            } else {
                user = new DefaultUser(name, passwordHash, "SHA");
            }
           
            addUser(user);
        } catch (Exception e) {
            e.printStackTrace();  // encoding failed
        }
    }

    public boolean addUser(String username, String password) {
        User user;
        if (m_forceUseJamesUser) {
            user = new DefaultJamesUser(username, password);
        } else {
            user = new DefaultUser(username, password);
        }
        return addUser(user);
    }

    public Object getAttributes(String name) {
        return null;  // trivial implementation
    }

    public User getUserByName(String name) {
        return (User) m_users.get(name);
    }

    public User getUserByNameCaseInsensitive(String name) {
        return null;  // trivial implementation
    }

    public String getRealName(String name) {
        return ((User) m_users.get(name)).getUserName();
    }

    public boolean updateUser(User user) {
        return false;  // trivial implementation
    }

    public void removeUser(String name) {
        m_users.remove(name);
    }

    public boolean contains(String name) {
        return m_users.containsKey(name);
    }

    public boolean containsCaseInsensitive(String name) {
        return false;  // trivial implementation
    }

    public boolean test(String name, Object attributes) {
        return false;  // trivial implementation
    }

    public boolean test(String name, String password) {
        User user = getUserByName(name);
        if (user == null) return false;
        return user.verifyPassword(password);
    }

    public int countUsers() {
        return m_users.size();
    }

    protected List listUserNames() {
        Iterator users = m_users.values().iterator();
        List userNames = new LinkedList();
        while ( users.hasNext() ) {
            User user = (User)users.next();
            userNames.add(user.getUserName());
        }

        return userNames;
    }
    public Iterator list() {
        return listUserNames().iterator(); 
    }
}

@@ -3165,32 +3165,186 @@
 ing password) %7B%0A
+        if (m_users.containsKey(username)) return false;%0A        try %7B%0A            String passwordHash = DigestUtil.digestString((password), %22SHA%22);%0A%0A    
         User use
@@ -3346,24 +3346,25 @@
 r user;%0A
+%0A
         
 if (m_fo
@@ -3347,32 +3347,36 @@
  user;%0A%0A        
+    
 if (m_forceUseJa
@@ -3378,32 +3378,36 @@
 UseJamesUser) %7B%0A
+    
             user
@@ -3440,32 +3440,43 @@
 ername, password
+Hash, %22SHA%22
 );%0A        %7D els
@@ -3462,32 +3462,36 @@
 %22SHA%22);%0A        
+    
 %7D else %7B%0A       
@@ -3475,32 +3475,36 @@
        %7D else %7B%0A
+    
             user
@@ -3536,27 +3536,42 @@
 me, password
-);%0A
+Hash, %22SHA%22);%0A    
         %7D%0A  
@@ -3560,32 +3560,48 @@
 ;%0A            %7D%0A
+           %0A    
         return a
@@ -3610,24 +3610,141 @@
 User(user);%0A
+        %7D catch (Exception e) %7B%0A            e.printStackTrace();  // encoding failed%0A        %7D%0A        return false;%0A
     %7D%0A%0A    p

Fix MockUsersRepository to pass tests (broken after the JAMES-505 patch)