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 junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 23 public class ParseTest extends TestCase 24 { 25 26 private Options _options = null; 27 private Parser _parser = null; 28 29 public static Test suite() { 30 return new TestSuite(ParseTest.class); 31 } 32 33 public ParseTest(String name) 34 { 35 super(name); 36 } 37 38 public void setUp() 39 { 40 _options = new Options() 41 .addOption("a", 42 "enable-a", 43 false, 44 "turn [a] on or off") 45 .addOption("b", 46 "bfile", 47 true, 48 "set the value of [b]") 49 .addOption("c", 50 "copt", 51 false, 52 "turn [c] on or off"); 53 54 _parser = new PosixParser(); 55 } 56 57 public void tearDown() 58 { 59 60 } 61 62 public void testSimpleShort() 63 { 64 String[] args = new String[] { "-a", 65 "-b", "toast", 66 "foo", "bar" }; 67 68 try 69 { 70 CommandLine cl = _parser.parse(_options, args); 71 72 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 73 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 74 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 75 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); 76 } 77 catch (ParseException e) 78 { 79 fail( e.toString() ); 80 } 81 } 82 83 public void testSimpleLong() 84 { 85 String[] args = new String[] { "--enable-a", 86 "--bfile", "toast", 87 "foo", "bar" }; 88 89 try 90 { 91 CommandLine cl = _parser.parse(_options, args); 92 93 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 94 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 95 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 96 assertTrue( "Confirm arg of --bfile", cl.getOptionValue( "bfile" ).equals( "toast" ) ); 97 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); 98 } 99 catch (ParseException e) 100 { 101 fail( e.toString() ); 102 } 103 } 104 105 public void testComplexShort() 106 { 107 String[] args = new String[] { "-acbtoast", 108 "foo", "bar" }; 109 110 try 111 { 112 CommandLine cl = _parser.parse(_options, args); 113 114 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 115 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 116 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 117 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 118 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); 119 } 120 catch (ParseException e) 121 { 122 fail( e.toString() ); 123 } 124 } 125 126 public void testExtraOption() 127 { 128 String[] args = new String[] { "-adbtoast", 129 "foo", "bar" }; 130 131 boolean caught = false; 132 133 try 134 { 135 CommandLine cl = _parser.parse(_options, args); 136 137 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 138 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 139 assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 140 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3); 141 } 142 catch (UnrecognizedOptionException e) 143 { 144 caught = true; 145 } 146 catch (ParseException e) 147 { 148 fail( e.toString() ); 149 } 150 assertTrue( "Confirm UnrecognizedOptionException caught", caught ); 151 } 152 153 public void testMissingArg() 154 { 155 156 String[] args = new String[] { "-acb" }; 157 158 boolean caught = false; 159 160 try 161 { 162 CommandLine cl = _parser.parse(_options, args); 163 } 164 catch (MissingArgumentException e) 165 { 166 caught = true; 167 } 168 catch (ParseException e) 169 { 170 fail( e.toString() ); 171 } 172 173 assertTrue( "Confirm MissingArgumentException caught", caught ); 174 } 175 176 public void testStop() 177 { 178 String[] args = new String[] { "-c", 179 "foober", 180 "-btoast" }; 181 182 try 183 { 184 CommandLine cl = _parser.parse(_options, args, true); 185 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 186 assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); 187 } 188 catch (ParseException e) 189 { 190 fail( e.toString() ); 191 } 192 } 193 194 public void testMultiple() 195 { 196 String[] args = new String[] { "-c", 197 "foobar", 198 "-btoast" }; 199 200 try 201 { 202 CommandLine cl = _parser.parse(_options, args, true); 203 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 204 assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); 205 206 cl = _parser.parse(_options, cl.getArgs() ); 207 208 assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); 209 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 210 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 211 assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); 212 assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); 213 } 214 catch (ParseException e) 215 { 216 fail( e.toString() ); 217 } 218 } 219 220 public void testMultipleWithLong() 221 { 222 String[] args = new String[] { "--copt", 223 "foobar", 224 "--bfile", "toast" }; 225 226 try 227 { 228 CommandLine cl = _parser.parse(_options,args, 229 true); 230 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 231 assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); 232 233 cl = _parser.parse(_options, cl.getArgs() ); 234 235 assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); 236 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 237 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 238 assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); 239 assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); 240 } 241 catch (ParseException e) 242 { 243 fail( e.toString() ); 244 } 245 } 246 247 public void testDoubleDash() 248 { 249 String[] args = new String[] { "--copt", 250 "--", 251 "-b", "toast" }; 252 253 try 254 { 255 CommandLine cl = _parser.parse(_options, args); 256 257 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 258 assertTrue( "Confirm -b is not set", ! cl.hasOption("b") ); 259 assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); 260 261 } 262 catch (ParseException e) 263 { 264 fail( e.toString() ); 265 } 266 } 267 268 public void testSingleDash() 269 { 270 String[] args = new String[] { "--copt", 271 "-b", "-", 272 "-a", 273 "-" }; 274 275 try 276 { 277 CommandLine cl = _parser.parse(_options, args); 278 279 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 280 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 281 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("-") ); 282 assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); 283 assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") ); 284 } 285 catch (ParseException e) 286 { 287 fail( e.toString() ); 288 } 289 290 } 291 }