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 java.util.Arrays;
020import java.util.List;
021
022import junit.framework.TestCase;
023
024import org.apache.commons.cli2.CommandLine;
025import org.apache.commons.cli2.Group;
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.DefaultOption;
031
032/**
033 * http://issues.apache.org/jira/browse/CLI-158
034 */
035public class BugCLI158Test extends TestCase {
036
037    private Parser createDefaultValueParser(String[] defaults) {
038        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
039        final ArgumentBuilder abuilder = new ArgumentBuilder();
040        final GroupBuilder gbuilder = new GroupBuilder();
041
042        DefaultOption bOption = obuilder.withShortName("b")
043                .withLongName("b")
044                .withArgument(abuilder.withName("b")
045                        .withMinimum(0)
046                        .withMaximum(defaults.length)
047                        .withDefaults(Arrays.asList(defaults))
048                        .create())
049                .create();
050
051        Group options = gbuilder
052                .withName("options")
053                .withOption(bOption)
054                .create();
055
056        Parser parser = new Parser();
057        parser.setHelpTrigger("--help");
058        parser.setGroup(options);
059        return parser;
060    }
061
062    public void testSingleOptionSingleArgument() throws Exception {
063        Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
064        String enteredValue1 = "1";
065        String[] args = new String[]{"-b", enteredValue1};
066        CommandLine cl = parser.parse(args);
067        CommandLine cmd = cl;
068        assertNotNull(cmd);
069        List b = cmd.getValues("-b");
070        assertEquals("[" + enteredValue1 + ", 1000]", b + "");
071    }
072
073    public void testSingleOptionNoArgument() throws Exception {
074        Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
075        String[] args = new String[]{"-b"};
076        CommandLine cl = parser.parse(args);
077        CommandLine cmd = cl;
078        assertNotNull(cmd);
079        List b = cmd.getValues("-b");
080        assertEquals("[100, 1000]", b + "");
081    }
082
083    public void testSingleOptionMaximumNumberOfArgument() throws Exception {
084        String[] args = new String[]{"-b", "1", "2"};
085        final ArgumentBuilder abuilder = new ArgumentBuilder();
086        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
087        final GroupBuilder gbuilder = new GroupBuilder();
088
089        DefaultOption bOption = obuilder.withShortName("b")
090                .withLongName("b")
091                .withArgument(abuilder.withName("b")
092                        .withMinimum(2)
093                        .withMaximum(4)
094                        .withDefault("100")
095                        .withDefault("1000")
096                        .withDefault("10000")
097                        .create())
098                .create();
099
100        Group options = gbuilder
101                .withName("options")
102                .withOption(bOption)
103                .create();
104
105        Parser parser = new Parser();
106        parser.setHelpTrigger("--help");
107        parser.setGroup(options);
108        CommandLine cl = parser.parse(args);
109        CommandLine cmd = cl;
110        assertNotNull(cmd);
111        List b = cmd.getValues("-b");
112        assertEquals("[1, 2, 10000]", b + "");
113    }
114}