1 19 20 package org.apache.james.userrepository; 21 22 import org.apache.avalon.framework.configuration.ConfigurationException; 23 import org.apache.avalon.framework.configuration.DefaultConfiguration; 24 import org.apache.avalon.framework.container.ContainerUtil; 25 import org.apache.avalon.framework.logger.ConsoleLogger; 26 import org.apache.james.services.JamesUser; 27 import org.apache.james.services.UsersRepository; 28 import org.apache.james.services.VirtualUserTable; 29 import org.apache.james.test.mock.james.MockFileSystem; 30 import org.apache.james.test.mock.util.AttrValConfiguration; 31 import org.apache.james.test.util.Util; 32 import org.apache.mailet.MailAddress; 33 34 import java.util.Collection ; 35 import java.util.Iterator ; 36 37 40 public class JamesUsersJdbcRepositoryTest extends MockUsersRepositoryTest { 41 42 48 protected UsersRepository getUsersRepository() throws Exception { 49 JamesUsersJdbcRepository res = new JamesUsersJdbcRepository(); 50 String tableString = "jamesusers"; 51 configureAbstractJdbcUsersRepository(res, tableString); 52 return res; 53 } 54 55 61 protected void configureAbstractJdbcUsersRepository(AbstractJdbcUsersRepository res, String tableString) throws Exception , ConfigurationException { 62 res.setFileSystem(new MockFileSystem()); 63 res.setDatasources(Util.getDataSourceSelector()); 64 65 DefaultConfiguration configuration = new DefaultConfiguration("test"); 66 configuration.setAttribute("destinationURL", "db://maildb/"+tableString); 67 configuration.addChild(new AttrValConfiguration("sqlFile","file://conf/sqlResources.xml")); 68 res.enableLogging(new ConsoleLogger()); 69 res.configure(configuration ); 70 res.initialize(); 71 } 72 73 76 protected boolean getCheckCase() { 77 return true; 78 } 79 80 protected void disposeUsersRepository() { 81 Iterator i = this.usersRepository.list(); 82 while (i.hasNext()) { 83 this.usersRepository.removeUser((String ) i.next()); 84 } 85 ContainerUtil.dispose(this.usersRepository); 86 } 87 88 89 public void testVirtualUserTableImpl() throws Exception { 90 String username = "test"; 91 String password = "pass"; 92 String alias = "alias"; 93 String domain = "localhost"; 94 String forward = "forward@somewhere"; 95 96 JamesUsersJdbcRepository repos = (JamesUsersJdbcRepository) getUsersRepository(); 97 repos.setEnableAliases(true); 98 repos.setEnableForwarding(true); 99 repos.addUser(username,password); 100 101 JamesUser user = (JamesUser)repos.getUserByName(username); 102 user.setAlias(alias); 103 repos.updateUser(user); 104 105 Collection map = ((VirtualUserTable) repos).getMappings(username, domain); 106 assertNull("No mapping", map); 107 108 user.setAliasing(true); 109 repos.updateUser(user); 110 map = ((VirtualUserTable) repos).getMappings(username, domain); 111 assertEquals("One mapping", 1, map.size()); 112 assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain); 113 114 115 user.setForwardingDestination(new MailAddress(forward)); 116 repos.updateUser(user); 117 map = ((VirtualUserTable) repos).getMappings(username, domain); 118 assertTrue("One mapping", map.size() == 1); 119 assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain); 120 121 122 user.setForwarding(true); 123 repos.updateUser(user); 124 map = ((VirtualUserTable) repos).getMappings(username, domain); 125 Iterator mappings = map.iterator(); 126 assertTrue("Two mapping",map.size() == 2); 127 assertEquals("Alias found", mappings.next().toString(), alias + "@" + domain); 128 assertEquals("Forward found", mappings.next().toString(), forward); 129 } 130 131 132 } 133 | Popular Tags |