1 19 20 21 22 package org.apache.james; 23 24 import org.apache.avalon.cornerstone.services.store.Store; 25 import org.apache.avalon.framework.container.ContainerUtil; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.james.services.AbstractDNSServer; 28 import org.apache.james.services.DNSServer; 29 import org.apache.james.services.DomainList; 30 import org.apache.james.services.FileSystem; 31 import org.apache.james.services.MailServer; 32 import org.apache.james.services.MailServerTestAllImplementations; 33 import org.apache.james.services.UsersRepository; 34 import org.apache.james.services.UsersStore; 35 import org.apache.james.test.mock.avalon.MockLogger; 36 import org.apache.james.test.mock.avalon.MockServiceManager; 37 import org.apache.james.test.mock.avalon.MockStore; 38 import org.apache.james.test.mock.james.InMemorySpoolRepository; 39 import org.apache.james.test.mock.james.MockUsersStore; 40 import org.apache.james.userrepository.MockUsersRepository; 41 42 import java.io.File ; 43 import java.io.FileNotFoundException ; 44 import java.net.InetAddress ; 45 import java.net.UnknownHostException ; 46 import java.util.ArrayList ; 47 import java.util.List ; 48 49 public class JamesTest extends MailServerTestAllImplementations { 50 51 private File tempContextFile = null; 52 private InMemorySpoolRepository mockMailRepository; 53 54 public MailServer createMailServer() throws ServiceException { 55 James james = new James(); 56 james.service(setUpServiceManager()); 57 MockLogger mockLogger = new MockLogger(); 58 mockLogger.disableDebug(); 59 ContainerUtil.enableLogging(james, mockLogger); 60 try { 61 JamesTestConfiguration conf = new JamesTestConfiguration(); 62 conf.init(); 63 ContainerUtil.configure(james, conf); 64 ContainerUtil.initialize(james); 65 } catch (Exception e) { 66 e.printStackTrace(); 67 fail("James.initialize() failed"); 68 } 69 return james; 70 } 71 72 73 74 protected void tearDown() throws Exception { 75 if (tempContextFile != null) { 76 tempContextFile.delete(); 77 } 78 if (mockMailRepository != null) { 79 ContainerUtil.dispose(mockMailRepository); 80 } 81 super.tearDown(); 82 } 83 84 85 86 private MockServiceManager setUpServiceManager() { 87 MockServiceManager serviceManager = new MockServiceManager(); 88 MockUsersRepository mockUsersRepository = new MockUsersRepository(); 89 serviceManager.put(UsersRepository.ROLE, mockUsersRepository); 90 serviceManager.put(UsersStore.ROLE, new MockUsersStore(mockUsersRepository)); 91 serviceManager.put(FileSystem.ROLE, new FileSystem() { 92 93 public File getBasedir() throws FileNotFoundException { 94 return new File ("."); 95 } 96 97 public File getFile(String fileURL) throws FileNotFoundException { 98 return new File ("./conf/"); 99 } 100 101 }); 102 serviceManager.put(DomainList.ROLE, new DomainList() { 103 104 public List getDomains() { 105 ArrayList d = new ArrayList (); 106 d.add("localhost"); 107 return d; 108 } 109 110 public boolean containsDomain(String domain) { 111 return getDomains().contains(domain); 112 } 113 114 public void setAutoDetect(boolean autodetect) {} 115 116 public void setAutoDetectIP(boolean autodetectIP) {} 117 }); 118 MockStore mockStore = new MockStore(); 119 mockMailRepository = new InMemorySpoolRepository(); 120 mockStore.add(EXISTING_USER_NAME, mockMailRepository); 121 serviceManager.put(Store.ROLE, mockStore); 122 serviceManager.put(DNSServer.ROLE, setUpDNSServer()); 123 return serviceManager; 124 } 125 126 private DNSServer setUpDNSServer() { 127 DNSServer dns = new AbstractDNSServer() { 128 public String getHostName(InetAddress addr) { 129 return "localhost"; 130 } 131 132 public InetAddress getLocalHost() throws UnknownHostException { 133 throw new UnknownHostException ("Unknown"); 134 } 135 }; 136 return dns; 137 } 138 139 public boolean allowsPasswordlessUser() { 140 return false; 141 } 142 143 public boolean canTestUserExists() { 144 return true; 145 } 146 147 public boolean isUserExisting(MailServer mailServerImpl, String username) { 148 return ((James)mailServerImpl).isLocalUser(username); 149 } 150 } 151 | Popular Tags |