Skip to content

Commit b19a582

Browse files
committed
improve unit test
1 parent 931b82d commit b19a582

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

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

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.alibaba.dcm;
22

3+
import org.junit.BeforeClass;
34
import org.junit.Test;
45

56
import java.net.InetAddress;
@@ -16,34 +17,32 @@
1617
* @author ding.lid
1718
*/
1819
public class DnsCacheManipulatorTest {
19-
@Test
20-
public void test_getAllDnsCache() throws Exception {
21-
DnsCacheManipulator.clearDnsCache();
22-
23-
final String host = "www.test_getAllDnsCacheEntries.com";
24-
final String ip = "42.42.42.42";
20+
static final String DOMAIN1 = "www.hello1.com";
21+
static final String IP1 = "42.42.41.41";
22+
static final String DOMAIN2 = "www.hello2.com";
23+
static final String IP2 = "42.42.41.42";
24+
public static final String IP3 = "42.42.43.43";
2525

26-
DnsCacheManipulator.setDnsCache(host, ip);
26+
static final String DOMAIN_NOT_EXISTED = "www.domain-not-existed-7352jt-12559-AZ-7524087.com";
2727

28-
final List<DnsCacheEntry> allDnsCacheEntries = DnsCacheManipulator.getAllDnsCache();
29-
final List<DnsCacheEntry> expected = Arrays.asList(
30-
new DnsCacheEntry(host.toLowerCase(), new String[]{ip}, new Date(Long.MAX_VALUE)));
31-
32-
assertEquals(expected, allDnsCacheEntries);
28+
@BeforeClass
29+
public static void beforeClass() throws Exception {
30+
DnsCacheManipulator.clearDnsCache();
31+
assertEquals(0, DnsCacheManipulator.getAllDnsCache().size());
3332
}
3433

3534
@Test
3635
public void test_loadDnsCacheConfig() throws Exception {
3736
DnsCacheManipulator.loadDnsCacheConfig();
38-
final String ip = InetAddress.getByName("www.hello1.com").getHostAddress();
39-
assertEquals("42.42.41.41", ip);
37+
final String ip = InetAddress.getByName(DOMAIN1).getHostAddress();
38+
assertEquals(IP1, ip);
4039
}
4140

4241
@Test
4342
public void test_loadDnsCacheConfig_fromMyConfig() throws Exception {
4443
DnsCacheManipulator.loadDnsCacheConfig("my-dns-cache.properties");
45-
final String ip = InetAddress.getByName("www.hello1.com").getHostAddress();
46-
assertEquals("42.42.43.43", ip);
44+
final String ip = InetAddress.getByName(DOMAIN2).getHostAddress();
45+
assertEquals(IP2, ip);
4746
}
4847

4948
@Test
@@ -57,14 +56,26 @@ public void test_configNotFound() throws Exception {
5756
}
5857

5958
@Test
60-
public void test_DnsCache_canExpire() throws Exception {
61-
final String notExistedHost = "www.not-existed-host-test_DnsCache_canExpire.com";
59+
public void test_setDnsCache_getAllDnsCache() throws Exception {
60+
final String host = "www.test_setDnsCache_getAllDnsCache.com";
61+
DnsCacheManipulator.setDnsCache(host, IP3);
62+
63+
final List<DnsCacheEntry> allDnsCacheEntries = DnsCacheManipulator.getAllDnsCache();
64+
final List<DnsCacheEntry> expected = Arrays.asList(
65+
new DnsCacheEntry(host.toLowerCase(), new String[]{IP3}, new Date(Long.MAX_VALUE)));
66+
67+
assertEquals(expected, allDnsCacheEntries);
68+
}
69+
70+
@Test
71+
public void test_removeDnsCache() throws Exception {
72+
final String notExistedHost = "www.not-existed-host-test_removeDnsCache";
6273

63-
DnsCacheManipulator.setDnsCache(30, notExistedHost, "42.42.43.43");
74+
DnsCacheManipulator.setDnsCache(notExistedHost, IP3);
6475
final String ip = InetAddress.getByName(notExistedHost).getHostAddress();
65-
assertEquals("42.42.43.43", ip);
76+
assertEquals(IP3, ip);
6677

67-
Thread.sleep(32);
78+
DnsCacheManipulator.removeDnsCache(notExistedHost);
6879

6980
try {
7081
InetAddress.getByName(notExistedHost).getHostAddress();
@@ -76,17 +87,29 @@ public void test_DnsCache_canExpire() throws Exception {
7687
}
7788

7889
@Test
79-
public void test_removeDnsCache() throws Exception {
80-
final String notExistedHost = "www.not-existed-host-test_removeDnsCache";
90+
public void test_canResetExistedDomain_canExpire_thenReLookupBack() throws Exception {
91+
final String domain = "github.com";
8192

82-
DnsCacheManipulator.setDnsCache(notExistedHost, "42.42.43.43");
83-
final String ip = InetAddress.getByName(notExistedHost).getHostAddress();
84-
assertEquals("42.42.43.43", ip);
93+
final String ip = InetAddress.getByName(domain).getHostAddress();
8594

86-
DnsCacheManipulator.removeDnsCache(notExistedHost);
95+
DnsCacheManipulator.setDnsCache(30, domain, IP3);
96+
assertEquals(IP3, InetAddress.getByName(domain).getHostAddress());
97+
98+
Thread.sleep(32);
99+
100+
assertEquals(ip, InetAddress.getByName(domain).getHostAddress());
101+
}
102+
103+
@Test
104+
public void test_DnsCache_setNotExistedDomain_canExpire_thenReLookupAndNotExisted() throws Exception {
105+
DnsCacheManipulator.setDnsCache(30, DOMAIN_NOT_EXISTED, IP3);
106+
final String ip = InetAddress.getByName(DOMAIN_NOT_EXISTED).getHostAddress();
107+
assertEquals(IP3, ip);
108+
109+
Thread.sleep(32);
87110

88111
try {
89-
InetAddress.getByName(notExistedHost).getHostAddress();
112+
InetAddress.getByName(DOMAIN_NOT_EXISTED).getHostAddress();
90113
fail();
91114
} catch (UnknownHostException expected) {
92115
System.out.println(expected.toString());
@@ -96,7 +119,6 @@ public void test_removeDnsCache() throws Exception {
96119

97120
@Test
98121
public void test_multi_ips_in_config_file() throws Exception {
99-
DnsCacheManipulator.clearDnsCache();
100122
DnsCacheManipulator.loadDnsCacheConfig("dns-cache-multi-ips.properties");
101123

102124
final String host = "www.hello-multi-ips.com";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
www.hello1.com=42.42.43.43
1+
www.hello2.com=42.42.41.42

0 commit comments

Comments
 (0)