KickJava   Java API By Example, From Geeks To Geeks.

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


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: ValueTest.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
9  */

10
11 package org.apache.commons.cli;
12
13 import java.util.Arrays JavaDoc;
14
15 import junit.framework.Test;
16 import junit.framework.TestCase;
17 import junit.framework.TestSuite;
18
19 public class ValuesTest extends TestCase
20 {
21     /** CommandLine instance */
22     private CommandLine _cmdline = null;
23     private Option _option = null;
24
25     public static Test suite() {
26         return new TestSuite( ValuesTest.class );
27     }
28
29     public ValuesTest( String JavaDoc name )
30     {
31         super( name );
32     }
33
34     public void setUp()
35     {
36         Options opts = new Options();
37
38         opts.addOption("a",
39                        false,
40                        "toggle -a");
41
42         opts.addOption("b",
43                        true,
44                        "set -b");
45
46         opts.addOption("c",
47                        "c",
48                        false,
49                        "toggle -c");
50
51         opts.addOption("d",
52                        "d",
53                        true,
54                        "set -d");
55         
56         opts.addOption( OptionBuilder.withLongOpt( "e" )
57                                      .hasArgs()
58                                      .withDescription( "set -e ")
59                                      .create( 'e' ) );
60
61         opts.addOption("f",
62                        "f",
63                        false,
64                        "jk");
65         
66         opts.addOption( OptionBuilder.withLongOpt( "g" )
67                         .hasArgs( 2 )
68                         .withDescription( "set -g")
69                         .create( 'g' ) );
70
71         opts.addOption( OptionBuilder.withLongOpt( "h" )
72                         .hasArgs( 2 )
73                         .withDescription( "set -h")
74                         .create( 'h' ) );
75
76         opts.addOption( OptionBuilder.withLongOpt( "i" )
77                         .withDescription( "set -i")
78                         .create( 'i' ) );
79
80         opts.addOption( OptionBuilder.withLongOpt( "j" )
81                         .hasArgs( )
82                         .withDescription( "set -j")
83                         .withValueSeparator( '=' )
84                         .create( 'j' ) );
85
86         opts.addOption( OptionBuilder.withLongOpt( "k" )
87                         .hasArgs( )
88                         .withDescription( "set -k")
89                         .withValueSeparator( '=' )
90                         .create( 'k' ) );
91
92         _option = OptionBuilder.withLongOpt( "m" )
93                         .hasArgs( )
94                         .withDescription( "set -m")
95                         .withValueSeparator( )
96                         .create( 'm' );
97
98         opts.addOption( _option );
99         
100         String JavaDoc[] args = new String JavaDoc[] { "-a",
101                                        "-b", "foo",
102                                        "--c",
103                                        "--d", "bar",
104                                        "-e", "one", "two",
105                                        "-f",
106                                        "arg1", "arg2",
107                                        "-g", "val1", "val2" , "arg3",
108                                        "-h", "val1", "-i",
109                                        "-h", "val2",
110                                        "-jkey=value",
111                                        "-j", "key=value",
112                                        "-kkey1=value1",
113                                        "-kkey2=value2",
114                                        "-mkey=value"};
115
116         CommandLineParser parser = new PosixParser();
117
118         try
119         {
120             _cmdline = parser.parse(opts,args);
121         }
122         catch (ParseException e)
123         {
124             fail("Cannot setUp() CommandLine: " + e.toString());
125         }
126     }
127
128     public void tearDown()
129     {
130
131     }
132
133     public void testShortArgs()
134     {
135         assertTrue( _cmdline.hasOption("a") );
136         assertTrue( _cmdline.hasOption("c") );
137
138         assertNull( _cmdline.getOptionValues("a") );
139         assertNull( _cmdline.getOptionValues("c") );
140     }
141
142     public void testShortArgsWithValue()
143     {
144         assertTrue( _cmdline.hasOption("b") );
145         assertTrue( _cmdline.getOptionValue("b").equals("foo"));
146         assertTrue( _cmdline.getOptionValues("b").length == 1);
147
148         assertTrue( _cmdline.hasOption("d") );
149         assertTrue( _cmdline.getOptionValue("d").equals("bar"));
150         assertTrue( _cmdline.getOptionValues("d").length == 1);
151     }
152
153     public void testMultipleArgValues()
154     {
155         String JavaDoc[] result = _cmdline.getOptionValues("e");
156         String JavaDoc[] values = new String JavaDoc[] { "one", "two" };
157         assertTrue( _cmdline.hasOption("e") );
158         assertTrue( _cmdline.getOptionValues("e").length == 2);
159         assertTrue( Arrays.equals( values, _cmdline.getOptionValues("e") ) );
160     }
161
162     public void testTwoArgValues()
163     {
164         String JavaDoc[] result = _cmdline.getOptionValues("g");
165         String JavaDoc[] values = new String JavaDoc[] { "val1", "val2" };
166         assertTrue( _cmdline.hasOption("g") );
167         assertTrue( _cmdline.getOptionValues("g").length == 2);
168         assertTrue( Arrays.equals( values, _cmdline.getOptionValues("g") ) );
169     }
170
171     public void testComplexValues()
172     {
173         String JavaDoc[] result = _cmdline.getOptionValues("h");
174         String JavaDoc[] values = new String JavaDoc[] { "val1", "val2" };
175         assertTrue( _cmdline.hasOption("i") );
176         assertTrue( _cmdline.hasOption("h") );
177         assertTrue( _cmdline.getOptionValues("h").length == 2);
178         assertTrue( Arrays.equals( values, _cmdline.getOptionValues("h") ) );
179     }
180
181     public void testExtraArgs()
182     {
183         String JavaDoc[] args = new String JavaDoc[] { "arg1", "arg2", "arg3" };
184         assertTrue( _cmdline.getArgs().length == 3 );
185         assertTrue( Arrays.equals( args, _cmdline.getArgs() ) );
186     }
187
188     public void testCharSeparator()
189     {
190         // tests the char methods of CommandLine that delegate to
191
// the String methods
192
String JavaDoc[] values = new String JavaDoc[] { "key", "value", "key", "value" };
193         assertTrue( _cmdline.hasOption( "j" ) );
194         assertTrue( _cmdline.hasOption( 'j' ) );
195         assertTrue( _cmdline.getOptionValues( "j" ).length == 4);
196         assertTrue( _cmdline.getOptionValues( 'j' ).length == 4);
197         assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "j" ) ) );
198         assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'j' ) ) );
199
200         values = new String JavaDoc[] { "key1", "value1", "key2", "value2" };
201         assertTrue( _cmdline.hasOption( "k" ) );
202         assertTrue( _cmdline.hasOption( 'k' ) );
203         assertTrue( _cmdline.getOptionValues( "k" ).length == 4 );
204         assertTrue( _cmdline.getOptionValues( 'k' ).length == 4 );
205         assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "k" ) ) );
206         assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'k' ) ) );
207
208         values = new String JavaDoc[] { "key", "value" };
209         assertTrue( _cmdline.hasOption( "m" ) );
210         assertTrue( _cmdline.hasOption( 'm' ) );
211         assertTrue( _cmdline.getOptionValues( "m" ).length == 2);
212         assertTrue( _cmdline.getOptionValues( 'm' ).length == 2);
213         assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "m" ) ) );
214         assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'm' ) ) );
215     }
216
217     /**
218      * jkeyes - commented out this test as the new architecture
219      * breaks this type of functionality. I have left the test
220      * here in case I get a brainwave on how to resolve this.
221      */

222     /*
223     public void testGetValue()
224     {
225         // the 'm' option
226         assertTrue( _option.getValues().length == 2 );
227         assertEquals( _option.getValue(), "key" );
228         assertEquals( _option.getValue( 0 ), "key" );
229         assertEquals( _option.getValue( 1 ), "value" );
230
231         try {
232             assertEquals( _option.getValue( 2 ), "key" );
233             fail( "IndexOutOfBounds not caught" );
234         }
235         catch( IndexOutOfBoundsException exp ) {
236             
237         }
238
239         try {
240             assertEquals( _option.getValue( -1 ), "key" );
241             fail( "IndexOutOfBounds not caught" );
242         }
243         catch( IndexOutOfBoundsException exp ) {
244
245         }
246     }
247     */

248 }
249
Popular Tags