66
77import java .io .File ;
88import java .lang .management .ManagementFactory ;
9+ import java .net .InetAddress ;
10+ import java .util .ArrayList ;
11+ import java .util .Collections ;
912import java .util .Iterator ;
13+ import java .util .List ;
1014
1115import static org .junit .Assert .*;
1216
1317/**
14- * @author oldratlee
18+ * @author Jerry Lee ( oldratlee at gmail dot com)
1519 */
1620public class DcmToolTest {
1721 File outputFile ;
@@ -25,50 +29,87 @@ public void setUp() throws Exception {
2529 FileUtils .deleteQuietly (outputFile );
2630 FileUtils .touch (outputFile );
2731 assertTrue (outputFile .length () == 0 );
28- System .out .println ("Prepared output file: " + outputFile .getAbsolutePath ());
2932
30- outputFilePath = outputFile .getAbsolutePath ();
33+ outputFilePath = outputFile .getCanonicalPath ();
34+ System .out .println ("Prepared output file: " + outputFilePath );
3135
36+ setAgentFilePath ();
37+ assertNotNull (agentFilePath );
38+ System .out .println ("Found agent file: " + agentFilePath );
39+ }
3240
41+ public void setAgentFilePath () throws Exception {
42+ // find agent jar file from dcm lib project
3343 File dcmLibProjectDir = new File ("../library" );
3444 if (!dcmLibProjectDir .exists ()) {
3545 dcmLibProjectDir = new File ("library" );
46+ }
47+ if (dcmLibProjectDir .exists ()) {
48+ File dcmLibProjectTargetDir = new File (dcmLibProjectDir , "target" );
49+ if (dcmLibProjectTargetDir .exists ()) {
50+ System .out .println ("Found target dir: " + dcmLibProjectTargetDir );
51+ }
3652
37- if (!dcmLibProjectDir .exists ()) {
38- fail ("Not found dcm lib project!" );
53+ final Iterator <File > fileIterator = FileUtils .iterateFiles (dcmLibProjectTargetDir , new String []{"jar" }, false );
54+ while (fileIterator .hasNext ()) {
55+ final File next = fileIterator .next ();
56+ final String fileName = next .getName ();
57+ final String agentJarPath = next .getCanonicalPath ();
58+
59+ if (fileName .startsWith ("dns-cache-manipulator" )) {
60+ final String replaced = fileName .replace ("dns-cache-manipulator-" , "" ).replace ("-SNAPSHOT" , "" );
61+ if (!replaced .contains ("-" ))
62+ agentFilePath = agentJarPath ;
63+ }
3964 }
4065 }
41- File dcmLibProjectTargetDir = new File (dcmLibProjectDir , "target" );
42- if (!dcmLibProjectTargetDir .exists ()) {
43- fail ("Not found dcm lib project target dir!" );
44- }
45- System .out .println ("Found target dir: " + dcmLibProjectTargetDir );
46-
47- final Iterator <File > fileIterator = FileUtils .iterateFiles (dcmLibProjectTargetDir , new String []{"jar" }, false );
48- while (fileIterator .hasNext ()) {
49- final File next = fileIterator .next ();
50- final String fileName = next .getName ();
51- final String absolutePath = next .getAbsolutePath ();
52-
53- if (fileName .startsWith ("dns-cache-manipulator" )) {
54- final String replaced = fileName .replace ("dns-cache-manipulator-" , "" ).replace ("-SNAPSHOT" , "" );
55- if (!replaced .contains ("-" ))
56- agentFilePath = absolutePath ;
66+
67+ // find agent jar file from maven local repository
68+ if (agentFilePath == null ) {
69+ String home = System .getProperty ("user.home" );
70+ String m2DcmLibDependencyDir = home + "/.m2/repository/com/alibaba/dns-cache-manipulator" ;
71+
72+ List <String > list = new ArrayList <String >();
73+ final Iterator <File > fileIterator = FileUtils .iterateFiles (new File (m2DcmLibDependencyDir ), new String []{"jar" }, true );
74+ while (fileIterator .hasNext ()) {
75+ final File next = fileIterator .next ();
76+ final String fileName = next .getName ();
77+ final String agentJarPath = next .getCanonicalPath ();
78+
79+ if (fileName .startsWith ("dns-cache-manipulator" )) {
80+ final String replaced = fileName .replace ("dns-cache-manipulator-" , "" ).replace ("-SNAPSHOT" , "" );
81+ if (!replaced .contains ("-" )) {
82+ list .add (agentJarPath );
83+ }
84+ }
5785 }
58- }
5986
60- assertNotNull (agentFilePath );
61- System .out .println ("Found agent file: " + agentFilePath );
87+ assertTrue (list .size () > 0 );
88+ Collections .sort (list );
89+
90+ System .out .println ("List Agent jar from .m2: " + list );
91+ agentFilePath = list .get (list .size () - 1 );
92+ }
6293 }
6394
6495 @ Test
65- public void test_main () throws Exception {
96+ public void test_main_getPolicy () throws Exception {
6697 System .setProperty (DcmTool .DCM_TOOLS_TMP_FILE , outputFilePath );
6798 System .setProperty (DcmTool .DCM_TOOLS_AGENT_JAR , agentFilePath );
6899
69100 DcmTool .main (new String []{"-p" , pid (), "getPolicy" });
70101 }
71102
103+ @ Test
104+ public void test_main_set () throws Exception {
105+ System .setProperty (DcmTool .DCM_TOOLS_TMP_FILE , outputFilePath );
106+ System .setProperty (DcmTool .DCM_TOOLS_AGENT_JAR , agentFilePath );
107+
108+ DcmTool .main (new String []{"-p" , pid (), "set" , "baidu.com" , "1.1.2.2" });
109+
110+ assertEquals ("1.1.2.2" , InetAddress .getByName ("baidu.com" ).getHostAddress ());
111+ }
112+
72113 static String pid () {
73114 final String name = ManagementFactory .getRuntimeMXBean ().getName ();
74115 final int idx = name .indexOf ("@" );
0 commit comments