001: package abbot.script;
002:
003: import junit.extensions.abbot.*;
004: import abbot.script.parsers.Parser;
005:
006: /** Test ArgumentParser functions. */
007:
008: public class ArgumentParserTest extends ResolverFixture {
009:
010: public void testParseArgumentList() {
011: String[][] expected = {
012: { "one" }, // single
013: { "one", "" }, // trailing empty
014: { "", "two" }, // leading empty
015: { "", "" }, // two empty
016: { "one", "two", "three" }, // multiple
017: { null, "one", " two " }, // spaces
018: { "one", " two", " \"three, four, five\"" }, // quotes+commas
019: { "one", "two", "[three,three]", "[four,four]",
020: "[five%2cfive,six]" }, // arrays
021: { // comma should get unescaped in final arg
022: "one,one",
023: // brackets should be left unescaped after parsing
024: "[two]",
025: // comma within array should be left escaped
026: "[three%2cthree,three+]",
027: // shouldn't get confused by trailing brackets
028: "four,]",
029: // use quoting to protect arbitrary characters (strings or ids)
030: "\"[five,\"", }, };
031: String[] input = { "one", "one,", ",two", ",", "one,two,three",
032: "null,one, two ", "one, two, \"three, four, five\"",
033: "one,two,[three,three],[four,four],[five\\,five,six]",
034: "one\\,one,[two],[three%2cthree,three+],four\\,],\"[five,\"" };
035: for (int i = 0; i < input.length; i++) {
036: String[] args = ArgumentParser.parseArgumentList(input[i]);
037: assertEquals("Wrong number of arguments parsed from '"
038: + input[i] + "'", expected[i].length, args.length);
039: for (int j = 0; j < args.length; j++) {
040: assertEquals("Badly parsed", expected[i][j], args[j]);
041: }
042: }
043: }
044:
045: public void testSubstitution() {
046: // value shorter than name
047: String PROP_NAME = "my.prop";
048: String PROP_VALUE = "value";
049: // value longer than name
050: String PROP2_NAME = "p2";
051: String PROP2_VALUE = "${my.prop}${my.prop}";
052: getResolver().setProperty(PROP_NAME, PROP_VALUE);
053: getResolver().setProperty(PROP2_NAME, PROP2_VALUE);
054: String[][] args = {
055: { "${my.prop}", PROP_VALUE },
056: { "X${my.prop}", "X" + PROP_VALUE },
057: { "${my.prop}X", PROP_VALUE + "X" },
058: { "${my.prop", "${my.prop" },
059: { "${my.prop}${my.prop}", PROP_VALUE + PROP_VALUE },
060: { "A${my.prop}:${my.prop}B${",
061: "A" + PROP_VALUE + ":" + PROP_VALUE + "B${" },
062: { "${no.prop}${my.prop}", "${no.prop}" + PROP_VALUE },
063: { "${p2}", PROP2_VALUE }, // no recursion
064: };
065: for (int i = 0; i < args.length; i++) {
066: assertEquals("Bad substitution", args[i][1], ArgumentParser
067: .substitute(getResolver(), args[i][0]));
068: }
069: }
070:
071: public void testEvalWithSubstitution() throws Throwable {
072: getResolver().setProperty("my.prop", "value");
073: assertEquals("Bad substitution", "value", ArgumentParser.eval(
074: getResolver(), "${my.prop}", String.class));
075: }
076:
077: public void testEncodeDecodeArguments() {
078: String[] args = { "one", "[two, two+]", "three%2cthree",
079: "four,four" };
080: String encoded = ArgumentParser.encodeArguments(args);
081: assertEquals("Wrong encoding",
082: "one,[two, two+],three%%2Cthree,four%2cfour", encoded);
083: String[] decoded = ArgumentParser.parseArgumentList(encoded);
084: for (int i = 0; i < args.length; i++) {
085: assertEquals("Wrong decode arg " + i, args[i], decoded[i]);
086: }
087: }
088:
089: public void testReplace() {
090: String s1 = "A string with some stuff to replace";
091: String s2 = "A XXring with some XXuff to replace";
092: assertEquals("Bad replacement", s2, ArgumentParser.replace(s1,
093: "st", "XX"));
094: }
095:
096: public void testLoadParser() {
097: Parser cvt = ArgumentParser.getParser(java.io.File.class);
098: assertEquals("Wrong class loaded",
099: abbot.script.parsers.FileParser.class, cvt.getClass());
100: }
101:
102: public void testEvalInteger() throws Throwable {
103: abbot.script.Resolver resolver = getResolver();
104: assertEquals("Can't convert String to integer", new Integer(1),
105: ArgumentParser.eval(resolver, "1", int.class));
106: assertEquals("Can't convert String to Integer", new Integer(1),
107: ArgumentParser.eval(resolver, "1", Integer.class));
108: }
109:
110: public void testToStringArray() throws Exception {
111: Object[][] samples = {
112: { new String[] { "one", "two", "three", "four" },
113: "[one,two,three,four]" },
114: { new int[] { 1, 2, 3, 4 }, "[1,2,3,4]" } };
115: for (int i = 0; i < samples.length; i++) {
116: Object array = samples[i][0];
117: assertEquals("Wrong array conversion sample " + i,
118: samples[i][1], ArgumentParser.toString(array));
119: }
120: }
121:
122: public void testToString() {
123: Object o = new Object();
124: Object o2 = new Object() {
125: public String toString() {
126: return null;
127: }
128: };
129: assertEquals("should be encoded default",
130: ArgumentParser.DEFAULT_TOSTRING, ArgumentParser
131: .toString(o));
132: assertEquals("should be encoded null", ArgumentParser.NULL,
133: ArgumentParser.toString(o2));
134: }
135:
136: public void testIsDefaultToString() {
137: assertFalse("null is not a default toString", ArgumentParser
138: .isDefaultToString(null));
139: }
140:
141: /** Create a new test case with the given name. */
142: public ArgumentParserTest(String name) {
143: super (name);
144: }
145:
146: public static void main(String[] args) {
147: TestHelper.runTests(args, ArgumentParserTest.class);
148: }
149: }
|