Skip to content

Commit 79874ea

Browse files
easelAndré Behrens
authored andcommitted
Only attempt to set dns server if provided
- Extract environment construction to a seperate method - Add test case for environment construction - Fixes OfficeDev#417
1 parent 2b1d6f3 commit 79874ea

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/dns/DnsClient.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@
4242
*/
4343
public class DnsClient {
4444

45+
/**
46+
* Set up the environment used to construct the DirContext.
47+
*
48+
* @param dnsServerAddress
49+
* @return
50+
*/
51+
static Hashtable<String, String> getEnv(String dnsServerAddress) {
52+
// Set up environment for creating initial context
53+
Hashtable<String, String> env = new Hashtable<String, String>();
54+
env.put("java.naming.factory.initial",
55+
"com.sun.jndi.dns.DnsContextFactory");
56+
if(dnsServerAddress != null && !dnsServerAddress.isEmpty()) {
57+
env.put("java.naming.provider.url", "dns://" + dnsServerAddress);
58+
}
59+
return env;
60+
}
61+
4562
/**
4663
* Performs Dns query.
4764
*
@@ -58,15 +75,8 @@ public static <T extends DnsRecord> List<T> dnsQuery(Class<T> cls, String domain
5875

5976
List<T> dnsRecordList = new ArrayList<T>();
6077
try {
61-
62-
// Set up environment for creating initial context
63-
Hashtable<String, String> env = new Hashtable<String, String>();
64-
env.put("java.naming.factory.initial",
65-
"com.sun.jndi.dns.DnsContextFactory");
66-
env.put("java.naming.provider.url", "dns://" + dnsServerAddress);
67-
6878
// Create initial context
69-
DirContext ictx = new InitialDirContext(env);
79+
DirContext ictx = new InitialDirContext(getEnv(dnsServerAddress));
7080

7181
// Retrieve SRV record context attribute for the specified domain
7282
Attributes contextAttributes = ictx.getAttributes(domain,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* The MIT License
3+
* Copyright (c) 2012 Microsoft Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package microsoft.exchange.webservices.data.dns;
25+
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
import java.util.Hashtable;
30+
31+
public class DnsClientTest {
32+
@Test public void getEnvShouldSetNaming() {
33+
Hashtable<String, String> env = DnsClient.getEnv("");
34+
Assert.assertEquals(env.get("java.naming.factory.initial"),
35+
"com.sun.jndi.dns.DnsContextFactory");
36+
}
37+
38+
@Test public void getEnvShouldNotSetProviderUrl() throws Exception {
39+
Hashtable<String, String> env = DnsClient.getEnv("");
40+
Assert.assertFalse(env.containsKey("java.naming.provider.url"));
41+
env = DnsClient.getEnv(null);
42+
Assert.assertFalse(env.containsKey("java.naming.provider.url"));
43+
}
44+
45+
@Test public void getEnvShoulSetProviderUrl() throws Exception {
46+
Hashtable<String, String> env = DnsClient.getEnv("1.1.1.1");
47+
Assert.assertEquals(env.get("java.naming.provider.url"), "dns://1.1.1.1");
48+
}
49+
}

0 commit comments

Comments
 (0)