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 junit.textui.TestRunner;
023:
024: public class OptionBuilderTest extends TestCase {
025:
026: public OptionBuilderTest(String name) {
027: super (name);
028: }
029:
030: public static Test suite() {
031: return new TestSuite(OptionBuilderTest.class);
032: }
033:
034: public static void main(String args[]) {
035: TestRunner.run(suite());
036: }
037:
038: public void testCompleteOption() {
039: Option simple = OptionBuilder.withLongOpt("simple option")
040: .hasArg().isRequired().hasArgs()
041: .withType(new Float(10)).withDescription(
042: "this is a simple option").create('s');
043:
044: assertEquals("s", simple.getOpt());
045: assertEquals("simple option", simple.getLongOpt());
046: assertEquals("this is a simple option", simple.getDescription());
047: assertEquals(simple.getType().getClass(), Float.class);
048: assertTrue(simple.hasArg());
049: assertTrue(simple.isRequired());
050: assertTrue(simple.hasArgs());
051: }
052:
053: public void testTwoCompleteOptions() {
054: Option simple = OptionBuilder.withLongOpt("simple option")
055: .hasArg().isRequired().hasArgs()
056: .withType(new Float(10)).withDescription(
057: "this is a simple option").create('s');
058:
059: assertEquals("s", simple.getOpt());
060: assertEquals("simple option", simple.getLongOpt());
061: assertEquals("this is a simple option", simple.getDescription());
062: assertEquals(simple.getType().getClass(), Float.class);
063: assertTrue(simple.hasArg());
064: assertTrue(simple.isRequired());
065: assertTrue(simple.hasArgs());
066:
067: simple = OptionBuilder.withLongOpt("dimple option").hasArg()
068: .withDescription("this is a dimple option").create('d');
069:
070: assertEquals("d", simple.getOpt());
071: assertEquals("dimple option", simple.getLongOpt());
072: assertEquals("this is a dimple option", simple.getDescription());
073: assertNull(simple.getType());
074: assertTrue(simple.hasArg());
075: assertTrue(!simple.isRequired());
076: assertTrue(!simple.hasArgs());
077: }
078:
079: public void testBaseOptionCharOpt() {
080: Option base = OptionBuilder.withDescription(
081: "option description").create('o');
082:
083: assertEquals("o", base.getOpt());
084: assertEquals("option description", base.getDescription());
085: assertTrue(!base.hasArg());
086: }
087:
088: public void testBaseOptionStringOpt() {
089: Option base = OptionBuilder.withDescription(
090: "option description").create("o");
091:
092: assertEquals("o", base.getOpt());
093: assertEquals("option description", base.getDescription());
094: assertTrue(!base.hasArg());
095: }
096:
097: public void testSpecialOptChars() {
098:
099: // '?'
100: try {
101: Option opt = OptionBuilder.withDescription("help options")
102: .create('?');
103: assertEquals("?", opt.getOpt());
104: } catch (IllegalArgumentException arg) {
105: fail("IllegalArgumentException caught");
106: }
107:
108: // '@'
109: try {
110: Option opt = OptionBuilder.withDescription(
111: "read from stdin").create('@');
112: assertEquals("@", opt.getOpt());
113: } catch (IllegalArgumentException arg) {
114: fail("IllegalArgumentException caught");
115: }
116: }
117:
118: public void testOptionArgNumbers() {
119: Option opt = OptionBuilder
120: .withDescription("option description").hasArgs(2)
121: .create('o');
122: assertEquals(2, opt.getArgs());
123: }
124:
125: public void testIllegalOptions() {
126: // bad single character option
127: try {
128: Option opt = OptionBuilder.withDescription(
129: "option description").create('"');
130: fail("IllegalArgumentException not caught");
131: } catch (IllegalArgumentException exp) {
132: // success
133: }
134:
135: // bad character in option string
136: try {
137: Option opt = OptionBuilder.create("opt`");
138: fail("IllegalArgumentException not caught");
139: } catch (IllegalArgumentException exp) {
140: // success
141: }
142:
143: // valid option
144: try {
145: Option opt = OptionBuilder.create("opt");
146: // success
147: } catch (IllegalArgumentException exp) {
148: fail("IllegalArgumentException caught");
149: }
150: }
151: }
|