~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/commons-cli-1.1-src/src/test/org/apache/commons/cli/OptionBuilderTest.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
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
 
8
 *
 
9
 *     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
 */
 
17
package org.apache.commons.cli;
 
18
 
 
19
import junit.framework.Test;
 
20
import junit.framework.TestCase;
 
21
import junit.framework.TestSuite;
 
22
 
 
23
import junit.textui.TestRunner;
 
24
 
 
25
public class OptionBuilderTest extends TestCase {
 
26
 
 
27
    public OptionBuilderTest( String name ) {
 
28
        super( name );
 
29
    }
 
30
 
 
31
    public static Test suite() { 
 
32
        return new TestSuite( OptionBuilderTest.class ); 
 
33
    }
 
34
 
 
35
    public static void main( String args[] ) { 
 
36
        TestRunner.run( suite() );
 
37
    }
 
38
 
 
39
    public void testCompleteOption( ) {
 
40
        Option simple = OptionBuilder.withLongOpt( "simple option")
 
41
                                     .hasArg( )
 
42
                                     .isRequired( )
 
43
                                     .hasArgs( )
 
44
                                     .withType( new Float( 10 ) )
 
45
                                     .withDescription( "this is a simple option" )
 
46
                                     .create( 's' );
 
47
 
 
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() );
 
55
    }
 
56
 
 
57
    public void testTwoCompleteOptions( ) {
 
58
        Option simple = OptionBuilder.withLongOpt( "simple option")
 
59
                                     .hasArg( )
 
60
                                     .isRequired( )
 
61
                                     .hasArgs( )
 
62
                                     .withType( new Float( 10 ) )
 
63
                                     .withDescription( "this is a simple option" )
 
64
                                     .create( 's' );
 
65
 
 
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() );
 
73
 
 
74
        simple = OptionBuilder.withLongOpt( "dimple option")
 
75
                              .hasArg( )
 
76
                              .withDescription( "this is a dimple option" )
 
77
                              .create( 'd' );
 
78
 
 
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() );
 
86
    }
 
87
 
 
88
    public void testBaseOptionCharOpt() {
 
89
        Option base = OptionBuilder.withDescription( "option description")
 
90
                                   .create( 'o' );
 
91
 
 
92
        assertEquals( "o", base.getOpt() );
 
93
        assertEquals( "option description", base.getDescription() );
 
94
        assertTrue( !base.hasArg() );
 
95
    }
 
96
 
 
97
    public void testBaseOptionStringOpt() {
 
98
        Option base = OptionBuilder.withDescription( "option description")
 
99
                                   .create( "o" );
 
100
 
 
101
        assertEquals( "o", base.getOpt() );
 
102
        assertEquals( "option description", base.getDescription() );
 
103
        assertTrue( !base.hasArg() );
 
104
    }
 
105
 
 
106
    public void testSpecialOptChars() {
 
107
 
 
108
        // '?'
 
109
        try {
 
110
            Option opt = OptionBuilder.withDescription( "help options" )
 
111
                                      .create( '?' );
 
112
            assertEquals( "?", opt.getOpt() );
 
113
        }
 
114
        catch( IllegalArgumentException arg ) {
 
115
            fail( "IllegalArgumentException caught" );
 
116
        }
 
117
 
 
118
        // '@'
 
119
        try {
 
120
            Option opt = OptionBuilder.withDescription( "read from stdin" )
 
121
                                      .create( '@' );
 
122
            assertEquals( "@", opt.getOpt() );
 
123
        }
 
124
        catch( IllegalArgumentException arg ) {
 
125
            fail( "IllegalArgumentException caught" );
 
126
        }
 
127
    }
 
128
 
 
129
    public void testOptionArgNumbers() {
 
130
        Option opt = OptionBuilder.withDescription( "option description" )
 
131
                                  .hasArgs( 2 )
 
132
                                  .create( 'o' );
 
133
        assertEquals( 2, opt.getArgs() );
 
134
    }
 
135
 
 
136
    public void testIllegalOptions() {
 
137
        // bad single character option
 
138
        try {
 
139
            Option opt = OptionBuilder.withDescription( "option description" )
 
140
                                      .create( '"' );
 
141
            fail( "IllegalArgumentException not caught" );
 
142
        }
 
143
        catch( IllegalArgumentException exp ) {
 
144
            // success
 
145
        }
 
146
 
 
147
        // bad character in option string
 
148
        try {
 
149
            Option opt = OptionBuilder.create( "opt`" );
 
150
            fail( "IllegalArgumentException not caught" );
 
151
        }
 
152
        catch( IllegalArgumentException exp ) {
 
153
            // success
 
154
        }
 
155
 
 
156
        // valid option 
 
157
        try {
 
158
            Option opt = OptionBuilder.create( "opt" );
 
159
            // success
 
160
        }
 
161
        catch( IllegalArgumentException exp ) {
 
162
            fail( "IllegalArgumentException caught" );
 
163
        }
 
164
    }
 
165
}
 
 
b'\\ No newline at end of file'