KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.commons.cli;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6
7 import junit.textui.TestRunner;
8
9 public class OptionBuilderTest extends TestCase {
10
11     public OptionBuilderTest( String JavaDoc name ) {
12         super( name );
13     }
14
15     public static Test suite() {
16         return new TestSuite( OptionBuilderTest.class );
17     }
18
19     public static void main( String JavaDoc args[] ) {
20         TestRunner.run( suite() );
21     }
22
23     public void testCompleteOption( ) {
24         Option simple = OptionBuilder.withLongOpt( "simple option")
25                                      .hasArg( )
26                                      .isRequired( )
27                                      .hasArgs( )
28                                      .withType( new Float JavaDoc( 10 ) )
29                                      .withDescription( "this is a simple option" )
30                                      .create( 's' );
31
32         assertEquals( "s", simple.getOpt() );
33         assertEquals( "simple option", simple.getLongOpt() );
34         assertEquals( "this is a simple option", simple.getDescription() );
35         assertEquals( simple.getType().getClass(), Float JavaDoc.class );
36         assertTrue( simple.hasArg() );
37         assertTrue( simple.isRequired() );
38         assertTrue( simple.hasArgs() );
39     }
40
41     public void testTwoCompleteOptions( ) {
42         Option simple = OptionBuilder.withLongOpt( "simple option")
43                                      .hasArg( )
44                                      .isRequired( )
45                                      .hasArgs( )
46                                      .withType( new Float JavaDoc( 10 ) )
47                                      .withDescription( "this is a simple option" )
48                                      .create( 's' );
49
50         assertEquals( "s", simple.getOpt() );
51         assertEquals( "simple option", simple.getLongOpt() );
52         assertEquals( "this is a simple option", simple.getDescription() );
53         assertEquals( simple.getType().getClass(), Float JavaDoc.class );
54         assertTrue( simple.hasArg() );
55         assertTrue( simple.isRequired() );
56         assertTrue( simple.hasArgs() );
57
58         simple = OptionBuilder.withLongOpt( "dimple option")
59                               .hasArg( )
60                               .withDescription( "this is a dimple option" )
61                               .create( 'd' );
62
63         assertEquals( "d", simple.getOpt() );
64         assertEquals( "dimple option", simple.getLongOpt() );
65         assertEquals( "this is a dimple option", simple.getDescription() );
66         assertNull( simple.getType() );
67         assertTrue( simple.hasArg() );
68         assertTrue( !simple.isRequired() );
69         assertTrue( !simple.hasArgs() );
70     }
71
72     public void testBaseOptionCharOpt() {
73         Option base = OptionBuilder.withDescription( "option description")
74                                    .create( 'o' );
75
76         assertEquals( "o", base.getOpt() );
77         assertEquals( "option description", base.getDescription() );
78         assertTrue( !base.hasArg() );
79     }
80
81     public void testBaseOptionStringOpt() {
82         Option base = OptionBuilder.withDescription( "option description")
83                                    .create( "o" );
84
85         assertEquals( "o", base.getOpt() );
86         assertEquals( "option description", base.getDescription() );
87         assertTrue( !base.hasArg() );
88     }
89
90     public void testSpecialOptChars() {
91
92         // '?'
93
try {
94             Option opt = OptionBuilder.withDescription( "help options" )
95                                       .create( '?' );
96             assertEquals( "?", opt.getOpt() );
97         }
98         catch( IllegalArgumentException JavaDoc arg ) {
99             fail( "IllegalArgumentException caught" );
100         }
101
102         // '@'
103
try {
104             Option opt = OptionBuilder.withDescription( "read from stdin" )
105                                       .create( '@' );
106             assertEquals( "@", opt.getOpt() );
107         }
108         catch( IllegalArgumentException JavaDoc arg ) {
109             fail( "IllegalArgumentException caught" );
110         }
111     }
112
113     public void testOptionArgNumbers() {
114         Option opt = OptionBuilder.withDescription( "option description" )
115                                   .hasArgs( 2 )
116                                   .create( 'o' );
117         assertEquals( 2, opt.getArgs() );
118     }
119
120     public void testIllegalOptions() {
121         // bad single character option
122
try {
123             Option opt = OptionBuilder.withDescription( "option description" )
124                                       .create( '"' );
125             fail( "IllegalArgumentException not caught" );
126         }
127         catch( IllegalArgumentException JavaDoc exp ) {
128             // success
129
}
130
131         // bad character in option string
132
try {
133             Option opt = OptionBuilder.create( "opt`" );
134             fail( "IllegalArgumentException not caught" );
135         }
136         catch( IllegalArgumentException JavaDoc exp ) {
137             // success
138
}
139
140         // null option
141
try {
142             Option opt = OptionBuilder.create( null );
143             fail( "IllegalArgumentException not caught" );
144         }
145         catch( IllegalArgumentException JavaDoc exp ) {
146             // success
147
}
148
149         // valid option
150
try {
151             Option opt = OptionBuilder.create( "opt" );
152             // success
153
}
154         catch( IllegalArgumentException JavaDoc exp ) {
155             fail( "IllegalArgumentException caught" );
156         }
157     }
158 }
Popular Tags