1
|
|
package abbot.script;
|
2
|
|
|
3
|
|
import java.util.Arrays;
|
4
|
|
import junit.extensions.abbot.*;
|
5
|
|
import junit.framework.TestCase;
|
6
|
|
import abbot.script.parsers.Parser;
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
public class ArgumentParserTest extends ResolverFixture {
|
11
|
|
|
12
|
1
|
public void testParseArgumentList() {
|
13
|
1
|
String[][] expected = {
|
14
|
|
{ "one" },
|
15
|
|
{ "one", "" },
|
16
|
|
{ "", "two" },
|
17
|
|
{ "", "" },
|
18
|
|
{ "one", "two", "three" },
|
19
|
|
{ null, "one", " two " },
|
20
|
|
{ "one", " two", " \"three, four, five\"" },
|
21
|
|
{ "one", "two", "[three,three]",
|
22
|
|
"[four,four]", "[five%2cfive,six]" },
|
23
|
|
{
|
24
|
|
"one,one",
|
25
|
|
|
26
|
|
"[two]",
|
27
|
|
|
28
|
|
"[three%2cthree,three+]",
|
29
|
|
|
30
|
|
"four,]",
|
31
|
|
|
32
|
|
"\"[five,\"", },
|
33
|
|
};
|
34
|
1
|
String[] input = {
|
35
|
|
"one",
|
36
|
|
"one,",
|
37
|
|
",two",
|
38
|
|
",",
|
39
|
|
"one,two,three",
|
40
|
|
"null,one, two ",
|
41
|
|
"one, two, \"three, four, five\"",
|
42
|
|
"one,two,[three,three],[four,four],[five\\,five,six]",
|
43
|
|
"one\\,one,[two],[three%2cthree,three+],four\\,],\"[five,\""
|
44
|
|
};
|
45
|
1
|
for (int i=0;i < input.length;i++) {
|
46
|
9
|
String[] args = ArgumentParser.parseArgumentList(input[i]);
|
47
|
9
|
assertEquals("Wrong number of arguments parsed from '"
|
48
|
|
+ input[i] + "'",
|
49
|
|
expected[i].length, args.length);
|
50
|
9
|
for (int j=0;j < args.length;j++) {
|
51
|
26
|
assertEquals("Badly parsed", expected[i][j], args[j]);
|
52
|
|
}
|
53
|
|
}
|
54
|
|
}
|
55
|
|
|
56
|
1
|
public void testSubstitution() {
|
57
|
|
|
58
|
1
|
String PROP_NAME = "my.prop";
|
59
|
1
|
String PROP_VALUE = "value";
|
60
|
|
|
61
|
1
|
String PROP2_NAME = "p2";
|
62
|
1
|
String PROP2_VALUE = "${my.prop}${my.prop}";
|
63
|
1
|
getResolver().setProperty(PROP_NAME, PROP_VALUE);
|
64
|
1
|
getResolver().setProperty(PROP2_NAME, PROP2_VALUE);
|
65
|
1
|
String[][] args = {
|
66
|
|
{ "${my.prop}", PROP_VALUE },
|
67
|
|
{ "X${my.prop}", "X" + PROP_VALUE },
|
68
|
|
{ "${my.prop}X", PROP_VALUE + "X" },
|
69
|
|
{ "${my.prop", "${my.prop" },
|
70
|
|
{ "${my.prop}${my.prop}", PROP_VALUE + PROP_VALUE },
|
71
|
|
{ "A${my.prop}:${my.prop}B${",
|
72
|
|
"A" + PROP_VALUE + ":" + PROP_VALUE + "B${" },
|
73
|
|
{ "${no.prop}${my.prop}", "${no.prop}" + PROP_VALUE },
|
74
|
|
{ "${p2}", PROP2_VALUE },
|
75
|
|
};
|
76
|
1
|
for (int i=0;i < args.length;i++) {
|
77
|
8
|
assertEquals("Bad substitution", args[i][1],
|
78
|
|
ArgumentParser.substitute(getResolver(), args[i][0]));
|
79
|
|
}
|
80
|
|
}
|
81
|
|
|
82
|
1
|
public void testEvalWithSubstitution() throws Throwable {
|
83
|
1
|
getResolver().setProperty("my.prop", "value");
|
84
|
1
|
assertEquals("Bad substitution", "value",
|
85
|
|
ArgumentParser.eval(getResolver(), "${my.prop}",
|
86
|
|
String.class));
|
87
|
|
}
|
88
|
|
|
89
|
1
|
public void testEncodeDecodeArguments() {
|
90
|
1
|
String[] args = {
|
91
|
|
"one", "[two, two+]", "three%2cthree", "four,four"
|
92
|
|
};
|
93
|
1
|
String encoded = ArgumentParser.encodeArguments(args);
|
94
|
1
|
assertEquals("Wrong encoding",
|
95
|
|
"one,[two, two+],three%%2Cthree,four%2cfour", encoded);
|
96
|
1
|
String[] decoded = ArgumentParser.parseArgumentList(encoded);
|
97
|
1
|
for (int i=0;i < args.length;i++) {
|
98
|
4
|
assertEquals("Wrong decode arg " + i, args[i], decoded[i]);
|
99
|
|
}
|
100
|
|
}
|
101
|
|
|
102
|
1
|
public void testReplace() {
|
103
|
1
|
String s1 = "A string with some stuff to replace";
|
104
|
1
|
String s2 = "A XXring with some XXuff to replace";
|
105
|
1
|
assertEquals("Bad replacement", s2,
|
106
|
|
ArgumentParser.replace(s1, "st", "XX"));
|
107
|
|
}
|
108
|
|
|
109
|
1
|
public void testLoadParser() {
|
110
|
1
|
Parser cvt = ArgumentParser.getParser(java.io.File.class);
|
111
|
1
|
assertEquals("Wrong class loaded",
|
112
|
|
abbot.script.parsers.FileParser.class, cvt.getClass());
|
113
|
|
}
|
114
|
|
|
115
|
1
|
public void testEvalInteger() throws Throwable {
|
116
|
1
|
abbot.script.Resolver resolver = getResolver();
|
117
|
1
|
assertEquals("Can't convert String to integer",
|
118
|
|
new Integer(1),
|
119
|
|
ArgumentParser.eval(resolver, "1", int.class));
|
120
|
1
|
assertEquals("Can't convert String to Integer",
|
121
|
|
new Integer(1),
|
122
|
|
ArgumentParser.eval(resolver, "1", Integer.class));
|
123
|
|
}
|
124
|
|
|
125
|
1
|
public void testToStringArray() throws Exception {
|
126
|
1
|
Object[][] samples = {
|
127
|
|
{ new String[] { "one", "two", "three", "four" },
|
128
|
|
"[one,two,three,four]" },
|
129
|
|
{ new int[] { 1, 2, 3, 4 },
|
130
|
|
"[1,2,3,4]" }
|
131
|
|
};
|
132
|
1
|
for (int i=0;i < samples.length;i++) {
|
133
|
2
|
Object array = samples[i][0];
|
134
|
2
|
assertEquals("Wrong array conversion sample " + i,
|
135
|
|
samples[i][1],
|
136
|
|
ArgumentParser.toString(array));
|
137
|
|
}
|
138
|
|
}
|
139
|
|
|
140
|
1
|
public void testToString() {
|
141
|
1
|
Object o = new Object();
|
142
|
1
|
Object o2 = new Object() {
|
143
|
1
|
public String toString() {
|
144
|
1
|
return null;
|
145
|
|
}
|
146
|
|
};
|
147
|
1
|
assertEquals("should be encoded default",
|
148
|
|
ArgumentParser.DEFAULT_TOSTRING,
|
149
|
|
ArgumentParser.toString(o));
|
150
|
1
|
assertEquals("should be encoded null",
|
151
|
|
ArgumentParser.NULL,
|
152
|
|
ArgumentParser.toString(o2));
|
153
|
|
}
|
154
|
|
|
155
|
1
|
public void testIsDefaultToString() {
|
156
|
1
|
assertFalse("null is not a default toString",
|
157
|
|
ArgumentParser.isDefaultToString(null));
|
158
|
|
}
|
159
|
|
|
160
|
|
|
161
|
10
|
public ArgumentParserTest(String name) {
|
162
|
10
|
super(name);
|
163
|
|
}
|
164
|
|
|
165
|
0
|
public static void main(String[] args) {
|
166
|
0
|
TestHelper.runTests(args, ArgumentParserTest.class);
|
167
|
|
}
|
168
|
|
}
|
169
|
|
|