KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > avalon > ClutilTestCase


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.cli.avalon;
18 //Renamed from org.apache.avalon.excalibur.cli
19

20 import java.util.List JavaDoc;
21
22 import junit.framework.TestCase;
23
24 /**
25  *
26  */

27 public final class ClutilTestCase
28         extends TestCase
29 {
30     private static final String JavaDoc[] ARGLIST1 = new String JavaDoc[]
31     {
32         "--you", "are", "--all", "-cler", "kid"
33     };
34
35     private static final String JavaDoc[] ARGLIST2 = new String JavaDoc[]
36     {
37         "-Dstupid=idiot", "are", "--all", "here", "-d"
38     };
39
40     private static final String JavaDoc[] ARGLIST3 = new String JavaDoc[]
41     {
42         //duplicates
43
"-Dstupid=idiot", "are", "--all", "--all", "here"
44     };
45
46     private static final String JavaDoc[] ARGLIST4 = new String JavaDoc[]
47     {
48         //incompatable (blee/all)
49
"-Dstupid","idiot", "are", "--all", "--blee", "here"
50     };
51
52     private static final String JavaDoc[] ARGLIST5 = new String JavaDoc[]
53     {
54         "-f", "myfile.txt"
55     };
56
57     private static final int DEFINE_OPT = 'D';
58     private static final int CASE_CHECK_OPT = 'd';
59     private static final int YOU_OPT = 'y';
60     private static final int ALL_OPT = 'a';
61     private static final int CLEAR1_OPT = 'c';
62     private static final int CLEAR2_OPT = 'l';
63     private static final int CLEAR3_OPT = 'e';
64     private static final int CLEAR5_OPT = 'r';
65     private static final int BLEE_OPT = 'b';
66     private static final int FILE_OPT = 'f';
67     private static final int TAINT_OPT = 'T';
68
69     private static final CLOptionDescriptor DEFINE =
70             new CLOptionDescriptor( "define",
71                     CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
72                     DEFINE_OPT,
73                     "define" );
74     private static final CLOptionDescriptor DEFINE_MANY =
75         new CLOptionDescriptor( "define",
76              CLOptionDescriptor.ARGUMENTS_REQUIRED_2|CLOptionDescriptor.DUPLICATES_ALLOWED,
77              DEFINE_OPT,
78              "define" );
79
80     private static final CLOptionDescriptor CASE_CHECK =
81             new CLOptionDescriptor( "charCheck",
82                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
83                     CASE_CHECK_OPT,
84                     "check character case sensitivity" );
85     private static final CLOptionDescriptor YOU =
86             new CLOptionDescriptor( "you", CLOptionDescriptor.ARGUMENT_DISALLOWED, YOU_OPT, "you" );
87
88     private static final CLOptionDescriptor CLEAR1 =
89             new CLOptionDescriptor( "c", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR1_OPT, "c" );
90     private static final CLOptionDescriptor CLEAR2 =
91             new CLOptionDescriptor( "l", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR2_OPT, "l" );
92     private static final CLOptionDescriptor CLEAR3 =
93             new CLOptionDescriptor( "e", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR3_OPT, "e" );
94     private static final CLOptionDescriptor CLEAR5 =
95             new CLOptionDescriptor( "r", CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR5_OPT, "r" );
96     private static final CLOptionDescriptor BLEE =
97             new CLOptionDescriptor( "blee",
98                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
99                     BLEE_OPT,
100                     "blee" );
101
102     private static final CLOptionDescriptor ALL =
103             new CLOptionDescriptor( "all",
104                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
105                     ALL_OPT,
106                     "all",
107                     new CLOptionDescriptor[]{BLEE} );
108
109     private static final CLOptionDescriptor FILE =
110             new CLOptionDescriptor( "file",
111                     CLOptionDescriptor.ARGUMENT_REQUIRED,
112                     FILE_OPT,
113                     "the build file." );
114     private static final CLOptionDescriptor TAINT =
115             new CLOptionDescriptor( "taint",
116                     CLOptionDescriptor.ARGUMENT_OPTIONAL,
117                     TAINT_OPT,
118                     "turn on tainting checks (optional level)." );
119
120     public ClutilTestCase()
121     {
122         this( "Command Line Interpreter Test Case" );
123     }
124
125     public ClutilTestCase( String JavaDoc name )
126     {
127         super( name );
128     }
129
130     public void testOptionalArgWithSpace()
131     {
132         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
133         {
134             ALL, TAINT
135         };
136
137         final String JavaDoc[] args = new String JavaDoc[]{"-T", "param", "-a"};
138
139         final CLArgsParser parser = new CLArgsParser( args, options );
140
141         assertNull( parser.getErrorString(), parser.getErrorString() );
142
143         final List JavaDoc clOptions = parser.getArguments();
144         final int size = clOptions.size();
145
146         assertEquals( "Option count", 3, size );
147
148         final CLOption option0 = (CLOption)clOptions.get( 0 );
149         assertEquals( "Option Code: " + option0.getDescriptor().getId(), TAINT_OPT,
150                 option0.getDescriptor().getId() );
151         assertEquals( "Option Arg: " + option0.getArgument( 0 ),
152                 null, option0.getArgument( 0 ) );
153
154         final CLOption option1 = (CLOption)clOptions.get( 1 );
155         assertEquals( option1.getDescriptor().getId(), CLOption.TEXT_ARGUMENT );
156         assertEquals( option1.getArgument( 0 ), "param" );
157
158         final CLOption option2 = (CLOption)clOptions.get( 2 );
159         assertEquals( option2.getDescriptor().getId(), ALL_OPT );
160         assertEquals( option2.getArgument( 0 ), null );
161     }
162
163     public void testOptionalArgLong()
164     {
165         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
166         {
167             ALL, TAINT
168         };
169
170         // Check that optional args work woth long options
171
final String JavaDoc[] args = new String JavaDoc[]{"--taint", "param", "-a"};
172
173         final CLArgsParser parser = new CLArgsParser( args, options );
174
175         assertNull( parser.getErrorString(), parser.getErrorString() );
176
177         final List JavaDoc clOptions = parser.getArguments();
178         final int size = clOptions.size();
179
180         assertEquals( "Option count", 3, size );
181
182         final CLOption option0 = (CLOption)clOptions.get( 0 );
183         assertEquals( "Option Code: " + option0.getDescriptor().getId(), TAINT_OPT,
184                 option0.getDescriptor().getId() );
185         assertEquals( "Option Arg: " + option0.getArgument( 0 ),
186                 null, option0.getArgument( 0 ) );
187
188         final CLOption option1 = (CLOption)clOptions.get( 1 );
189         assertEquals( CLOption.TEXT_ARGUMENT , option1.getDescriptor().getId());
190         assertEquals( "param" , option1.getArgument( 0 ) );
191
192         final CLOption option2 = (CLOption)clOptions.get( 2 );
193         assertEquals( option2.getDescriptor().getId(), ALL_OPT );
194         assertEquals( option2.getArgument( 0 ), null );
195     }
196
197     public void testShortOptArgUnenteredBeforeOtherOpt()
198     {
199         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
200         {
201             ALL, TAINT
202         };
203
204         final String JavaDoc[] args = new String JavaDoc[]{"-T", "-a"};
205
206         final CLArgsParser parser = new CLArgsParser( args, options );
207
208         assertNull( parser.getErrorString(), parser.getErrorString() );
209
210         final List JavaDoc clOptions = parser.getArguments();
211         final int size = clOptions.size();
212
213         assertEquals( "Option count", 2, size );
214
215         final CLOption option0 = (CLOption)clOptions.get( 0 );
216         assertEquals( "Option Code: " + option0.getDescriptor().getId(), TAINT_OPT,
217                 option0.getDescriptor().getId() );
218         assertEquals( "Option Arg: " + option0.getArgument( 0 ), null, option0.getArgument( 0 ) );
219
220         final CLOption option1 = (CLOption)clOptions.get( 1 );
221         assertEquals( option1.getDescriptor().getId(), ALL_OPT );
222         assertEquals( option1.getArgument( 0 ), null );
223     }
224
225     public void testOptionalArgsWithArgShortBeforeOtherOpt()
226     {
227         //"-T3","-a"
228
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
229         {
230             ALL, TAINT
231         };
232
233         final String JavaDoc[] args = new String JavaDoc[]{"-T3", "-a"};
234
235         //System.out.println("[before parsing]");
236

237         final CLArgsParser parser = new CLArgsParser( args, options );
238
239         //System.out.println("[after parsing]");
240

241         assertNull( parser.getErrorString(), parser.getErrorString() );
242
243         final List JavaDoc clOptions = parser.getArguments();
244         final int size = clOptions.size();
245
246         assertEquals( size, 2 );
247         final CLOption option0 = (CLOption)clOptions.get( 0 );
248         assertEquals( option0.getDescriptor().getId(), TAINT_OPT );
249         assertEquals( option0.getArgument( 0 ), "3" );
250
251         final CLOption option1 = (CLOption)clOptions.get( 1 );
252         assertEquals( ALL_OPT, option1.getDescriptor().getId() );
253         assertEquals( null, option1.getArgument( 0 ) );
254     }
255
256     public void testOptionalArgsNoArgShortBeforeOtherOpt()
257     {
258         //"-T","-a"
259
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
260         {
261             ALL, TAINT
262         };
263
264         final String JavaDoc[] args = new String JavaDoc[]{"-T", "-a"};
265
266         //System.out.println("[before parsing]");
267
final CLArgsParser parser = new CLArgsParser( args, options );
268
269         //System.out.println("[after parsing]");
270

271         assertNull( parser.getErrorString(), parser.getErrorString() );
272
273         final List JavaDoc clOptions = parser.getArguments();
274         final int size = clOptions.size();
275
276         assertEquals( size, 2 );
277         final CLOption option0 = (CLOption)clOptions.get( 0 );
278         assertEquals( TAINT_OPT, option0.getDescriptor().getId() );
279         assertEquals( null, option0.getArgument( 0 ) );
280
281         final CLOption option1 = (CLOption)clOptions.get( 1 );
282         assertEquals( ALL_OPT, option1.getDescriptor().getId() );
283         assertEquals( null, option1.getArgument( 0 ) );
284     }
285
286     public void testFullParse()
287     {
288         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
289         {
290             YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
291         };
292
293         final CLArgsParser parser = new CLArgsParser( ARGLIST1, 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, 8 );
301         assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(), YOU_OPT );
302         assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(), 0 );
303         assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(), ALL_OPT );
304         assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(), CLEAR1_OPT );
305         assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(), CLEAR2_OPT );
306         assertEquals( ((CLOption)clOptions.get( 5 )).getDescriptor().getId(), CLEAR3_OPT );
307         assertEquals( ((CLOption)clOptions.get( 6 )).getDescriptor().getId(), CLEAR5_OPT );
308         assertEquals( ((CLOption)clOptions.get( 7 )).getDescriptor().getId(), 0 );
309     }
310
311     public void testDuplicateOptions()
312     {
313         //"-Dstupid=idiot","are","--all","--all","here"
314
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
315         {
316             DEFINE, ALL, CLEAR1
317         };
318
319         final CLArgsParser parser = new CLArgsParser( ARGLIST3, options );
320
321         assertNull( parser.getErrorString(), parser.getErrorString() );
322
323         final List JavaDoc clOptions = parser.getArguments();
324         final int size = clOptions.size();
325
326         assertEquals( size, 5 );
327         assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(), DEFINE_OPT );
328         assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(), 0 );
329         assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(), ALL_OPT );
330         assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(), ALL_OPT );
331         assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(), 0 );
332     }
333
334     public void testIncompatableOptions()
335     {
336         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
337         {
338             DEFINE, ALL, CLEAR1, BLEE
339         };
340
341         final CLArgsParser parser = new CLArgsParser( ARGLIST4, options );
342
343         assertNotNull( parser.getErrorString() );
344
345         final List JavaDoc clOptions = parser.getArguments();
346         final int size = clOptions.size();
347
348         assertEquals( size, 5 );
349         assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(), DEFINE_OPT );
350         assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(), 0 );
351         assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(), ALL_OPT );
352         assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(), BLEE_OPT );
353         assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(), 0 );
354     }
355
356     public void testSingleArg()
357     {
358         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
359         {
360             FILE
361         };
362
363         final CLArgsParser parser = new CLArgsParser( ARGLIST5, options );
364
365         assertNull( parser.getErrorString(), parser.getErrorString() );
366
367         final List JavaDoc clOptions = parser.getArguments();
368         final int size = clOptions.size();
369
370         assertEquals( size, 1 );
371         assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(), FILE_OPT );
372         assertEquals( ((CLOption)clOptions.get( 0 )).getArgument(), "myfile.txt" );
373     }
374     public void testSingleArg2()
375     {
376         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
377         {
378             FILE
379         };
380
381         final CLArgsParser parser = new CLArgsParser(
382                 new String JavaDoc[]{"-f-=,=-"} // Check delimiters are allowed
383
, options );
384
385         assertNull( parser.getErrorString(), parser.getErrorString() );
386
387         final List JavaDoc clOptions = parser.getArguments();
388         final int size = clOptions.size();
389
390         assertEquals( 1, size );
391         assertEquals( FILE_OPT ,((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
392         assertEquals( "-=,=-", ((CLOption)clOptions.get( 0 )).getArgument() );
393     }
394
395     public void testSingleArg3()
396     {
397         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
398         {
399             FILE
400         };
401
402         final CLArgsParser parser = new CLArgsParser(
403                 new String JavaDoc[]{"--file=-=,-"} // Check delimiters are allowed
404
, options );
405
406         assertNull( parser.getErrorString(), parser.getErrorString() );
407
408         final List JavaDoc clOptions = parser.getArguments();
409         final int size = clOptions.size();
410
411         assertEquals( 1, size );
412         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
413         assertEquals( "-=,-" , ((CLOption)clOptions.get( 0 )).getArgument() );
414     }
415
416     public void testSingleArg4()
417     {
418         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
419         {
420             FILE
421         };
422
423         final CLArgsParser parser = new CLArgsParser(
424                 new String JavaDoc[]{"--file","myfile.txt"}
425                 , options );
426
427         assertNull( parser.getErrorString(), parser.getErrorString() );
428
429         final List JavaDoc clOptions = parser.getArguments();
430         final int size = clOptions.size();
431
432         assertEquals( 1, size );
433         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
434         assertEquals( "myfile.txt", ((CLOption)clOptions.get( 0 )).getArgument() );
435     }
436
437     public void testSingleArg5()
438     {
439         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
440         {
441             FILE
442         };
443
444         final CLArgsParser parser = new CLArgsParser(
445                 new String JavaDoc[]{"-f","myfile.txt"}
446                 , options );
447
448         assertNull( parser.getErrorString(), parser.getErrorString() );
449
450         final List JavaDoc clOptions = parser.getArguments();
451         final int size = clOptions.size();
452
453         assertEquals( 1, size );
454         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
455         assertEquals( "myfile.txt", ((CLOption)clOptions.get( 0 )).getArgument() );
456     }
457
458     public void testSingleArg6()
459     {
460         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
461         {
462             FILE
463         };
464
465         final CLArgsParser parser = new CLArgsParser(
466                 new String JavaDoc[]{"-f","-=-"}
467                 , options );
468
469         assertNull( parser.getErrorString(), parser.getErrorString() );
470
471         final List JavaDoc clOptions = parser.getArguments();
472         final int size = clOptions.size();
473
474         assertEquals( 1, size );
475         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
476         assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
477     }
478
479     public void testSingleArg7()
480     {
481         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
482         {
483             FILE
484         };
485
486         final CLArgsParser parser = new CLArgsParser(
487                 new String JavaDoc[]{"--file=-=-"}
488                 , options );
489
490         assertNull( parser.getErrorString(), parser.getErrorString() );
491
492         final List JavaDoc clOptions = parser.getArguments();
493         final int size = clOptions.size();
494
495         assertEquals( 1, size );
496         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
497         assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
498     }
499
500     public void testSingleArg8()
501     {
502         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
503         {
504             FILE
505         };
506
507         final CLArgsParser parser = new CLArgsParser(
508                 new String JavaDoc[]{"--file","-=-"}
509                 , options );
510
511         assertNull( parser.getErrorString(), parser.getErrorString() );
512
513         final List JavaDoc clOptions = parser.getArguments();
514         final int size = clOptions.size();
515
516         assertEquals( 1, size );
517         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
518         assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
519     }
520
521     public void testSingleArg9()
522     {
523         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
524         {
525             FILE
526         };
527
528         final CLArgsParser parser = new CLArgsParser(
529                 new String JavaDoc[]{"--file","-=-"}
530                 , options );
531
532         assertNull( parser.getErrorString(), parser.getErrorString() );
533
534         final List JavaDoc clOptions = parser.getArguments();
535         final int size = clOptions.size();
536
537         assertEquals( 1, size );
538         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
539         assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
540     }
541
542     public void testCombinedArgs1()
543     {
544         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
545         {
546             BLEE, TAINT
547         };
548
549         final CLArgsParser parser = new CLArgsParser(
550                 new String JavaDoc[]{"-bT","rest"}
551                 , options );
552
553         assertNull( parser.getErrorString(), parser.getErrorString() );
554
555         final List JavaDoc clOptions = parser.getArguments();
556         final int size = clOptions.size();
557         assertEquals( 3, size);
558         assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId());
559         assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1 )).getDescriptor().getId());
560         assertEquals( 0, ((CLOption)clOptions.get( 2 )).getDescriptor().getId());
561         assertEquals( "rest", ((CLOption)clOptions.get( 2 )).getArgument());
562     }
563
564     public void testCombinedArgs2()
565     {
566         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
567         {
568             BLEE, TAINT, FILE
569         };
570
571         final CLArgsParser parser = new CLArgsParser(
572                 new String JavaDoc[]{"-bT","-fa"}
573                 , options );
574
575         assertNull( parser.getErrorString(), parser.getErrorString() );
576
577         final List JavaDoc clOptions = parser.getArguments();
578         final int size = clOptions.size();
579         assertEquals( 3, size);
580         assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId());
581         assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1 )).getDescriptor().getId());
582         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 2 )).getDescriptor().getId());
583         assertEquals( "a", ((CLOption)clOptions.get( 2 )).getArgument());
584     }
585
586     public void testCombinedArgs3()
587     {
588         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
589         {
590             BLEE, TAINT, FILE
591         };
592
593         final CLArgsParser parser = new CLArgsParser(
594                 new String JavaDoc[]{"-bT","--","-fa"}// Should not detect trailing option
595
, options );
596
597         assertNull( parser.getErrorString(), parser.getErrorString() );
598
599         final List JavaDoc clOptions = parser.getArguments();
600         final int size = clOptions.size();
601         assertEquals( 3, size);
602         assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId());
603         assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1 )).getDescriptor().getId());
604         assertEquals( 0, ((CLOption)clOptions.get( 2 )).getDescriptor().getId());
605         assertEquals( "-fa", ((CLOption)clOptions.get( 2 )).getArgument());
606     }
607
608     public void testCombinedArgs4()
609     {
610         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
611         {
612             BLEE, TAINT, FILE
613         };
614
615         final CLArgsParser parser = new CLArgsParser(
616                 new String JavaDoc[]{"-bT","rest","-fa"} // should detect trailing option
617
, options );
618
619         assertNull( parser.getErrorString(), parser.getErrorString() );
620
621         final List JavaDoc clOptions = parser.getArguments();
622         final int size = clOptions.size();
623         assertEquals( 4, size);
624         assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId());
625         assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1 )).getDescriptor().getId());
626         assertEquals( 0, ((CLOption)clOptions.get( 2 )).getDescriptor().getId());
627         assertEquals( "rest", ((CLOption)clOptions.get( 2 )).getArgument());
628         assertEquals( FILE_OPT, ((CLOption)clOptions.get( 3 )).getDescriptor().getId());
629         assertEquals( "a", ((CLOption)clOptions.get( 3 )).getArgument());
630     }
631
632     public void test2ArgsParse()
633     {
634         //"-Dstupid=idiot","are","--all","here"
635
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
636         {
637             DEFINE, ALL, CLEAR1, CASE_CHECK
638         };
639
640         final CLArgsParser parser = new CLArgsParser( ARGLIST2, options );
641
642         assertNull( parser.getErrorString(), parser.getErrorString() );
643
644         final List JavaDoc clOptions = parser.getArguments();
645         final int size = clOptions.size();
646
647         assertEquals( size, 5 );
648         assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(), DEFINE_OPT );
649         assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(), 0 );
650         assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(), ALL_OPT );
651         assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(), 0 );
652         assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(), CASE_CHECK_OPT );
653
654         final CLOption option = (CLOption)clOptions.get( 0 );
655         assertEquals( "stupid", option.getArgument( 0 ) );
656         assertEquals( "idiot", option.getArgument( 1 ) );
657     }
658
659     public void test2ArgsParse2()
660     {
661         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
662         {
663             DEFINE
664         };
665
666         final CLArgsParser parser = new CLArgsParser(
667                 new String JavaDoc[] {"--define","a-b,c=d-e,f"}, // Check "-" is allowed in arg2
668
options );
669
670         assertNull( parser.getErrorString(), parser.getErrorString() );
671
672         final List JavaDoc clOptions = parser.getArguments();
673         final int size = clOptions.size();
674
675         assertEquals( 1, size );
676         assertEquals( DEFINE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
677
678         final CLOption option = (CLOption)clOptions.get( 0 );
679         assertEquals( "a-b,c", option.getArgument( 0 ) );
680         assertEquals( "d-e,f", option.getArgument( 1 ) );
681     }
682
683     public void test2ArgsParse3()
684     {
685         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
686         {
687             DEFINE
688         };
689
690         final CLArgsParser parser = new CLArgsParser(
691                 new String JavaDoc[] {"-D","A-b,c","G-e,f"},
692                 options );
693
694         assertNull( parser.getErrorString(), parser.getErrorString() );
695
696         final List JavaDoc clOptions = parser.getArguments();
697         final int size = clOptions.size();
698
699         assertEquals( 1, size );
700         assertEquals( DEFINE_OPT, ((CLOption)clOptions.get( 0 )).getDescriptor().getId() );
701
702         final CLOption option = (CLOption)clOptions.get( 0 );
703         assertEquals( "A-b,c", option.getArgument( 0 ) );
704         assertEquals( "G-e,f", option.getArgument( 1 ) );
705     }
706
707     public void test2ArgsParse4()
708     {
709         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
710         {
711             DEFINE_MANY
712         };
713
714         final CLArgsParser parser = new CLArgsParser(
715                 new String JavaDoc[] {"-Dval1=-1","-D","val2=-2",
716                         "--define=val-3=-3","--define","val4-=-4"},
717                 options );
718
719         assertNull( parser.getErrorString(), parser.getErrorString() );
720
721         final List JavaDoc clOptions = parser.getArguments();
722         final int size = clOptions.size();
723
724         assertEquals( 4, size );
725         for (int i=0;i<size;i++){
726         assertEquals( DEFINE_OPT, ((CLOption)clOptions.get( i )).getDescriptor().getId() );
727         }
728
729         CLOption option;
730         option = (CLOption)clOptions.get( 0 );
731         assertEquals( "val1", option.getArgument( 0 ) );
732         assertEquals( "-1", option.getArgument( 1 ) );
733
734         option = (CLOption)clOptions.get( 1 );
735         assertEquals( "val2", option.getArgument( 0 ) );
736         assertEquals( "-2", option.getArgument( 1 ) );
737
738         option = (CLOption)clOptions.get( 2 );
739         assertEquals( "val-3", option.getArgument( 0 ) );
740         assertEquals( "-3", option.getArgument( 1 ) );
741         
742         option = (CLOption)clOptions.get( 3 );
743         assertEquals( "val4-", option.getArgument( 0 ) );
744         assertEquals( "-4", option.getArgument( 1 ) );
745     }
746
747     public void testPartParse()
748     {
749         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
750         {
751             YOU
752         };
753
754         final ParserControl control = new AbstractParserControl()
755         {
756             public boolean isFinished( int lastOptionCode )
757             {
758                 return (lastOptionCode == YOU_OPT);
759             }
760         };
761
762         final CLArgsParser parser = new CLArgsParser( ARGLIST1, options, control );
763
764         assertNull( parser.getErrorString(), parser.getErrorString() );
765
766         final List JavaDoc clOptions = parser.getArguments();
767         final int size = clOptions.size();
768
769         assertEquals( size, 1 );
770         assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(), YOU_OPT );
771     }
772
773     public void test2PartParse()
774     {
775         final CLOptionDescriptor[] options1 = new CLOptionDescriptor[]
776         {
777             YOU
778         };
779
780         final CLOptionDescriptor[] options2 = new CLOptionDescriptor[]
781         {
782             ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
783         };
784
785         final ParserControl control1 = new AbstractParserControl()
786         {
787             public boolean isFinished( int lastOptionCode )
788             {
789                 return (lastOptionCode == YOU_OPT);
790             }
791         };
792
793         final CLArgsParser parser1 = new CLArgsParser( ARGLIST1, options1, control1 );
794
795         assertNull( parser1.getErrorString(), parser1.getErrorString() );
796
797         final List JavaDoc clOptions1 = parser1.getArguments();
798         final int size1 = clOptions1.size();
799
800         assertEquals( size1, 1 );
801         assertEquals( ((CLOption)clOptions1.get( 0 )).getDescriptor().getId(), YOU_OPT );
802
803         final CLArgsParser parser2 =
804                 new CLArgsParser( parser1.getUnparsedArgs(), options2 );
805
806         assertNull( parser2.getErrorString(), parser2.getErrorString() );
807
808         final List JavaDoc clOptions2 = parser2.getArguments();
809         final int size2 = clOptions2.size();
810
811         assertEquals( size2, 7 );
812         assertEquals( ((CLOption)clOptions2.get( 0 )).getDescriptor().getId(), 0 );
813         assertEquals( ((CLOption)clOptions2.get( 1 )).getDescriptor().getId(), ALL_OPT );
814         assertEquals( ((CLOption)clOptions2.get( 2 )).getDescriptor().getId(), CLEAR1_OPT );
815         assertEquals( ((CLOption)clOptions2.get( 3 )).getDescriptor().getId(), CLEAR2_OPT );
816         assertEquals( ((CLOption)clOptions2.get( 4 )).getDescriptor().getId(), CLEAR3_OPT );
817         assertEquals( ((CLOption)clOptions2.get( 5 )).getDescriptor().getId(), CLEAR5_OPT );
818         assertEquals( ((CLOption)clOptions2.get( 6 )).getDescriptor().getId(), 0 );
819     }
820
821     public void test2PartPartialParse()
822     {
823         final CLOptionDescriptor[] options1 = new CLOptionDescriptor[]
824         {
825             YOU, ALL, CLEAR1
826         };
827
828         final CLOptionDescriptor[] options2 = new CLOptionDescriptor[]{};
829
830         final ParserControl control1 = new AbstractParserControl()
831         {
832             public boolean isFinished( final int lastOptionCode )
833             {
834                 return (lastOptionCode == CLEAR1_OPT);
835             }
836         };
837
838         final CLArgsParser parser1 = new CLArgsParser( ARGLIST1, options1, control1 );
839
840         assertNull( parser1.getErrorString(), parser1.getErrorString() );
841
842         final List JavaDoc clOptions1 = parser1.getArguments();
843         final int size1 = clOptions1.size();
844
845         assertEquals( size1, 4 );
846         assertEquals( ((CLOption)clOptions1.get( 0 )).getDescriptor().getId(), YOU_OPT );
847         assertEquals( ((CLOption)clOptions1.get( 1 )).getDescriptor().getId(), 0 );
848         assertEquals( ((CLOption)clOptions1.get( 2 )).getDescriptor().getId(), ALL_OPT );
849         assertEquals( ((CLOption)clOptions1.get( 3 )).getDescriptor().getId(), CLEAR1_OPT );
850
851         assertTrue( parser1.getUnparsedArgs()[0].equals( "ler" ) );
852
853         final CLArgsParser parser2 =
854                 new CLArgsParser( parser1.getUnparsedArgs(), options2 );
855
856         assertNull( parser2.getErrorString(), parser2.getErrorString() );
857
858         final List JavaDoc clOptions2 = parser2.getArguments();
859         final int size2 = clOptions2.size();
860
861         assertEquals( size2, 2 );
862         assertEquals( ((CLOption)clOptions2.get( 0 )).getDescriptor().getId(), 0 );
863         assertEquals( ((CLOption)clOptions2.get( 1 )).getDescriptor().getId(), 0 );
864     }
865
866     public void testDuplicatesFail()
867     {
868         final CLOptionDescriptor[] options = new CLOptionDescriptor[]
869         {
870             YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
871         };
872
873         final CLArgsParser parser = new CLArgsParser( ARGLIST1, options );
874
875         assertNull( parser.getErrorString(), parser.getErrorString() );
876     }
877
878     public void testIncomplete2Args()
879     {
880         //"-Dstupid="
881
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
882         {
883             DEFINE
884         };
885
886         final CLArgsParser parser = new CLArgsParser( new String JavaDoc[]{"-Dstupid="}, options );
887
888         assertNull( parser.getErrorString(), parser.getErrorString() );
889
890         final List JavaDoc clOptions = parser.getArguments();
891         final int size = clOptions.size();
892
893         assertEquals( size, 1 );
894         final CLOption option = (CLOption)clOptions.get( 0 );
895         assertEquals( option.getDescriptor().getId(), DEFINE_OPT );
896         assertEquals( option.getArgument( 0 ), "stupid" );
897         assertEquals( option.getArgument( 1 ), "" );
898     }
899
900     public void testIncomplete2ArgsMixed()
901     {
902         //"-Dstupid=","-c"
903
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
904         {
905             DEFINE, CLEAR1
906         };
907
908         final String JavaDoc[] args = new String JavaDoc[]{"-Dstupid=", "-c"};
909
910         final CLArgsParser parser = new CLArgsParser( args, options );
911
912         assertNull( parser.getErrorString(), parser.getErrorString() );
913
914         final List JavaDoc clOptions = parser.getArguments();
915         final int size = clOptions.size();
916
917         assertEquals( size, 2 );
918         assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(), CLEAR1_OPT );
919         final CLOption option = (CLOption)clOptions.get( 0 );
920         assertEquals( option.getDescriptor().getId(), DEFINE_OPT );
921         assertEquals( option.getArgument( 0 ), "stupid" );
922         assertEquals( option.getArgument( 1 ), "" );
923     }
924
925     public void testIncomplete2ArgsMixedNoEq()
926     {
927         //"-Dstupid","-c"
928
final CLOptionDescriptor[] options = new CLOptionDescriptor[]
929         {
930             DEFINE, CLEAR1
931         };
932
933         final String JavaDoc[] args = new String JavaDoc[]{"-DStupid", "-c"};
934
935         final CLArgsParser parser = new CLArgsParser( args, options );
936
937         assertNull( parser.getErrorString(), parser.getErrorString() );
938
939         final List JavaDoc clOptions = parser.getArguments();
940         final int size = clOptions.size();
941
942         assertEquals( size, 2 );
943         assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(), CLEAR1_OPT );
944         final CLOption option = (CLOption)clOptions.get( 0 );
945         assertEquals( option.getDescriptor().getId(), DEFINE_OPT );
946         assertEquals( option.getArgument( 0 ), "Stupid" );
947         assertEquals( option.getArgument( 1 ), "" );
948     }
949
950     /**
951      * Test the getArgumentById and getArgumentByName lookup methods.
952      */

953     public void testArgumentLookup()
954     {
955         final String JavaDoc[] args = {"-f", "testarg"};
956         final CLOptionDescriptor[] options = {FILE};
957         final CLArgsParser parser = new CLArgsParser( args, options );
958
959         CLOption optionById = parser.getArgumentById( FILE_OPT );
960         assertNotNull( optionById );
961         assertEquals( FILE_OPT, optionById.getDescriptor().getId() );
962
963         CLOption optionByName = parser.getArgumentByName( FILE.getName() );
964         assertNotNull( optionByName );
965         assertEquals( FILE_OPT, optionByName.getDescriptor().getId() );
966     }
967
968     /**
969      * Test that you can have null long forms.
970      */

971     public void testNullLongForm()
972     {
973         final CLOptionDescriptor test =
974                 new CLOptionDescriptor( null,
975                         CLOptionDescriptor.ARGUMENT_DISALLOWED,
976                         'n',
977                         "test null long form" );
978
979         final String JavaDoc[] args = {"-n", "testarg"};
980         final CLOptionDescriptor[] options = {test};
981         final CLArgsParser parser = new CLArgsParser( args, options );
982
983         final CLOption optionByID = parser.getArgumentById( 'n' );
984         assertNotNull( optionByID );
985         assertEquals( 'n', optionByID.getDescriptor().getId() );
986
987         final CLOption optionByName = parser.getArgumentByName( FILE.getName() );
988         assertNull( "Looking for non-existent option by name", optionByName );
989     }
990
991     /**
992      * Test that you can have null descriptions.
993      */

994     public void testNullDescription()
995     {
996         final CLOptionDescriptor test =
997                 new CLOptionDescriptor( "nulltest",
998                         CLOptionDescriptor.ARGUMENT_DISALLOWED,
999                         'n',
1000                        null );
1001
1002        final String JavaDoc[] args = {"-n", "testarg"};
1003        final CLOptionDescriptor[] options = {test};
1004        final CLArgsParser parser = new CLArgsParser( args, options );
1005
1006        final CLOption optionByID = parser.getArgumentById( 'n' );
1007        assertNotNull( optionByID );
1008        assertEquals( 'n', optionByID.getDescriptor().getId() );
1009
1010        final StringBuffer JavaDoc sb = CLUtil.describeOptions( options );
1011        final String JavaDoc lineSeparator = System.getProperty( "line.separator" );
1012        assertEquals( "Testing display of null description",
1013                "\t-n, --nulltest" + lineSeparator,
1014                sb.toString() );
1015    }
1016    /*
1017     * TODO add tests to check for:
1018     * - name clash
1019     * - long option abbreviations (match shortest unique abbreviation)
1020     */

1021    
1022}
1023
Popular Tags