/****************************************************************
 * 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.user.jdbc;

import java.util.Collection;
import java.util.Iterator;

import javax.sql.DataSource;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.derby.jdbc.EmbeddedDriver;
import org.apache.james.filesystem.api.mock.MockFileSystem;
import org.apache.james.lifecycle.api.LifecycleUtil;
import org.apache.james.rrt.api.RecipientRewriteTable;
import org.apache.james.user.api.UsersRepository;
import org.apache.james.user.api.UsersRepositoryException;
import org.apache.james.user.api.model.JamesUser;
import org.apache.mailet.MailAddress;
import org.slf4j.LoggerFactory;

/**
 * Test basic behaviors of UsersFileRepository
 */
public class JamesUsersJdbcRepositoryTest extends AbstractUsersJdbcRepositoryTest {

    /**
     * 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;
    }

    /**
     * @param res
     * @param tableString
     * @throws Exception
     * @throws ConfigurationException
     */
    protected void configureAbstractJdbcUsersRepository(AbstractJdbcUsersRepository res, String tableString) throws Exception, ConfigurationException {
        res.setFileSystem(new MockFileSystem());
        DataSource dataSource = getDataSource();
        res.setDatasource(dataSource);
        DefaultConfigurationBuilder configuration = new DefaultConfigurationBuilder();
        configuration.addProperty("[@destinationURL]", "db://maildb/" + tableString);
        configuration.addProperty("sqlFile", "file://conf/sqlResources.xml");
        res.setLog(LoggerFactory.getLogger("MockLog"));
        res.configure(configuration);
        res.init();
    }

    private BasicDataSource getDataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(EmbeddedDriver.class.getName());
        ds.setUrl("jdbc:derby:target/testdb;create=true");
        ds.setUsername("james");
        ds.setPassword("james");
        return ds;
    }

    /**
     * @return
     */
    protected boolean getCheckCase() {
        return true;
    }

    protected void disposeUsersRepository() throws UsersRepositoryException {
        Iterator<String> i = this.usersRepository.list();
        while (i.hasNext()) {
            this.usersRepository.removeUser((String) i.next());
        }
        LifecycleUtil.dispose(this.usersRepository);
    }

    public void testRecipientRewriteTableImpl() throws Exception {
        String username = "test";
        String password = "pass";
        String alias = "alias";
        String domain = "localhost";
        String forward = "forward@somewhere";

        JamesUsersJdbcRepository repos = (JamesUsersJdbcRepository) getUsersRepository();
        repos.setEnableAliases(true);
        repos.setEnableForwarding(true);
        repos.addUser(username, password);

        JamesUser user = (JamesUser) repos.getUserByName(username);
        user.setAlias(alias);
        repos.updateUser(user);

        Collection<String> map = ((RecipientRewriteTable) repos).getMappings(username, domain);
        assertNull("No mapping", map);

        user.setAliasing(true);
        repos.updateUser(user);
        map = ((RecipientRewriteTable) repos).getMappings(username, domain);
        assertEquals("One mapping", 1, map.size());
        assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain);

        user.setForwardingDestination(new MailAddress(forward));
        repos.updateUser(user);
        map = ((RecipientRewriteTable) repos).getMappings(username, domain);
        assertTrue("One mapping", map.size() == 1);
        assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain);

        user.setForwarding(true);
        repos.updateUser(user);
        map = ((RecipientRewriteTable) repos).getMappings(username, domain);
        Iterator<String> mappings = map.iterator();
        assertTrue("Two mapping", map.size() == 2);
        assertEquals("Alias found", mappings.next().toString(), alias + "@" + domain);
        assertEquals("Forward found", mappings.next().toString(), forward);
    }

}

@@ -1224,37 +1224,8 @@
 c;%0A%0A
-import java.util.Collection;%0A
 impo
@@ -1247,16 +1247,16 @@
 erator;%0A
+
 %0Aimport 
@@ -1621,63 +1621,8 @@
 il;%0A
-import org.apache.james.rrt.api.RecipientRewriteTable;%0A
 impo
@@ -1730,96 +1730,8 @@
 on;%0A
-import org.apache.james.user.api.model.JamesUser;%0Aimport org.apache.mailet.MailAddress;%0A
 impo
@@ -3735,16 +3735,16 @@
       %7D%0A
+
         
@@ -3799,1761 +3799,6 @@
  %7D%0A%0A
-    public void testRecipientRewriteTableImpl() throws Exception %7B%0A        String username = %22test%22;%0A        String password = %22pass%22;%0A        String alias = %22alias%22;%0A        String domain = %22localhost%22;%0A        String forward = %22forward@somewhere%22;%0A%0A        JamesUsersJdbcRepository repos = (JamesUsersJdbcRepository) getUsersRepository();%0A        repos.setEnableAliases(true);%0A        repos.setEnableForwarding(true);%0A        repos.addUser(username, password);%0A%0A        JamesUser user = (JamesUser) repos.getUserByName(username);%0A        user.setAlias(alias);%0A        repos.updateUser(user);%0A%0A        Collection%3CString%3E map = ((RecipientRewriteTable) repos).getMappings(username, domain);%0A        assertNull(%22No mapping%22, map);%0A%0A        user.setAliasing(true);%0A        repos.updateUser(user);%0A        map = ((RecipientRewriteTable) repos).getMappings(username, domain);%0A        assertEquals(%22One mapping%22, 1, map.size());%0A        assertEquals(%22Alias found%22, map.iterator().next().toString(), alias + %22@%22 + domain);%0A%0A        user.setForwardingDestination(new MailAddress(forward));%0A        repos.updateUser(user);%0A        map = ((RecipientRewriteTable) repos).getMappings(username, domain);%0A        assertTrue(%22One mapping%22, map.size() == 1);%0A        assertEquals(%22Alias found%22, map.iterator().next().toString(), alias + %22@%22 + domain);%0A%0A        user.setForwarding(true);%0A        repos.updateUser(user);%0A        map = ((RecipientRewriteTable) repos).getMappings(username, domain);%0A        Iterator%3CString%3E mappings = map.iterator();%0A        assertTrue(%22Two mapping%22, map.size() == 2);%0A        assertEquals(%22Alias found%22, mappings.next().toString(), alias + %22@%22 + domain);%0A        assertEquals(%22Forward found%22, mappings.next().toString(), forward);%0A    %7D%0A%0A
 %7D%0A

Remove test method related to virtual user table, now RecipientRewriteTable with its own tests