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.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.util.Properties;
27  
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  @SuppressWarnings("deprecation") // tests some deprecated classes
32  public class OptionGroupTest {
33      private Options options;
34      private final Parser parser = new PosixParser();
35  
36      @BeforeEach
37      public void setUp() {
38          final Option file = new Option("f", "file", false, "file to process");
39          final Option dir = new Option("d", "directory", false, "directory to process");
40          final OptionGroup group = new OptionGroup();
41          group.addOption(file);
42          group.addOption(dir);
43          options = new Options().addOptionGroup(group);
44  
45          final Option section = new Option("s", "section", false, "section to process");
46          final Option chapter = new Option("c", "chapter", false, "chapter to process");
47          final OptionGroup group2 = new OptionGroup();
48          group2.addOption(section);
49          group2.addOption(chapter);
50  
51          options.addOptionGroup(group2);
52  
53          final Option importOpt = new Option(null, "import", false, "section to process");
54          final Option exportOpt = new Option(null, "export", false, "chapter to process");
55          final OptionGroup group3 = new OptionGroup();
56          group3.addOption(importOpt);
57          group3.addOption(exportOpt);
58          options.addOptionGroup(group3);
59  
60          options.addOption("r", "revision", false, "revision number");
61      }
62  
63      @Test
64      public void testGetNames() {
65          final OptionGroup group = new OptionGroup();
66          group.addOption(OptionBuilder.create('a'));
67          group.addOption(OptionBuilder.create('b'));
68  
69          assertNotNull(group.getNames(), "null names");
70          assertEquals(2, group.getNames().size());
71          assertTrue(group.getNames().contains("a"));
72          assertTrue(group.getNames().contains("b"));
73      }
74  
75      @Test
76      public void testNoOptionsExtraArgs() throws Exception {
77          final String[] args = {"arg1", "arg2"};
78  
79          final CommandLine cl = parser.parse(options, args);
80  
81          assertFalse(cl.hasOption("r"), "Confirm -r is NOT set");
82          assertFalse(cl.hasOption("f"), "Confirm -f is NOT set");
83          assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
84          assertFalse(cl.hasOption("s"), "Confirm -s is NOT set");
85          assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
86          assertEquals(2, cl.getArgList().size(), "Confirm TWO extra args");
87      }
88  
89      @Test
90      public void testSingleLongOption() throws Exception {
91          final String[] args = {"--file"};
92  
93          final CommandLine cl = parser.parse(options, args);
94  
95          assertFalse(cl.hasOption("r"), "Confirm -r is NOT set");
96          assertTrue(cl.hasOption("f"), "Confirm -f is set");
97          assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
98          assertFalse(cl.hasOption("s"), "Confirm -s is NOT set");
99          assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
100         assertTrue(cl.getArgList().isEmpty(), "Confirm no extra args");
101     }
102 
103     @Test
104     public void testSingleOption() throws Exception {
105         final String[] args = {"-r"};
106 
107         final CommandLine cl = parser.parse(options, args);
108 
109         assertTrue(cl.hasOption("r"), "Confirm -r is set");
110         assertFalse(cl.hasOption("f"), "Confirm -f is NOT set");
111         assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
112         assertFalse(cl.hasOption("s"), "Confirm -s is NOT set");
113         assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
114         assertTrue(cl.getArgList().isEmpty(), "Confirm no extra args");
115     }
116 
117     @Test
118     public void testSingleOptionFromGroup() throws Exception {
119         final String[] args = {"-f"};
120 
121         final CommandLine cl = parser.parse(options, args);
122 
123         assertFalse(cl.hasOption("r"), "Confirm -r is NOT set");
124         assertTrue(cl.hasOption("f"), "Confirm -f is set");
125         assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
126         assertFalse(cl.hasOption("s"), "Confirm -s is NOT set");
127         assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
128         assertTrue(cl.getArgList().isEmpty(), "Confirm no extra args");
129     }
130 
131     @Test
132     public void testToString() {
133         final OptionGroup group1 = new OptionGroup();
134         group1.addOption(new Option(null, "foo", false, "Foo"));
135         group1.addOption(new Option(null, "bar", false, "Bar"));
136 
137         if (!"[--bar Bar, --foo Foo]".equals(group1.toString())) {
138             assertEquals("[--foo Foo, --bar Bar]", group1.toString());
139         }
140 
141         final OptionGroup group2 = new OptionGroup();
142         group2.addOption(new Option("f", "foo", false, "Foo"));
143         group2.addOption(new Option("b", "bar", false, "Bar"));
144 
145         if (!"[-b Bar, -f Foo]".equals(group2.toString())) {
146             assertEquals("[-f Foo, -b Bar]", group2.toString());
147         }
148     }
149 
150     @Test
151     public void testTwoLongOptionsFromGroup() throws Exception {
152         final String[] args = {"--file", "--directory"};
153 
154         try {
155             parser.parse(options, args);
156             fail("two arguments from group not allowed");
157         } catch (final AlreadySelectedException e) {
158             assertNotNull(e.getOptionGroup(), "null option group");
159             assertEquals("f", e.getOptionGroup().getSelected(), "selected option");
160             assertEquals("d", e.getOption().getOpt(), "option");
161         }
162     }
163 
164     @Test
165     public void testTwoOptionsFromDifferentGroup() throws Exception {
166         final String[] args = {"-f", "-s"};
167 
168         final CommandLine cl = parser.parse(options, args);
169         assertFalse(cl.hasOption("r"), "Confirm -r is NOT set");
170         assertTrue(cl.hasOption("f"), "Confirm -f is set");
171         assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
172         assertTrue(cl.hasOption("s"), "Confirm -s is set");
173         assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
174         assertTrue(cl.getArgList().isEmpty(), "Confirm NO extra args");
175     }
176 
177     @Test
178     public void testTwoOptionsFromGroup() throws Exception {
179         final String[] args = {"-f", "-d"};
180 
181         try {
182             parser.parse(options, args);
183             fail("two arguments from group not allowed");
184         } catch (final AlreadySelectedException e) {
185             assertNotNull(e.getOptionGroup(), "null option group");
186             assertEquals("f", e.getOptionGroup().getSelected(), "selected option");
187             assertEquals("d", e.getOption().getOpt(), "option");
188         }
189     }
190 
191     @Test
192     public void testTwoOptionsFromGroupWithProperties() throws Exception {
193         final String[] args = {"-f"};
194 
195         final Properties properties = new Properties();
196         properties.put("d", "true");
197 
198         final CommandLine cl = parser.parse(options, args, properties);
199         assertTrue(cl.hasOption("f"));
200         assertFalse(cl.hasOption("d"));
201     }
202 
203     @Test
204     public void testTwoValidLongOptions() throws Exception {
205         final String[] args = {"--revision", "--file"};
206 
207         final CommandLine cl = parser.parse(options, args);
208 
209         assertTrue(cl.hasOption("r"), "Confirm -r is set");
210         assertTrue(cl.hasOption("f"), "Confirm -f is set");
211         assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
212         assertFalse(cl.hasOption("s"), "Confirm -s is NOT set");
213         assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
214         assertTrue(cl.getArgList().isEmpty(), "Confirm no extra args");
215     }
216 
217     @Test
218     public void testTwoValidOptions() throws Exception {
219         final String[] args = {"-r", "-f"};
220 
221         final CommandLine cl = parser.parse(options, args);
222 
223         assertTrue(cl.hasOption("r"), "Confirm -r is set");
224         assertTrue(cl.hasOption("f"), "Confirm -f is set");
225         assertFalse(cl.hasOption("d"), "Confirm -d is NOT set");
226         assertFalse(cl.hasOption("s"), "Confirm -s is NOT set");
227         assertFalse(cl.hasOption("c"), "Confirm -c is NOT set");
228         assertTrue(cl.getArgList().isEmpty(), "Confirm no extra args");
229     }
230 
231     @Test
232     public void testValidLongOnlyOptions() throws Exception {
233         final CommandLine cl1 = parser.parse(options, new String[] {"--export"});
234         assertTrue(cl1.hasOption("export"), "Confirm --export is set");
235 
236         final CommandLine cl2 = parser.parse(options, new String[] {"--import"});
237         assertTrue(cl2.hasOption("import"), "Confirm --import is set");
238     }
239 }