You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2006/10/07 17:58:31 UTC

svn commit: r453943 - in /james/server/trunk: ./ src/java/org/apache/james/userrepository/ src/test/org/apache/james/test/mock/avalon/ src/test/org/apache/james/test/mock/james/ src/test/org/apache/james/test/mock/util/ src/test/org/apache/james/userre...

Author: bago
Date: Sat Oct  7 08:58:30 2006
New Revision: 453943

URL: http://svn.apache.org/viewvc?view=rev&rev=453943
Log:
Fixed DefaultUsersJdbcRepository: it was using bad placeholders and it was not working!
Fixed ListUsersJdbcRepository: it threw an NPE on test password (made it to return false)
Removed unused code from the usersrepository.
Added extensive test coverage to the usersrepository package (excluding the LDAP repository)
Overall coverage raised from 25% to 27%

Added:
    james/server/trunk/src/test/org/apache/james/test/mock/james/MockFileSystem.java   (with props)
    james/server/trunk/src/test/org/apache/james/test/mock/util/AttrValConfiguration.java   (with props)
    james/server/trunk/src/test/org/apache/james/userrepository/DefaultUsersJdbcRepositoryTest.java   (with props)
    james/server/trunk/src/test/org/apache/james/userrepository/JamesUsersJdbcRepositoryTest.java   (with props)
    james/server/trunk/src/test/org/apache/james/userrepository/ListUsersJdbcRepositoryTest.java   (with props)
    james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepositoryTest.java   (with props)
    james/server/trunk/src/test/org/apache/james/userrepository/UsersFileRepositoryTest.java   (with props)
Modified:
    james/server/trunk/pom.xml
    james/server/trunk/src/java/org/apache/james/userrepository/AbstractJdbcUsersRepository.java
    james/server/trunk/src/java/org/apache/james/userrepository/AbstractUsersRepository.java
    james/server/trunk/src/java/org/apache/james/userrepository/DefaultUsersJdbcRepository.java
    james/server/trunk/src/java/org/apache/james/userrepository/ListUsersJdbcRepository.java
    james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java
    james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepository.java

Modified: james/server/trunk/pom.xml
URL: http://svn.apache.org/viewvc/james/server/trunk/pom.xml?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/pom.xml (original)
+++ james/server/trunk/pom.xml Sat Oct  7 08:58:30 2006
@@ -365,6 +365,13 @@
     </dependency>
     
     <dependency>
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derby</artifactId>
+      <version>10.1.3.1</version>
+      <scope>test</scope>
+    </dependency>
+    
+    <dependency>
       <groupId>oro</groupId>
       <artifactId>oro</artifactId>
       <version>2.0.8</version>

Modified: james/server/trunk/src/java/org/apache/james/userrepository/AbstractJdbcUsersRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/userrepository/AbstractJdbcUsersRepository.java?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/src/java/org/apache/james/userrepository/AbstractJdbcUsersRepository.java (original)
+++ james/server/trunk/src/java/org/apache/james/userrepository/AbstractJdbcUsersRepository.java Sat Oct  7 08:58:30 2006
@@ -107,7 +107,7 @@
      * Set the DataSourceSelector
      * @param datasources the DataSourceSelector
      */
-    public void setDatasources(DataSourceSelector datasources) {
+    void setDatasources(DataSourceSelector datasources) {
         m_datasources = datasources;
     }
 
@@ -136,7 +136,7 @@
      * 
      * @param system the new service
      */
-    private void setFileSystem(FileSystem system) {
+    void setFileSystem(FileSystem system) {
         this.fileSystem = system;
     }
 

Modified: james/server/trunk/src/java/org/apache/james/userrepository/AbstractUsersRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/userrepository/AbstractUsersRepository.java?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/src/java/org/apache/james/userrepository/AbstractUsersRepository.java (original)
+++ james/server/trunk/src/java/org/apache/james/userrepository/AbstractUsersRepository.java Sat Oct  7 08:58:30 2006
@@ -72,26 +72,12 @@
      */
     protected abstract void doUpdateUser(User user);
 
-    //
-    // Extended protected methods.
-    // These provide very basic default implementations, which will work,
-    // but may need to be overridden by subclasses for performance reasons.
-    //
     /**
      * Produces the complete list of User names, with correct case.
      * @return a <code>List</code> of <code>String</code>s representing
      *         user names.
      */
-    protected List listUserNames() {
-        Iterator users = listAllUsers();
-        List userNames = new LinkedList();
-        while ( users.hasNext() ) {
-            User user = (User)users.next();
-            userNames.add(user.getUserName());
-        }
-
-        return userNames;
-    }
+    protected abstract List listUserNames();
 
     /**
      * Gets a user by name, ignoring case if specified.
@@ -157,15 +143,6 @@
             throw new RuntimeException("Improper use of deprecated method" 
                                        + " - use addUser(User user)");
         }
-    }
-
-    /**
-     * @see org.apache.james.services.UsersRepository#addUser(java.lang.String, java.lang.String)
-     */
-    public boolean addUser(String username, String password)  {
-        User newbie = new DefaultJamesUser(username, "SHA");
-        newbie.setPassword(password);
-        return addUser(newbie);
     }
 
     /**

Modified: james/server/trunk/src/java/org/apache/james/userrepository/DefaultUsersJdbcRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/userrepository/DefaultUsersJdbcRepository.java?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/src/java/org/apache/james/userrepository/DefaultUsersJdbcRepository.java (original)
+++ james/server/trunk/src/java/org/apache/james/userrepository/DefaultUsersJdbcRepository.java Sat Oct  7 08:58:30 2006
@@ -63,8 +63,8 @@
     {
         DefaultUser defUser = (DefaultUser)user;
         userInsert.setString(1, defUser.getUserName());
-        userInsert.setString(2, defUser.getHashAlgorithm());
-        userInsert.setString(3, defUser.getHashedPassword());
+        userInsert.setString(2, defUser.getHashedPassword());
+        userInsert.setString(3, defUser.getHashAlgorithm());
     }
 
     /**
@@ -75,9 +75,9 @@
         throws SQLException 
     {
         DefaultUser defUser = (DefaultUser)user;
+        userUpdate.setString(1, defUser.getHashedPassword());
+        userUpdate.setString(2, defUser.getHashAlgorithm());
         userUpdate.setString(3, defUser.getUserName());
-        userUpdate.setString(1, defUser.getHashAlgorithm());
-        userUpdate.setString(2, defUser.getHashedPassword());
     }
     
     /**

Modified: james/server/trunk/src/java/org/apache/james/userrepository/ListUsersJdbcRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/userrepository/ListUsersJdbcRepository.java?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/src/java/org/apache/james/userrepository/ListUsersJdbcRepository.java (original)
+++ james/server/trunk/src/java/org/apache/james/userrepository/ListUsersJdbcRepository.java Sat Oct  7 08:58:30 2006
@@ -36,6 +36,15 @@
 public class ListUsersJdbcRepository extends AbstractJdbcUsersRepository
 {
 
+    
+    /**
+     * @see org.apache.james.userrepository.AbstractUsersRepository#test(java.lang.String, java.lang.String)
+     */
+    public boolean test(String name, String password) {
+        // list repository does not store passwords so we always return false!
+        return false;
+    }
+
     /**
      * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#readUserFromResultSet(java.sql.ResultSet)
      */

Modified: james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java (original)
+++ james/server/trunk/src/test/org/apache/james/test/mock/avalon/MockStore.java Sat Oct  7 08:58:30 2006
@@ -43,7 +43,9 @@
     }
 
     private Object get(Object object) {
-        return m_storedObjectMap.get(extractKeyObject(object));
+        Object key = extractKeyObject(object);
+        System.err.println(key);
+        return m_storedObjectMap.get(key);
     }
 
     private Object extractKeyObject(Object object) {

Added: james/server/trunk/src/test/org/apache/james/test/mock/james/MockFileSystem.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/test/mock/james/MockFileSystem.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/james/MockFileSystem.java (added)
+++ james/server/trunk/src/test/org/apache/james/test/mock/james/MockFileSystem.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,43 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.test.mock.james;
+
+import org.apache.james.services.FileSystem;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+
+public class MockFileSystem implements FileSystem {
+    public File getBasedir() throws FileNotFoundException {
+        return new File(".");
+    }
+
+    public File getFile(String fileURL) throws FileNotFoundException {
+        if (fileURL.startsWith("file://")) {
+            if (fileURL.startsWith("file://conf/")) {
+                return new File("./src"+fileURL.substring(6));
+            } else {
+                throw new UnsupportedOperationException("getFile: "+fileURL);
+            }
+        } else {
+            throw new UnsupportedOperationException("getFile: "+fileURL);
+        }
+    }
+}

Propchange: james/server/trunk/src/test/org/apache/james/test/mock/james/MockFileSystem.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/server/trunk/src/test/org/apache/james/test/mock/util/AttrValConfiguration.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/test/mock/util/AttrValConfiguration.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/test/mock/util/AttrValConfiguration.java (added)
+++ james/server/trunk/src/test/org/apache/james/test/mock/util/AttrValConfiguration.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,37 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.test.mock.util;
+
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+
+public class AttrValConfiguration extends DefaultConfiguration {
+    
+    public AttrValConfiguration(String name, String value) {
+        super(name);
+        //setName(name+"1");
+        setValue(value);
+    }
+    
+    public AttrValConfiguration(String name, String attrName, String value) {
+        super(name);
+        addChild(new AttrValConfiguration(attrName,value));
+    }
+    
+}

Propchange: james/server/trunk/src/test/org/apache/james/test/mock/util/AttrValConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/server/trunk/src/test/org/apache/james/userrepository/DefaultUsersJdbcRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/userrepository/DefaultUsersJdbcRepositoryTest.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/userrepository/DefaultUsersJdbcRepositoryTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/userrepository/DefaultUsersJdbcRepositoryTest.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,102 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.avalon.cornerstone.blocks.datasources.DefaultDataSourceSelector;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.logger.ConsoleLogger;
+import org.apache.james.services.UsersRepository;
+import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.test.mock.james.MockFileSystem;
+import org.apache.james.test.mock.util.AttrValConfiguration;
+
+import java.util.Iterator;
+
+/**
+ * Test basic behaviours of UsersFileRepository
+ */
+public class DefaultUsersJdbcRepositoryTest extends MockUsersRepositoryTest {
+
+    /**
+     * Create the repository to be tested.
+     * 
+     * @return the user repository
+     * @throws Exception 
+     */
+    protected UsersRepository getUsersRepository() throws Exception {
+        DefaultUsersJdbcRepository res = new DefaultUsersJdbcRepository();
+        String tableString = "defusers";
+        configureAbstractJdbcUsersRepository(res, tableString);
+        return res;
+    }
+
+    /**
+     * @param res
+     * @param tableString
+     * @throws Exception
+     * @throws ConfigurationException
+     */
+    protected void configureAbstractJdbcUsersRepository(AbstractJdbcUsersRepository res, String tableString) throws Exception, ConfigurationException {
+        res.setFileSystem(new MockFileSystem());
+        DefaultDataSourceSelector dataSourceSelector = new DefaultDataSourceSelector();
+        dataSourceSelector.enableLogging(new MockLogger());
+        DefaultConfiguration dc = new DefaultConfiguration("database-connections");
+        DefaultConfiguration ds = new DefaultConfiguration("data-source");
+        ds.setAttribute("name","maildb");
+        ds.setAttribute("class","org.apache.james.util.dbcp.JdbcDataSource");
+        
+        ds.addChild(new AttrValConfiguration("driver","org.apache.derby.jdbc.EmbeddedDriver"));
+        ds.addChild(new AttrValConfiguration("dburl","jdbc:derby:testdb;create=true"));
+        ds.addChild(new AttrValConfiguration("user","james"));
+        ds.addChild(new AttrValConfiguration("password","james"));
+
+        ds.addChild(new AttrValConfiguration("max","20"));
+        dc.addChild(ds);
+        dataSourceSelector.configure(dc);
+        dataSourceSelector.initialize();  
+        
+        res.setDatasources(dataSourceSelector );
+        
+        DefaultConfiguration configuration = new DefaultConfiguration("test");
+        configuration.setAttribute("destinationURL", "db://maildb/"+tableString);
+        configuration.addChild(new AttrValConfiguration("sqlFile","file://conf/sqlResources.xml"));
+        res.enableLogging(new ConsoleLogger());
+        res.configure(configuration );
+        res.initialize();
+    }
+
+    /**
+     * @return
+     */
+    protected boolean getCheckCase() {
+        return false;
+    }
+
+    protected void disposeUsersRepository() {
+        Iterator i = this.usersRepository.list();
+        while (i.hasNext()) {
+            this.usersRepository.removeUser((String) i.next());
+        }
+        ContainerUtil.dispose(this.usersRepository);
+    }
+
+}

Propchange: james/server/trunk/src/test/org/apache/james/userrepository/DefaultUsersJdbcRepositoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/server/trunk/src/test/org/apache/james/userrepository/JamesUsersJdbcRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/userrepository/JamesUsersJdbcRepositoryTest.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/userrepository/JamesUsersJdbcRepositoryTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/userrepository/JamesUsersJdbcRepositoryTest.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,107 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.avalon.cornerstone.blocks.datasources.DefaultDataSourceSelector;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.logger.ConsoleLogger;
+import org.apache.james.services.UsersRepository;
+import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.test.mock.james.MockFileSystem;
+import org.apache.james.test.mock.util.AttrValConfiguration;
+
+import java.util.Iterator;
+
+/**
+ * Test basic behaviours of UsersFileRepository
+ */
+public class JamesUsersJdbcRepositoryTest extends MockUsersRepositoryTest {
+
+    /**
+     * Create the repository to be tested.
+     * 
+     * @return the user repository
+     * @throws Exception 
+     */
+    protected UsersRepository getUsersRepository() throws Exception {
+        JamesUsersJdbcRepository res = new JamesUsersJdbcRepository();
+        String tableString = "jamesusers";
+        configureAbstractJdbcUsersRepository(res, tableString);
+        return res;
+    }
+
+    
+    protected boolean getAllowMultipleUsersWithDifferentCases() {
+        return false;
+    }
+
+    /**
+     * @param res
+     * @param tableString
+     * @throws Exception
+     * @throws ConfigurationException
+     */
+    protected void configureAbstractJdbcUsersRepository(AbstractJdbcUsersRepository res, String tableString) throws Exception, ConfigurationException {
+        res.setFileSystem(new MockFileSystem());
+        DefaultDataSourceSelector dataSourceSelector = new DefaultDataSourceSelector();
+        dataSourceSelector.enableLogging(new MockLogger());
+        DefaultConfiguration dc = new DefaultConfiguration("database-connections");
+        DefaultConfiguration ds = new DefaultConfiguration("data-source");
+        ds.setAttribute("name","maildb");
+        ds.setAttribute("class","org.apache.james.util.dbcp.JdbcDataSource");
+        
+        ds.addChild(new AttrValConfiguration("driver","org.apache.derby.jdbc.EmbeddedDriver"));
+        ds.addChild(new AttrValConfiguration("dburl","jdbc:derby:testdb;create=true"));
+        ds.addChild(new AttrValConfiguration("user","james"));
+        ds.addChild(new AttrValConfiguration("password","james"));
+
+        ds.addChild(new AttrValConfiguration("max","20"));
+        dc.addChild(ds);
+        dataSourceSelector.configure(dc);
+        dataSourceSelector.initialize();  
+        
+        res.setDatasources(dataSourceSelector );
+        
+        DefaultConfiguration configuration = new DefaultConfiguration("test");
+        configuration.setAttribute("destinationURL", "db://maildb/"+tableString);
+        configuration.addChild(new AttrValConfiguration("sqlFile","file://conf/sqlResources.xml"));
+        res.enableLogging(new ConsoleLogger());
+        res.configure(configuration );
+        res.initialize();
+    }
+
+    /**
+     * @return
+     */
+    protected boolean getCheckCase() {
+        return true;
+    }
+
+    protected void disposeUsersRepository() {
+        Iterator i = this.usersRepository.list();
+        while (i.hasNext()) {
+            this.usersRepository.removeUser((String) i.next());
+        }
+        ContainerUtil.dispose(this.usersRepository);
+    }
+
+}

Propchange: james/server/trunk/src/test/org/apache/james/userrepository/JamesUsersJdbcRepositoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/server/trunk/src/test/org/apache/james/userrepository/ListUsersJdbcRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/userrepository/ListUsersJdbcRepositoryTest.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/userrepository/ListUsersJdbcRepositoryTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/userrepository/ListUsersJdbcRepositoryTest.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,104 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.avalon.cornerstone.blocks.datasources.DefaultDataSourceSelector;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.logger.ConsoleLogger;
+import org.apache.james.services.UsersRepository;
+import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.test.mock.james.MockFileSystem;
+import org.apache.james.test.mock.util.AttrValConfiguration;
+
+import java.util.Iterator;
+
+/**
+ * Test basic behaviours of UsersFileRepository
+ */
+public class ListUsersJdbcRepositoryTest extends MockUsersRepositoryTest {
+
+    /**
+     * Create the repository to be tested.
+     * 
+     * @return the user repository
+     * @throws Exception 
+     */
+    protected UsersRepository getUsersRepository() throws Exception {
+        ListUsersJdbcRepository res = new ListUsersJdbcRepository();
+        String tableString = "listusers";
+        configureAbstractJdbcUsersRepository(res, tableString);
+        return res;
+    }
+
+    protected boolean getPasswordsEnabled() {
+        return false;
+    }
+
+    
+    protected boolean getCheckCase() {
+        return false;
+    }
+
+    /**
+     * @param res
+     * @param tableString
+     * @throws Exception
+     * @throws ConfigurationException
+     */
+    protected void configureAbstractJdbcUsersRepository(AbstractJdbcUsersRepository res, String tableString) throws Exception, ConfigurationException {
+        res.setFileSystem(new MockFileSystem());
+        DefaultDataSourceSelector dataSourceSelector = new DefaultDataSourceSelector();
+        dataSourceSelector.enableLogging(new MockLogger());
+        DefaultConfiguration dc = new DefaultConfiguration("database-connections");
+        DefaultConfiguration ds = new DefaultConfiguration("data-source");
+        ds.setAttribute("name","maildb");
+        ds.setAttribute("class","org.apache.james.util.dbcp.JdbcDataSource");
+        
+        ds.addChild(new AttrValConfiguration("driver","org.apache.derby.jdbc.EmbeddedDriver"));
+        ds.addChild(new AttrValConfiguration("dburl","jdbc:derby:testdb;create=true"));
+        ds.addChild(new AttrValConfiguration("user","james"));
+        ds.addChild(new AttrValConfiguration("password","james"));
+
+        ds.addChild(new AttrValConfiguration("max","20"));
+        dc.addChild(ds);
+        dataSourceSelector.configure(dc);
+        dataSourceSelector.initialize();  
+        
+        res.setDatasources(dataSourceSelector );
+        
+        DefaultConfiguration configuration = new DefaultConfiguration("test");
+        configuration.setAttribute("destinationURL", "db://maildb/"+tableString);
+        configuration.addChild(new AttrValConfiguration("sqlFile","file://conf/sqlResources.xml"));
+        res.enableLogging(new ConsoleLogger());
+        res.configure(configuration );
+        res.initialize();
+    }
+
+    protected void disposeUsersRepository() {
+        Iterator i = this.usersRepository.list();
+        while (i.hasNext()) {
+            this.usersRepository.removeUser((String) i.next());
+        }
+        ContainerUtil.dispose(this.usersRepository);
+    }
+
+}

Propchange: james/server/trunk/src/test/org/apache/james/userrepository/ListUsersJdbcRepositoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepository.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepository.java?view=diff&rev=453943&r1=453942&r2=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepository.java (original)
+++ james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepository.java Sat Oct  7 08:58:30 2006
@@ -62,6 +62,9 @@
     }
 
     public void addUser(String name, Object attributes) {
+        if (!(attributes instanceof String)) {
+            throw new IllegalArgumentException();
+        }
         try {
             String passwordHash = DigestUtil.digestString(((String) attributes), "SHA");
 
@@ -116,7 +119,10 @@
     }
 
     public boolean updateUser(User user) {
-        return false;  // trivial implementation
+        if (m_users.containsKey(user.getUserName())) {
+            m_users.put(user.getUserName(), user);
+            return true;
+        } else return false;  // trivial implementation
     }
 
     public void removeUser(String name) {
@@ -128,11 +134,11 @@
     }
 
     public boolean containsCaseInsensitive(String name) {
-        return false;  // trivial implementation
+        throw new UnsupportedOperationException("mock");
     }
 
     public boolean test(String name, Object attributes) {
-        return false;  // trivial implementation
+        throw new UnsupportedOperationException("mock");
     }
 
     public boolean test(String name, String password) {

Added: james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepositoryTest.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepositoryTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepositoryTest.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,223 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.avalon.framework.container.ContainerUtil;
+import org.apache.james.services.User;
+import org.apache.james.services.UsersRepository;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+/**
+ * Test basic behaviours of UsersFileRepository
+ */
+public class MockUsersRepositoryTest extends TestCase {
+
+    /**
+     * Users repository
+     */
+    protected UsersRepository usersRepository;
+
+    /**
+     * Create the repository to be tested.
+     * 
+     * @return the user repository
+     * @throws Exception 
+     */
+    protected UsersRepository getUsersRepository() throws Exception {
+        return new MockUsersRepository();
+    }
+
+    public void testUsersRepositoryEmpty() {
+        assertEquals("users repository not empty", 0, usersRepository.countUsers());
+        assertFalse("users repository not empty", usersRepository.list().hasNext());
+    }
+    
+    public void testAddUserOnce() {
+        boolean res = usersRepository.addUser("username", "password");
+        assertTrue("User not added", res);
+        res = usersRepository.addUser("username", "password2");
+        assertFalse("User added twice!", res);
+        try {
+            usersRepository.addUser("username2", (Object) "password2");
+            assertTrue(usersRepository.contains("username2"));
+            User u = new DefaultJamesUser("username3","SHA","password3");
+            usersRepository.addUser(u);
+            assertTrue(usersRepository.contains("username3"));
+        } catch (UnsupportedOperationException e) {
+            
+        }
+        
+        try {
+            usersRepository.addUser("username2", new Object());
+            fail("adduser should throw an exception if a non string is passed");
+        } catch (Exception e) {
+            
+        }
+        
+    }
+    
+    public void testUserAddedIsFound() {
+        boolean res = usersRepository.addUser("username", "password");
+        assertTrue("User not added", res);
+        User user = usersRepository.getUserByName("username");
+        assertNotNull(user);
+        assertEquals("username does not match", user.getUserName(), "username");
+        assertTrue("user not contained in the repository", usersRepository.contains("username"));
+        try {
+            assertTrue("case insensitive user not found in the repository", usersRepository.containsCaseInsensitive("userName"));
+        } catch (UnsupportedOperationException e) {
+            // some implementation could not support deprecated methods
+        }
+        
+        User u = usersRepository.getUserByName("uSERNAMe");
+        assertNull("found the user searching for a different case!", u);
+        
+        String realname = usersRepository.getRealName("uSERNAMe");
+        assertEquals("name is different", "username", realname);
+    }
+    
+    public void testUserListing() {
+        ArrayList keys = new ArrayList(3);
+        keys.add("username1");
+        keys.add("username2");
+        keys.add("username3");
+        for (Iterator i = keys.iterator(); i.hasNext(); ) {
+            String username = (String) i.next();
+            boolean res = usersRepository.addUser(username, username);
+            assertTrue("User "+username+" not added", res);
+        }
+        assertEquals("Wrong number of users found", keys.size(), usersRepository.countUsers());
+
+        // check list return all and only the expected users
+        ArrayList check = new ArrayList(keys);
+        for (Iterator i = usersRepository.list(); i.hasNext(); ) {
+            String username = (String) i.next();
+            if (getPasswordsEnabled()) {
+                assertTrue(usersRepository.test(username, username));
+                User u = usersRepository.getUserByName(username);
+                u.setPassword("newpass");
+                assertTrue(usersRepository.updateUser(u));
+            }
+            assertTrue(check.contains(username));
+            check.remove(username);
+        }
+        assertEquals("Some user has not be found", 0, check.size());
+    }
+    
+    public void testUserPassword() {
+        assertTrue("user not added", usersRepository.addUser("username","password"));
+        assertEquals("didn't accept the correct password ", usersRepository.test("username", "password"), getPasswordsEnabled());
+        assertFalse("accepted the wrong password #1", usersRepository.test("username", "password2"));
+        assertFalse("accepted the wrong password #2", usersRepository.test("username2", "password"));
+        assertFalse("accepted the wrong password #3", usersRepository.test("username", "Password"));
+        assertFalse("accepted the wrong password #4", usersRepository.test("username", "passwords"));
+        assertFalse("accepted the wrong password #5", usersRepository.test("userName", "password"));
+    }
+    
+    protected boolean getPasswordsEnabled() {
+        return true;
+    }
+
+    public void testUserAddRemoveCycle() {
+        assertFalse("accepted login when no user existed", usersRepository.test("username", "password"));
+        try {
+            usersRepository.removeUser("username");
+            // UsersFileRepository accept this call for every argument
+            // fail("removing an unknown user didn't fail!");
+        } catch (Exception e) {
+            
+        }
+        assertTrue("user not added", usersRepository.addUser("username","password"));
+        assertEquals("didn't accept the correct password", usersRepository.test("username", "password"),getPasswordsEnabled());
+        User user = usersRepository.getUserByName("username");
+        user.setPassword("newpass");
+        try {
+            assertTrue("user not updated", usersRepository.updateUser(user));
+            assertEquals("new password accepted", usersRepository.test("username", "newpass"), getPasswordsEnabled());
+            assertFalse("old password rejected", usersRepository.test("username", "password"));
+        } catch (UnsupportedOperationException e) {
+            // if updating users is not allowed check that this is a repository without password checking
+            assertFalse(getPasswordsEnabled());
+        }
+        try {
+            usersRepository.removeUser("username");
+        } catch (Exception e) {
+            fail("removing the user failed!");
+        }
+        assertFalse("user not existing", usersRepository.contains("username"));
+        assertFalse("new password rejected", usersRepository.test("username", "newpass"));
+        assertFalse("updated a non existing user: should fail!", usersRepository.updateUser(user));
+    }
+    
+    
+    public void testCaseInsensitivesMethods() {
+        assertTrue("User not added", usersRepository.addUser("userName", "password"));
+        try {
+            assertTrue(usersRepository.containsCaseInsensitive("usERname"));
+            assertNotNull(usersRepository.getUserByNameCaseInsensitive("userNAMe"));
+        } catch (UnsupportedOperationException e) {
+            // some implementations do not support it.
+        }
+        assertEquals(usersRepository.addUser("USERNAME", "password"), getAllowMultipleUsersWithDifferentCases());
+        
+    }
+    
+    
+    /**
+     * @return
+     */
+    protected boolean getCheckCase() {
+        return true;
+    }
+    
+    protected boolean getAllowMultipleUsersWithDifferentCases() {
+        return getCheckCase();
+    }
+
+
+    /**
+     * @see junit.framework.TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+        this.usersRepository = getUsersRepository();
+    }
+
+    /**
+     * @see junit.framework.TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception {
+        disposeUsersRepository();
+        super.tearDown();
+    }
+
+
+    /**
+     * Dispose the repository
+     */
+    protected void disposeUsersRepository() {
+        ContainerUtil.dispose(this.usersRepository);
+    }
+
+}

Propchange: james/server/trunk/src/test/org/apache/james/userrepository/MockUsersRepositoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/server/trunk/src/test/org/apache/james/userrepository/UsersFileRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/userrepository/UsersFileRepositoryTest.java?view=auto&rev=453943
==============================================================================
--- james/server/trunk/src/test/org/apache/james/userrepository/UsersFileRepositoryTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/userrepository/UsersFileRepositoryTest.java Sat Oct  7 08:58:30 2006
@@ -0,0 +1,89 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.logger.ConsoleLogger;
+import org.apache.avalon.framework.service.DefaultServiceManager;
+import org.apache.james.mailrepository.filepair.File_Persistent_Object_Repository;
+import org.apache.james.services.FileSystem;
+import org.apache.james.services.UsersRepository;
+import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.test.mock.avalon.MockStore;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Iterator;
+
+/**
+ * Test basic behaviours of UsersFileRepository
+ */
+public class UsersFileRepositoryTest extends MockUsersRepositoryTest {
+
+    /**
+     * Create the repository to be tested.
+     * 
+     * @return the user repository
+     * @throws Exception 
+     */
+    protected UsersRepository getUsersRepository() throws Exception {
+        UsersFileRepository res = new UsersFileRepository();
+        DefaultServiceManager serviceManager = new DefaultServiceManager();
+        serviceManager.put(FileSystem.ROLE, new FileSystem() {
+
+            public File getBasedir() throws FileNotFoundException {
+                return new File(".");
+            }
+
+            public File getFile(String fileURL) throws FileNotFoundException {
+                throw new UnsupportedOperationException();
+            }
+            
+        });
+        MockStore mockStore = new MockStore();
+        File_Persistent_Object_Repository file_Persistent_Object_Repository = new File_Persistent_Object_Repository();
+        file_Persistent_Object_Repository.service(serviceManager);
+        file_Persistent_Object_Repository.enableLogging(new MockLogger());
+        DefaultConfiguration defaultConfiguration22 = new DefaultConfiguration("conf");
+        defaultConfiguration22.setAttribute("destinationURL", "file://var/users"+File.separator);
+        file_Persistent_Object_Repository.configure(defaultConfiguration22);
+        file_Persistent_Object_Repository.initialize();
+        mockStore.add("OBJECT.users"+File.separator, file_Persistent_Object_Repository);
+        res.setStore(mockStore);
+        DefaultConfiguration configuration = new DefaultConfiguration("test");
+        DefaultConfiguration destinationConf = new DefaultConfiguration("destination");
+        destinationConf.setAttribute("URL", "file://var/users"+File.separator);
+        configuration.addChild(destinationConf);
+        res.enableLogging(new ConsoleLogger());
+        res.configure(configuration );
+        res.initialize();
+        return res;
+    }
+
+    protected void disposeUsersRepository() {
+        Iterator i = this.usersRepository.list();
+        while (i.hasNext()) {
+            this.usersRepository.removeUser((String) i.next());
+        }
+        ContainerUtil.dispose(this.usersRepository);
+    }
+
+}

Propchange: james/server/trunk/src/test/org/apache/james/userrepository/UsersFileRepositoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org