1 19 20 21 22 23 package org.apache.james.domain; 24 25 import java.net.InetAddress ; 26 import java.net.UnknownHostException ; 27 import java.util.ArrayList ; 28 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.DefaultConfiguration; 31 import org.apache.avalon.framework.container.ContainerUtil; 32 import org.apache.james.services.AbstractDNSServer; 33 import org.apache.james.services.DNSServer; 34 import org.apache.james.services.ManageableDomainList; 35 import org.apache.james.test.mock.avalon.MockLogger; 36 import org.apache.james.test.mock.avalon.MockServiceManager; 37 38 import junit.framework.TestCase; 39 40 public class XMLDomainListTest extends TestCase { 41 42 private Configuration setUpConfiguration(boolean auto,boolean autoIP,ArrayList names) { 43 DefaultConfiguration configuration = new DefaultConfiguration("test"); 44 DefaultConfiguration sNamesConf = new DefaultConfiguration("domainnames"); 45 DefaultConfiguration autoConf = new DefaultConfiguration("autodetect"); 46 autoConf.setValue(auto); 47 configuration.addChild(autoConf); 48 49 DefaultConfiguration autoIPConf = new DefaultConfiguration("autodetectIP"); 50 autoIPConf.setValue(autoIP); 51 configuration.addChild(autoIPConf); 52 53 for (int i= 0; i< names.size(); i++) { 54 DefaultConfiguration nameConf = new DefaultConfiguration("domainname"); 55 56 nameConf.setValue(names.get(i).toString()); 57 sNamesConf.addChild(nameConf); 58 } 59 60 configuration.addChild(sNamesConf); 61 return configuration; 62 } 63 64 private DNSServer setUpDNSServer(final String hostName) { 65 DNSServer dns = new AbstractDNSServer() { 66 public String getHostName(InetAddress inet) { 67 return hostName; 68 } 69 70 public InetAddress [] getAllByName(String name) throws UnknownHostException { 71 return new InetAddress [] { InetAddress.getByName("127.0.0.1")}; 72 } 73 74 public InetAddress getLocalHost() throws UnknownHostException { 75 return InetAddress.getLocalHost(); 76 } 77 }; 78 return dns; 79 } 80 81 private MockServiceManager setUpServiceManager(DNSServer dns) { 82 MockServiceManager service = new MockServiceManager(); 83 service.put(DNSServer.ROLE, dns); 84 return service; 85 } 86 87 public void testGetDomains() throws Exception { 88 ArrayList domains = new ArrayList (); 89 domains.add("domain1."); 90 domains.add("domain2."); 91 92 XMLDomainList dom = new XMLDomainList(); 93 ContainerUtil.enableLogging(dom,new MockLogger()); 94 ContainerUtil.service(dom,setUpServiceManager(setUpDNSServer("localhost"))); 95 ContainerUtil.configure(dom, setUpConfiguration(false,false,domains)); 96 ContainerUtil.initialize(dom); 97 98 assertTrue("Two domain found",dom.getDomains().size() ==2); 99 } 100 101 public void testGetDomainsAutoDetectNotLocalHost() throws Exception { 102 ArrayList domains = new ArrayList (); 103 domains.add("domain1."); 104 105 XMLDomainList dom = new XMLDomainList(); 106 ContainerUtil.enableLogging(dom,new MockLogger()); 107 ContainerUtil.service(dom,setUpServiceManager(setUpDNSServer("local"))); 108 ContainerUtil.configure(dom, setUpConfiguration(true,false,domains)); 109 ContainerUtil.initialize(dom); 110 111 assertEquals("Two domains found",dom.getDomains().size(), 2); 112 } 113 114 public void testGetDomainsAutoDetectLocalHost() throws Exception { 115 ArrayList domains = new ArrayList (); 116 domains.add("domain1."); 117 118 ManageableDomainList dom = new XMLDomainList(); 119 ContainerUtil.enableLogging(dom,new MockLogger()); 120 ContainerUtil.service(dom,setUpServiceManager(setUpDNSServer("localhost"))); 121 ContainerUtil.configure(dom, setUpConfiguration(true,false,domains)); 122 ContainerUtil.initialize(dom); 123 124 assertEquals("One domain found",dom.getDomains().size(), 1); 125 } 126 } 127 | Popular Tags |