001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Alexei Y. Zakharov
021: * @version $Revision: 1.1.2.5 $
022: */package org.apache.harmony.jndi.provider.dns;
023:
024: import java.util.Hashtable;
025:
026: import javax.naming.CompositeName;
027: import javax.naming.Context;
028: import javax.naming.Name;
029: import javax.naming.NamingException;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: * <code>org.apache.harmony.jndi.provider.dns.DNSContext</class> class unit test.
037: * @author Alexei Zakharov
038: * @version $Revision: 1.1.2.5 $
039: */
040: public class DNSContextTest extends TestCase {
041:
042: private DNSNameParser nameParser = null;
043:
044: @Override
045: protected void setUp() {
046: nameParser = new DNSNameParser();
047: }
048:
049: public void testGetNameInNamespace() throws NamingException {
050: Context ctx;
051: String propStr1 = "dns://localhost/example.com";
052: String propStr2 = "dns://localhost";
053: Hashtable<String, String> env = new Hashtable<String, String>();
054:
055: env.put(Context.PROVIDER_URL, propStr1);
056: ctx = new DNSContextFactory().getInitialContext(env);
057: assertEquals("example.com.", ctx.getNameInNamespace());
058: env.put(Context.PROVIDER_URL, propStr2);
059: ctx = new DNSContextFactory().getInitialContext(env);
060: assertEquals(".", ctx.getNameInNamespace());
061: }
062:
063: public void testComposeName() throws NamingException {
064: Name name = null;
065: Name prefix = null;
066: Name result = null;
067: String resultStr = null;
068: String propStr1 = "dns://localhost/example.com";
069: Hashtable<String, String> env = new Hashtable<String, String>();
070: Context ctx;
071:
072: env.put(Context.PROVIDER_URL, propStr1);
073: ctx = new DNSContextFactory().getInitialContext(env);
074: // #composeName(Name, Name)
075: // NULL & NULL
076: try {
077: ctx.composeName(name, prefix);
078: fail("NullPointerException has not been thrown");
079: } catch (NullPointerException e) {
080: }
081: // CompositeName & CompositeName
082: name = new CompositeName("host1/file1.html");
083: prefix = new CompositeName("/example.com");
084: result = ctx.composeName(name, prefix);
085: assertEquals("/host1.example.com/file1.html", result.toString());
086: // DNSName & CompositeName
087: name = nameParser.parse("host1.mysubdomain");
088: prefix = new CompositeName("schema2:/example.com");
089: result = ctx.composeName(name, prefix);
090: assertEquals("schema2:/host1.mysubdomain.example.com", result
091: .toString());
092: // CompositeName & DNSName
093: name = new CompositeName("host1/file1.html");
094: prefix = nameParser.parse("subdomain.example.com.");
095: result = ctx.composeName(name, prefix);
096: assertEquals("host1.subdomain.example.com./file1.html", result
097: .toString());
098: // DNSName & DNSName
099: name = nameParser.parse("host1.subdomain1");
100: prefix = nameParser.parse("subdomain2.example.com.");
101: result = ctx.composeName(name, prefix);
102: assertEquals("host1.subdomain1.subdomain2.example.com.", result
103: .toString());
104:
105: // error
106: name = ProviderConstants.ROOT_ZONE_NAME_OBJ;
107: prefix = new CompositeName("schema33:/domain.com");
108: try {
109: result = ctx.composeName(name, prefix);
110: fail("NamingException should be thrown");
111: } catch (NamingException e) {
112: }
113:
114: // string form
115: resultStr = ctx.composeName("host1/file1.html", "/example.com");
116: assertEquals("/host1.example.com/file1.html", resultStr);
117: resultStr = ctx.composeName("host1", "example.com");
118: assertEquals("host1.example.com", resultStr);
119: resultStr = ctx.composeName("host1/mamba", "example.com");
120: assertEquals("host1.example.com/mamba", resultStr);
121: resultStr = ctx.composeName("host1.subdomain",
122: "schema17:/example.com");
123: assertEquals("schema17:/host1.subdomain.example.com", resultStr);
124: // error
125: try {
126: ctx.composeName(".", "schema17:/example.com");
127: fail("NamingException should be thrown");
128: } catch (NamingException e) {
129: }
130: }
131:
132: public void testConstructor() throws NoSuchFieldException,
133: IllegalArgumentException, SecurityException,
134: NamingException {
135: Hashtable<String, String> env = new Hashtable<String, String>();
136: DNSContext context = null;
137: env.put(Context.AUTHORITATIVE, "true");
138: env.put(DNSContext.LOOKUP_ATTR, "IN A");
139: env.put(DNSContext.RECURSION, "true");
140: env.put(DNSContext.TIMEOUT_INITIAL, "5000");
141: env.put(DNSContext.TIMEOUT_RETRIES, "5");
142: env.put(DNSContext.THREADS_MAX, "17");
143: env.put(Context.PROVIDER_URL, "dns://superdns.com/intel.com");
144: context = (DNSContext) new DNSContextFactory()
145: .getInitialContext(env);
146: assertTrue(TestMgr.getBoolField(context, "authoritative"));
147: assertEquals(ProviderConstants.A_TYPE, TestMgr.getIntField(
148: context, "lookupAttrType"));
149: assertEquals(ProviderConstants.IN_CLASS, TestMgr.getIntField(
150: context, "lookupAttrClass"));
151: assertTrue(TestMgr.getBoolField(context, "recursion"));
152: assertEquals(5000, TestMgr.getIntField(context,
153: "timeoutInitial"));
154: assertEquals(5, TestMgr.getIntField(context, "timeoutRetries"));
155: assertEquals(17, TestMgr.getIntField(context, "maxThreads"));
156:
157: env.put(Context.AUTHORITATIVE, "blah blah blah");
158: env.put(DNSContext.LOOKUP_ATTR, "MX");
159: env.put(DNSContext.RECURSION, "trueee");
160: env.remove(DNSContext.THREADS_MAX);
161: context = (DNSContext) new DNSContextFactory()
162: .getInitialContext(env);
163: assertFalse(TestMgr.getBoolField(context, "authoritative"));
164: assertEquals(ProviderConstants.MX_TYPE, TestMgr.getIntField(
165: context, "lookupAttrType"));
166: assertEquals(ProviderConstants.IN_CLASS, TestMgr.getIntField(
167: context, "lookupAttrClass"));
168: assertFalse(TestMgr.getBoolField(context, "recursion"));
169: assertEquals(ProviderConstants.DEFAULT_MAX_THREADS, TestMgr
170: .getIntField(context, "maxThreads"));
171:
172: env.put(DNSContext.LOOKUP_ATTR, "IN ZZZZZZZ");
173: try {
174: context = (DNSContext) new DNSContextFactory()
175: .getInitialContext(env);
176: fail("NamingException has not been thrown");
177: } catch (NamingException e) {
178: }
179:
180: env.put(DNSContext.LOOKUP_ATTR, "ZZZZZZZ");
181: try {
182: context = (DNSContext) new DNSContextFactory()
183: .getInitialContext(env);
184: fail("NamingException has not been thrown");
185: } catch (NamingException e) {
186: }
187: env.put(DNSContext.LOOKUP_ATTR, "TXT");
188:
189: env.put(DNSContext.TIMEOUT_INITIAL, "q");
190: try {
191: context = (DNSContext) new DNSContextFactory()
192: .getInitialContext(env);
193: fail("NumberFormatException has not been thrown");
194: } catch (NumberFormatException e) {
195: }
196: env.put(DNSContext.TIMEOUT_INITIAL, "5000");
197:
198: env.put(DNSContext.TIMEOUT_RETRIES, "q");
199: try {
200: context = (DNSContext) new DNSContextFactory()
201: .getInitialContext(env);
202: fail("NumberFormatException has not been thrown");
203: } catch (NumberFormatException e) {
204: }
205: env.put(DNSContext.TIMEOUT_RETRIES, "5");
206:
207: env.put(Context.PROVIDER_URL,
208: "dns://dnsserver1.com/super.zone.ru. "
209: + "dns://123.456.78.90/super.zone.ru");
210: context = (DNSContext) new DNSContextFactory()
211: .getInitialContext(env);
212: /*
213: slist = SList.getInstance();
214: serv = slist.getServerByName("super.zone.ru", "dnsserver1.com", 53);
215: if (serv == null) {
216: fail("DNS server has not been added");
217: }
218: serv = slist.getServerByIP("super.zone.ru.", "123.456.78.90", 53);
219: if (serv == null) {
220: fail("DNS server has not been added");
221: }
222: */
223: env.put(Context.PROVIDER_URL, "file:/etc/passwd");
224: try {
225: context = (DNSContext) new DNSContextFactory()
226: .getInitialContext(env);
227: fail("NamingException has not been thrown");
228: } catch (NamingException e) {
229: }
230:
231: }
232:
233: /**
234: * Tests <code>addToEnvironment(), getEnvironment()</code> and
235: * <code>removeFromEnvironment()</code> methods.
236: */
237: public void testEnvironment() throws NamingException {
238: DNSContext context = null;
239: Hashtable<String, String> env = new Hashtable<String, String>();
240: Hashtable<?, ?> env2 = null;
241:
242: // no side effect
243: env.put(DNSContext.TIMEOUT_INITIAL, "2000");
244: context = (DNSContext) new DNSContextFactory()
245: .getInitialContext(env);
246: env.put(DNSContext.TIMEOUT_INITIAL, "2001");
247: env2 = context.getEnvironment();
248: assertEquals("2000", env2.get(DNSContext.TIMEOUT_INITIAL));
249:
250: // add to environment
251: context.addToEnvironment(DNSContext.TIMEOUT_RETRIES, "15");
252: env2 = context.getEnvironment();
253: assertEquals("15", env2.get(DNSContext.TIMEOUT_RETRIES));
254:
255: // replace with new value
256: context.addToEnvironment(DNSContext.TIMEOUT_RETRIES, "16");
257: env2 = context.getEnvironment();
258: assertEquals("16", env2.get(DNSContext.TIMEOUT_RETRIES));
259:
260: // remove from environment
261: context.removeFromEnvironment(DNSContext.TIMEOUT_INITIAL);
262: env2 = context.getEnvironment();
263: assertNull(env2.get(DNSContext.TIMEOUT_INITIAL));
264: }
265:
266: // public void testConstructCannotProceedException() {
267: // // TODO
268: // }
269:
270: @Override
271: protected void tearDown() {
272: nameParser = null;
273: }
274:
275: public static Test suite() {
276: return new TestSuite(DNSContextTest.class);
277: }
278:
279: public static void main(String[] args) {
280: junit.textui.TestRunner.run(DNSContextTest.class);
281: }
282:
283: }
|