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 java.util.Arrays;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: public class ValuesTest extends TestCase {
025: /** CommandLine instance */
026: private CommandLine _cmdline = null;
027: private Option _option = null;
028:
029: public static Test suite() {
030: return new TestSuite(ValuesTest.class);
031: }
032:
033: public ValuesTest(String name) {
034: super (name);
035: }
036:
037: public void setUp() {
038: Options opts = new Options();
039:
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.withLongOpt("e").hasArgs()
049: .withDescription("set -e ").create('e'));
050:
051: opts.addOption("f", "f", false, "jk");
052:
053: opts.addOption(OptionBuilder.withLongOpt("g").hasArgs(2)
054: .withDescription("set -g").create('g'));
055:
056: opts.addOption(OptionBuilder.withLongOpt("h").hasArgs(2)
057: .withDescription("set -h").create('h'));
058:
059: opts.addOption(OptionBuilder.withLongOpt("i").withDescription(
060: "set -i").create('i'));
061:
062: opts.addOption(OptionBuilder.withLongOpt("j").hasArgs()
063: .withDescription("set -j").withValueSeparator('=')
064: .create('j'));
065:
066: opts.addOption(OptionBuilder.withLongOpt("k").hasArgs()
067: .withDescription("set -k").withValueSeparator('=')
068: .create('k'));
069:
070: _option = OptionBuilder.withLongOpt("m").hasArgs()
071: .withDescription("set -m").withValueSeparator().create(
072: 'm');
073:
074: opts.addOption(_option);
075:
076: String[] args = new String[] { "-a", "-b", "foo", "--c", "--d",
077: "bar", "-e", "one", "two", "-f", "arg1", "arg2", "-g",
078: "val1", "val2", "arg3", "-h", "val1", "-i", "-h",
079: "val2", "-jkey=value", "-j", "key=value",
080: "-kkey1=value1", "-kkey2=value2", "-mkey=value" };
081:
082: CommandLineParser parser = new PosixParser();
083:
084: try {
085: _cmdline = parser.parse(opts, args);
086: } catch (ParseException e) {
087: fail("Cannot setUp() CommandLine: " + e.toString());
088: }
089: }
090:
091: public void tearDown() {
092:
093: }
094:
095: public void testShortArgs() {
096: assertTrue(_cmdline.hasOption("a"));
097: assertTrue(_cmdline.hasOption("c"));
098:
099: assertNull(_cmdline.getOptionValues("a"));
100: assertNull(_cmdline.getOptionValues("c"));
101: }
102:
103: public void testShortArgsWithValue() {
104: assertTrue(_cmdline.hasOption("b"));
105: assertTrue(_cmdline.getOptionValue("b").equals("foo"));
106: assertTrue(_cmdline.getOptionValues("b").length == 1);
107:
108: assertTrue(_cmdline.hasOption("d"));
109: assertTrue(_cmdline.getOptionValue("d").equals("bar"));
110: assertTrue(_cmdline.getOptionValues("d").length == 1);
111: }
112:
113: public void testMultipleArgValues() {
114: String[] result = _cmdline.getOptionValues("e");
115: String[] values = new String[] { "one", "two" };
116: assertTrue(_cmdline.hasOption("e"));
117: assertTrue(_cmdline.getOptionValues("e").length == 2);
118: assertTrue(Arrays.equals(values, _cmdline.getOptionValues("e")));
119: }
120:
121: public void testTwoArgValues() {
122: String[] result = _cmdline.getOptionValues("g");
123: String[] values = new String[] { "val1", "val2" };
124: assertTrue(_cmdline.hasOption("g"));
125: assertTrue(_cmdline.getOptionValues("g").length == 2);
126: assertTrue(Arrays.equals(values, _cmdline.getOptionValues("g")));
127: }
128:
129: public void testComplexValues() {
130: String[] result = _cmdline.getOptionValues("h");
131: String[] values = new String[] { "val1", "val2" };
132: assertTrue(_cmdline.hasOption("i"));
133: assertTrue(_cmdline.hasOption("h"));
134: assertTrue(_cmdline.getOptionValues("h").length == 2);
135: assertTrue(Arrays.equals(values, _cmdline.getOptionValues("h")));
136: }
137:
138: public void testExtraArgs() {
139: String[] args = new String[] { "arg1", "arg2", "arg3" };
140: assertTrue(_cmdline.getArgs().length == 3);
141: assertTrue(Arrays.equals(args, _cmdline.getArgs()));
142: }
143:
144: public void testCharSeparator() {
145: // tests the char methods of CommandLine that delegate to
146: // the String methods
147: String[] values = new String[] { "key", "value", "key", "value" };
148: assertTrue(_cmdline.hasOption("j"));
149: assertTrue(_cmdline.hasOption('j'));
150: assertEquals(4, _cmdline.getOptionValues("j").length);
151: assertEquals(4, _cmdline.getOptionValues('j').length);
152: assertTrue(Arrays.equals(values, _cmdline.getOptionValues("j")));
153: assertTrue(Arrays.equals(values, _cmdline.getOptionValues('j')));
154:
155: values = new String[] { "key1", "value1", "key2", "value2" };
156: assertTrue(_cmdline.hasOption("k"));
157: assertTrue(_cmdline.hasOption('k'));
158: assertTrue(_cmdline.getOptionValues("k").length == 4);
159: assertTrue(_cmdline.getOptionValues('k').length == 4);
160: assertTrue(Arrays.equals(values, _cmdline.getOptionValues("k")));
161: assertTrue(Arrays.equals(values, _cmdline.getOptionValues('k')));
162:
163: values = new String[] { "key", "value" };
164: assertTrue(_cmdline.hasOption("m"));
165: assertTrue(_cmdline.hasOption('m'));
166: assertTrue(_cmdline.getOptionValues("m").length == 2);
167: assertTrue(_cmdline.getOptionValues('m').length == 2);
168: assertTrue(Arrays.equals(values, _cmdline.getOptionValues("m")));
169: assertTrue(Arrays.equals(values, _cmdline.getOptionValues('m')));
170: }
171:
172: /**
173: * jkeyes - commented out this test as the new architecture
174: * breaks this type of functionality. I have left the test
175: * here in case I get a brainwave on how to resolve this.
176: */
177: /*
178: public void testGetValue()
179: {
180: // the 'm' option
181: assertTrue( _option.getValues().length == 2 );
182: assertEquals( _option.getValue(), "key" );
183: assertEquals( _option.getValue( 0 ), "key" );
184: assertEquals( _option.getValue( 1 ), "value" );
185:
186: try {
187: assertEquals( _option.getValue( 2 ), "key" );
188: fail( "IndexOutOfBounds not caught" );
189: }
190: catch( IndexOutOfBoundsException exp ) {
191:
192: }
193:
194: try {
195: assertEquals( _option.getValue( -1 ), "key" );
196: fail( "IndexOutOfBounds not caught" );
197: }
198: catch( IndexOutOfBoundsException exp ) {
199:
200: }
201: }
202: */
203: }
|