22
33import com .alibaba .dcm .DnsCacheManipulator ;
44import org .apache .commons .io .FileUtils ;
5+ import org .junit .After ;
56import org .junit .Before ;
67import org .junit .Test ;
78
89import java .io .File ;
10+ import java .util .List ;
911
12+ import static org .hamcrest .CoreMatchers .containsString ;
1013import static org .hamcrest .CoreMatchers .startsWith ;
1114import static org .junit .Assert .assertArrayEquals ;
1215import static org .junit .Assert .assertEquals ;
1922 */
2023public class DcmAgentTest {
2124 File outputFile ;
25+ String outputFilePath ;
2226
2327 @ Before
2428 public void setUp () throws Exception {
@@ -28,15 +32,17 @@ public void setUp() throws Exception {
2832 assertTrue (outputFile .length () == 0 );
2933 System .out .println ("Prepared output file: " + outputFile .getAbsolutePath ());
3034
35+ outputFilePath = outputFile .getAbsolutePath ();
36+
3137 DnsCacheManipulator .clearDnsCache ();
3238 }
3339
34- void checkOutputFile () throws Exception {
40+ @ After
41+ public void tearDown () throws Exception {
3542 System .out .println ("============================================" );
3643 System .out .println ("Agent Output File Content" );
3744 System .out .println ("============================================" );
3845 final String text = FileUtils .readFileToString (outputFile );
39- assertTrue (text .length () > 0 );
4046 System .out .println (text );
4147 }
4248
@@ -47,10 +53,10 @@ public void test_agentmain_empty() throws Exception {
4753
4854 @ Test
4955 public void test_agentmain_file () throws Exception {
50- final String output = outputFile .getAbsolutePath ();
51- DcmAgent .agentmain ("file " + output );
56+ DcmAgent .agentmain ("file " + outputFilePath );
5257
53- checkOutputFile ();
58+ final List <String > content = FileUtils .readLines (outputFile );
59+ assertThat (content .get (0 ), containsString ("No action in agent argument, do nothing!" ));
5460 }
5561
5662 @ Test
@@ -61,11 +67,12 @@ public void test_agentmain_set() throws Exception {
6167
6268 @ Test
6369 public void test_agentmain_set_toFile () throws Exception {
64- final String output = outputFile .getAbsolutePath ();
65- DcmAgent .agentmain ("set baidu.com 1.2.3.4 file " + output );
70+ DcmAgent .agentmain ("set baidu.com 1.2.3.4 file " + outputFilePath );
6671 assertEquals ("1.2.3.4" , DnsCacheManipulator .getDnsCache ("baidu.com" ).getIp ());
67-
68- checkOutputFile ();
72+
73+ final List <String > content = FileUtils .readLines (outputFile );
74+ assertThat (content .get (0 ), containsString ("set DONE." ));
75+ assertEquals (DcmAgent .DCM_AGENT_SUCCESS_MARK_LINE , content .get (content .size () - 1 ));
6976 }
7077
7178 @ Test
@@ -125,26 +132,35 @@ public void test_agentmain_skipNoActionArguments() throws Exception {
125132
126133 @ Test
127134 public void test_agentmain_actionNeedMoreArgument () throws Exception {
128- try {
129- DcmAgent .agentmain (" setNegativePolicy " );
130- fail ();
131- } catch (IllegalStateException expected ) {
132- assertThat (expected .getMessage (), startsWith ("action setNegativePolicy need more argument" ));
133- }
135+ DnsCacheManipulator .setDnsNegativeCachePolicy (1110 );
136+
137+ DcmAgent .agentmain (" setNegativePolicy file " + outputFilePath );
138+
139+ assertEquals (1110 , DnsCacheManipulator .getDnsNegativeCachePolicy ());
140+
141+ final List <String > content = FileUtils .readLines (outputFile );
142+ assertThat (content .get (0 ), containsString ("Error to do action setNegativePolicy" ));
143+ assertThat (content .get (0 ), containsString ("action setNegativePolicy need more argument!" ));
134144 }
135145
136146 @ Test
137147 public void test_agentmain_actionTooMoreArgument () throws Exception {
138- try {
139- DcmAgent .agentmain (" setNegativePolicy 737 HaHa " );
140- fail ();
141- } catch (IllegalStateException expected ) {
142- assertThat (expected .getMessage (), startsWith ("Too more arguments for Action" ));
143- }
148+ DnsCacheManipulator .setDnsNegativeCachePolicy (1111 );
149+
150+ DcmAgent .agentmain (" setNegativePolicy 737 HaHa file " + outputFilePath );
151+
152+ assertEquals (1111 , DnsCacheManipulator .getDnsNegativeCachePolicy ());
153+
154+ final List <String > content = FileUtils .readLines (outputFile );
155+ assertThat (content .get (0 ), containsString ("Error to do action setNegativePolicy 737 HaHa" ));
156+ assertThat (content .get (0 ), containsString ("Too more arguments for Action setNegativePolicy! arguments: [737, HaHa]" ));
144157 }
145158
146159 @ Test
147160 public void test_agentmain_unknownAction () throws Exception {
148- DcmAgent .agentmain (" unknownAction arg1 arg2 " );
161+ DcmAgent .agentmain (" unknownAction arg1 arg2 file " + outputFilePath );
162+
163+ final List <String > content = FileUtils .readLines (outputFile );
164+ assertThat (content .get (0 ), containsString ("No action in agent argument, do nothing!" ));
149165 }
150166}
0 commit comments