View Javadoc
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  
18  package org.apache.commons.cli;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  @SuppressWarnings("deprecation") // tests some deprecated classes
29  public class ValuesTest {
30      private CommandLine cmd;
31  
32      @BeforeEach
33      public void setUp() throws Exception {
34          final Options options = new Options();
35  
36          options.addOption("a", false, "toggle -a");
37          options.addOption("b", true, "set -b");
38          options.addOption("c", "c", false, "toggle -c");
39          options.addOption("d", "d", true, "set -d");
40  
41          options.addOption(OptionBuilder.withLongOpt("e").hasArgs().withDescription("set -e ").create('e'));
42          options.addOption("f", "f", false, "jk");
43          options.addOption(OptionBuilder.withLongOpt("g").hasArgs(2).withDescription("set -g").create('g'));
44          options.addOption(OptionBuilder.withLongOpt("h").hasArg().withDescription("set -h").create('h'));
45          options.addOption(OptionBuilder.withLongOpt("i").withDescription("set -i").create('i'));
46          options.addOption(OptionBuilder.withLongOpt("j").hasArgs().withDescription("set -j").withValueSeparator('=').create('j'));
47          options.addOption(OptionBuilder.withLongOpt("k").hasArgs().withDescription("set -k").withValueSeparator('=').create('k'));
48          options.addOption(OptionBuilder.withLongOpt("m").hasArgs().withDescription("set -m").withValueSeparator().create('m'));
49  
50          //@formatter:off
51          final String[] args = {
52              "-a",
53              "-b", "foo",
54              "--c",
55              "--d", "bar",
56              "-e", "one", "two",
57              "-f",
58              "arg1", "arg2",
59              "-g", "val1", "val2", "arg3",
60              "-h", "val1", "-i",
61              "-h", "val2",
62              "-jkey=value",
63              "-j", "key=value",
64              "-kkey1=value1",
65              "-kkey2=value2",
66              "-mkey=value"
67          };
68          //@formatter:on
69  
70          final CommandLineParser parser = new PosixParser();
71  
72          cmd = parser.parse(options, args);
73      }
74  
75      @Test
76      public void testCharSeparator() {
77          // tests the char methods of CommandLine that delegate to the String methods
78          assertTrue(cmd.hasOption("j"), "Option j is not set");
79          assertTrue(cmd.hasOption('j'), "Option j is not set");
80          assertArrayEquals(new String[] {"key", "value", "key", "value"}, cmd.getOptionValues("j"));
81          assertArrayEquals(new String[] {"key", "value", "key", "value"}, cmd.getOptionValues('j'));
82  
83          assertTrue(cmd.hasOption("k"), "Option k is not set");
84          assertTrue(cmd.hasOption('k'), "Option k is not set");
85          assertArrayEquals(new String[] {"key1", "value1", "key2", "value2"}, cmd.getOptionValues("k"));
86          assertArrayEquals(new String[] {"key1", "value1", "key2", "value2"}, cmd.getOptionValues('k'));
87  
88          assertTrue(cmd.hasOption("m"), "Option m is not set");
89          assertTrue(cmd.hasOption('m'), "Option m is not set");
90          assertArrayEquals(new String[] {"key", "value"}, cmd.getOptionValues("m"));
91          assertArrayEquals(new String[] {"key", "value"}, cmd.getOptionValues('m'));
92      }
93  
94      @Test
95      public void testComplexValues() {
96          assertTrue(cmd.hasOption("i"), "Option i is not set");
97          assertTrue(cmd.hasOption("h"), "Option h is not set");
98          assertArrayEquals(new String[] {"val1", "val2"}, cmd.getOptionValues("h"));
99      }
100 
101     @Test
102     public void testExtraArgs() {
103         assertArrayEquals(new String[] {"arg1", "arg2", "arg3"}, cmd.getArgs(), "Extra args");
104     }
105 
106     @Test
107     public void testMultipleArgValues() {
108         assertTrue(cmd.hasOption("e"), "Option e is not set");
109         assertArrayEquals(new String[] {"one", "two"}, cmd.getOptionValues("e"));
110     }
111 
112     @Test
113     public void testShortArgs() {
114         assertTrue(cmd.hasOption("a"), "Option a is not set");
115         assertTrue(cmd.hasOption("c"), "Option c is not set");
116 
117         assertNull(cmd.getOptionValues("a"));
118         assertNull(cmd.getOptionValues("c"));
119     }
120 
121     @Test
122     public void testShortArgsWithValue() {
123         assertTrue(cmd.hasOption("b"), "Option b is not set");
124         assertEquals("foo", cmd.getOptionValue("b"));
125         assertEquals(1, cmd.getOptionValues("b").length);
126 
127         assertTrue(cmd.hasOption("b"), "Option b is not set");
128         assertEquals("bar", cmd.getOptionValue("d"));
129         assertEquals(1, cmd.getOptionValues("d").length);
130     }
131 
132     @Test
133     public void testTwoArgValues() {
134         assertTrue(cmd.hasOption("g"), "Option g is not set");
135         assertArrayEquals(new String[] {"val1", "val2"}, cmd.getOptionValues("g"));
136     }
137 
138     /**
139      * jkeyes - commented out this test as the new architecture breaks this type of functionality. I have left the test here
140      * in case I get a brainwave on how to resolve this.
141      */
142     /*
143      * public void testGetValue() { // the 'm' option assertTrue(_option.getValues().length == 2); assertEquals(
144      * _option.getValue(), "key"); assertEquals(_option.getValue(0), "key"); assertEquals(_option.getValue(1),
145      * "value");
146      *
147      * try { assertEquals(_option.getValue(2), "key"); fail("IndexOutOfBounds not caught"); } catch (
148      * IndexOutOfBoundsException exp) {
149      *
150      * }
151      *
152      * try { assertEquals(_option.getValue(-1), "key"); fail("IndexOutOfBounds not caught"); } catch (
153      * IndexOutOfBoundsException exp) {
154      *
155      * } }
156      */
157 }