2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.cli;
19
import junit.framework.Test;
20
import junit.framework.TestCase;
21
import junit.framework.TestSuite;
23
import junit.textui.TestRunner;
25
public class OptionBuilderTest extends TestCase {
27
public OptionBuilderTest( String name ) {
31
public static Test suite() {
32
return new TestSuite( OptionBuilderTest.class );
35
public static void main( String args[] ) {
36
TestRunner.run( suite() );
39
public void testCompleteOption( ) {
40
Option simple = OptionBuilder.withLongOpt( "simple option")
44
.withType( new Float( 10 ) )
45
.withDescription( "this is a simple option" )
48
assertEquals( "s", simple.getOpt() );
49
assertEquals( "simple option", simple.getLongOpt() );
50
assertEquals( "this is a simple option", simple.getDescription() );
51
assertEquals( simple.getType().getClass(), Float.class );
52
assertTrue( simple.hasArg() );
53
assertTrue( simple.isRequired() );
54
assertTrue( simple.hasArgs() );
57
public void testTwoCompleteOptions( ) {
58
Option simple = OptionBuilder.withLongOpt( "simple option")
62
.withType( new Float( 10 ) )
63
.withDescription( "this is a simple option" )
66
assertEquals( "s", simple.getOpt() );
67
assertEquals( "simple option", simple.getLongOpt() );
68
assertEquals( "this is a simple option", simple.getDescription() );
69
assertEquals( simple.getType().getClass(), Float.class );
70
assertTrue( simple.hasArg() );
71
assertTrue( simple.isRequired() );
72
assertTrue( simple.hasArgs() );
74
simple = OptionBuilder.withLongOpt( "dimple option")
76
.withDescription( "this is a dimple option" )
79
assertEquals( "d", simple.getOpt() );
80
assertEquals( "dimple option", simple.getLongOpt() );
81
assertEquals( "this is a dimple option", simple.getDescription() );
82
assertNull( simple.getType() );
83
assertTrue( simple.hasArg() );
84
assertTrue( !simple.isRequired() );
85
assertTrue( !simple.hasArgs() );
88
public void testBaseOptionCharOpt() {
89
Option base = OptionBuilder.withDescription( "option description")
92
assertEquals( "o", base.getOpt() );
93
assertEquals( "option description", base.getDescription() );
94
assertTrue( !base.hasArg() );
97
public void testBaseOptionStringOpt() {
98
Option base = OptionBuilder.withDescription( "option description")
101
assertEquals( "o", base.getOpt() );
102
assertEquals( "option description", base.getDescription() );
103
assertTrue( !base.hasArg() );
106
public void testSpecialOptChars() {
110
Option opt = OptionBuilder.withDescription( "help options" )
112
assertEquals( "?", opt.getOpt() );
114
catch( IllegalArgumentException arg ) {
115
fail( "IllegalArgumentException caught" );
120
Option opt = OptionBuilder.withDescription( "read from stdin" )
122
assertEquals( "@", opt.getOpt() );
124
catch( IllegalArgumentException arg ) {
125
fail( "IllegalArgumentException caught" );
129
public void testOptionArgNumbers() {
130
Option opt = OptionBuilder.withDescription( "option description" )
133
assertEquals( 2, opt.getArgs() );
136
public void testIllegalOptions() {
137
// bad single character option
139
Option opt = OptionBuilder.withDescription( "option description" )
141
fail( "IllegalArgumentException not caught" );
143
catch( IllegalArgumentException exp ) {
147
// bad character in option string
149
Option opt = OptionBuilder.create( "opt`" );
150
fail( "IllegalArgumentException not caught" );
152
catch( IllegalArgumentException exp ) {
158
Option opt = OptionBuilder.create( "opt" );
161
catch( IllegalArgumentException exp ) {
162
fail( "IllegalArgumentException caught" );
b'\\ No newline at end of file'