001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.ArrayList;
030: import java.util.Arrays;
031: import java.util.Collection;
032: import java.util.Collections;
033: import java.util.Enumeration;
034: import java.util.LinkedHashMap;
035: import java.util.List;
036: import java.util.Map;
037: import java.util.Properties;
038: import java.util.Set;
039:
040: import junit.framework.TestCase;
041:
042: import org.cougaar.bootstrap.SystemProperties;
043:
044: public class TestArguments extends TestCase {
045:
046: // test a trivial case
047: public void test_parse0() {
048: Arguments args = new Arguments("foo=bar, x=y, num=1234");
049:
050: assertEquals(args.getString("foo"), "bar");
051: assertEquals(args.getString("x"), "y");
052: assertEquals(args.getInt("num"), 1234);
053: }
054:
055: // test the typical plugin usage w/ -D support
056: //
057: // e.g., run with:
058: // -Dorg.cougaar.util.TestArguments$MyPlugin.a=b
059: // -Dorg.cougaar.util.TestArguments$MyPlugin.foo=ignoreMe
060: // -Dorg.cougaar.util.TestArguments$MyBase.p=q
061: // -Dorg.cougaar.util.TestArguments$MyBase.a=hideMe
062: // -Djava.lang.Object.j=k
063: // -Djava.util.List.unrela=tedStuff
064: // and you should get:
065: // foo=bar, num=1234, a=b, p=q, j=k
066: public void test_parse1() {
067: Class cl = MyPlugin.class;
068: Collection c = Arrays.asList(new String[] { "foo=bar",
069: "num=1234" });
070:
071: Arguments args = new Arguments(c, cl);
072:
073: assertEquals(args.getString("foo"), "bar");
074: assertEquals(args.getInt("num"), 1234);
075:
076: Map<String, String> props = getProps(cl);
077: for (Map.Entry<String, String> me : props.entrySet()) {
078: String key = me.getKey();
079: if ("foo".equals(key) || "num".equals(key))
080: continue;
081: assertEquals(args.getString(key), me.getValue());
082: }
083: }
084:
085: private Map<String, String> getProps(Object o) {
086: if (o == null)
087: return Collections.emptyMap();
088: List<String> prefixes;
089: if (o instanceof Class) {
090: prefixes = new ArrayList<String>();
091: for (Class cl = (Class) o; cl != null; cl = cl
092: .getSuperclass()) {
093: prefixes.add(cl.getName() + ".");
094: }
095: } else {
096: prefixes = Collections.singletonList((String) o);
097: }
098: Map<String, String> ret = new LinkedHashMap<String, String>();
099: for (String s : prefixes) {
100: Properties props = SystemProperties
101: .getSystemPropertiesWithPrefix(s);
102: if (props == null || props.isEmpty())
103: continue;
104: for (Enumeration en = props.propertyNames(); en
105: .hasMoreElements();) {
106: String name = (String) en.nextElement();
107: if (!name.startsWith(s))
108: continue;
109: String key = name.substring(s.length());
110: if (key.length() <= 0)
111: continue;
112: if (ret.containsKey(key))
113: continue;
114: String value = props.getProperty(name);
115: ret.put(key, value);
116: }
117: }
118: return ret;
119: }
120:
121: // test non-string input types
122: public void test_parse2() {
123: Arguments a1 = new Arguments(new String[] { "alpha=beta",
124: "foo=bar", "x=y" });
125: assertEquals("{alpha=[beta], foo=[bar], x=[y]}", a1.toString());
126: Map<String, String> m = new LinkedHashMap<String, String>();
127: m.put("alpha", "zeta");
128: m.put("x", "z");
129: Arguments a2 = new Arguments(m, null, a1);
130: assertEquals("{alpha=[zeta], x=[z], foo=[bar]}", a2.toString());
131: }
132:
133: // test defaults and non-string types
134: public void test_parse3() {
135: Arguments args = new Arguments("one=1, filterMe=blah, two=2",
136: "org.MyPlugin.", "def=ault", "one, two, def, fromProp");
137:
138: String fromProp = SystemProperties
139: .getProperty("org.MyPlugin.fromProp");
140: boolean hasProp = (fromProp != null);
141:
142: assertTrue(args.size() == 3 + (hasProp ? 1 : 0));
143:
144: assertEquals("{one=[1], two=[2], "
145: + (hasProp ? "fromProp=[" + fromProp + "], " : "")
146: + "def=[ault]}", args.toString());
147:
148: assertEquals("1", args.getString("one"));
149: assertEquals("2", args.getString("two"));
150: assertEquals(Collections.singletonList("2"), args
151: .getStrings("two"));
152: assertEquals("ault", args.getString("def"));
153: assertEquals(null, args.getString("bar"));
154: assertEquals((hasProp ? Integer.parseInt(fromProp) : 1234),
155: args.getInt("fromProp", 1234));
156: }
157:
158: // test "split" method
159: public void test_split0() {
160: Arguments args = new Arguments("foo=f1, bar=b1, qux=q1,"
161: + "foo=f2, bar=b2, qux=q2," + "foo=f3, bar=b3, qux=q3");
162: List<Arguments> l = args.split();
163:
164: assertEquals(3, l.size());
165: assertEquals("{foo=[f1], bar=[b1], qux=[q1]}", l.get(0)
166: .toString());
167: assertEquals("{foo=[f2], bar=[b2], qux=[q2]}", l.get(1)
168: .toString());
169: assertEquals("{foo=[f3], bar=[b3], qux=[q3]}", l.get(2)
170: .toString());
171: }
172:
173: // test "split" method with mixed value sizes
174: public void test_split1() {
175: Arguments args = new Arguments("a=b," + "x=v0, x=v1, x=v2,"
176: + "z=42," + "p=q, p=r");
177: List<Arguments> l = args.split();
178:
179: assertEquals(3, l.size());
180: assertEquals("{a=[b], x=[v0], z=[42], p=[q]}", l.get(0)
181: .toString());
182: assertEquals("{x=[v1], p=[r]}", l.get(1).toString());
183: assertEquals("{x=[v2]}", l.get(2).toString());
184: }
185:
186: // test "swap" method
187: public void test_swap0() {
188: Arguments args = new Arguments("a=1, b=2, c=3");
189:
190: Arguments a2 = args.swap("a", "c");
191: assertEquals("{c=[1], a=[3], b=[2]}", a2.toString());
192:
193: a2 = args.swap("a", "x");
194: assertEquals("{x=[1], b=[2], c=[3]}", a2.toString());
195:
196: a2 = args.swap("x", "y");
197: assertEquals("{a=[1], b=[2], c=[3]}", a2.toString());
198: }
199:
200: // test "set" methods
201: public void test_set0() {
202: Arguments args = new Arguments("a=1, b=2, c=3");
203:
204: Arguments a2 = args.setString("a", "foo");
205: assertEquals("{a=[foo], b=[2], c=[3]}", a2.toString());
206:
207: a2 = args.setString("a", null);
208: assertEquals("{b=[2], c=[3]}", a2.toString());
209:
210: a2 = args.setStrings("a", Arrays.asList(new String[] { "foo",
211: "bar" }));
212: assertEquals("{a=[foo, bar], b=[2], c=[3]}", a2.toString());
213: }
214:
215: // test "callSetters" reflection
216: public void test_reflection0() {
217: String s = "l=1, l=2, s=x, i=123, dub=4.5, lng=6789, "
218: + "col=q, col=r, col=s, str=bar, integ=99, d=-3.1";
219: Arguments args = new Arguments("unk=alpha, " + s
220: + ", junk=beta, s=duplicate, i=42");
221: Foo f = new Foo();
222: Set<String> unset = args.callSetters(f);
223: assertEquals("{" + s + ", ignored=1234}", f.toString());
224: assertEquals("[unk, junk]", unset.toString());
225: }
226:
227: public static final class Foo {
228: public List l;
229: public String s = "bad";
230: public int i = -123;
231: public double dub = -4.56;
232: public long lng = -798;
233:
234: public int ignored = 1234;
235:
236: private Collection col;
237: private String str;
238: private int integ;
239: private double d;
240:
241: public void setCol(Collection c) {
242: col = c;
243: }
244:
245: public void setStr(String s) {
246: str = s;
247: }
248:
249: public void setInteg(int i) {
250: integ = i;
251: }
252:
253: public void setD(double d) {
254: this .d = d;
255: }
256:
257: public void setLng(long l) {
258: this .lng = l;
259: }
260:
261: public String toString() {
262: String ret = "{";
263: if (l != null) {
264: for (Object o : l) {
265: ret += "l=" + o + ", ";
266: }
267: }
268: ret += "s=" + s + ", ";
269: ret += "i=" + i + ", ";
270: ret += "dub=" + dub + ", ";
271: ret += "lng=" + lng + ", ";
272: if (col != null) {
273: for (Object o : col) {
274: ret += "col=" + o + ", ";
275: }
276: }
277: ret += "str=" + str + ", ";
278: ret += "integ=" + integ + ", ";
279: ret += "d=" + d + ", ";
280: ret += "ignored=" + ignored;
281: ret += "}";
282: return ret;
283: }
284: }
285:
286: // test "toString" formatting
287: public void test_format0() {
288: Arguments args = new Arguments("A=B, X=V0, X=V1, X=V2");
289:
290: assertEquals("{A=[B], X=[V0, V1, V2]}", args.toString());
291: assertEquals("the_A is the_B * the_X is the_V0", args.toString(
292: "the_$key is the_$value", " * "));
293: assertEquals("(A eq B) +\n(X eq [V0, V1, V2])", args.toString(
294: "($key eq $vals)", " +\n"));
295: assertEquals("A=B&X=V0&X=V1&X=V2", args.toString(
296: "$key=$veach", "&"));
297: assertEquals(("<argument name=\"A\" value=\"B\"/>\n"
298: + "<argument name=\"X\" value=\"V0\"/>\n"
299: + "<argument name=\"X\" value=\"V1\"/>\n"
300: + "<argument name=\"X\" value=\"V2\"/>"), args
301: .toString("<argument name=\"$key\" value=\"$veach\"/>",
302: "\n"));
303: assertEquals("A=[B], X=[V0, V1, V2]", args.toString(
304: "$key=$vlist", ", "));
305: }
306:
307: // test read-back from toString
308: public void test_format1() {
309: Arguments a1 = new Arguments("a=1, b=2a, c=3, b=2b");
310: String s = a1.toString("$key=$veach");
311: Arguments a2 = new Arguments(s);
312: assertEquals(a1, a2);
313: assertEquals(a1.toString(), a2.toString());
314: }
315:
316: // dummy classes for -D prefix testing:
317: public static class MyPlugin extends MyBase {
318: }
319:
320: public static class MyBase extends MyModel {
321: }
322:
323: public static class MyModel {
324: }
325: }
|