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 OptionGroupTest extends TestCase 28 { 29 30 private Options _options = null; 31 private CommandLineParser parser = new PosixParser(); 32 33 34 public static Test suite() 35 { 36 return new TestSuite ( OptionGroupTest.class ); 37 } 38 39 public OptionGroupTest( String name ) 40 { 41 super( name ); 42 } 43 44 public void setUp() 45 { 46 Option file = new Option( "f", "file", false, "file to process" ); 47 Option dir = new Option( "d", "directory", false, "directory to process" ); 48 OptionGroup group = new OptionGroup(); 49 group.addOption( file ); 50 group.addOption( dir ); 51 _options = new Options().addOptionGroup( group ); 52 53 Option section = new Option( "s", "section", false, "section to process" ); 54 Option chapter = new Option( "c", "chapter", false, "chapter to process" ); 55 OptionGroup group2 = new OptionGroup(); 56 group2.addOption( section ); 57 group2.addOption( chapter ); 58 59 _options.addOptionGroup( group2 ); 60 61 Option importOpt = new Option( null, "import", false, "section to process" ); 62 Option exportOpt = new Option( null, "export", false, "chapter to process" ); 63 OptionGroup group3 = new OptionGroup(); 64 group3.addOption( importOpt ); 65 group3.addOption( exportOpt ); 66 _options.addOptionGroup( group3 ); 67 68 _options.addOption( "r", "revision", false, "revision number" ); 69 } 70 71 public void tearDown() 72 { 73 } 74 75 public void testSingleOptionFromGroup() 76 { 77 String[] args = new String[] { "-f" }; 78 79 try 80 { 81 CommandLine cl = parser.parse( _options, args); 82 83 assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); 84 assertTrue( "Confirm -f is set", cl.hasOption("f") ); 85 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 86 assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); 87 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 88 assertTrue( "Confirm no extra args", cl.getArgList().size() == 0); 89 } 90 catch (ParseException e) 91 { 92 fail( e.toString() ); 93 } 94 } 95 96 public void testSingleOption() 97 { 98 String[] args = new String[] { "-r" }; 99 100 try 101 { 102 CommandLine cl = parser.parse( _options, args); 103 104 assertTrue( "Confirm -r is set", cl.hasOption("r") ); 105 assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") ); 106 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 107 assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); 108 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 109 assertTrue( "Confirm no extra args", cl.getArgList().size() == 0); 110 } 111 catch (ParseException e) 112 { 113 fail( e.toString() ); 114 } 115 } 116 117 public void testTwoValidOptions() 118 { 119 String[] args = new String[] { "-r", "-f" }; 120 121 try 122 { 123 CommandLine cl = parser.parse( _options, args); 124 125 assertTrue( "Confirm -r is set", cl.hasOption("r") ); 126 assertTrue( "Confirm -f is set", cl.hasOption("f") ); 127 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 128 assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); 129 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 130 assertTrue( "Confirm no extra args", cl.getArgList().size() == 0); 131 } 132 catch (ParseException e) 133 { 134 fail( e.toString() ); 135 } 136 } 137 138 public void testSingleLongOption() 139 { 140 String[] args = new String[] { "--file" }; 141 142 try 143 { 144 CommandLine cl = parser.parse( _options, args); 145 146 assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); 147 assertTrue( "Confirm -f is set", cl.hasOption("f") ); 148 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 149 assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); 150 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 151 assertTrue( "Confirm no extra args", cl.getArgList().size() == 0); 152 } 153 catch (ParseException e) 154 { 155 fail( e.toString() ); 156 } 157 } 158 159 public void testTwoValidLongOptions() 160 { 161 String[] args = new String[] { "--revision", "--file" }; 162 163 try 164 { 165 CommandLine cl = parser.parse( _options, args); 166 167 assertTrue( "Confirm -r is set", cl.hasOption("r") ); 168 assertTrue( "Confirm -f is set", cl.hasOption("f") ); 169 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 170 assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); 171 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 172 assertTrue( "Confirm no extra args", cl.getArgList().size() == 0); 173 } 174 catch (ParseException e) 175 { 176 fail( e.toString() ); 177 } 178 } 179 180 public void testNoOptionsExtraArgs() 181 { 182 String[] args = new String[] { "arg1", "arg2" }; 183 184 try 185 { 186 CommandLine cl = parser.parse( _options, args); 187 188 assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); 189 assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") ); 190 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 191 assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") ); 192 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 193 assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2); 194 } 195 catch (ParseException e) 196 { 197 fail( e.toString() ); 198 } 199 } 200 201 public void testTwoOptionsFromGroup() 202 { 203 String[] args = new String[] { "-f", "-d" }; 204 205 try 206 { 207 CommandLine cl = parser.parse( _options, args); 208 fail( "two arguments from group not allowed" ); 209 } 210 catch (ParseException e) 211 { 212 if( !( e instanceof AlreadySelectedException ) ) 213 { 214 fail( "incorrect exception caught:" + e.getMessage() ); 215 } 216 } 217 } 218 219 public void testTwoLongOptionsFromGroup() 220 { 221 String[] args = new String[] { "--file", "--directory" }; 222 223 try 224 { 225 CommandLine cl = parser.parse( _options, args); 226 fail( "two arguments from group not allowed" ); 227 } 228 catch (ParseException e) 229 { 230 if( !( e instanceof AlreadySelectedException ) ) 231 { 232 fail( "incorrect exception caught:" + e.getMessage() ); 233 } 234 } 235 } 236 237 public void testTwoOptionsFromDifferentGroup() 238 { 239 String[] args = new String[] { "-f", "-s" }; 240 241 try 242 { 243 CommandLine cl = parser.parse( _options, args); 244 assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") ); 245 assertTrue( "Confirm -f is set", cl.hasOption("f") ); 246 assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") ); 247 assertTrue( "Confirm -s is set", cl.hasOption("s") ); 248 assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") ); 249 assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0); 250 } 251 catch (ParseException e) 252 { 253 fail( e.toString() ); 254 } 255 } 256 257 public void testValidLongOnlyOptions() 258 { 259 try 260 { 261 CommandLine cl = parser.parse( _options, new String[]{"--export"}); 262 assertTrue( "Confirm --export is set", cl.hasOption("export") ); 263 } 264 catch (ParseException e) 265 { 266 fail( e.toString() ); 267 } 268 269 try 270 { 271 CommandLine cl = parser.parse( _options, new String[]{"--import"}); 272 assertTrue( "Confirm --import is set", cl.hasOption("import") ); 273 } 274 catch (ParseException e) 275 { 276 fail( e.toString() ); 277 } 278 } 279 280 281 }