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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  
25  import java.util.Properties;
26  import java.util.concurrent.atomic.AtomicReference;
27  import java.util.function.Supplier;
28  
29  import org.junit.jupiter.api.Test;
30  
31  @SuppressWarnings("deprecation") // tests some deprecated classes
32  public class CommandLineTest {
33  
34      @Test
35      public void testBuilder() {
36          final CommandLine.Builder builder = new CommandLine.Builder();
37          builder.addArg("foo").addArg("bar");
38          builder.addOption(Option.builder("T").build());
39          final CommandLine cmd = builder.build();
40  
41          assertEquals("foo", cmd.getArgs()[0]);
42          assertEquals("bar", cmd.getArgList().get(1));
43          assertEquals("T", cmd.getOptions()[0].getOpt());
44      }
45  
46      @Test
47      public void testBuilderNullArgs() {
48          final CommandLine.Builder builder = new CommandLine.Builder();
49          builder.addArg(null).addArg(null);
50          builder.addOption(Option.builder("T").build());
51          final CommandLine cmd = builder.build();
52  
53          assertEquals(0, cmd.getArgs().length);
54          assertEquals("T", cmd.getOptions()[0].getOpt());
55      }
56  
57      @Test
58      public void testBuilderNullOption() {
59          final CommandLine.Builder builder = new CommandLine.Builder();
60          builder.addArg("foo").addArg("bar");
61          builder.addOption(null);
62          builder.addOption(null);
63          builder.addOption(null);
64          final CommandLine cmd = builder.build();
65  
66          assertEquals("foo", cmd.getArgs()[0]);
67          assertEquals("bar", cmd.getArgList().get(1));
68          assertEquals(0, cmd.getOptions().length);
69      }
70  
71      @Test
72      public void testDeprecatedDefaultOption() {
73          final CommandLine.Builder builder = new CommandLine.Builder();
74          builder.addArg("foo").addArg("bar");
75          final Option opt = Option.builder().option("T").deprecated().build();
76          builder.addOption(opt);
77          final AtomicReference<Option> handler = new AtomicReference<>();
78          final CommandLine cmd = builder.build();
79          cmd.getOptionValue(opt.getOpt());
80          handler.set(null);
81          cmd.getOptionValue("Nope");
82          assertNull(handler.get());
83      }
84  
85      @Test
86      public void testDeprecatedOption() {
87          final CommandLine.Builder builder = new CommandLine.Builder();
88          builder.addArg("foo").addArg("bar");
89          final Option opt = Option.builder().option("T").deprecated().build();
90          builder.addOption(opt);
91          final AtomicReference<Option> handler = new AtomicReference<>();
92          final CommandLine cmd = builder.setDeprecatedHandler(handler::set).build();
93          cmd.getOptionValue(opt.getOpt());
94          assertSame(opt, handler.get());
95          handler.set(null);
96          cmd.getOptionValue("Nope");
97          assertNull(handler.get());
98      }
99  
100     @Test
101     public void testGetOptionProperties() throws Exception {
102         final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"};
103 
104         final Options options = new Options();
105         options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D'));
106         options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create());
107 
108         final Parser parser = new GnuParser();
109         final CommandLine cl = parser.parse(options, args);
110 
111         final Properties props = cl.getOptionProperties("D");
112         assertNotNull(props, "null properties");
113         assertEquals(4, props.size(), "number of properties in " + props);
114         assertEquals("value1", props.getProperty("param1"), "property 1");
115         assertEquals("value2", props.getProperty("param2"), "property 2");
116         assertEquals("true", props.getProperty("param3"), "property 3");
117         assertEquals("value4", props.getProperty("param4"), "property 4");
118 
119         assertEquals("bar", cl.getOptionProperties("property").getProperty("foo"), "property with long format");
120     }
121 
122     @Test
123     public void testGetOptionPropertiesWithOption() throws Exception {
124         final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"};
125 
126         final Options options = new Options();
127         final Option optionD = OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D');
128         final Option optionProperty = OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create();
129         options.addOption(optionD);
130         options.addOption(optionProperty);
131 
132         final Parser parser = new GnuParser();
133         final CommandLine cl = parser.parse(options, args);
134 
135         final Properties props = cl.getOptionProperties(optionD);
136         assertNotNull(props, "null properties");
137         assertEquals(4, props.size(), "number of properties in " + props);
138         assertEquals("value1", props.getProperty("param1"), "property 1");
139         assertEquals("value2", props.getProperty("param2"), "property 2");
140         assertEquals("true", props.getProperty("param3"), "property 3");
141         assertEquals("value4", props.getProperty("param4"), "property 4");
142 
143         assertEquals("bar", cl.getOptionProperties(optionProperty).getProperty("foo"), "property with long format");
144     }
145 
146     @Test
147     public void testGetOptionsBuilder() {
148         final CommandLine cmd = CommandLine.builder().build();
149         assertNotNull(cmd.getOptions());
150         assertEquals(0, cmd.getOptions().length);
151 
152         cmd.addOption(null);
153         cmd.addOption(new Option("a", null));
154         cmd.addOption(new Option("b", null));
155         cmd.addOption(new Option("c", null));
156 
157         assertEquals(3, cmd.getOptions().length);
158     }
159 
160     @Test
161     public void testGetOptionsCtor() {
162         final CommandLine cmd = new CommandLine();
163         assertNotNull(cmd.getOptions());
164         assertEquals(0, cmd.getOptions().length);
165 
166         cmd.addOption(new Option("a", null));
167         cmd.addOption(new Option("b", null));
168         cmd.addOption(new Option("c", null));
169         cmd.addOption(null);
170 
171         assertEquals(3, cmd.getOptions().length);
172     }
173 
174     @Test
175     public void testGetParsedOptionValue() throws Exception {
176         final Options options = new Options();
177         options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i"));
178         options.addOption(OptionBuilder.hasArg().create("f"));
179 
180         final CommandLineParser parser = new DefaultParser();
181         final CommandLine cmd = parser.parse(options, new String[] {"-i", "123", "-f", "foo"});
182 
183         assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue());
184         assertEquals("foo", cmd.getParsedOptionValue("f"));
185     }
186 
187     @Test
188     public void testGetParsedOptionValueUsingDefault() throws Exception {
189         final Options options = new Options();
190         final Option optI = Option.builder("i").hasArg().type(Number.class).build();
191         final Option optF = Option.builder("f").hasArg().build();
192         options.addOption(optI);
193         options.addOption(optF);
194 
195         final CommandLineParser parser = new DefaultParser();
196         final CommandLine cmd = parser.parse(options, new String[] {"-i", "123"});
197         final Supplier<String> nullSupplier = null;
198 
199         assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue());
200         assertEquals("foo", cmd.getParsedOptionValue(optF, "foo"));
201         assertEquals("foo", cmd.getParsedOptionValue(optF, () -> "foo"));
202         assertNull(cmd.getParsedOptionValue(optF, null));
203         assertNull(cmd.getParsedOptionValue(optF, nullSupplier));
204         assertNull(cmd.getParsedOptionValue(optF, () -> null));
205 
206         assertEquals("foo", cmd.getParsedOptionValue("f", "foo"));
207         assertEquals("foo", cmd.getParsedOptionValue("f", () -> "foo"));
208         assertNull(cmd.getParsedOptionValue("f", null));
209         assertNull(cmd.getParsedOptionValue("f", nullSupplier));
210         assertNull(cmd.getParsedOptionValue("f", () -> null));
211 
212         assertEquals("foo", cmd.getParsedOptionValue('f', "foo"));
213         assertEquals("foo", cmd.getParsedOptionValue('f', () -> "foo"));
214         assertNull(cmd.getParsedOptionValue('f', null));
215         assertNull(cmd.getParsedOptionValue('f', nullSupplier));
216         assertNull(cmd.getParsedOptionValue('f', () -> null));
217 
218     }
219 
220     @Test
221     public void testGetParsedOptionValueWithChar() throws Exception {
222         final Options options = new Options();
223         options.addOption(Option.builder("i").hasArg().type(Number.class).build());
224         options.addOption(Option.builder("f").hasArg().build());
225 
226         final CommandLineParser parser = new DefaultParser();
227         final CommandLine cmd = parser.parse(options, new String[] {"-i", "123", "-f", "foo"});
228 
229         assertEquals(123, ((Number) cmd.getParsedOptionValue('i')).intValue());
230         assertEquals("foo", cmd.getParsedOptionValue('f'));
231     }
232 
233     @Test
234     public void testGetParsedOptionValueWithOption() throws Exception {
235         final Options options = new Options();
236         final Option optI = Option.builder("i").hasArg().type(Number.class).build();
237         final Option optF = Option.builder("f").hasArg().build();
238         options.addOption(optI);
239         options.addOption(optF);
240 
241         final CommandLineParser parser = new DefaultParser();
242         final CommandLine cmd = parser.parse(options, new String[] {"-i", "123", "-f", "foo"});
243 
244         assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue());
245         assertEquals("foo", cmd.getParsedOptionValue(optF));
246     }
247 
248     @Test
249     public void testNullOption() throws Exception {
250         final Options options = new Options();
251         final Option optI = Option.builder("i").hasArg().type(Number.class).build();
252         final Option optF = Option.builder("f").hasArg().build();
253         options.addOption(optI);
254         options.addOption(optF);
255         final CommandLineParser parser = new DefaultParser();
256         final CommandLine cmd = parser.parse(options, new String[] {"-i", "123", "-f", "foo"});
257         assertNull(cmd.getOptionValue((Option) null));
258         assertNull(cmd.getParsedOptionValue((Option) null));
259     }
260 }