KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > domain > XMLDomainListTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20
21
22
23 package org.apache.james.domain;
24
25 import java.net.InetAddress JavaDoc;
26 import java.net.UnknownHostException JavaDoc;
27 import java.util.ArrayList JavaDoc;
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 JavaDoc 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 JavaDoc hostName) {
65         DNSServer dns = new AbstractDNSServer() {
66             public String JavaDoc getHostName(InetAddress JavaDoc inet) {
67                 return hostName;
68             }
69             
70             public InetAddress JavaDoc[] getAllByName(String JavaDoc name) throws UnknownHostException JavaDoc {
71                 return new InetAddress JavaDoc[] { InetAddress.getByName("127.0.0.1")};
72             }
73             
74             public InetAddress JavaDoc getLocalHost() throws UnknownHostException JavaDoc {
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 JavaDoc {
88         ArrayList JavaDoc domains = new ArrayList JavaDoc();
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 JavaDoc {
102         ArrayList JavaDoc domains = new ArrayList JavaDoc();
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 JavaDoc {
115         ArrayList JavaDoc domains = new ArrayList JavaDoc();
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