1 19 20 package org.apache.james.userrepository; 21 22 import org.apache.avalon.framework.configuration.DefaultConfiguration; 23 import org.apache.avalon.framework.container.ContainerUtil; 24 import org.apache.avalon.framework.logger.ConsoleLogger; 25 import org.apache.avalon.framework.service.DefaultServiceManager; 26 import org.apache.james.mailrepository.filepair.File_Persistent_Object_Repository; 27 import org.apache.james.services.FileSystem; 28 import org.apache.james.services.JamesUser; 29 import org.apache.james.services.UsersRepository; 30 import org.apache.james.services.VirtualUserTable; 31 import org.apache.james.test.mock.avalon.MockLogger; 32 import org.apache.james.test.mock.avalon.MockStore; 33 import org.apache.mailet.MailAddress; 34 35 import java.io.File ; 36 import java.io.FileNotFoundException ; 37 import java.util.Collection ; 38 import java.util.Iterator ; 39 40 43 public class UsersFileRepositoryTest extends MockUsersRepositoryTest { 44 45 51 protected UsersRepository getUsersRepository() throws Exception { 52 UsersFileRepository res = new UsersFileRepository(); 53 DefaultServiceManager serviceManager = new DefaultServiceManager(); 54 serviceManager.put(FileSystem.ROLE, new FileSystem() { 55 56 public File getBasedir() throws FileNotFoundException { 57 return new File ("."); 58 } 59 60 public File getFile(String fileURL) throws FileNotFoundException { 61 throw new UnsupportedOperationException (); 62 } 63 64 }); 65 MockStore mockStore = new MockStore(); 66 File_Persistent_Object_Repository file_Persistent_Object_Repository = new File_Persistent_Object_Repository(); 67 file_Persistent_Object_Repository.service(serviceManager); 68 file_Persistent_Object_Repository.enableLogging(new MockLogger()); 69 DefaultConfiguration defaultConfiguration22 = new DefaultConfiguration("conf"); 70 defaultConfiguration22.setAttribute("destinationURL", "file://target/var/users"); 71 file_Persistent_Object_Repository.configure(defaultConfiguration22); 72 file_Persistent_Object_Repository.initialize(); 73 mockStore.add("OBJECT.users", file_Persistent_Object_Repository); 74 res.setStore(mockStore); 75 DefaultConfiguration configuration = new DefaultConfiguration("test"); 76 DefaultConfiguration destinationConf = new DefaultConfiguration("destination"); 77 destinationConf.setAttribute("URL", "file://target/var/users"); 78 configuration.addChild(destinationConf); 79 res.enableLogging(new ConsoleLogger()); 80 res.configure(configuration ); 81 res.initialize(); 82 return res; 83 } 84 85 protected void disposeUsersRepository() { 86 Iterator i = this.usersRepository.list(); 87 while (i.hasNext()) { 88 this.usersRepository.removeUser((String ) i.next()); 89 } 90 ContainerUtil.dispose(this.usersRepository); 91 } 92 93 public void testVirtualUserTableImpl() throws Exception { 94 String username = "test"; 95 String password = "pass"; 96 String alias = "alias"; 97 String domain = "localhost"; 98 String forward = "forward@somewhere"; 99 100 UsersFileRepository repos = (UsersFileRepository) getUsersRepository(); 101 repos.setEnableAliases(true); 102 repos.setEnableForwarding(true); 103 repos.addUser(username,password); 104 105 JamesUser user = (JamesUser)repos.getUserByName(username); 106 user.setAlias(alias); 107 repos.updateUser(user); 108 109 Collection map = ((VirtualUserTable) repos).getMappings(username, domain); 110 assertNull("No mapping", map); 111 112 user.setAliasing(true); 113 repos.updateUser(user); 114 map = ((VirtualUserTable) repos).getMappings(username, domain); 115 assertEquals("One mapping", 1, map.size()); 116 assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain); 117 118 119 user.setForwardingDestination(new MailAddress(forward)); 120 repos.updateUser(user); 121 map = ((VirtualUserTable) repos).getMappings(username, domain); 122 assertTrue("One mapping", map.size() == 1); 123 assertEquals("Alias found", map.iterator().next().toString(), alias + "@" + domain); 124 125 126 user.setForwarding(true); 127 repos.updateUser(user); 128 map = ((VirtualUserTable) repos).getMappings(username, domain); 129 Iterator mappings = map.iterator(); 130 assertTrue("Two mapping",map.size() == 2); 131 assertEquals("Alias found", mappings.next().toString(), alias + "@" + domain); 132 assertEquals("Forward found", mappings.next().toString(), forward); 133 } 134 135 136 } 137 | Popular Tags |