Skip to content

Commit 1db2c6a

Browse files
committed
clean code alibaba#24
1 parent 783e36f commit 1db2c6a

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

library/src/main/java/com/alibaba/dcm/agent/DcmAgent.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ public class DcmAgent {
1919
public static final String FILE = "file";
2020

2121
public static void agentmain(String agentArgument) throws Exception {
22+
System.out.printf("Run %s!\n", DcmAgent.class.getName());
23+
2224
agentArgument = agentArgument.trim();
2325
if (agentArgument.isEmpty()) {
2426
System.out.println(DcmAgent.class.getName() + ": empty agent argument, do nothing!");
2527
return;
2628
}
29+
System.out.println("Arguments: " + agentArgument);
2730

2831
initAction2Method();
2932

@@ -32,23 +35,36 @@ public static void agentmain(String agentArgument) throws Exception {
3235
try {
3336
final Map<String, List<String>> action2Arguments = parseArgument(agentArgument);
3437
if (action2Arguments.containsKey(FILE)) {
35-
outputStream = new FileOutputStream(action2Arguments.get(FILE).get(0), true);
38+
outputStream = new FileOutputStream(action2Arguments.get(FILE).get(0), false);
3639
final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
3740

3841
writer = new PrintWriter(outputStreamWriter, true);
3942
action2Arguments.remove(FILE);
4043
}
41-
writer.printf("Run %s!\n", DcmAgent.class.getName());
42-
writer.println("Arguments: " + agentArgument);
4344

4445
if (action2Arguments.isEmpty()) {
45-
writer.println(DcmAgent.class.getName() + ": No action in agent argument, do nothing!");
46+
System.out.println(DcmAgent.class.getName() + ": No action in agent argument, do nothing!");
47+
writer.println("No action in agent argument, do nothing! agent argument: " + agentArgument);
48+
return;
4649
}
4750

4851
for (Map.Entry<String, List<String>> entry : action2Arguments.entrySet()) {
4952
final String action = entry.getKey();
5053
final List<String> arguments = entry.getValue();
51-
doAction(action, arguments.toArray(new String[0]), writer);
54+
55+
if (!action2Method.containsKey(action)) {
56+
StringBuilder sb = new StringBuilder();
57+
for (String argument : arguments) {
58+
if (sb.length() > 0) {
59+
sb.append(" ");
60+
}
61+
sb.append(argument);
62+
}
63+
writer.printf("Unknown action %s! ignore %<s %s !\n", action, sb);
64+
}
65+
66+
final Object result = doAction(action, arguments.toArray(new String[0]));
67+
printResult(action, result, writer);
5268
}
5369
} finally {
5470
if (outputStream != null) {
@@ -63,7 +79,7 @@ static Map<String, List<String>> parseArgument(String argument) {
6379
int idx = 0;
6480
Map<String, List<String>> action2Arguments = new HashMap<String, List<String>>();
6581
while (idx < split.length) {
66-
final String action = split[idx++].toLowerCase();
82+
final String action = split[idx++];
6783
if (!action2Method.containsKey(action)) {
6884
continue; // TODO error message
6985
}
@@ -81,26 +97,16 @@ static Map<String, List<String>> parseArgument(String argument) {
8197
return action2Arguments;
8298
}
8399

84-
static void doAction(String action, String[] arguments, PrintWriter writer) throws Exception {
100+
static Object doAction(String action, String[] arguments) throws Exception {
85101
Method method = action2Method.get(action);
86102

87-
if (method == null) {
88-
StringBuilder sb = new StringBuilder();
89-
for (String argument : arguments) {
90-
if (sb.length() > 0) {
91-
sb.append(" ");
92-
}
93-
sb.append(argument);
94-
}
95-
writer.printf("Unknown action %s! ignore %<s %s !\n", action, sb);
96-
}
97-
98103
final Class<?>[] parameterTypes = method.getParameterTypes();
99104
final Object[] methodArgs = convertStringArray2Arguments(action, arguments, parameterTypes);
100-
final Object ret = method.invoke(null, methodArgs);
101-
if (ret != null) {
102-
writer.println(ret);
103-
}
105+
return method.invoke(null, methodArgs);
106+
}
107+
108+
static void printResult(String action, Object result, PrintWriter writer) {
109+
104110
}
105111

106112
static volatile Map<String, Method> action2Method;

library/src/test/java/com/alibaba/dcm/agent/DcmAgentTest.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
import java.io.File;
99

10+
import static org.hamcrest.CoreMatchers.startsWith;
1011
import static org.junit.Assert.assertArrayEquals;
1112
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertThat;
1214
import static org.junit.Assert.assertTrue;
15+
import static org.junit.Assert.fail;
1316

1417
/**
1518
* @author Jerry Lee (oldratlee at gmail dot com)
@@ -75,43 +78,53 @@ public void test_agentmain_clear() throws Exception {
7578

7679
@Test
7780
public void test_agentmain_setPolicy() throws Exception {
78-
DcmAgent.agentmain(" setPolicy 5 ");
79-
assertEquals(5, DnsCacheManipulator.getDnsCachePolicy());
81+
DcmAgent.agentmain(" setPolicy 345 ");
82+
assertEquals(345, DnsCacheManipulator.getDnsCachePolicy());
8083
}
8184

8285
@Test
8386
public void test_agentmain_getPolicy() throws Exception {
84-
DnsCacheManipulator.setDnsCachePolicy(77);
85-
DcmAgent.agentmain(" getPolicy 5 ");
86-
assertEquals(77, DnsCacheManipulator.getDnsCachePolicy());
87+
DnsCacheManipulator.setDnsCachePolicy(456);
88+
DcmAgent.agentmain(" getPolicy ");
89+
assertEquals(456, DnsCacheManipulator.getDnsCachePolicy());
8790
}
8891

8992
@Test
9093
public void test_agentmain_setNegativePolicy() throws Exception {
91-
DcmAgent.agentmain(" setNegativePolicy 42 ");
94+
DcmAgent.agentmain(" setNegativePolicy 42 ");
9295
assertEquals(42, DnsCacheManipulator.getDnsNegativeCachePolicy());
9396
}
9497

9598
@Test
9699
public void test_agentmain_getNegativePolicy() throws Exception {
97100
DnsCacheManipulator.setDnsNegativeCachePolicy(45);
98-
DcmAgent.agentmain(" getNegativePolicy 5 ");
101+
DcmAgent.agentmain(" getNegativePolicy");
99102
assertEquals(45, DnsCacheManipulator.getDnsNegativeCachePolicy());
100103
}
101-
104+
102105
@Test
103106
public void test_agentmain_skipNoActionArguments() throws Exception {
104107
DcmAgent.agentmain(" arg1 arg2 ");
105108
}
106-
109+
107110
@Test
108111
public void test_agentmain_actionNeedMoreArgument() throws Exception {
109-
DcmAgent.agentmain(" setNegativePolicy ");
112+
try {
113+
DcmAgent.agentmain(" setNegativePolicy ");
114+
fail();
115+
} catch (IllegalStateException expected) {
116+
assertThat(expected.getMessage(), startsWith("action setNegativePolicy need more argument"));
117+
}
110118
}
111-
119+
112120
@Test
113121
public void test_agentmain_actionTooMoreArgument() throws Exception {
114-
DcmAgent.agentmain(" setNegativePolicy 737 HaHa ");
122+
try {
123+
DcmAgent.agentmain(" setNegativePolicy 737 HaHa ");
124+
fail();
125+
} catch (IllegalStateException expected) {
126+
assertThat(expected.getMessage(), startsWith("Too more arguments for Action"));
127+
}
115128
}
116129

117130
@Test

0 commit comments

Comments
 (0)