001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.bug;
018
019import junit.framework.TestCase;
020
021import org.apache.commons.cli2.Group;
022import org.apache.commons.cli2.Option;
023import org.apache.commons.cli2.OptionException;
024import org.apache.commons.cli2.builder.DefaultOptionBuilder;
025import org.apache.commons.cli2.builder.GroupBuilder;
026import org.apache.commons.cli2.commandline.Parser;
027
028/**
029 * @author John Keyes
030 */
031public class Bug13886Test extends TestCase {
032
033    public Bug13886Test(final String name) {
034        super(name);
035    }
036
037    public void testMandatoryGroup() throws Exception {
038        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
039        final GroupBuilder gbuilder = new GroupBuilder();
040
041        final Option a = obuilder.withShortName("a").create();
042
043        final Option b = obuilder.withShortName("b").create();
044
045        final Group options =
046            gbuilder
047                .withOption(a)
048                .withOption(b)
049                .withMaximum(1)
050                .withMinimum(1)
051                .create();
052
053        final Parser parser = new Parser();
054        parser.setGroup(options);
055
056        try {
057            parser.parse(new String[] {
058            });
059            fail("Expected MissingOptionException not caught");
060        }
061        catch (final OptionException exp) {
062            assertEquals("Missing option -a|-b", exp.getMessage());
063        }
064
065        try {
066            parser.parse(new String[] { "-a" });
067        }
068        catch (final OptionException exp) {
069            fail("Unexpected MissingOptionException caught");
070        }
071
072        try {
073            parser.parse(new String[] { "-b" });
074        }
075        catch (final OptionException exp) {
076            fail("Unexpected MissingOptionException caught");
077        }
078
079        try {
080            parser.parse(new String[] { "-a", "-b" });
081            fail("Expected UnexpectedOptionException not caught");
082        }
083        catch (final OptionException exp) {
084            assertEquals(
085                "Unexpected -b while processing -a|-b",
086                exp.getMessage());
087        }
088    }
089}