11package com .alibaba .dcm ;
22
3+ import org .junit .Before ;
34import org .junit .BeforeClass ;
45import org .junit .Test ;
56
@@ -25,10 +26,11 @@ public class DnsCacheManipulatorTest {
2526
2627 static final String DOMAIN_NOT_EXISTED = "www.domain-not-existed-7352jt-12559-AZ-7524087.com" ;
2728
28- @ BeforeClass
29- public static void beforeClass () throws Exception {
29+ @ Before
30+ public void before () throws Exception {
3031 DnsCacheManipulator .clearDnsCache ();
31- assertEquals (0 , DnsCacheManipulator .getAllDnsCache ().size ());
32+ assertTrue (DnsCacheManipulator .getAllDnsCache ().isEmpty ());
33+ assertTrue (DnsCacheManipulator .getWholeDnsCache ().getNegativeCache ().isEmpty ());
3234 }
3335
3436 @ Test
@@ -65,43 +67,51 @@ public void test_setDnsCache_getAllDnsCache() throws Exception {
6567 new DnsCacheEntry (host .toLowerCase (), new String []{IP3 }, new Date (Long .MAX_VALUE )));
6668
6769 assertEquals (expected , allDnsCacheEntries );
70+ assertTrue (DnsCacheManipulator .getWholeDnsCache ().getNegativeCache ().isEmpty ());
6871 }
6972
7073 @ Test
71- public void test_removeDnsCache () throws Exception {
72- final String notExistedHost = "www.not-existed-host-test_removeDnsCache" ;
74+ public void test_canSetExistedDomain_canExpire_thenReLookupBack () throws Exception {
75+ final String domain = "github.com" ;
76+
77+ final String ip = InetAddress .getByName (domain ).getHostAddress ();
78+
79+ DnsCacheManipulator .setDnsCache (30 , domain , IP3 );
80+ assertEquals (IP3 , InetAddress .getByName (domain ).getHostAddress ());
81+
82+ Thread .sleep (32 );
83+
84+ assertEquals (ip , InetAddress .getByName (domain ).getHostAddress ());
85+ }
7386
74- DnsCacheManipulator .setDnsCache (notExistedHost , IP3 );
75- final String ip = InetAddress .getByName (notExistedHost ).getHostAddress ();
87+ @ Test
88+ public void test_canSetNotExistedDomain_RemoveThenReLookupAndNotExisted () throws Exception {
89+ DnsCacheManipulator .setDnsCache (DOMAIN_NOT_EXISTED , IP3 );
90+ final String ip = InetAddress .getByName (DOMAIN_NOT_EXISTED ).getHostAddress ();
7691 assertEquals (IP3 , ip );
7792
78- DnsCacheManipulator .removeDnsCache (notExistedHost );
93+ DnsCacheManipulator .removeDnsCache (DOMAIN_NOT_EXISTED );
7994
8095 try {
81- InetAddress .getByName (notExistedHost ).getHostAddress ();
96+ InetAddress .getByName (DOMAIN_NOT_EXISTED ).getHostAddress ();
8297 fail ();
8398 } catch (UnknownHostException expected ) {
8499 System .out .println (expected .toString ());
85100 assertTrue (true );
86101 }
87- }
88102
89- @ Test
90- public void test_canResetExistedDomain_canExpire_thenReLookupBack () throws Exception {
91- final String domain = "github.com" ;
103+ final List < DnsCacheEntry > cache = DnsCacheManipulator . listDnsCache ();
104+ System . out . println ( "test_canSetNotExistedDomain_RemoveThenReLookupAndNotExisted expired entries: " + cache );
105+ assertTrue ( cache . isEmpty ()) ;
92106
93- final String ip = InetAddress .getByName (domain ).getHostAddress ();
94-
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 ());
107+ final List <DnsCacheEntry > negativeCache = DnsCacheManipulator .getWholeDnsCache ().getNegativeCache ();
108+ assertEquals (1 , negativeCache .size ());
109+ System .out .println ("test_canSetNotExistedDomain_RemoveThenReLookupAndNotExisted expired negative entries: " + negativeCache );
110+ assertEquals (DOMAIN_NOT_EXISTED .toLowerCase (), negativeCache .get (0 ).getHost ());
101111 }
102112
103113 @ Test
104- public void test_DnsCache_setNotExistedDomain_canExpire_thenReLookupAndNotExisted () throws Exception {
114+ public void test_setNotExistedDomain_canExpire_thenReLookupAndNotExisted () throws Exception {
105115 DnsCacheManipulator .setDnsCache (30 , DOMAIN_NOT_EXISTED , IP3 );
106116 final String ip = InetAddress .getByName (DOMAIN_NOT_EXISTED ).getHostAddress ();
107117 assertEquals (IP3 , ip );
@@ -115,6 +125,15 @@ public void test_DnsCache_setNotExistedDomain_canExpire_thenReLookupAndNotExiste
115125 System .out .println (expected .toString ());
116126 assertTrue (true );
117127 }
128+
129+ final List <DnsCacheEntry > cache = DnsCacheManipulator .listDnsCache ();
130+ System .out .println ("test_setNotExistedDomain_canExpire_thenReLookupAndNotExisted expired entries: " + cache );
131+ assertTrue (cache .isEmpty ());
132+
133+ final List <DnsCacheEntry > negativeCache = DnsCacheManipulator .getWholeDnsCache ().getNegativeCache ();
134+ System .out .println ("test_setNotExistedDomain_canExpire_thenReLookupAndNotExisted expired negative entries: " + negativeCache );
135+ assertEquals (1 , negativeCache .size ());
136+ assertEquals (DOMAIN_NOT_EXISTED .toLowerCase (), negativeCache .get (0 ).getHost ());
118137 }
119138
120139 @ Test
0 commit comments