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: */package org.apache.commons.cli;
17:
18: import junit.framework.Test;
19: import junit.framework.TestCase;
20: import junit.framework.TestSuite;
21:
22: /**
23: * @author John Keyes (john at integralsource.com)
24: * @version $Revision: 542144 $
25: */
26: public class ParseRequiredTest extends TestCase {
27:
28: private Options _options = null;
29: private CommandLineParser parser = new PosixParser();
30:
31: public static Test suite() {
32: return new TestSuite(ParseRequiredTest.class);
33: }
34:
35: public ParseRequiredTest(String name) {
36: super (name);
37: }
38:
39: public void setUp() {
40: _options = new Options().addOption("a", "enable-a", false,
41: "turn [a] on or off").addOption(
42: OptionBuilder.withLongOpt("bfile").hasArg()
43: .isRequired().withDescription(
44: "set the value of [b]").create('b'));
45: }
46:
47: public void tearDown() {
48:
49: }
50:
51: public void testWithRequiredOption() {
52: String[] args = new String[] { "-b", "file" };
53:
54: try {
55: CommandLine cl = parser.parse(_options, args);
56:
57: assertTrue("Confirm -a is NOT set", !cl.hasOption("a"));
58: assertTrue("Confirm -b is set", cl.hasOption("b"));
59: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
60: .equals("file"));
61: assertTrue("Confirm NO of extra args", cl.getArgList()
62: .size() == 0);
63: } catch (ParseException e) {
64: fail(e.toString());
65: }
66: }
67:
68: public void testOptionAndRequiredOption() {
69: String[] args = new String[] { "-a", "-b", "file" };
70:
71: try {
72: CommandLine cl = parser.parse(_options, args);
73:
74: assertTrue("Confirm -a is set", cl.hasOption("a"));
75: assertTrue("Confirm -b is set", cl.hasOption("b"));
76: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
77: .equals("file"));
78: assertTrue("Confirm NO of extra args", cl.getArgList()
79: .size() == 0);
80: } catch (ParseException e) {
81: fail(e.toString());
82: }
83: }
84:
85: public void testMissingRequiredOption() {
86: String[] args = new String[] { "-a" };
87:
88: try {
89: CommandLine cl = parser.parse(_options, args);
90: fail("exception should have been thrown");
91: } catch (ParseException e) {
92: if (!(e instanceof MissingOptionException)) {
93: fail("expected to catch MissingOptionException");
94: }
95: }
96: }
97:
98: }
|