1 10 11 package org.apache.commons.cli; 12 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 public class BugsTest extends TestCase 18 { 19 20 private CommandLine _cmdline = null; 21 private Option _option = null; 22 23 public static Test suite() { 24 return new TestSuite( BugsTest.class ); 25 } 26 27 public BugsTest( String name ) 28 { 29 super( name ); 30 } 31 32 public void setUp() 33 { 34 } 35 36 public void tearDown() 37 { 38 } 39 40 public void test11457() { 41 Options options = new Options(); 42 options.addOption( OptionBuilder.withLongOpt( "verbose" ) 43 .create() ); 44 String [] args = new String [] { "--verbose" }; 45 46 CommandLineParser parser = new PosixParser(); 47 48 try { 49 CommandLine cmd = parser.parse( options, args ); 50 assertTrue( cmd.hasOption( "verbose" ) ); 51 } 52 catch( ParseException exp ) { 53 exp.printStackTrace(); 54 fail( "Unexpected Exception: " + exp.getMessage() ); 55 } 56 } 57 58 public void test11458() 59 { 60 Options options = new Options(); 61 options.addOption( OptionBuilder.withValueSeparator( '=' ) 62 .hasArgs() 63 .create( 'D' ) ); 64 options.addOption( OptionBuilder.withValueSeparator( ':' ) 65 .hasArgs() 66 .create( 'p' ) ); 67 String [] args = new String [] { "-DJAVA_HOME=/opt/java" , 68 "-pfile1:file2:file3" }; 69 70 CommandLineParser parser = new PosixParser(); 71 72 try { 73 CommandLine cmd = parser.parse( options, args ); 74 75 String [] values = cmd.getOptionValues( 'D' ); 76 77 assertEquals( values[0], "JAVA_HOME" ); 78 assertEquals( values[1], "/opt/java" ); 79 80 values = cmd.getOptionValues( 'p' ); 81 82 assertEquals( values[0], "file1" ); 83 assertEquals( values[1], "file2" ); 84 assertEquals( values[2], "file3" ); 85 86 java.util.Iterator iter = cmd.iterator(); 87 while( iter.hasNext() ) { 88 Option opt = (Option)iter.next(); 89 switch( opt.getId() ) { 90 case 'D': 91 assertEquals( opt.getValue( 0 ), "JAVA_HOME" ); 92 assertEquals( opt.getValue( 1 ), "/opt/java" ); 93 break; 94 case 'p': 95 assertEquals( opt.getValue( 0 ), "file1" ); 96 assertEquals( opt.getValue( 1 ), "file2" ); 97 assertEquals( opt.getValue( 2 ), "file3" ); 98 break; 99 default: 100 fail( "-D option not found" ); 101 } 102 } 103 } 104 catch( ParseException exp ) { 105 fail( "Unexpected Exception:\nMessage:" + exp.getMessage() 106 + "Type: " + exp.getClass().getName() ); 107 } 108 } 109 110 public void test11680() 111 { 112 Options options = new Options(); 113 options.addOption("f", true, "foobar"); 114 options.addOption("m", true, "missing"); 115 String [] args = new String [] { "-f" , "foo" }; 116 117 CommandLineParser parser = new PosixParser(); 118 119 try { 120 CommandLine cmd = parser.parse( options, args ); 121 122 try { 123 cmd.getOptionValue( "f", "default f"); 124 cmd.getOptionValue( "m", "default m"); 125 } 126 catch( NullPointerException exp ) { 127 fail( "NullPointer caught: " + exp.getMessage() ); 128 } 129 } 130 catch( ParseException exp ) { 131 fail( "Unexpected Exception: " + exp.getMessage() ); 132 } 133 } 134 135 public void test11456() 136 { 137 Options options = new Options(); 139 options.addOption( OptionBuilder.hasOptionalArg() 140 .create( 'a' ) ); 141 options.addOption( OptionBuilder.hasArg() 142 .create( 'b' ) ); 143 String [] args = new String [] { "-a", "-bvalue" }; 144 145 CommandLineParser parser = new PosixParser(); 146 147 try { 148 CommandLine cmd = parser.parse( options, args ); 149 assertEquals( cmd.getOptionValue( 'b' ), "value" ); 150 } 151 catch( ParseException exp ) { 152 fail( "Unexpected Exception: " + exp.getMessage() ); 153 } 154 155 options = new Options(); 157 options.addOption( OptionBuilder.hasOptionalArg() 158 .create( 'a' ) ); 159 options.addOption( OptionBuilder.hasArg() 160 .create( 'b' ) ); 161 args = new String [] { "-a", "-b", "value" }; 162 163 parser = new GnuParser(); 164 165 try { 166 CommandLine cmd = parser.parse( options, args ); 167 assertEquals( cmd.getOptionValue( 'b' ), "value" ); 168 } 169 catch( ParseException exp ) { 170 fail( "Unexpected Exception: " + exp.getMessage() ); 171 } 172 173 } 174 175 public void test12210() { 176 Options mainOptions = new Options(); 178 180 182 String [] argv = new String [] { "-exec", "-exec_opt1", "-exec_opt2" }; 183 OptionGroup grp = new OptionGroup(); 184 185 grp.addOption(new Option("exec",false,"description for this option")); 186 187 grp.addOption(new Option("rep",false,"description for this option")); 188 189 mainOptions.addOptionGroup(grp); 190 191 Options execOptions = new Options(); 193 execOptions.addOption("exec_opt1",false," desc"); 194 execOptions.addOption("exec_opt2",false," desc"); 195 196 Options repOptions = new Options(); 198 repOptions.addOption("repopto",false,"desc"); 199 repOptions.addOption("repoptt",false,"desc"); 200 201 GnuParser parser = new GnuParser(); 203 204 206 try { 210 CommandLine cmd = parser.parse(mainOptions,argv,true); 211 argv = cmd.getArgs(); 213 214 if(cmd.hasOption("exec")){ 215 cmd = parser.parse(execOptions,argv,false); 216 assertTrue( cmd.hasOption("exec_opt1") ); 218 assertTrue( cmd.hasOption("exec_opt2") ); 219 } 220 else if(cmd.hasOption("rep")){ 221 cmd = parser.parse(repOptions,argv,false); 222 } 224 else { 225 fail( "exec option not found" ); 226 } 227 } 228 catch( ParseException exp ) { 229 fail( "Unexpected exception: " + exp.getMessage() ); 230 } 231 } 232 233 public void test13425() { 234 Options options = new Options(); 235 Option oldpass = OptionBuilder.withLongOpt( "old-password" ) 236 .withDescription( "Use this option to specify the old password" ) 237 .hasArg() 238 .create( 'o' ); 239 Option newpass = OptionBuilder.withLongOpt( "new-password" ) 240 .withDescription( "Use this option to specify the new password" ) 241 .hasArg() 242 .create( 'n' ); 243 244 String [] args = { 245 "-o", 246 "-n", 247 "newpassword" 248 }; 249 250 options.addOption( oldpass ); 251 options.addOption( newpass ); 252 253 Parser parser = new PosixParser(); 254 255 try { 256 CommandLine line = parser.parse( options, args ); 257 } 258 catch( Exception exp ) { 260 assertTrue( exp != null ); 261 return; 262 } 263 fail( "MissingArgumentException not caught." ); 264 } 265 266 public void test13666() { 267 Options options = new Options(); 268 Option dir = OptionBuilder.withDescription( "dir" ) 269 .hasArg() 270 .create( 'd' ); 271 options.addOption( dir ); 272 try { 273 HelpFormatter formatter = new HelpFormatter(); 274 formatter.printHelp( "dir", options ); 275 } 276 catch( Exception exp ) { 277 fail( "Unexpected Exception: " + exp.getMessage() ); 278 } 279 } 280 281 public void test13935() { 282 OptionGroup directions = new OptionGroup(); 283 284 Option left = new Option( "l", "left", false, "go left" ); 285 Option right = new Option( "r", "right", false, "go right" ); 286 Option straight = new Option( "s", "straight", false, "go straight" ); 287 Option forward = new Option( "f", "forward", false, "go forward" ); 288 forward.setRequired( true ); 289 290 directions.addOption( left ); 291 directions.addOption( right ); 292 directions.setRequired( true ); 293 294 Options opts = new Options(); 295 opts.addOptionGroup( directions ); 296 opts.addOption( straight ); 297 298 CommandLineParser parser = new PosixParser(); 299 boolean exception = false; 300 301 String [] args = new String [] { }; 302 try { 303 CommandLine line = parser.parse( opts, args ); 304 } 305 catch( ParseException exp ) { 306 exception = true; 307 } 308 309 if( !exception ) { 310 fail( "Expected exception not caught."); 311 } 312 313 exception = false; 314 315 args = new String [] { "-s" }; 316 try { 317 CommandLine line = parser.parse( opts, args ); 318 } 319 catch( ParseException exp ) { 320 exception = true; 321 } 322 323 if( !exception ) { 324 fail( "Expected exception not caught."); 325 } 326 327 exception = false; 328 329 args = new String [] { "-s", "-l" }; 330 try { 331 CommandLine line = parser.parse( opts, args ); 332 } 333 catch( ParseException exp ) { 334 fail( "Unexpected exception: " + exp.getMessage() ); 335 } 336 337 opts.addOption( forward ); 338 args = new String [] { "-s", "-l", "-f" }; 339 try { 340 CommandLine line = parser.parse( opts, args ); 341 } 342 catch( ParseException exp ) { 343 fail( "Unexpected exception: " + exp.getMessage() ); 344 } 345 } 346 } 347 | Popular Tags |