KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > ParseTest


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  *
8  * $Id: ParseTest.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
9  */

10
11 package org.apache.commons.cli;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 public class ParseTest extends TestCase
18 {
19
20     private Options _options = null;
21     private CommandLineParser _parser = null;
22
23     public static Test suite() {
24         return new TestSuite(ParseTest.class);
25     }
26
27     public ParseTest(String JavaDoc name)
28     {
29         super(name);
30     }
31
32     public void setUp()
33     {
34         _options = new Options()
35             .addOption("a",
36                        "enable-a",
37                        false,
38                        "turn [a] on or off")
39             .addOption("b",
40                        "bfile",
41                        true,
42                        "set the value of [b]")
43             .addOption("c",
44                        "copt",
45                        false,
46                        "turn [c] on or off");
47
48         _parser = new PosixParser();
49     }
50
51     public void tearDown()
52     {
53
54     }
55
56     public void testSimpleShort()
57     {
58         String JavaDoc[] args = new String JavaDoc[] { "-a",
59                                        "-b", "toast",
60                                        "foo", "bar" };
61
62         try
63         {
64             CommandLine cl = _parser.parse(_options, args);
65             
66             assertTrue( "Confirm -a is set", cl.hasOption("a") );
67             assertTrue( "Confirm -b is set", cl.hasOption("b") );
68             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
69             assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
70         }
71         catch (ParseException e)
72         {
73             fail( e.toString() );
74         }
75     }
76
77     public void testSimpleLong()
78     {
79         String JavaDoc[] args = new String JavaDoc[] { "--enable-a",
80                                        "--bfile", "toast",
81                                        "foo", "bar" };
82
83         try
84         {
85             CommandLine cl = _parser.parse(_options, args);
86             
87             assertTrue( "Confirm -a is set", cl.hasOption("a") );
88             assertTrue( "Confirm -b is set", cl.hasOption("b") );
89             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
90             assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
91         }
92         catch (ParseException e)
93         {
94             fail( e.toString() );
95         }
96     }
97
98     public void testComplexShort()
99     {
100         String JavaDoc[] args = new String JavaDoc[] { "-acbtoast",
101                                        "foo", "bar" };
102
103         try
104         {
105             CommandLine cl = _parser.parse(_options, args);
106             
107             assertTrue( "Confirm -a is set", cl.hasOption("a") );
108             assertTrue( "Confirm -b is set", cl.hasOption("b") );
109             assertTrue( "Confirm -c is set", cl.hasOption("c") );
110             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
111             assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
112         }
113         catch (ParseException e)
114         {
115             fail( e.toString() );
116         }
117     }
118
119     public void testExtraOption()
120     {
121         String JavaDoc[] args = new String JavaDoc[] { "-adbtoast",
122                                        "foo", "bar" };
123
124         boolean caught = false;
125
126         try
127         {
128             CommandLine cl = _parser.parse(_options, args);
129             
130             assertTrue( "Confirm -a is set", cl.hasOption("a") );
131             assertTrue( "Confirm -b is set", cl.hasOption("b") );
132             assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") );
133             assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3);
134         }
135         catch (UnrecognizedOptionException e)
136         {
137             caught = true;
138         }
139         catch (ParseException e)
140         {
141             fail( e.toString() );
142         }
143         assertTrue( "Confirm UnrecognizedOptionException caught", caught );
144     }
145
146     public void testMissingArg()
147     {
148
149         String JavaDoc[] args = new String JavaDoc[] { "-acb" };
150
151         boolean caught = false;
152
153         try
154         {
155             CommandLine cl = _parser.parse(_options, args);
156         }
157         catch (MissingArgumentException e)
158         {
159             caught = true;
160         }
161         catch (ParseException e)
162         {
163             fail( e.toString() );
164         }
165
166         assertTrue( "Confirm MissingArgumentException caught", caught );
167     }
168
169     public void testStop()
170     {
171         String JavaDoc[] args = new String JavaDoc[] { "-c",
172                                        "foober",
173                                        "-btoast" };
174
175         try
176         {
177             CommandLine cl = _parser.parse(_options, args, true);
178             assertTrue( "Confirm -c is set", cl.hasOption("c") );
179             assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
180         }
181         catch (ParseException e)
182         {
183             fail( e.toString() );
184         }
185     }
186
187     public void testMultiple()
188     {
189         String JavaDoc[] args = new String JavaDoc[] { "-c",
190                                        "foobar",
191                                        "-btoast" };
192
193         try
194         {
195             CommandLine cl = _parser.parse(_options, args, true);
196             assertTrue( "Confirm -c is set", cl.hasOption("c") );
197             assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
198
199             cl = _parser.parse(_options, cl.getArgs() );
200
201             assertTrue( "Confirm -c is not set", ! cl.hasOption("c") );
202             assertTrue( "Confirm -b is set", cl.hasOption("b") );
203             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
204             assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
205             assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") );
206         }
207         catch (ParseException e)
208         {
209             fail( e.toString() );
210         }
211     }
212
213     public void testMultipleWithLong()
214     {
215         String JavaDoc[] args = new String JavaDoc[] { "--copt",
216                                        "foobar",
217                                        "--bfile", "toast" };
218
219         try
220         {
221             CommandLine cl = _parser.parse(_options,args,
222                                             true);
223             assertTrue( "Confirm -c is set", cl.hasOption("c") );
224             assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
225
226             cl = _parser.parse(_options, cl.getArgs() );
227
228             assertTrue( "Confirm -c is not set", ! cl.hasOption("c") );
229             assertTrue( "Confirm -b is set", cl.hasOption("b") );
230             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
231             assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
232             assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") );
233         }
234         catch (ParseException e)
235         {
236             fail( e.toString() );
237         }
238     }
239
240     public void testDoubleDash()
241     {
242         String JavaDoc[] args = new String JavaDoc[] { "--copt",
243                                        "--",
244                                        "-b", "toast" };
245
246         try
247         {
248             CommandLine cl = _parser.parse(_options, args);
249
250             assertTrue( "Confirm -c is set", cl.hasOption("c") );
251             assertTrue( "Confirm -b is not set", ! cl.hasOption("b") );
252             assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
253
254         }
255         catch (ParseException e)
256         {
257             fail( e.toString() );
258         }
259     }
260
261     public void testSingleDash()
262     {
263         String JavaDoc[] args = new String JavaDoc[] { "--copt",
264                                        "-b", "-",
265                                        "-a",
266                                        "-" };
267
268         try
269         {
270             CommandLine cl = _parser.parse(_options, args);
271
272             assertTrue( "Confirm -a is set", cl.hasOption("a") );
273             assertTrue( "Confirm -b is set", cl.hasOption("b") );
274             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("-") );
275             assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
276             assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") );
277         }
278         catch (ParseException e)
279         {
280             fail( e.toString() );
281         }
282         
283     }
284 }
285
Popular Tags