KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > cli > test > ClutilTestCase


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.txt file.
7  */

8 package org.apache.avalon.excalibur.cli.test;
9
10 import java.util.List JavaDoc;
11 import junit.framework.TestCase;
12 import org.apache.avalon.excalibur.cli.AbstractParserControl;
13 import org.apache.avalon.excalibur.cli.CLArgsParser;
14 import org.apache.avalon.excalibur.cli.CLOption;
15 import org.apache.avalon.excalibur.cli.CLOptionDescriptor;
16 import org.apache.avalon.excalibur.cli.ParserControl;
17
18 /**
19  *
20  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
21  */

22 public final class ClutilTestCase
23     extends TestCase
24 {
25     public ClutilTestCase()
26     {
27         this("Command Line Interpreter Test Case");
28     }
29
30     public ClutilTestCase( String JavaDoc name )
31     {
32         super( name );
33     }
34
35     private final static String JavaDoc[] ARGLIST1 =
36     {
37         "--you","are","--all","-cler","kid"
38     };
39
40     private final static String JavaDoc[] ARGLIST2 =
41     {
42         "-Dstupid=idiot","are","--all","here","-d"
43     };
44
45     private final static String JavaDoc[] ARGLIST3 =
46     {
47         //duplicates
48
"-Dstupid=idiot","are","--all","--all","here"
49     };
50
51     private final static String JavaDoc[] ARGLIST4 =
52     {
53         //incompatable (blee/all)
54
"-Dstupid=idiot","are","--all","--blee","here"
55     };
56
57     private final static String JavaDoc[] ARGLIST5 =
58     {
59         "-f","myfile.txt"
60     };
61
62     private static final int DEFINE_OPT = 'D';
63     private static final int CASE_CHECK_OPT = 'd';
64     private static final int YOU_OPT = 'y';
65     private static final int ALL_OPT = 'a';
66     private static final int CLEAR1_OPT = 'c';
67     private static final int CLEAR2_OPT = 'l';
68     private static final int CLEAR3_OPT = 'e';
69     private static final int CLEAR5_OPT = 'r';
70     private static final int BLEE_OPT = 'b';
71     private static final int FILE_OPT = 'f';
72     private static final int TAINT_OPT = 'T';
73
74     private final static CLOptionDescriptor DEFINE =
75         new CLOptionDescriptor( "define",
76                                 CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
77                                 DEFINE_OPT,
78                                 "define" );
79     private final static CLOptionDescriptor CASE_CHECK =
80         new CLOptionDescriptor( "charCheck",
81                                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
82                                 CASE_CHECK_OPT,
83                                 "check character case sensitivity");
84     private final static CLOptionDescriptor YOU =
85         new CLOptionDescriptor( "you", CLOptionDescriptor.ARGUMENT_DISALLOWED, YOU_OPT, "you" );
86
87     private final static CLOptionDescriptor ALL =
88         new CLOptionDescriptor( "all",
89                                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
90                                 ALL_OPT,
91                                 "all",
92                                 new int[] { BLEE_OPT } );
93
94     private final static CLOptionDescriptor CLEAR1 =
95         new CLOptionDescriptor( "c", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR1_OPT, "c" );
96     private final static CLOptionDescriptor CLEAR2 =
97         new CLOptionDescriptor( "l", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR2_OPT, "l" );
98     private final static CLOptionDescriptor CLEAR3 =
99         new CLOptionDescriptor( "e", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR3_OPT, "e" );
100     private final static CLOptionDescriptor CLEAR5 =
101         new CLOptionDescriptor( "r", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR5_OPT, "r" );
102     private final static CLOptionDescriptor BLEE =
103         new CLOptionDescriptor( "blee",
104                                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
105                                 BLEE_OPT,
106                                 "blee" );
107     private final static CLOptionDescriptor FILE =
108         new CLOptionDescriptor( "file",
109                                 CLOptionDescriptor.ARGUMENT_REQUIRED,
110                                 FILE_OPT,
111                                 "the build file." );
112     private final static CLOptionDescriptor TAINT =
113         new CLOptionDescriptor( "taint",
114                                 CLOptionDescriptor.ARGUMENT_OPTIONAL,
115                                 TAINT_OPT,
116                                 "turn on tainting checks (optional level)." );
117
118     public static void main( final String JavaDoc[] args )
119     {
120         final ClutilTestCase test = new ClutilTestCase();
121         test.testShortOptArgUnenteredBeforeOtherOpt();
122     }
123
124     public void testShortOptArgUnenteredBeforeOtherOpt()
125     {
126         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
127         {
128             ALL, TAINT
129         };
130
131         final String JavaDoc[] args = new String JavaDoc[] { "-T", "-a" };
132
133         //System.out.println("[before parsing]");
134
final CLArgsParser parser = new CLArgsParser( args, options );
135         //System.out.println("[after parsing]");
136

137         assertNull( parser.getErrorString(), parser.getErrorString() );
138
139         final List JavaDoc clOptions = parser.getArguments();
140         final int size = clOptions.size();
141
142         //System.out.println( new StringBuffer("clOptions: ").append(clOptions) );
143
assertEquals( size, 2 );
144
145         final CLOption option0 = (CLOption)clOptions.get( 0 );
146         assertEquals( "Option Code: " + option0.getId(), TAINT_OPT, option0.getId() );
147         assertEquals( "Option Arg: " + option0.getArgument( 0 ), null, option0.getArgument( 0 ) );
148
149         final CLOption option1 = (CLOption)clOptions.get( 1 );
150         assertEquals( option1.getId(), ALL_OPT );
151         assertEquals( option1.getArgument( 0 ), null );
152     }
153
154     public void testOptionalArgsWithArgShortBeforeOtherOpt()
155     {
156         //"-T3","-a"
157
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
158         {
159             ALL, TAINT
160         };
161
162         final String JavaDoc[] args = new String JavaDoc[] { "-T3", "-a" };
163
164         //System.out.println("[before parsing]");
165

166         final CLArgsParser parser = new CLArgsParser( args, options );
167
168         //System.out.println("[after parsing]");
169

170         assertNull( parser.getErrorString(), parser.getErrorString() );
171
172         final List JavaDoc clOptions = parser.getArguments();
173         final int size = clOptions.size();
174
175         assertEquals( size, 2 );
176         final CLOption option0 = (CLOption)clOptions.get( 0 );
177         assertEquals( option0.getId(), TAINT_OPT );
178         assertEquals( option0.getArgument( 0 ), "3" );
179
180         final CLOption option1 = (CLOption)clOptions.get( 1 );
181         assertEquals( ALL_OPT, option1.getId() );
182         assertEquals( null, option1.getArgument( 0 ) );
183     }
184
185     public void testOptionalArgsNoArgShortBeforeOtherOpt()
186     {
187         //"-T","-a"
188
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
189         {
190             ALL, TAINT
191         };
192
193         final String JavaDoc[] args = new String JavaDoc[] { "-T", "-a" };
194
195         //System.out.println("[before parsing]");
196

197         final CLArgsParser parser = new CLArgsParser( args, options );
198
199         //System.out.println("[after parsing]");
200

201         assertNull( parser.getErrorString(), parser.getErrorString() );
202
203         final List JavaDoc clOptions = parser.getArguments();
204         final int size = clOptions.size();
205
206         assertEquals( size, 2 );
207         final CLOption option0 = (CLOption)clOptions.get( 0 );
208         assertEquals( TAINT_OPT, option0.getId() );
209         assertEquals( null, option0.getArgument( 0 ) );
210
211         final CLOption option1 = (CLOption)clOptions.get( 1 );
212         assertEquals( ALL_OPT, option1.getId() );
213         assertEquals( null, option1.getArgument( 0 ) );
214     }
215
216     public void testFullParse()
217     {
218         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
219         {
220             YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
221         };
222
223         final CLArgsParser parser = new CLArgsParser( ARGLIST1, options );
224
225         assertNull( parser.getErrorString(), parser.getErrorString() );
226
227         final List JavaDoc clOptions = parser.getArguments();
228         final int size = clOptions.size();
229
230         assertEquals( size, 8 );
231         assertEquals( ((CLOption)clOptions.get( 0 )).getId(), YOU_OPT );
232         assertEquals( ((CLOption)clOptions.get( 1 )).getId(), 0 );
233         assertEquals( ((CLOption)clOptions.get( 2 )).getId(), ALL_OPT );
234         assertEquals( ((CLOption)clOptions.get( 3 )).getId(), CLEAR1_OPT );
235         assertEquals( ((CLOption)clOptions.get( 4 )).getId(), CLEAR2_OPT );
236         assertEquals( ((CLOption)clOptions.get( 5 )).getId(), CLEAR3_OPT );
237         assertEquals( ((CLOption)clOptions.get( 6 )).getId(), CLEAR5_OPT );
238         assertEquals( ((CLOption)clOptions.get( 7 )).getId(), 0 );
239     }
240
241     public void testDuplicateOptions()
242     {
243         //"-Dstupid=idiot","are","--all","--all","here"
244
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
245         {
246             DEFINE, ALL, CLEAR1
247         };
248
249         final CLArgsParser parser = new CLArgsParser( ARGLIST3, options );
250
251         assertNull( parser.getErrorString(), parser.getErrorString() );
252
253         final List JavaDoc clOptions = parser.getArguments();
254         final int size = clOptions.size();
255
256         assertEquals( size, 5 );
257         assertEquals( ((CLOption)clOptions.get( 0 )).getId(), DEFINE_OPT );
258         assertEquals( ((CLOption)clOptions.get( 1 )).getId(), 0 );
259         assertEquals( ((CLOption)clOptions.get( 2 )).getId(), ALL_OPT );
260         assertEquals( ((CLOption)clOptions.get( 3 )).getId(), ALL_OPT );
261         assertEquals( ((CLOption)clOptions.get( 4 )).getId(), 0 );
262     }
263
264     public void testIncompatableOptions()
265     {
266         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
267         {
268             DEFINE, ALL, CLEAR1, BLEE
269         };
270
271         final CLArgsParser parser = new CLArgsParser( ARGLIST4, options );
272
273         assertNotNull( parser.getErrorString() );
274
275         final List JavaDoc clOptions = parser.getArguments();
276         final int size = clOptions.size();
277
278         assertEquals( size, 5 );
279         assertEquals( ((CLOption)clOptions.get( 0 )).getId(), DEFINE_OPT );
280         assertEquals( ((CLOption)clOptions.get( 1 )).getId(), 0 );
281         assertEquals( ((CLOption)clOptions.get( 2 )).getId(), ALL_OPT );
282         assertEquals( ((CLOption)clOptions.get( 3 )).getId(), BLEE_OPT );
283         assertEquals( ((CLOption)clOptions.get( 4 )).getId(), 0 );
284     }
285
286     public void testSingleArg()
287     {
288         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
289         {
290             FILE
291         };
292
293         final CLArgsParser parser = new CLArgsParser( ARGLIST5, options );
294
295         assertNull( parser.getErrorString(), parser.getErrorString() );
296
297         final List JavaDoc clOptions = parser.getArguments();
298         final int size = clOptions.size();
299
300         assertEquals( size, 1 );
301         assertEquals( ((CLOption)clOptions.get( 0 )).getId(), FILE_OPT );
302         assertEquals( ((CLOption)clOptions.get( 0 )).getArgument(), "myfile.txt" );
303     }
304
305     public void test2ArgsParse()
306     {
307         //"-Dstupid=idiot","are","--all","here"
308
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
309         {
310             DEFINE, ALL, CLEAR1, CASE_CHECK
311         };
312
313         final CLArgsParser parser = new CLArgsParser( ARGLIST2, options );
314
315         assertNull( parser.getErrorString(), parser.getErrorString() );
316
317         final List JavaDoc clOptions = parser.getArguments();
318         final int size = clOptions.size();
319
320         assertEquals( size, 5 );
321         assertEquals( ((CLOption)clOptions.get( 0 )).getId(), DEFINE_OPT );
322         assertEquals( ((CLOption)clOptions.get( 1 )).getId(), 0 );
323         assertEquals( ((CLOption)clOptions.get( 2 )).getId(), ALL_OPT );
324         assertEquals( ((CLOption)clOptions.get( 3 )).getId(), 0 );
325         assertEquals( ((CLOption)clOptions.get( 4 )).getId(), CASE_CHECK_OPT );
326
327         final CLOption option = (CLOption)clOptions.get( 0 );
328         assertEquals( "stupid", option.getArgument( 0 ) );
329         assertEquals( "idiot", option.getArgument( 1 ) );
330     }
331
332     public void testPartParse()
333     {
334         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
335         {
336             YOU
337         };
338
339         final ParserControl control = new AbstractParserControl()
340             {
341                 public boolean isFinished( int lastOptionCode )
342                 {
343                     return (lastOptionCode == YOU_OPT);
344                 }
345             };
346
347         final CLArgsParser parser = new CLArgsParser( ARGLIST1, options, control );
348
349         assertNull( parser.getErrorString(), parser.getErrorString() );
350
351         final List JavaDoc clOptions = parser.getArguments();
352         final int size = clOptions.size();
353
354         assertEquals( size, 1 );
355         assertEquals( ((CLOption)clOptions.get( 0 )).getId(), YOU_OPT );
356     }
357
358     public void test2PartParse()
359     {
360         final CLOptionDescriptor[] options1 = new CLOptionDescriptor[]
361         {
362             YOU
363         };
364
365         final CLOptionDescriptor[] options2 = new CLOptionDescriptor[]
366         {
367             ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
368         };
369
370         final ParserControl control1 = new AbstractParserControl()
371             {
372                 public boolean isFinished( int lastOptionCode )
373                 {
374                     return (lastOptionCode == YOU_OPT);
375                 }
376             };
377
378         final CLArgsParser parser1 = new CLArgsParser( ARGLIST1, options1, control1 );
379
380         assertNull(parser1.getErrorString(), parser1.getErrorString() );
381
382         final List JavaDoc clOptions1 = parser1.getArguments();
383         final int size1 = clOptions1.size();
384
385         assertEquals( size1, 1 );
386         assertEquals( ((CLOption)clOptions1.get( 0 )).getId(), YOU_OPT );
387
388         final CLArgsParser parser2 =
389             new CLArgsParser( parser1.getUnparsedArgs(), options2 );
390
391         assertNull(parser2.getErrorString(), parser2.getErrorString() );
392
393         final List JavaDoc clOptions2 = parser2.getArguments();
394         final int size2 = clOptions2.size();
395
396         assertEquals( size2, 7 );
397         assertEquals( ((CLOption)clOptions2.get( 0 )).getId(), 0 );
398         assertEquals( ((CLOption)clOptions2.get( 1 )).getId(), ALL_OPT );
399         assertEquals( ((CLOption)clOptions2.get( 2 )).getId(), CLEAR1_OPT );
400         assertEquals( ((CLOption)clOptions2.get( 3 )).getId(), CLEAR2_OPT );
401         assertEquals( ((CLOption)clOptions2.get( 4 )).getId(), CLEAR3_OPT );
402         assertEquals( ((CLOption)clOptions2.get( 5 )).getId(), CLEAR5_OPT );
403         assertEquals( ((CLOption)clOptions2.get( 6 )).getId(), 0 );
404     }
405
406     public void test2PartPartialParse()
407     {
408         final CLOptionDescriptor[] options1 = new CLOptionDescriptor[]
409         {
410             YOU, ALL, CLEAR1
411         };
412
413         final CLOptionDescriptor[] options2 = new CLOptionDescriptor[] {};
414
415         final ParserControl control1 = new AbstractParserControl()
416             {
417                 public boolean isFinished( final int lastOptionCode )
418                 {
419                     return (lastOptionCode == CLEAR1_OPT);
420                 }
421             };
422
423         final CLArgsParser parser1 = new CLArgsParser( ARGLIST1, options1, control1 );
424
425         assertNull( parser1.getErrorString(), parser1.getErrorString() );
426
427         final List JavaDoc clOptions1 = parser1.getArguments();
428         final int size1 = clOptions1.size();
429
430         assertEquals( size1, 4 );
431         assertEquals( ((CLOption)clOptions1.get( 0 )).getId(), YOU_OPT );
432         assertEquals( ((CLOption)clOptions1.get( 1 )).getId(), 0 );
433         assertEquals( ((CLOption)clOptions1.get( 2 )).getId(), ALL_OPT );
434         assertEquals( ((CLOption)clOptions1.get( 3 )).getId(), CLEAR1_OPT );
435
436         assertTrue( parser1.getUnparsedArgs()[0].equals("ler") );
437
438         final CLArgsParser parser2 =
439             new CLArgsParser( parser1.getUnparsedArgs(), options2 );
440
441         assertNull( parser2.getErrorString(), parser2.getErrorString() );
442
443         final List JavaDoc clOptions2 = parser2.getArguments();
444         final int size2 = clOptions2.size();
445
446         assertEquals( size2, 2 );
447         assertEquals( ((CLOption)clOptions2.get( 0 )).getId(), 0 );
448         assertEquals( ((CLOption)clOptions2.get( 1 )).getId(), 0 );
449     }
450
451
452     public void testDuplicatesFail()
453     {
454         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
455         {
456             YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
457         };
458
459         //duplicate as
460
final String JavaDoc[] DUPLICATE_ARGLIST =
461         {
462             "--you","are","--all","-clear","kid"
463         };
464
465         final CLArgsParser parser = new CLArgsParser( ARGLIST1, options );
466
467         assertNull( parser.getErrorString(), parser.getErrorString() );
468     }
469
470     public void testIncomplete2Args()
471     {
472         //"-Dstupid="
473
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
474         {
475             DEFINE
476         };
477
478         final CLArgsParser parser = new CLArgsParser( new String JavaDoc[] { "-Dstupid=" }, options );
479
480         assertNull( parser.getErrorString(), parser.getErrorString() );
481
482         final List JavaDoc clOptions = parser.getArguments();
483         final int size = clOptions.size();
484
485         assertEquals( size, 1 );
486         final CLOption option = (CLOption)clOptions.get( 0 );
487         assertEquals( option.getId(), DEFINE_OPT );
488         assertEquals( option.getArgument( 0 ), "stupid" );
489         assertEquals( option.getArgument( 1 ), "" );
490     }
491
492     public void testIncomplete2ArgsMixed()
493     {
494         //"-Dstupid=","-c"
495
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
496         {
497             DEFINE, CLEAR1
498         };
499
500         final String JavaDoc[] args = new String JavaDoc[] { "-Dstupid=", "-c" };
501
502         final CLArgsParser parser = new CLArgsParser( args, options );
503
504         assertNull( parser.getErrorString(), parser.getErrorString() );
505
506         final List JavaDoc clOptions = parser.getArguments();
507         final int size = clOptions.size();
508
509         assertEquals( size, 2 );
510         assertEquals( ((CLOption)clOptions.get( 1 )).getId(), CLEAR1_OPT );
511         final CLOption option = (CLOption)clOptions.get( 0 );
512         assertEquals( option.getId(), DEFINE_OPT );
513         assertEquals( option.getArgument( 0 ), "stupid" );
514         assertEquals( option.getArgument( 1 ), "" );
515     }
516
517     public void fail_testIncomplete2ArgsMixedNoEq()
518     {
519         //"-Dstupid","-c"
520
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
521         {
522             DEFINE, CLEAR1
523         };
524
525         final String JavaDoc[] args = new String JavaDoc[] { "-Dstupid", "-c" };
526
527         final CLArgsParser parser = new CLArgsParser( args, options );
528
529         assertNull( parser.getErrorString(), parser.getErrorString() );
530
531         final List JavaDoc clOptions = parser.getArguments();
532         final int size = clOptions.size();
533
534         assertEquals( size, 2 );
535         assertEquals( ((CLOption)clOptions.get( 1 )).getId(), CLEAR1_OPT );
536         final CLOption option = (CLOption)clOptions.get( 0 );
537         assertEquals( option.getId(), DEFINE_OPT );
538         assertEquals( option.getArgument( 0 ), "stupid" );
539         assertEquals( option.getArgument( 1 ), "" );
540     }
541
542     /**
543      * Test the getArgumentById and getArgumentByName lookup methods.
544      */

545     public void testArgumentLookup()
546     {
547         final String JavaDoc[] args = {"-f", "testarg"};
548         final CLOptionDescriptor[] options = { FILE };
549         final CLArgsParser parser = new CLArgsParser( args, options );
550
551         CLOption optionById = parser.getArgumentById( FILE_OPT );
552         assertNotNull( optionById );
553         assertEquals( FILE_OPT, optionById.getId() );
554
555         CLOption optionByName = parser.getArgumentByName( FILE.getName() );
556         assertNotNull( optionByName );
557         assertEquals( FILE_OPT, optionByName.getId() );
558     }
559 }
560
Popular Tags