Debian Codegraph @

src/test/java/org/apache/commons/cli/bug/BugCLI71Test.java

raw
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.bug;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.CommandLineParser;
25 import org.apache.commons.cli.MissingArgumentException;
26 import org.apache.commons.cli.Option;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.PosixParser;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 @SuppressWarnings("deprecation") // tests some deprecated classes
33 public class BugCLI71Test {
34 private Options options;
35 private CommandLineParser parser;
36
37 @Before
38 public void setUp() {
39 options = new Options();
40
41 final Option algorithm = new Option("a", "algo", true, "the algorithm which it to perform executing");
42 algorithm.setArgName("algorithm name");
43 options.addOption(algorithm);
44
45 final Option key = new Option("k", "key", true, "the key the setted algorithm uses to process");
46 algorithm.setArgName("value");
47 options.addOption(key);
48
49 parser = new PosixParser();
50 }
51
52 @Test
53 public void testBasic() throws Exception {
54 final String[] args = {"-a", "Caesar", "-k", "A"};
55 final CommandLine line = parser.parse(options, args);
56 assertEquals("Caesar", line.getOptionValue("a"));
57 assertEquals("A", line.getOptionValue("k"));
58 }
59
60 @Test
61 public void testGetsDefaultIfOptional() throws Exception {
62 final String[] args = {"-k", "-a", "Caesar"};
63 options.getOption("k").setOptionalArg(true);
64 final CommandLine line = parser.parse(options, args);
65
66 assertEquals("Caesar", line.getOptionValue("a"));
67 assertEquals("a", line.getOptionValue('k', "a"));
68 }
69
70 @Test
71 public void testLackOfError() throws Exception {
72 final String[] args = {"-k", "-a", "Caesar"};
73 try {
74 parser.parse(options, args);
75 fail("MissingArgumentException expected");
76 } catch (final MissingArgumentException e) {
77 assertEquals("option missing an argument", "k", e.getOption().getOpt());
78 }
79 }
80
81 @Test
82 public void testMistakenArgument() throws Exception {
83 String[] args = {"-a", "Caesar", "-k", "A"};
84 CommandLine line = parser.parse(options, args);
85 args = new String[] {"-a", "Caesar", "-k", "a"};
86 line = parser.parse(options, args);
87 assertEquals("Caesar", line.getOptionValue("a"));
88 assertEquals("a", line.getOptionValue("k"));
89 }
90
91 }