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: public class ParseTest extends TestCase {
023:
024: private Options _options = null;
025: private Parser _parser = null;
026:
027: public static Test suite() {
028: return new TestSuite(ParseTest.class);
029: }
030:
031: public ParseTest(String name) {
032: super (name);
033: }
034:
035: public void setUp() {
036: _options = new Options().addOption("a", "enable-a", false,
037: "turn [a] on or off").addOption("b", "bfile", true,
038: "set the value of [b]").addOption("c", "copt", false,
039: "turn [c] on or off");
040:
041: _parser = new PosixParser();
042: }
043:
044: public void tearDown() {
045:
046: }
047:
048: public void testSimpleShort() {
049: String[] args = new String[] { "-a", "-b", "toast", "foo",
050: "bar" };
051:
052: try {
053: CommandLine cl = _parser.parse(_options, args);
054:
055: assertTrue("Confirm -a is set", cl.hasOption("a"));
056: assertTrue("Confirm -b is set", cl.hasOption("b"));
057: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
058: .equals("toast"));
059: assertTrue("Confirm size of extra args", cl.getArgList()
060: .size() == 2);
061: } catch (ParseException e) {
062: fail(e.toString());
063: }
064: }
065:
066: public void testSimpleLong() {
067: String[] args = new String[] { "--enable-a", "--bfile",
068: "toast", "foo", "bar" };
069:
070: try {
071: CommandLine cl = _parser.parse(_options, args);
072:
073: assertTrue("Confirm -a is set", cl.hasOption("a"));
074: assertTrue("Confirm -b is set", cl.hasOption("b"));
075: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
076: .equals("toast"));
077: assertTrue("Confirm arg of --bfile", cl.getOptionValue(
078: "bfile").equals("toast"));
079: assertTrue("Confirm size of extra args", cl.getArgList()
080: .size() == 2);
081: } catch (ParseException e) {
082: fail(e.toString());
083: }
084: }
085:
086: public void testComplexShort() {
087: String[] args = new String[] { "-acbtoast", "foo", "bar" };
088:
089: try {
090: CommandLine cl = _parser.parse(_options, args);
091:
092: assertTrue("Confirm -a is set", cl.hasOption("a"));
093: assertTrue("Confirm -b is set", cl.hasOption("b"));
094: assertTrue("Confirm -c is set", cl.hasOption("c"));
095: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
096: .equals("toast"));
097: assertTrue("Confirm size of extra args", cl.getArgList()
098: .size() == 2);
099: } catch (ParseException e) {
100: fail(e.toString());
101: }
102: }
103:
104: public void testExtraOption() {
105: String[] args = new String[] { "-adbtoast", "foo", "bar" };
106:
107: boolean caught = false;
108:
109: try {
110: CommandLine cl = _parser.parse(_options, args);
111:
112: assertTrue("Confirm -a is set", cl.hasOption("a"));
113: assertTrue("Confirm -b is set", cl.hasOption("b"));
114: assertTrue("confirm arg of -b", cl.getOptionValue("b")
115: .equals("toast"));
116: assertTrue("Confirm size of extra args", cl.getArgList()
117: .size() == 3);
118: } catch (UnrecognizedOptionException e) {
119: caught = true;
120: } catch (ParseException e) {
121: fail(e.toString());
122: }
123: assertTrue("Confirm UnrecognizedOptionException caught", caught);
124: }
125:
126: public void testMissingArg() {
127:
128: String[] args = new String[] { "-acb" };
129:
130: boolean caught = false;
131:
132: try {
133: CommandLine cl = _parser.parse(_options, args);
134: } catch (MissingArgumentException e) {
135: caught = true;
136: } catch (ParseException e) {
137: fail(e.toString());
138: }
139:
140: assertTrue("Confirm MissingArgumentException caught", caught);
141: }
142:
143: public void testStop() {
144: String[] args = new String[] { "-c", "foober", "-btoast" };
145:
146: try {
147: CommandLine cl = _parser.parse(_options, args, true);
148: assertTrue("Confirm -c is set", cl.hasOption("c"));
149: assertTrue("Confirm 2 extra args: "
150: + cl.getArgList().size(),
151: cl.getArgList().size() == 2);
152: } catch (ParseException e) {
153: fail(e.toString());
154: }
155: }
156:
157: public void testMultiple() {
158: String[] args = new String[] { "-c", "foobar", "-btoast" };
159:
160: try {
161: CommandLine cl = _parser.parse(_options, args, true);
162: assertTrue("Confirm -c is set", cl.hasOption("c"));
163: assertTrue("Confirm 2 extra args: "
164: + cl.getArgList().size(),
165: cl.getArgList().size() == 2);
166:
167: cl = _parser.parse(_options, cl.getArgs());
168:
169: assertTrue("Confirm -c is not set", !cl.hasOption("c"));
170: assertTrue("Confirm -b is set", cl.hasOption("b"));
171: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
172: .equals("toast"));
173: assertTrue("Confirm 1 extra arg: "
174: + cl.getArgList().size(),
175: cl.getArgList().size() == 1);
176: assertTrue("Confirm value of extra arg: "
177: + cl.getArgList().get(0), cl.getArgList().get(0)
178: .equals("foobar"));
179: } catch (ParseException e) {
180: fail(e.toString());
181: }
182: }
183:
184: public void testMultipleWithLong() {
185: String[] args = new String[] { "--copt", "foobar", "--bfile",
186: "toast" };
187:
188: try {
189: CommandLine cl = _parser.parse(_options, args, true);
190: assertTrue("Confirm -c is set", cl.hasOption("c"));
191: assertTrue("Confirm 3 extra args: "
192: + cl.getArgList().size(),
193: cl.getArgList().size() == 3);
194:
195: cl = _parser.parse(_options, cl.getArgs());
196:
197: assertTrue("Confirm -c is not set", !cl.hasOption("c"));
198: assertTrue("Confirm -b is set", cl.hasOption("b"));
199: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
200: .equals("toast"));
201: assertTrue("Confirm 1 extra arg: "
202: + cl.getArgList().size(),
203: cl.getArgList().size() == 1);
204: assertTrue("Confirm value of extra arg: "
205: + cl.getArgList().get(0), cl.getArgList().get(0)
206: .equals("foobar"));
207: } catch (ParseException e) {
208: fail(e.toString());
209: }
210: }
211:
212: public void testDoubleDash() {
213: String[] args = new String[] { "--copt", "--", "-b", "toast" };
214:
215: try {
216: CommandLine cl = _parser.parse(_options, args);
217:
218: assertTrue("Confirm -c is set", cl.hasOption("c"));
219: assertTrue("Confirm -b is not set", !cl.hasOption("b"));
220: assertTrue("Confirm 2 extra args: "
221: + cl.getArgList().size(),
222: cl.getArgList().size() == 2);
223:
224: } catch (ParseException e) {
225: fail(e.toString());
226: }
227: }
228:
229: public void testSingleDash() {
230: String[] args = new String[] { "--copt", "-b", "-", "-a", "-" };
231:
232: try {
233: CommandLine cl = _parser.parse(_options, args);
234:
235: assertTrue("Confirm -a is set", cl.hasOption("a"));
236: assertTrue("Confirm -b is set", cl.hasOption("b"));
237: assertTrue("Confirm arg of -b", cl.getOptionValue("b")
238: .equals("-"));
239: assertTrue(
240: "Confirm 1 extra arg: " + cl.getArgList().size(),
241: cl.getArgList().size() == 1);
242: assertTrue("Confirm value of extra arg: "
243: + cl.getArgList().get(0), cl.getArgList().get(0)
244: .equals("-"));
245: } catch (ParseException e) {
246: fail(e.toString());
247: }
248:
249: }
250: }
|