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: public class ArgumentIsOptionTest extends TestCase {
23: private Options options = null;
24: private CommandLineParser parser = null;
25:
26: public ArgumentIsOptionTest(String name) {
27: super (name);
28: }
29:
30: public static Test suite() {
31: return new TestSuite(ArgumentIsOptionTest.class);
32: }
33:
34: public void setUp() {
35: options = new Options().addOption("p", false, "Option p")
36: .addOption("attr", true, "Option accepts argument");
37:
38: parser = new PosixParser();
39: }
40:
41: public void tearDown() {
42: }
43:
44: public void testOptionAndOptionWithArgument() {
45: String[] args = new String[] { "-p", "-attr", "p" };
46:
47: try {
48: CommandLine cl = parser.parse(options, args);
49: assertTrue("Confirm -p is set", cl.hasOption("p"));
50: assertTrue("Confirm -attr is set", cl.hasOption("attr"));
51: assertTrue("Confirm arg of -attr", cl
52: .getOptionValue("attr").equals("p"));
53: assertTrue("Confirm all arguments recognized",
54: cl.getArgs().length == 0);
55: } catch (ParseException e) {
56: fail(e.toString());
57: }
58: }
59:
60: public void testOptionWithArgument() {
61: String[] args = new String[] { "-attr", "p" };
62:
63: try {
64: CommandLine cl = parser.parse(options, args);
65: assertFalse("Confirm -p is set", cl.hasOption("p"));
66: assertTrue("Confirm -attr is set", cl.hasOption("attr"));
67: assertTrue("Confirm arg of -attr", cl
68: .getOptionValue("attr").equals("p"));
69: assertTrue("Confirm all arguments recognized",
70: cl.getArgs().length == 0);
71: } catch (ParseException e) {
72: fail(e.toString());
73: }
74: }
75:
76: public void testOption() {
77: String[] args = new String[] { "-p" };
78:
79: try {
80: CommandLine cl = parser.parse(options, args);
81: assertTrue("Confirm -p is set", cl.hasOption("p"));
82: assertFalse("Confirm -attr is not set", cl
83: .hasOption("attr"));
84: assertTrue("Confirm all arguments recognized",
85: cl.getArgs().length == 0);
86: } catch (ParseException e) {
87: fail(e.toString());
88: }
89: }
90: }
|