KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > console > CmdLineTest


1 package org.sapia.console;
2
3 import junit.framework.*;
4
5
6 /**
7  * @author Yanick Duchesne
8  * 5-May-2003
9  */

10 public class CmdLineTest extends TestCase {
11   CmdLine _cmd;
12
13   /**
14    * Constructor for CmdLineTest.
15    */

16   public CmdLineTest(String JavaDoc name) {
17     super(name);
18   }
19
20   /**
21    * @see junit.framework.TestCase#setUp()
22    */

23   protected void setUp() throws Exception JavaDoc {
24     _cmd = CmdLine.parse(
25         "arg1 arg2 -opt1 val1 arg3 -opt2 val2 -opt3 \"opt3 value\" -opt4 -opt5");
26   }
27
28   public void testFirst() throws Exception JavaDoc {
29     super.assertEquals("arg1", ((Arg) _cmd.first()).getName());
30   }
31
32   public void testLast() throws Exception JavaDoc {
33     super.assertEquals("opt5", ((Option) _cmd.last()).getName());
34   }
35
36   public void testFilterArgs() throws Exception JavaDoc {
37     CmdLine args = _cmd.filterArgs();
38     super.assertEquals(3, args.size());
39   }
40
41   public void testAssertNextArg() throws Exception JavaDoc {
42     CmdLine args = _cmd.filterArgs();
43
44     while (args.hasNext()) {
45       args.assertNextArg();
46     }
47   }
48
49   public void testAssertArgs() throws Exception JavaDoc {
50     CmdLine args = _cmd.filterArgs();
51     args.assertNextArg(new String JavaDoc[] { "arg1", "arg2", "arg3" });
52     args.assertNextArg(new String JavaDoc[] { "arg1", "arg2", "arg3" });
53     args.assertNextArg(new String JavaDoc[] { "arg1", "arg2", "arg3" });
54
55     try {
56       args.assertNextArg(new String JavaDoc[] { "arg1", "arg2", "arg3" });
57       throw new Exception JavaDoc("InputException should have been thrown");
58     } catch (InputException e) {
59       // ok
60
}
61   }
62
63   public void testContainsOption() throws Exception JavaDoc {
64     super.assertTrue("Option missing", _cmd.containsOption("opt4", false));
65     super.assertTrue("Option should not have been found",
66       !_cmd.containsOption("opt4", true));
67   }
68
69   public void testAssertOption() throws Exception JavaDoc {
70     _cmd.assertOption("opt3", true);
71     _cmd.assertOption("opt2", "val2");
72     _cmd.assertOption("opt2", new String JavaDoc[] { "val1", "val2", "val3" });
73   }
74
75   public void testChop() throws Exception JavaDoc {
76     int size = _cmd.size();
77     _cmd.chop();
78     super.assertEquals(size - 1, _cmd.size());
79   }
80
81   public void testChopArg() throws Exception JavaDoc {
82     super.assertEquals("arg1", _cmd.chopArg().getName());
83   }
84 }
85
Popular Tags