01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.commons.cli.bug;
18:
19: import junit.framework.TestCase;
20:
21: import org.apache.commons.cli.*;
22:
23: public class BugCLI71Test extends TestCase {
24:
25: private Options options;
26: private CommandLineParser parser;
27:
28: public void setUp() {
29: options = new Options();
30:
31: Option algorithm = new Option("a", "algo", true,
32: "the algorithm which it to perform executing");
33: algorithm.setArgName("algorithm name");
34: options.addOption(algorithm);
35:
36: Option key = new Option("k", "key", true,
37: "the key the setted algorithm uses to process");
38: algorithm.setArgName("value");
39: options.addOption(key);
40:
41: parser = new PosixParser();
42: }
43:
44: public void testBasic() throws Exception {
45: String[] args = new String[] { "-a", "Caesar", "-k", "A" };
46: CommandLine line = parser.parse(options, args);
47: assertEquals("Caesar", line.getOptionValue("a"));
48: assertEquals("A", line.getOptionValue("k"));
49: }
50:
51: public void testMistakenArgument() throws Exception {
52: String[] args = new String[] { "-a", "Caesar", "-k", "A" };
53: CommandLine line = parser.parse(options, args);
54: args = new String[] { "-a", "Caesar", "-k", "a" };
55: line = parser.parse(options, args);
56: assertEquals("Caesar", line.getOptionValue("a"));
57: assertEquals("a", line.getOptionValue("k"));
58: }
59:
60: public void testLackOfError() throws Exception {
61: String[] args = new String[] { "-k", "-a", "Caesar" };
62: try {
63: CommandLine line = parser.parse(options, args);
64: fail("MissingArgumentException expected");
65: } catch (MissingArgumentException mae) {
66: // expected
67: }
68: }
69:
70: public void testGetsDefaultIfOptional() throws Exception {
71: String[] args = new String[] { "-k", "-a", "Caesar" };
72: options.getOption("k").setOptionalArg(true);
73: CommandLine line = parser.parse(options, args);
74:
75: assertEquals("Caesar", line.getOptionValue("a"));
76: assertEquals("a", line.getOptionValue("k", "a"));
77: }
78:
79: }
|