KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > dnsserver > DNSServerTest


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 package org.apache.james.dnsserver;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.james.test.mock.avalon.MockLogger;
26 import org.xbill.DNS.Cache;
27 import org.xbill.DNS.DClass;
28 import org.xbill.DNS.Lookup;
29 import org.xbill.DNS.Message;
30 import org.xbill.DNS.Name;
31 import org.xbill.DNS.RRset;
32 import org.xbill.DNS.Record;
33 import org.xbill.DNS.Resolver;
34 import org.xbill.DNS.SOARecord;
35 import org.xbill.DNS.SetResponse;
36 import org.xbill.DNS.Zone;
37
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.util.Collection JavaDoc;
42 import java.util.Iterator JavaDoc;
43
44 import junit.framework.TestCase;
45
46 public class DNSServerTest extends TestCase {
47
48     private TestableDNSServer dnsServer;
49     private Cache defaultCache;
50     private Resolver defaultResolver;
51     private Name[] defaultSearchPaths;
52     
53     public void testNoMX() throws Exception JavaDoc {
54         dnsServer.setResolver(null);
55         dnsServer.setCache(new ZoneCache("dnstest.com."));
56         //a.setSearchPath(new String[] { "searchdomain.com." });
57
Collection JavaDoc records = dnsServer.findMXRecords("nomx.dnstest.com.");
58         assertEquals(1, records.size());
59         assertEquals("nomx.dnstest.com.", records.iterator()
60                 .next());
61     }
62     
63     public void testBadMX() throws Exception JavaDoc {
64         dnsServer.setResolver(null);
65         dnsServer.setCache(new ZoneCache("dnstest.com."));
66         //a.setSearchPath(new String[] { "searchdomain.com." });
67
Collection JavaDoc records = dnsServer.findMXRecords("badmx.dnstest.com.");
68         assertEquals(1, records.size());
69         assertEquals("badhost.dnstest.com.", records.iterator()
70                 .next());
71         Iterator JavaDoc it = dnsServer.getSMTPHostAddresses("badmx.dnstest.com.");
72         assertFalse(it.hasNext());
73     }
74     
75     public void testINARecords() throws Exception JavaDoc {
76         // Zone z = loadZone("pippo.com.");
77
dnsServer.setResolver(null);
78         dnsServer.setCache(new ZoneCache("pippo.com."));
79         // dnsServer.setLookupper(new ZoneLookupper(z));
80
Collection JavaDoc records = dnsServer.findMXRecords("www.pippo.com.");
81         assertEquals(1, records.size());
82         assertEquals("pippo.com.inbound.mxlogic.net.", records.iterator()
83                 .next());
84     }
85
86     public void testMXCatches() throws Exception JavaDoc {
87         // Zone z = loadZone("test-zone.com.");
88
dnsServer.setResolver(null);
89         dnsServer.setCache(new ZoneCache("test-zone.com."));
90         // dnsServer.setLookupper(new ZoneLookupper(z));
91
Collection JavaDoc res = dnsServer.findMXRecords("test-zone.com.");
92         try {
93             res.add(new Object JavaDoc());
94             fail("MX Collection should not be modifiable");
95         } catch (UnsupportedOperationException JavaDoc e) {
96         }
97         assertEquals(1,res.size());
98         assertEquals("mail.test-zone.com.",res.iterator().next());
99     }
100
101     public void testCNAMEasMXrecords() throws Exception JavaDoc {
102         // Zone z = loadZone("brandilyncollins.com.");
103
dnsServer.setResolver(null);
104         dnsServer.setCache(new ZoneCache("brandilyncollins.com."));
105         // dnsServer.setLookupper(new ZoneLookupper(z));
106
Iterator JavaDoc records = dnsServer.getSMTPHostAddresses("brandilyncollins.com.");
107         assertEquals(true, records.hasNext());
108     }
109
110     protected void setUp() throws Exception JavaDoc {
111         dnsServer = new TestableDNSServer();
112         DefaultConfigurationBuilder db = new DefaultConfigurationBuilder();
113
114         Configuration c = db.build(
115                 new ByteArrayInputStream JavaDoc("<dnsserver><autodiscover>true</autodiscover><authoritative>false</authoritative></dnsserver>".getBytes()),
116                 "dnsserver");
117         ContainerUtil.enableLogging(dnsServer, new MockLogger());
118         ContainerUtil.configure(dnsServer, c);
119         ContainerUtil.initialize(dnsServer);
120         
121         
122         defaultCache = Lookup.getDefaultCache(DClass.IN);
123         defaultResolver = Lookup.getDefaultResolver();
124         defaultSearchPaths = Lookup.getDefaultSearchPath();
125         Lookup.setDefaultCache(null, DClass.IN);
126         Lookup.setDefaultResolver(null);
127         Lookup.setDefaultSearchPath(new Name[] {});
128     }
129
130     protected void tearDown() throws Exception JavaDoc {
131         dnsServer.setCache(null);
132         ContainerUtil.dispose(dnsServer);
133         Lookup.setDefaultCache(defaultCache, DClass.IN);
134         Lookup.setDefaultResolver(defaultResolver);
135         Lookup.setDefaultSearchPath(defaultSearchPaths);
136     }
137
138     private Zone loadZone(String JavaDoc zoneName) throws IOException JavaDoc {
139         String JavaDoc zoneFilename = zoneName + "zone";
140         URL JavaDoc zoneResource = getClass().getResource(zoneFilename);
141         assertNotNull("test resource for zone could not be loaded: " + zoneFilename, zoneResource);
142         String JavaDoc zoneFile = zoneResource.getFile();
143         Zone zone = new Zone(Name.fromString(zoneName),zoneFile);
144         return zone;
145     }
146
147     private final class ZoneCache extends Cache {
148
149         Zone z = null;
150         
151         public ZoneCache(String JavaDoc string) throws IOException JavaDoc {
152             z = loadZone(string);
153         }
154
155         public SetResponse addMessage(Message arg0) {
156             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
157         }
158
159         public synchronized void addNegative(Name arg0, int arg1, SOARecord arg2, int arg3) {
160             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
161         }
162
163         public synchronized void addRecord(Record arg0, int arg1, Object JavaDoc arg2) {
164             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
165         }
166
167         public synchronized void addRRset(RRset arg0, int arg1) {
168             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
169         }
170
171         public synchronized void clearCache() {
172             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
173         }
174
175         public RRset[] findAnyRecords(Name arg0, int arg1) {
176             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
177         }
178
179         public RRset[] findRecords(Name arg0, int arg1) {
180             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
181         }
182
183         public void flushName(Name arg0) {
184             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
185         }
186
187         public void flushSet(Name arg0, int arg1) {
188             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
189         }
190
191         public int getDClass() {
192             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
193         }
194
195         public int getMaxCache() {
196             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
197         }
198
199         public int getMaxEntries() {
200             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
201         }
202
203         public int getMaxNCache() {
204             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
205         }
206
207         public int getSize() {
208             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
209         }
210
211         protected synchronized SetResponse lookup(Name arg0, int arg1, int arg2) {
212             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
213         }
214
215         public SetResponse lookupRecords(Name arg0, int arg1, int arg2) {
216             System.out.println("Cache.lookupRecords "+arg0+","+arg1+","+arg2);
217             return z.findRecords(arg0, arg1);
218             //return super.lookupRecords(arg0, arg1, arg2);
219
}
220
221         public void setCleanInterval(int arg0) {
222             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
223         }
224
225         public void setMaxCache(int arg0) {
226             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
227         }
228
229         public void setMaxEntries(int arg0) {
230             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
231         }
232
233         public void setMaxNCache(int arg0) {
234             throw new UnsupportedOperationException JavaDoc("ZoneCache is a mock used only for testing purpose");
235         }
236     }
237
238     private final class TestableDNSServer extends DNSServer {
239         
240         public void setResolver(Resolver r) {
241             resolver = r;
242         }
243
244         public Resolver getResolver() {
245             return resolver;
246         }
247         
248         public void setCache(Cache c) {
249             cache = c;
250         }
251         
252         public Cache getCache() {
253             return cache;
254         }
255     }
256
257 }
258
Popular Tags