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: import java.util.Arrays;
023: import java.util.Properties;
024:
025: public class ValueTest extends TestCase {
026:
027: public static Test suite() {
028: return new TestSuite(ValueTest.class);
029: }
030:
031: private CommandLine _cl = null;
032: private CommandLine _clOptional = null;
033: private Options opts = new Options();
034:
035: public ValueTest(String name) {
036: super (name);
037: }
038:
039: public void setUp() {
040: opts.addOption("a", false, "toggle -a");
041:
042: opts.addOption("b", true, "set -b");
043:
044: opts.addOption("c", "c", false, "toggle -c");
045:
046: opts.addOption("d", "d", true, "set -d");
047:
048: opts.addOption(OptionBuilder.hasOptionalArg().create('e'));
049:
050: opts.addOption(OptionBuilder.hasOptionalArg().withLongOpt(
051: "fish").create());
052:
053: opts.addOption(OptionBuilder.hasOptionalArgs().withLongOpt(
054: "gravy").create());
055:
056: opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt(
057: "hide").create());
058:
059: opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
060:
061: opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
062:
063: opts.addOption(OptionBuilder.hasArgs().withValueSeparator(',')
064: .create('k'));
065:
066: String[] args = new String[] { "-a", "-b", "foo", "--c", "--d",
067: "bar" };
068:
069: try {
070: Parser parser = new PosixParser();
071: _cl = parser.parse(opts, args);
072: } catch (ParseException e) {
073: fail("Cannot setUp() CommandLine: " + e.toString());
074: }
075: }
076:
077: public void tearDown() {
078:
079: }
080:
081: public void testShortNoArg() {
082: assertTrue(_cl.hasOption("a"));
083: assertNull(_cl.getOptionValue("a"));
084: }
085:
086: public void testShortWithArg() {
087: assertTrue(_cl.hasOption("b"));
088: assertNotNull(_cl.getOptionValue("b"));
089: assertEquals(_cl.getOptionValue("b"), "foo");
090: }
091:
092: public void testLongNoArg() {
093: assertTrue(_cl.hasOption("c"));
094: assertNull(_cl.getOptionValue("c"));
095: }
096:
097: public void testLongWithArg() {
098: assertTrue(_cl.hasOption("d"));
099: assertNotNull(_cl.getOptionValue("d"));
100: assertEquals(_cl.getOptionValue("d"), "bar");
101: }
102:
103: public void testShortOptionalArgNoValue() {
104: String[] args = new String[] { "-e" };
105: try {
106: Parser parser = new PosixParser();
107: CommandLine cmd = parser.parse(opts, args);
108: assertTrue(cmd.hasOption("e"));
109: assertNull(cmd.getOptionValue("e"));
110: } catch (ParseException e) {
111: fail("Cannot setUp() CommandLine: " + e.toString());
112: }
113: }
114:
115: public void testShortOptionalArgValue() {
116: String[] args = new String[] { "-e", "everything" };
117: try {
118: Parser parser = new PosixParser();
119: CommandLine cmd = parser.parse(opts, args);
120: assertTrue(cmd.hasOption("e"));
121: assertEquals("everything", cmd.getOptionValue("e"));
122: } catch (ParseException e) {
123: fail("Cannot setUp() CommandLine: " + e.toString());
124: }
125: }
126:
127: public void testLongOptionalNoValue() {
128: String[] args = new String[] { "--fish" };
129: try {
130: Parser parser = new PosixParser();
131: CommandLine cmd = parser.parse(opts, args);
132: assertTrue(cmd.hasOption("fish"));
133: assertNull(cmd.getOptionValue("fish"));
134: } catch (ParseException e) {
135: fail("Cannot setUp() CommandLine: " + e.toString());
136: }
137: }
138:
139: public void testLongOptionalArgValue() {
140: String[] args = new String[] { "--fish", "face" };
141: try {
142: Parser parser = new PosixParser();
143: CommandLine cmd = parser.parse(opts, args);
144: assertTrue(cmd.hasOption("fish"));
145: assertEquals("face", cmd.getOptionValue("fish"));
146: } catch (ParseException e) {
147: fail("Cannot setUp() CommandLine: " + e.toString());
148: }
149: }
150:
151: public void testShortOptionalArgValues() {
152: String[] args = new String[] { "-j", "ink", "idea" };
153: try {
154: Parser parser = new PosixParser();
155: CommandLine cmd = parser.parse(opts, args);
156: assertTrue(cmd.hasOption("j"));
157: assertEquals("ink", cmd.getOptionValue("j"));
158: assertEquals("ink", cmd.getOptionValues("j")[0]);
159: assertEquals("idea", cmd.getOptionValues("j")[1]);
160: assertEquals(cmd.getArgs().length, 0);
161: } catch (ParseException e) {
162: fail("Cannot setUp() CommandLine: " + e.toString());
163: }
164: }
165:
166: public void testLongOptionalArgValues() {
167: String[] args = new String[] { "--gravy", "gold", "garden" };
168: try {
169: Parser parser = new PosixParser();
170: CommandLine cmd = parser.parse(opts, args);
171: assertTrue(cmd.hasOption("gravy"));
172: assertEquals("gold", cmd.getOptionValue("gravy"));
173: assertEquals("gold", cmd.getOptionValues("gravy")[0]);
174: assertEquals("garden", cmd.getOptionValues("gravy")[1]);
175: assertEquals(cmd.getArgs().length, 0);
176: } catch (ParseException e) {
177: fail("Cannot setUp() CommandLine: " + e.toString());
178: }
179: }
180:
181: public void testShortOptionalNArgValues() {
182: String[] args = new String[] { "-i", "ink", "idea", "isotope",
183: "ice" };
184: try {
185: Parser parser = new PosixParser();
186: CommandLine cmd = parser.parse(opts, args);
187: assertTrue(cmd.hasOption("i"));
188: assertEquals("ink", cmd.getOptionValue("i"));
189: assertEquals("ink", cmd.getOptionValues("i")[0]);
190: assertEquals("idea", cmd.getOptionValues("i")[1]);
191: assertEquals(cmd.getArgs().length, 2);
192: assertEquals("isotope", cmd.getArgs()[0]);
193: assertEquals("ice", cmd.getArgs()[1]);
194: } catch (ParseException e) {
195: fail("Cannot setUp() CommandLine: " + e.toString());
196: }
197: }
198:
199: public void testLongOptionalNArgValues() {
200: String[] args = new String[] { "--hide", "house", "hair",
201: "head" };
202:
203: Parser parser = new PosixParser();
204:
205: try {
206: CommandLine cmd = parser.parse(opts, args);
207: assertTrue(cmd.hasOption("hide"));
208: assertEquals("house", cmd.getOptionValue("hide"));
209: assertEquals("house", cmd.getOptionValues("hide")[0]);
210: assertEquals("hair", cmd.getOptionValues("hide")[1]);
211: assertEquals(cmd.getArgs().length, 1);
212: assertEquals("head", cmd.getArgs()[0]);
213: } catch (ParseException e) {
214: fail("Cannot setUp() CommandLine: " + e.toString());
215: }
216: }
217:
218: public void testPropertyOptionSingularValue() {
219: Properties properties = new Properties();
220: properties.setProperty("hide", "seek");
221:
222: Parser parser = new PosixParser();
223:
224: try {
225: CommandLine cmd = parser.parse(opts, null, properties);
226: assertTrue(cmd.hasOption("hide"));
227: assertEquals("seek", cmd.getOptionValue("hide"));
228: assertTrue(!cmd.hasOption("fake"));
229: } catch (ParseException e) {
230: fail("Cannot setUp() CommandLine: " + e.toString());
231: }
232: }
233:
234: public void testPropertyOptionFlags() {
235: Properties properties = new Properties();
236: properties.setProperty("a", "true");
237: properties.setProperty("c", "yes");
238: properties.setProperty("e", "1");
239:
240: Parser parser = new PosixParser();
241:
242: try {
243: CommandLine cmd = parser.parse(opts, null, properties);
244: assertTrue(cmd.hasOption("a"));
245: assertTrue(cmd.hasOption("c"));
246: assertTrue(cmd.hasOption("e"));
247: } catch (ParseException e) {
248: fail("Cannot setUp() CommandLine: " + e.toString());
249: }
250:
251: properties = new Properties();
252: properties.setProperty("a", "false");
253: properties.setProperty("c", "no");
254: properties.setProperty("e", "0");
255: try {
256: CommandLine cmd = parser.parse(opts, null, properties);
257: assertTrue(!cmd.hasOption("a"));
258: assertTrue(!cmd.hasOption("c"));
259: assertTrue(!cmd.hasOption("e"));
260: } catch (ParseException e) {
261: fail("Cannot setUp() CommandLine: " + e.toString());
262: }
263:
264: properties = new Properties();
265: properties.setProperty("a", "TRUE");
266: properties.setProperty("c", "nO");
267: properties.setProperty("e", "TrUe");
268: try {
269: CommandLine cmd = parser.parse(opts, null, properties);
270: assertTrue(cmd.hasOption("a"));
271: assertTrue(!cmd.hasOption("c"));
272: assertTrue(cmd.hasOption("e"));
273: } catch (ParseException e) {
274: fail("Cannot setUp() CommandLine: " + e.toString());
275: }
276:
277: properties = new Properties();
278: properties.setProperty("a", "just a string");
279: properties.setProperty("e", "");
280: try {
281: CommandLine cmd = parser.parse(opts, null, properties);
282: assertTrue(!cmd.hasOption("a"));
283: assertTrue(!cmd.hasOption("c"));
284: assertTrue(!cmd.hasOption("e"));
285: } catch (ParseException e) {
286: fail("Cannot setUp() CommandLine: " + e.toString());
287: }
288:
289: }
290:
291: public void testPropertyOptionMultipleValues() {
292: Properties properties = new Properties();
293: properties.setProperty("k", "one,two");
294:
295: Parser parser = new PosixParser();
296:
297: String[] values = new String[] { "one", "two" };
298: try {
299: CommandLine cmd = parser.parse(opts, null, properties);
300: assertTrue(cmd.hasOption("k"));
301: assertTrue(Arrays.equals(values, cmd.getOptionValues('k')));
302: } catch (ParseException e) {
303: fail("Cannot setUp() CommandLine: " + e.toString());
304: }
305: }
306:
307: public void testPropertyOverrideValues() {
308: String[] args = new String[] { "-j", "found", "-i", "ink" };
309:
310: Properties properties = new Properties();
311: properties.setProperty("j", "seek");
312: try {
313: Parser parser = new PosixParser();
314: CommandLine cmd = parser.parse(opts, args, properties);
315: assertTrue(cmd.hasOption("j"));
316: assertEquals("found", cmd.getOptionValue("j"));
317: assertTrue(cmd.hasOption("i"));
318: assertEquals("ink", cmd.getOptionValue("i"));
319: assertTrue(!cmd.hasOption("fake"));
320: } catch (ParseException e) {
321: fail("Cannot setUp() CommandLine: " + e.toString());
322: }
323: }
324:
325: }
|