DcmAgent { public static void agentmain(String agentArgument) throws Exception { System.out.printf("%s: attached with agent argument: %s.\n", DcmAgent.class.getName(), agentArgument); agentArgument = agentArgument.trim(); if (agentArgument.isEmpty()) { System.out.println(DcmAgent.class.getName() + ": agent argument is blank, do nothing!"); return; } initAction2Method(); FileOutputStream fileOutputStream = null; try { final Map<String, List<String>> action2Arguments = parseAgentArgument(agentArgument); PrintWriter filePrinter = null; if (action2Arguments.containsKey(FILE)) { fileOutputStream = new FileOutputStream(action2Arguments.get(FILE).get(0), false); final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); filePrinter = new PrintWriter(outputStreamWriter, true); action2Arguments.remove(FILE); } if (action2Arguments.isEmpty()) { System.out.println(DcmAgent.class.getName() + ": No action in agent argument, do nothing!"); if (filePrinter != null) { filePrinter.printf("No action in agent argument, do nothing! agent argument: %s.\n", agentArgument); } return; } boolean allSuccess = true; for (Map.Entry<String, List<String>> entry : action2Arguments.entrySet()) { final String action = entry.getKey(); final List<String> arguments = entry.getValue(); final String argumentString = join(arguments); if (!action2Method.containsKey(action)) { System.out.printf("%s: Unknown action %s, ignore! action: %<s %s!\n", DcmAgent.class.getName(), action, argumentString); if (filePrinter != null) { filePrinter.printf("Unknown action %s, ignore! action: %<s %s !\n", action, argumentString); } continue; } try { final Object result = doAction(action, arguments.toArray(new String[0])); printResult(action, result, filePrinter); } catch (Exception e) { allSuccess = false; final String exString = throwable2StackString(e); System.out.printf("%s: Error to do action %s %s, cause: %s\n", DcmAgent.class.getName(), action, argumentString, exString); if (filePrinter != null) { filePrinter.printf("Error to do action %s %s, cause: %s\n", action, argumentString, exString); } } } if (allSuccess && filePrinter != null) { filePrinter.println(DCM_AGENT_SUCCESS_MARK_LINE); } } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (Throwable e) { } } } }  static void agentmain(String agentArgument);  }
@Test public void test_agentmain_empty() throws Exception { DcmAgent.agentmain(" "); }
@Test public void test_agentmain_setPolicy() throws Exception { DcmAgent.agentmain(" setPolicy 345 "); assertEquals(345, DnsCacheManipulator.getDnsCachePolicy()); }
@Test public void test_agentmain_getPolicy() throws Exception { DnsCacheManipulator.setDnsCachePolicy(456); DcmAgent.agentmain(" getPolicy "); assertEquals(456, DnsCacheManipulator.getDnsCachePolicy()); }
@Test public void test_agentmain_setNegativePolicy() throws Exception { DcmAgent.agentmain(" setNegativePolicy 42 "); assertEquals(42, DnsCacheManipulator.getDnsNegativeCachePolicy()); }
@Test public void test_agentmain_getNegativePolicy() throws Exception { DnsCacheManipulator.setDnsNegativeCachePolicy(45); DcmAgent.agentmain(" getNegativePolicy"); assertEquals(45, DnsCacheManipulator.getDnsNegativeCachePolicy()); }
@Test public void test_agentmain_skipNoActionArguments() throws Exception { DcmAgent.agentmain(" arg1 arg2 "); }
@Test public void test_agentmain_actionNeedMoreArgument() throws Exception { DnsCacheManipulator.setDnsNegativeCachePolicy(1110); DcmAgent.agentmain(" setNegativePolicy file " + outputFilePath); assertEquals(1110, DnsCacheManipulator.getDnsNegativeCachePolicy()); final List<String> content = FileUtils.readLines(outputFile); assertThat(content.get(0), containsString("Error to do action setNegativePolicy")); assertThat(content.get(0), containsString("action setNegativePolicy need more argument!")); }
@Test public void test_agentmain_actionTooMoreArgument() throws Exception { DnsCacheManipulator.setDnsNegativeCachePolicy(1111); DcmAgent.agentmain(" setNegativePolicy 737 HaHa file " + outputFilePath); assertEquals(1111, DnsCacheManipulator.getDnsNegativeCachePolicy()); final List<String> content = FileUtils.readLines(outputFile); assertThat(content.get(0), containsString("Error to do action setNegativePolicy 737 HaHa")); assertThat(content.get(0), containsString("Too more arguments for Action setNegativePolicy! arguments: [737, HaHa]")); }
@Test public void test_agentmain_unknownAction() throws Exception { DcmAgent.agentmain(" unknownAction arg1 arg2 file " + outputFilePath); final List<String> content = FileUtils.readLines(outputFile); assertThat(content.get(0), containsString("No action in agent argument, do nothing!")); }
@Test public void test_agentmain_file() throws Exception { DcmAgent.agentmain("file " + outputFilePath); final List<String> content = FileUtils.readLines(outputFile); assertThat(content.get(0), containsString("No action in agent argument, do nothing!")); }
@Test public void test_agentmain_set() throws Exception { DcmAgent.agentmain("set baidu.com 1.2.3.4"); assertEquals("1.2.3.4", DnsCacheManipulator.getDnsCache("baidu.com").getIp()); }
@Test public void test_agentmain_set_toFile() throws Exception { DcmAgent.agentmain("set baidu.com 1.2.3.4 file " + outputFilePath); assertEquals("1.2.3.4", DnsCacheManipulator.getDnsCache("baidu.com").getIp()); final List<String> content = FileUtils.readLines(outputFile); assertEquals(DcmAgent.DCM_AGENT_SUCCESS_MARK_LINE, content.get(content.size() - 1)); }
@Test public void test_agentmain_set_MultiIp() throws Exception { DcmAgent.agentmain("set baidu.com 1.1.1.1 2.2.2.2"); assertArrayEquals(new String[]{"1.1.1.1", "2.2.2.2"}, DnsCacheManipulator.getDnsCache("baidu.com").getIps()); }
@Test public void test_agentmain_get() throws Exception { DnsCacheManipulator.setDnsCache("baidu.com", "3.3.3.3"); DcmAgent.agentmain("get baidu.com"); }
@Test public void test_agentmain_rm() throws Exception { DnsCacheManipulator.setDnsCache("baidu.com", "3.3.3.3"); DcmAgent.agentmain("rm baidu.com"); assertNull(DnsCacheManipulator.getDnsCache("baidu.com")); }
@Test public void test_agentmain_rm_withFile() throws Exception { DnsCacheManipulator.setDnsCache("baidu.com", "3.3.3.3"); assertNotNull(DnsCacheManipulator.getDnsCache("baidu.com")); DcmAgent.agentmain("rm baidu.com file " + outputFilePath); assertNull(DnsCacheManipulator.getDnsCache("baidu.com")); }
@Test public void test_agentmain_list() throws Exception { DcmAgent.agentmain(" list "); }
@Test public void test_agentmain_clear() throws Exception { DnsCacheManipulator.setDnsCache("baidu.com", "3.3.3.3"); DcmAgent.agentmain(" clear "); assertEquals(0, DnsCacheManipulator.listDnsCache().size()); }