|
| 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 | +} |
0 commit comments