001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.commons.cli;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: /**
023: * @author John Keyes (john at integralsource.com)
024: * @version $Revision: 542144 $
025: */
026: public class OptionGroupTest extends TestCase {
027:
028: private Options _options = null;
029: private CommandLineParser parser = new PosixParser();
030:
031: public static Test suite() {
032: return new TestSuite(OptionGroupTest.class);
033: }
034:
035: public OptionGroupTest(String name) {
036: super (name);
037: }
038:
039: public void setUp() {
040: Option file = new Option("f", "file", false, "file to process");
041: Option dir = new Option("d", "directory", false,
042: "directory to process");
043: OptionGroup group = new OptionGroup();
044: group.addOption(file);
045: group.addOption(dir);
046: _options = new Options().addOptionGroup(group);
047:
048: Option section = new Option("s", "section", false,
049: "section to process");
050: Option chapter = new Option("c", "chapter", false,
051: "chapter to process");
052: OptionGroup group2 = new OptionGroup();
053: group2.addOption(section);
054: group2.addOption(chapter);
055:
056: _options.addOptionGroup(group2);
057:
058: Option importOpt = new Option(null, "import", false,
059: "section to process");
060: Option exportOpt = new Option(null, "export", false,
061: "chapter to process");
062: OptionGroup group3 = new OptionGroup();
063: group3.addOption(importOpt);
064: group3.addOption(exportOpt);
065: _options.addOptionGroup(group3);
066:
067: _options.addOption("r", "revision", false, "revision number");
068: }
069:
070: public void tearDown() {
071: }
072:
073: public void testSingleOptionFromGroup() {
074: String[] args = new String[] { "-f" };
075:
076: try {
077: CommandLine cl = parser.parse(_options, args);
078:
079: assertTrue("Confirm -r is NOT set", !cl.hasOption("r"));
080: assertTrue("Confirm -f is set", cl.hasOption("f"));
081: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
082: assertTrue("Confirm -s is NOT set", !cl.hasOption("s"));
083: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
084: assertTrue("Confirm no extra args",
085: cl.getArgList().size() == 0);
086: } catch (ParseException e) {
087: fail(e.toString());
088: }
089: }
090:
091: public void testSingleOption() {
092: String[] args = new String[] { "-r" };
093:
094: try {
095: CommandLine cl = parser.parse(_options, args);
096:
097: assertTrue("Confirm -r is set", cl.hasOption("r"));
098: assertTrue("Confirm -f is NOT set", !cl.hasOption("f"));
099: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
100: assertTrue("Confirm -s is NOT set", !cl.hasOption("s"));
101: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
102: assertTrue("Confirm no extra args",
103: cl.getArgList().size() == 0);
104: } catch (ParseException e) {
105: fail(e.toString());
106: }
107: }
108:
109: public void testTwoValidOptions() {
110: String[] args = new String[] { "-r", "-f" };
111:
112: try {
113: CommandLine cl = parser.parse(_options, args);
114:
115: assertTrue("Confirm -r is set", cl.hasOption("r"));
116: assertTrue("Confirm -f is set", cl.hasOption("f"));
117: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
118: assertTrue("Confirm -s is NOT set", !cl.hasOption("s"));
119: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
120: assertTrue("Confirm no extra args",
121: cl.getArgList().size() == 0);
122: } catch (ParseException e) {
123: fail(e.toString());
124: }
125: }
126:
127: public void testSingleLongOption() {
128: String[] args = new String[] { "--file" };
129:
130: try {
131: CommandLine cl = parser.parse(_options, args);
132:
133: assertTrue("Confirm -r is NOT set", !cl.hasOption("r"));
134: assertTrue("Confirm -f is set", cl.hasOption("f"));
135: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
136: assertTrue("Confirm -s is NOT set", !cl.hasOption("s"));
137: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
138: assertTrue("Confirm no extra args",
139: cl.getArgList().size() == 0);
140: } catch (ParseException e) {
141: fail(e.toString());
142: }
143: }
144:
145: public void testTwoValidLongOptions() {
146: String[] args = new String[] { "--revision", "--file" };
147:
148: try {
149: CommandLine cl = parser.parse(_options, args);
150:
151: assertTrue("Confirm -r is set", cl.hasOption("r"));
152: assertTrue("Confirm -f is set", cl.hasOption("f"));
153: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
154: assertTrue("Confirm -s is NOT set", !cl.hasOption("s"));
155: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
156: assertTrue("Confirm no extra args",
157: cl.getArgList().size() == 0);
158: } catch (ParseException e) {
159: fail(e.toString());
160: }
161: }
162:
163: public void testNoOptionsExtraArgs() {
164: String[] args = new String[] { "arg1", "arg2" };
165:
166: try {
167: CommandLine cl = parser.parse(_options, args);
168:
169: assertTrue("Confirm -r is NOT set", !cl.hasOption("r"));
170: assertTrue("Confirm -f is NOT set", !cl.hasOption("f"));
171: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
172: assertTrue("Confirm -s is NOT set", !cl.hasOption("s"));
173: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
174: assertTrue("Confirm TWO extra args",
175: cl.getArgList().size() == 2);
176: } catch (ParseException e) {
177: fail(e.toString());
178: }
179: }
180:
181: public void testTwoOptionsFromGroup() {
182: String[] args = new String[] { "-f", "-d" };
183:
184: try {
185: CommandLine cl = parser.parse(_options, args);
186: fail("two arguments from group not allowed");
187: } catch (ParseException e) {
188: if (!(e instanceof AlreadySelectedException)) {
189: fail("incorrect exception caught:" + e.getMessage());
190: }
191: }
192: }
193:
194: public void testTwoLongOptionsFromGroup() {
195: String[] args = new String[] { "--file", "--directory" };
196:
197: try {
198: CommandLine cl = parser.parse(_options, args);
199: fail("two arguments from group not allowed");
200: } catch (ParseException e) {
201: if (!(e instanceof AlreadySelectedException)) {
202: fail("incorrect exception caught:" + e.getMessage());
203: }
204: }
205: }
206:
207: public void testTwoOptionsFromDifferentGroup() {
208: String[] args = new String[] { "-f", "-s" };
209:
210: try {
211: CommandLine cl = parser.parse(_options, args);
212: assertTrue("Confirm -r is NOT set", !cl.hasOption("r"));
213: assertTrue("Confirm -f is set", cl.hasOption("f"));
214: assertTrue("Confirm -d is NOT set", !cl.hasOption("d"));
215: assertTrue("Confirm -s is set", cl.hasOption("s"));
216: assertTrue("Confirm -c is NOT set", !cl.hasOption("c"));
217: assertTrue("Confirm NO extra args",
218: cl.getArgList().size() == 0);
219: } catch (ParseException e) {
220: fail(e.toString());
221: }
222: }
223:
224: public void testValidLongOnlyOptions() {
225: try {
226: CommandLine cl = parser.parse(_options,
227: new String[] { "--export" });
228: assertTrue("Confirm --export is set", cl
229: .hasOption("export"));
230: } catch (ParseException e) {
231: fail(e.toString());
232: }
233:
234: try {
235: CommandLine cl = parser.parse(_options,
236: new String[] { "--import" });
237: assertTrue("Confirm --import is set", cl
238: .hasOption("import"));
239: } catch (ParseException e) {
240: fail(e.toString());
241: }
242: }
243:
244: }
|