001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.cli2.bug;
020
021import junit.framework.TestCase;
022
023import org.apache.commons.cli2.Argument;
024import org.apache.commons.cli2.Group;
025import org.apache.commons.cli2.OptionException;
026import org.apache.commons.cli2.builder.ArgumentBuilder;
027import org.apache.commons.cli2.builder.DefaultOptionBuilder;
028import org.apache.commons.cli2.builder.GroupBuilder;
029import org.apache.commons.cli2.commandline.Parser;
030import org.apache.commons.cli2.option.SourceDestArgument;
031
032/**
033 * The first is a loop in Parser.parse() if I set a non-declared option. This 
034 * code goes into a loop in Parser.java method parse this 'while' loop runs 
035 * endless
036 * 
037 * @author Steve Alberty
038 */
039public class BugLoopingOptionLookAlikeTest extends TestCase {
040
041    public void testLoopingOptionLookAlike() {
042        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
043        final ArgumentBuilder abuilder = new ArgumentBuilder();
044        final GroupBuilder gbuilder = new GroupBuilder();
045        final Group options = gbuilder
046            .withName("ant")
047            .withOption(obuilder.withShortName("help").withDescription("print this message").create())
048            .withOption(obuilder.withShortName("projecthelp").withDescription("print project help information").create())
049            .withOption(abuilder.withName("target").create())
050            .create();
051        
052        final Parser parser = new Parser();
053        parser.setGroup(options);
054        try {
055            parser.parse(new String[] { "-abcdef",
056                    "testfile.txt ", });
057            fail("OptionException");
058        } catch (OptionException e) {
059            assertEquals("Unexpected -abcdef while processing ant",e.getMessage());
060        }
061    }
062    
063    public void testLoopingOptionLookAlike2() {
064        final ArgumentBuilder abuilder = new ArgumentBuilder();
065        final GroupBuilder gbuilder = new GroupBuilder();
066        final Argument inputfile_opt = abuilder.withName("input").withMinimum(1).withMaximum(1).create();
067        final Argument outputfile_opt = abuilder.withName("output").withMinimum(1).withMaximum(1).create();
068        final Argument targets = new SourceDestArgument(inputfile_opt, outputfile_opt);
069        final Group options = gbuilder.withOption(targets).create();
070        final Parser parser = new Parser();
071        parser.setGroup(options);
072        try {
073            parser.parse(new String[] { "testfile.txt", "testfile.txt", "testfile.txt", "testfile.txt" });
074            fail("OptionException");
075        } catch (OptionException e) {
076            assertEquals("Unexpected testfile.txt while processing ", e.getMessage());
077        }
078    }    
079}