KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > test > GetoptsTest


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