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 /*** 24 * @author John Keyes (john at integralsource.com) 25 * @version $Revision: 542144 $ 26 */ 27 public class ParseRequiredTest extends TestCase 28 { 29 30 private Options _options = null; 31 private CommandLineParser parser = new PosixParser(); 32 33 public static Test suite() { 34 return new TestSuite(ParseRequiredTest.class); 35 } 36 37 public ParseRequiredTest(String name) 38 { 39 super(name); 40 } 41 42 public void setUp() 43 { 44 _options = new Options() 45 .addOption("a", 46 "enable-a", 47 false, 48 "turn [a] on or off") 49 .addOption( OptionBuilder.withLongOpt( "bfile" ) 50 .hasArg() 51 .isRequired() 52 .withDescription( "set the value of [b]" ) 53 .create( 'b' ) ); 54 } 55 56 public void tearDown() 57 { 58 59 } 60 61 public void testWithRequiredOption() 62 { 63 String[] args = new String[] { "-b", "file" }; 64 65 try 66 { 67 CommandLine cl = parser.parse(_options,args); 68 69 assertTrue( "Confirm -a is NOT set", !cl.hasOption("a") ); 70 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 71 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("file") ); 72 assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0); 73 } 74 catch (ParseException e) 75 { 76 fail( e.toString() ); 77 } 78 } 79 80 public void testOptionAndRequiredOption() 81 { 82 String[] args = new String[] { "-a", "-b", "file" }; 83 84 try 85 { 86 CommandLine cl = parser.parse(_options,args); 87 88 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 89 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 90 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("file") ); 91 assertTrue( "Confirm NO of extra args", cl.getArgList().size() == 0); 92 } 93 catch (ParseException e) 94 { 95 fail( e.toString() ); 96 } 97 } 98 99 public void testMissingRequiredOption() 100 { 101 String[] args = new String[] { "-a" }; 102 103 try 104 { 105 CommandLine cl = parser.parse(_options,args); 106 fail( "exception should have been thrown" ); 107 } 108 catch (ParseException e) 109 { 110 if( !( e instanceof MissingOptionException ) ) 111 { 112 fail( "expected to catch MissingOptionException" ); 113 } 114 } 115 } 116 117 }