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 org.apache.commons.cli2.Argument;
020import org.apache.commons.cli2.Group;
021import org.apache.commons.cli2.Option;
022import org.apache.commons.cli2.OptionException;
023import org.apache.commons.cli2.builder.ArgumentBuilder;
024import org.apache.commons.cli2.builder.CommandBuilder;
025import org.apache.commons.cli2.builder.DefaultOptionBuilder;
026import org.apache.commons.cli2.builder.GroupBuilder;
027import org.apache.commons.cli2.commandline.Parser;
028import junit.framework.TestCase;
029
030public class Bug28005Test extends TestCase {
031    public void testInfiniteLoop() {
032        final DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
033        final ArgumentBuilder argumentBuilder = new ArgumentBuilder();
034        final GroupBuilder groupBuilder = new GroupBuilder();
035        final CommandBuilder commandBuilder = new CommandBuilder();
036
037        final Option inputFormatOption =
038            optionBuilder
039                .withLongName("input-format")
040                //.withArgument(argumentBuilder.create())
041                .create();
042
043        final Argument argument =
044            argumentBuilder
045                .withName("file")
046                .create();
047
048        final Group children =
049            groupBuilder
050                .withName("options")
051                .withOption(inputFormatOption)
052                .create();
053
054        final Option command =
055            commandBuilder
056                .withName("convert")
057                .withChildren(children)
058                .withArgument(argument)
059                .create();
060
061        final Group root =
062            groupBuilder
063                .withName("commands")
064                .withOption(command)
065                .create();
066
067        final Parser parser = new Parser();
068        parser.setGroup(root);
069        final String[] args = new String[]{"convert", "test.txt",
070                "--input-format", "a"};
071
072        try {
073            parser.parse(args);
074            fail("a isn't valid!!");
075        } catch (OptionException e) {
076            assertEquals("Unexpected a while processing commands",e.getMessage());
077        }
078    }
079}