Skip to content

Commit dd3e070

Browse files
committed
add unit test DcmToolTest
1 parent fca10f7 commit dd3e070

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

tool/src/main/java/com/alibaba/dcm/tool/DcmTool.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
public class DcmTool {
2121
static final String DCM_AGENT_SUCCESS_MARK_LINE = "!!DCM SUCCESS!!";
22+
static final String DCM_TOOLS_TMP_FILE = "DCM_TOOLS_TMP_FILE";
23+
static final String DCM_TOOLS_AGENT_JAR = "DCM_TOOLS_AGENT_JAR";
2224

2325
static List<String> actionList = new ArrayList<String>();
2426

@@ -35,10 +37,9 @@ public class DcmTool {
3537
actionList.add("getNegativePolicy");
3638
}
3739

38-
3940
public static void main(String[] args) throws Exception {
40-
final String tmpFile = System.getenv("DCM_TOOLS_TMP_FILE");
41-
final String agentJar = System.getenv("DCM_TOOLS_AGENT_JAR");
41+
final String tmpFile = getConfig(DCM_TOOLS_TMP_FILE);
42+
final String agentJar = getConfig(DCM_TOOLS_AGENT_JAR);
4243
if (tmpFile == null || tmpFile.trim().isEmpty() || agentJar == null || agentJar.trim().isEmpty()) {
4344
throw new IllegalStateException("blank tmp file " + tmpFile + ", or blank agent jar file " + agentJar);
4445
}
@@ -110,4 +111,12 @@ public static void main(String[] args) throws Exception {
110111
exit(1);
111112
}
112113
}
114+
115+
static String getConfig(String name) {
116+
String var = System.getenv(name);
117+
if (var == null) {
118+
var = System.getProperty(name);
119+
}
120+
return var;
121+
}
113122
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.alibaba.dcm.tool;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
import java.lang.management.ManagementFactory;
9+
import java.util.Iterator;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* @author oldratlee
15+
*/
16+
public class DcmToolTest {
17+
File outputFile;
18+
String outputFilePath;
19+
20+
String agentFilePath;
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
outputFile = new File("target/output.log");
25+
FileUtils.deleteQuietly(outputFile);
26+
FileUtils.touch(outputFile);
27+
assertTrue(outputFile.length() == 0);
28+
System.out.println("Prepared output file: " + outputFile.getAbsolutePath());
29+
30+
outputFilePath = outputFile.getAbsolutePath();
31+
32+
33+
File dcmLibProjectDir = new File("../library");
34+
if (!dcmLibProjectDir.exists()) {
35+
dcmLibProjectDir = new File("library");
36+
37+
if (!dcmLibProjectDir.exists()) {
38+
fail("Not found dcm lib project!");
39+
}
40+
}
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;
57+
}
58+
}
59+
60+
assertNotNull(agentFilePath);
61+
System.out.println("Found agent file: " + agentFilePath);
62+
}
63+
64+
@Test
65+
public void test_main() throws Exception {
66+
System.setProperty(DcmTool.DCM_TOOLS_TMP_FILE, outputFilePath);
67+
System.setProperty(DcmTool.DCM_TOOLS_AGENT_JAR, agentFilePath);
68+
69+
DcmTool.main(new String[]{"-p", pid(), "getPolicy"});
70+
}
71+
72+
static String pid() {
73+
final String name = ManagementFactory.getRuntimeMXBean().getName();
74+
final int idx = name.indexOf("@");
75+
return name.substring(0, idx);
76+
}
77+
}

tool/src/test/java/com/alibaba/dcm/tool/TestMainForAgentInject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
public class TestMainForAgentInject {
1010
public static void main(String[] args) throws Exception {
1111
while (true) {
12-
System.out.println(new Date());
13-
System.out.printf("\tbaidu.com: %s\n\n",
12+
System.out.printf("%s: baidu.com: %s\n", new Date(),
1413
InetAddress.getByName("baidu.com").getHostAddress());
14+
1515
Thread.sleep(1000);
1616
}
1717
}

0 commit comments

Comments
 (0)