1 /***
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.cli;
18
19 import java.util.Arrays;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 public class ValuesTest extends TestCase
26 {
27 /*** CommandLine instance */
28 private CommandLine _cmdline = null;
29 private Option _option = null;
30
31 public static Test suite() {
32 return new TestSuite( ValuesTest.class );
33 }
34
35 public ValuesTest( String name )
36 {
37 super( name );
38 }
39
40 public void setUp()
41 {
42 Options opts = new Options();
43
44 opts.addOption("a",
45 false,
46 "toggle -a");
47
48 opts.addOption("b",
49 true,
50 "set -b");
51
52 opts.addOption("c",
53 "c",
54 false,
55 "toggle -c");
56
57 opts.addOption("d",
58 "d",
59 true,
60 "set -d");
61
62 opts.addOption( OptionBuilder.withLongOpt( "e" )
63 .hasArgs()
64 .withDescription( "set -e ")
65 .create( 'e' ) );
66
67 opts.addOption("f",
68 "f",
69 false,
70 "jk");
71
72 opts.addOption( OptionBuilder.withLongOpt( "g" )
73 .hasArgs( 2 )
74 .withDescription( "set -g")
75 .create( 'g' ) );
76
77 opts.addOption( OptionBuilder.withLongOpt( "h" )
78 .hasArgs( 2 )
79 .withDescription( "set -h")
80 .create( 'h' ) );
81
82 opts.addOption( OptionBuilder.withLongOpt( "i" )
83 .withDescription( "set -i")
84 .create( 'i' ) );
85
86 opts.addOption( OptionBuilder.withLongOpt( "j" )
87 .hasArgs( )
88 .withDescription( "set -j")
89 .withValueSeparator( '=' )
90 .create( 'j' ) );
91
92 opts.addOption( OptionBuilder.withLongOpt( "k" )
93 .hasArgs( )
94 .withDescription( "set -k")
95 .withValueSeparator( '=' )
96 .create( 'k' ) );
97
98 _option = OptionBuilder.withLongOpt( "m" )
99 .hasArgs( )
100 .withDescription( "set -m")
101 .withValueSeparator( )
102 .create( 'm' );
103
104 opts.addOption( _option );
105
106 String[] args = new String[] { "-a",
107 "-b", "foo",
108 "--c",
109 "--d", "bar",
110 "-e", "one", "two",
111 "-f",
112 "arg1", "arg2",
113 "-g", "val1", "val2" , "arg3",
114 "-h", "val1", "-i",
115 "-h", "val2",
116 "-jkey=value",
117 "-j", "key=value",
118 "-kkey1=value1",
119 "-kkey2=value2",
120 "-mkey=value"};
121
122 CommandLineParser parser = new PosixParser();
123
124 try
125 {
126 _cmdline = parser.parse(opts,args);
127 }
128 catch (ParseException e)
129 {
130 fail("Cannot setUp() CommandLine: " + e.toString());
131 }
132 }
133
134 public void tearDown()
135 {
136
137 }
138
139 public void testShortArgs()
140 {
141 assertTrue( _cmdline.hasOption("a") );
142 assertTrue( _cmdline.hasOption("c") );
143
144 assertNull( _cmdline.getOptionValues("a") );
145 assertNull( _cmdline.getOptionValues("c") );
146 }
147
148 public void testShortArgsWithValue()
149 {
150 assertTrue( _cmdline.hasOption("b") );
151 assertTrue( _cmdline.getOptionValue("b").equals("foo"));
152 assertTrue( _cmdline.getOptionValues("b").length == 1);
153
154 assertTrue( _cmdline.hasOption("d") );
155 assertTrue( _cmdline.getOptionValue("d").equals("bar"));
156 assertTrue( _cmdline.getOptionValues("d").length == 1);
157 }
158
159 public void testMultipleArgValues()
160 {
161 String[] result = _cmdline.getOptionValues("e");
162 String[] values = new String[] { "one", "two" };
163 assertTrue( _cmdline.hasOption("e") );
164 assertTrue( _cmdline.getOptionValues("e").length == 2);
165 assertTrue( Arrays.equals( values, _cmdline.getOptionValues("e") ) );
166 }
167
168 public void testTwoArgValues()
169 {
170 String[] result = _cmdline.getOptionValues("g");
171 String[] values = new String[] { "val1", "val2" };
172 assertTrue( _cmdline.hasOption("g") );
173 assertTrue( _cmdline.getOptionValues("g").length == 2);
174 assertTrue( Arrays.equals( values, _cmdline.getOptionValues("g") ) );
175 }
176
177 public void testComplexValues()
178 {
179 String[] result = _cmdline.getOptionValues("h");
180 String[] values = new String[] { "val1", "val2" };
181 assertTrue( _cmdline.hasOption("i") );
182 assertTrue( _cmdline.hasOption("h") );
183 assertTrue( _cmdline.getOptionValues("h").length == 2);
184 assertTrue( Arrays.equals( values, _cmdline.getOptionValues("h") ) );
185 }
186
187 public void testExtraArgs()
188 {
189 String[] args = new String[] { "arg1", "arg2", "arg3" };
190 assertTrue( _cmdline.getArgs().length == 3 );
191 assertTrue( Arrays.equals( args, _cmdline.getArgs() ) );
192 }
193
194 public void testCharSeparator()
195 {
196
197
198 String[] values = new String[] { "key", "value", "key", "value" };
199 assertTrue( _cmdline.hasOption( "j" ) );
200 assertTrue( _cmdline.hasOption( 'j' ) );
201 assertEquals( 4, _cmdline.getOptionValues( "j" ).length );
202 assertEquals( 4, _cmdline.getOptionValues( 'j' ).length );
203 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "j" ) ) );
204 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'j' ) ) );
205
206 values = new String[] { "key1", "value1", "key2", "value2" };
207 assertTrue( _cmdline.hasOption( "k" ) );
208 assertTrue( _cmdline.hasOption( 'k' ) );
209 assertTrue( _cmdline.getOptionValues( "k" ).length == 4 );
210 assertTrue( _cmdline.getOptionValues( 'k' ).length == 4 );
211 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "k" ) ) );
212 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'k' ) ) );
213
214 values = new String[] { "key", "value" };
215 assertTrue( _cmdline.hasOption( "m" ) );
216 assertTrue( _cmdline.hasOption( 'm' ) );
217 assertTrue( _cmdline.getOptionValues( "m" ).length == 2);
218 assertTrue( _cmdline.getOptionValues( 'm' ).length == 2);
219 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "m" ) ) );
220 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'm' ) ) );
221 }
222
223 /***
224 * jkeyes - commented out this test as the new architecture
225 * breaks this type of functionality. I have left the test
226 * here in case I get a brainwave on how to resolve this.
227 */
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 }