|
| 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.cli2.bug; |
| 18 | + |
| 19 | +import junit.framework.TestCase; |
| 20 | + |
| 21 | +import org.apache.commons.cli2.CommandLine; |
| 22 | +import org.apache.commons.cli2.Group; |
| 23 | +import org.apache.commons.cli2.Option; |
| 24 | +import org.apache.commons.cli2.OptionException; |
| 25 | +import org.apache.commons.cli2.builder.ArgumentBuilder; |
| 26 | +import org.apache.commons.cli2.builder.DefaultOptionBuilder; |
| 27 | +import org.apache.commons.cli2.builder.GroupBuilder; |
| 28 | +import org.apache.commons.cli2.commandline.Parser; |
| 29 | + |
| 30 | +/** |
| 31 | + * Group options are not added to the command line when child elements are |
| 32 | + * detected. This causes the validation of maximum and minimum to fail. |
| 33 | + * |
| 34 | + * @author Oliver Heger |
| 35 | + * @version $Id$ |
| 36 | + */ |
| 37 | +public class BugCLI123Test extends TestCase { |
| 38 | + /** An option of the parent group. */ |
| 39 | + private Option parentOption; |
| 40 | + |
| 41 | + /** An option of the child group. */ |
| 42 | + private Option childOption1; |
| 43 | + |
| 44 | + /** Another option of the child group. */ |
| 45 | + private Option childOption2; |
| 46 | + |
| 47 | + /** The parent group. */ |
| 48 | + private Group parentGroup; |
| 49 | + |
| 50 | + /** The child group. */ |
| 51 | + private Group childGroup; |
| 52 | + |
| 53 | + /** The parser. */ |
| 54 | + private Parser parser; |
| 55 | + |
| 56 | + protected void setUp() throws Exception { |
| 57 | + super.setUp(); |
| 58 | + final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); |
| 59 | + final ArgumentBuilder abuilder = new ArgumentBuilder(); |
| 60 | + final GroupBuilder gbuilder = new GroupBuilder(); |
| 61 | + parentOption = obuilder.withLongName("parent").withShortName("p") |
| 62 | + .withArgument(abuilder.withName("name").create()).create(); |
| 63 | + childOption1 = obuilder.withLongName("child").withShortName("c") |
| 64 | + .withArgument(abuilder.withName("c").create()).create(); |
| 65 | + childOption2 = obuilder.withLongName("sub").withShortName("s") |
| 66 | + .withArgument(abuilder.withName("s").create()).create(); |
| 67 | + childGroup = gbuilder.withName("childOptions").withMinimum(0) |
| 68 | + .withMaximum(2).withOption(childOption1).withOption( |
| 69 | + childOption2).create(); |
| 70 | + parentGroup = gbuilder.withName("parentOptions").withMinimum(1) |
| 71 | + .withMaximum(1).withOption(parentOption).withOption(childGroup) |
| 72 | + .create(); |
| 73 | + parser = new Parser(); |
| 74 | + parser.setGroup(parentGroup); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * A single option of the child group is specified. |
| 79 | + */ |
| 80 | + public void testSingleChildOption() throws OptionException { |
| 81 | + CommandLine cl = parser.parse(new String[] { "--child", "test" }); |
| 82 | + assertTrue("Child option not found", cl.hasOption(childOption1)); |
| 83 | + assertEquals("Wrong value for option", "test", cl |
| 84 | + .getValue(childOption1)); |
| 85 | + assertTrue("Child group not found", cl.hasOption(childGroup)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Two options of the child group are specified. |
| 90 | + */ |
| 91 | + public void testMultipleChildOptions() throws OptionException { |
| 92 | + CommandLine cl = parser.parse(new String[] { "--child", "test", |
| 93 | + "--sub", "anotherTest" }); |
| 94 | + assertTrue("Child option not found", cl.hasOption(childOption1)); |
| 95 | + assertEquals("Wrong value for option", "test", cl |
| 96 | + .getValue(childOption1)); |
| 97 | + assertTrue("Sub option not found", cl.hasOption(childOption2)); |
| 98 | + assertEquals("Wrong value for sub option", "anotherTest", cl |
| 99 | + .getValue(childOption2)); |
| 100 | + assertTrue("Child group not found", cl.hasOption(childGroup)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * The option defined for the parent group is specified. |
| 105 | + */ |
| 106 | + public void testSingleParentOption() throws OptionException { |
| 107 | + CommandLine cl = parser.parse(new String[] { "--parent", "yes" }); |
| 108 | + assertTrue("Parent option not found", cl.hasOption(parentOption)); |
| 109 | + assertEquals("Wrong value for option", "yes", cl.getValue(parentOption)); |
| 110 | + assertFalse("Found child group", cl.hasOption(childGroup)); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The parent option and an option of the child group is specified. This |
| 115 | + * should cause an exception. |
| 116 | + */ |
| 117 | + public void testParentOptionAndChildOption() throws OptionException { |
| 118 | + try { |
| 119 | + parser.parse(new String[] { "--parent", "error", "--child", |
| 120 | + "exception" }); |
| 121 | + fail("Maximum restriction for parent not verified!"); |
| 122 | + } catch (OptionException oex) { |
| 123 | + // ok |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments