Skip to content

Commit 17e642e

Browse files
committed
= refactor unit test
- rename getAllHostAddresses -> getAllIps - try-finally System.clearProperty
1 parent 06de3c2 commit 17e642e

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

library/src/test/java/com/alibaba/dcm/DnsCacheManipulatorTest.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
import java.net.InetAddress;
88
import java.net.UnknownHostException;
9-
import java.util.Date;
10-
import java.util.HashSet;
11-
import java.util.List;
12-
import java.util.Set;
9+
import java.util.*;
1310

1411
import static com.alibaba.dcm.Util.getIpByName;
1512
import static com.alibaba.dcm.internal.TestTimeUtil.NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS;
@@ -34,7 +31,7 @@ public class DnsCacheManipulatorTest {
3431
private static final String DOMAIN_CUSTOMIZED = "www.customized.com";
3532
private static final String IP_CUSTOMIZED = "42.42.42.42";
3633

37-
private static final String DOMAIN_NOT_EXISTED = "www.domain-not-existed-7352jt-12559-AZ-7524087.com";
34+
private static final String DOMAIN_NOT_EXISTED = "www.domain-not-existed-2D4B2C4E-61D5-46B3-81FA-3A975156D1AE.com";
3835

3936
@BeforeClass
4037
public static void beforeClass() {
@@ -65,11 +62,15 @@ public void test_loadDnsCacheConfig() throws Exception {
6562
@Test
6663
public void test_loadDnsCacheConfig_from_D_Option() throws Exception {
6764
final String key = "dcm.config.filename";
68-
System.setProperty(key, "customized-dns-cache.properties");
69-
DnsCacheManipulator.loadDnsCacheConfig();
70-
final String ip = getIpByName(DOMAIN_CUSTOMIZED);
71-
assertEquals(IP_CUSTOMIZED, ip);
72-
System.clearProperty(key);
65+
try {
66+
System.setProperty(key, "customized-dns-cache.properties");
67+
DnsCacheManipulator.loadDnsCacheConfig();
68+
69+
final String ip = getIpByName(DOMAIN_CUSTOMIZED);
70+
assertEquals(IP_CUSTOMIZED, ip);
71+
} finally {
72+
System.clearProperty(key);
73+
}
7374
}
7475

7576
@Test
@@ -85,10 +86,8 @@ public void test_setMultiIp() throws Exception {
8586
String ip = getIpByName("multi.ip.com");
8687
assertEquals("1.1.1.1", ip);
8788

88-
InetAddress[] all = InetAddress.getAllByName("multi.ip.com");
89-
assertEquals(2, all.length);
90-
String[] ips = {all[0].getHostAddress(), all[1].getHostAddress()};
91-
assertArrayEquals(new String[]{"1.1.1.1", "2.2.2.2"}, ips);
89+
List<String> allIps = getAllIps("multi.ip.com");
90+
assertEquals(Arrays.asList("1.1.1.1", "2.2.2.2"), allIps);
9291
}
9392

9493
@Test
@@ -123,19 +122,19 @@ public void test_setDnsCache_getAllDnsCache() {
123122
public void test_canSetExistedDomain_canExpire_thenReLookupBack() throws Exception {
124123
final String domain = "github.com";
125124

126-
Set<String> expected = getAllHostAddresses(domain);
125+
List<String> expected = getAllIps(domain);
127126

128127
DnsCacheManipulator.setDnsCache(30, domain, IP3);
129128
assertEquals(IP3, getIpByName(domain));
130129

131130
sleep(32);
132131

133-
assertEquals(expected, getAllHostAddresses(domain));
132+
assertEquals(expected, getAllIps(domain));
134133
}
135134

136-
private static Set<String> getAllHostAddresses(String domain) throws Exception {
135+
private static List<String> getAllIps(String domain) throws Exception {
137136
final InetAddress[] allByName = InetAddress.getAllByName(domain);
138-
Set<String> all = new HashSet<String>();
137+
List<String> all = new ArrayList<String>();
139138
for (InetAddress inetAddress : allByName) {
140139
all.add(inetAddress.getHostAddress());
141140
}

0 commit comments

Comments
 (0)