01: package jimm.datavision.test;
02:
03: import jimm.util.Getopts;
04: import junit.framework.TestCase;
05: import junit.framework.TestSuite;
06: import junit.framework.Test;
07:
08: public class GetoptsTest extends TestCase {
09:
10: protected static final String DEFAULT_ARGS_LIST[] = { "-a", "-b",
11: "-c", "-f", "farg", "-g", "-eearg", "non-option-arg one",
12: "non-option-arg two" };
13:
14: protected String[] args;
15: protected Getopts g;
16:
17: public static Test suite() {
18: return new TestSuite(GetoptsTest.class);
19: }
20:
21: public GetoptsTest(String name) {
22: super (name);
23: }
24:
25: public void setUp() {
26: args = DEFAULT_ARGS_LIST;
27: g = new Getopts("abcd:e:f:gh:i", args);
28: }
29:
30: public void testSimpleOptions() {
31: String[] args = { "-a", "-b", "-c", "-f", "farg", "-g",
32: "-eearg", "non-option-arg one", "non-option-arg two", };
33: Getopts g = new Getopts("abcd:e:f:gh:i", args);
34:
35: assertTrue(!g.error());
36:
37: assertTrue(g.hasOption('a'));
38: assertTrue(g.hasOption('b'));
39: assertTrue(g.hasOption('c'));
40: assertTrue(!g.hasOption('d'));
41: assertTrue(g.hasOption('e'));
42: assertTrue(g.hasOption('f'));
43: assertTrue(g.hasOption('g'));
44: assertTrue(!g.hasOption('h'));
45: assertTrue(!g.hasOption('i'));
46: }
47:
48: public void testIllegalArg() {
49: // Add new illegal argument -z to front of list
50: String[] argsWithIllegalValue = new String[args.length + 1];
51: System.arraycopy(args, 0, argsWithIllegalValue, 1, args.length);
52: argsWithIllegalValue[0] = "-z";
53: g = new Getopts("abcd:e:f:gh:i", argsWithIllegalValue);
54:
55: assertTrue(g.error()); // That -z doesn't belong
56: assertTrue(!g.hasOption('z'));
57: }
58:
59: public void testDefaultValues() {
60: assertEquals("", g.option('d'));
61: assertNull(g.option('d', null));
62: assertEquals("xyzzy", g.option('d', "xyzzy"));
63: assertEquals("earg", g.option('e'));
64: assertEquals("farg", g.option('f'));
65: }
66:
67: public void testRemainingArgs() {
68: assertEquals(2, g.argc());
69: assertEquals(args[args.length - 2], g.argv(0));
70: assertEquals(args[args.length - 1], g.argv(1));
71: }
72:
73: public void testSimpleCommandLine() {
74: String[] args = { "-p", "password", "filename" };
75: Getopts g = new Getopts("cdhlxnp:s:r:", args);
76:
77: assertTrue(!g.error());
78: assertEquals("password", g.option('p'));
79: assertEquals(1, g.argc());
80: assertEquals("filename", g.argv(0));
81: }
82:
83: public static void main(String[] args) {
84: junit.textui.TestRunner.run(suite());
85: System.exit(0);
86: }
87:
88: }
|