View Javadoc
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  
18  package org.apache.commons.cli;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import org.junit.jupiter.api.Test;
27  
28  @SuppressWarnings("deprecation") // OptionBuilder is marked deprecated
29  public class OptionBuilderTest {
30      @Test
31      public void testBaseOptionCharOpt() {
32          final Option base = OptionBuilder.withDescription("option description").create('o');
33  
34          assertEquals("o", base.getOpt());
35          assertEquals("option description", base.getDescription());
36          assertFalse(base.hasArg());
37      }
38  
39      @Test
40      public void testBaseOptionStringOpt() {
41          final Option base = OptionBuilder.withDescription("option description").create("o");
42  
43          assertEquals("o", base.getOpt());
44          assertEquals("option description", base.getDescription());
45          assertFalse(base.hasArg());
46      }
47  
48      @Test
49      public void testBuilderIsResettedAlways() {
50          try {
51              OptionBuilder.withDescription("JUnit").create('"');
52              fail("IllegalArgumentException expected");
53          } catch (final IllegalArgumentException e) {
54              // expected
55          }
56          assertNull(OptionBuilder.create('x').getDescription(), "we inherited a description");
57  
58          try {
59              OptionBuilder.withDescription("JUnit").create();
60              fail("IllegalArgumentException expected");
61          } catch (final IllegalArgumentException e) {
62              // expected
63          }
64          assertNull(OptionBuilder.create('x').getDescription(), "we inherited a description");
65      }
66  
67      @Test
68      public void testCompleteOption() {
69          //@formatter:off
70          final Option simple = OptionBuilder.withLongOpt("simple option")
71                                       .hasArg()
72                                       .isRequired()
73                                       .hasArgs()
74                                       .withType(Float.class)
75                                       .withDescription("this is a simple option")
76                                       .create('s');
77          //@formatter:on
78  
79          assertEquals("s", simple.getOpt());
80          assertEquals("simple option", simple.getLongOpt());
81          assertEquals("this is a simple option", simple.getDescription());
82          assertEquals(simple.getType(), Float.class);
83          assertTrue(simple.hasArg());
84          assertTrue(simple.isRequired());
85          assertTrue(simple.hasArgs());
86      }
87  
88      @Test
89      public void testCreateIncompleteOption() {
90          try {
91              OptionBuilder.hasArg().create();
92              fail("Incomplete option should be rejected");
93          } catch (final IllegalArgumentException e) {
94              // expected
95  
96              // implicitly reset the builder
97              OptionBuilder.create("opt");
98          }
99      }
100 
101     @Test
102     public void testIllegalOptions() {
103         // bad single character option
104         try {
105             OptionBuilder.withDescription("option description").create('"');
106             fail("IllegalArgumentException not caught");
107         } catch (final IllegalArgumentException exp) {
108             // success
109         }
110 
111         // bad character in option string
112         try {
113             OptionBuilder.create("opt`");
114             fail("IllegalArgumentException not caught");
115         } catch (final IllegalArgumentException exp) {
116             // success
117         }
118 
119         // valid option
120         try {
121             OptionBuilder.create("opt");
122             // success
123         } catch (final IllegalArgumentException exp) {
124             fail("IllegalArgumentException caught");
125         }
126     }
127 
128     @Test
129     public void testOptionArgNumbers() {
130         //@formatter:off
131         final Option opt = OptionBuilder.withDescription("option description")
132                                   .hasArgs(2)
133                                   .create('o');
134         //@formatter:on
135         assertEquals(2, opt.getArgs());
136     }
137 
138     @Test
139     public void testSpecialOptChars() throws Exception {
140         // '?'
141         final Option opt1 = OptionBuilder.withDescription("help options").create('?');
142         assertEquals("?", opt1.getOpt());
143 
144         // '@'
145         final Option opt2 = OptionBuilder.withDescription("read from stdin").create('@');
146         assertEquals("@", opt2.getOpt());
147 
148         // ' '
149         try {
150             OptionBuilder.create(' ');
151             fail("IllegalArgumentException not caught");
152         } catch (final IllegalArgumentException e) {
153             // success
154         }
155     }
156 
157     @Test
158     public void testTwoCompleteOptions() {
159         //@formatter:off
160         Option simple = OptionBuilder.withLongOpt("simple option")
161                                      .hasArg()
162                                      .isRequired()
163                                      .hasArgs()
164                                      .withType(Float.class)
165                                      .withDescription("this is a simple option")
166                                      .create('s');
167         //@formatter:on
168 
169         assertEquals("s", simple.getOpt());
170         assertEquals("simple option", simple.getLongOpt());
171         assertEquals("this is a simple option", simple.getDescription());
172         assertEquals(simple.getType(), Float.class);
173         assertTrue(simple.hasArg());
174         assertTrue(simple.isRequired());
175         assertTrue(simple.hasArgs());
176 
177         //@formatter:off
178         simple = OptionBuilder.withLongOpt("dimple option")
179                               .hasArg()
180                               .withDescription("this is a dimple option")
181                               .create('d');
182         //@formatter:on
183 
184         assertEquals("d", simple.getOpt());
185         assertEquals("dimple option", simple.getLongOpt());
186         assertEquals("this is a dimple option", simple.getDescription());
187         assertEquals(String.class, simple.getType());
188         assertTrue(simple.hasArg());
189         assertFalse(simple.isRequired());
190         assertFalse(simple.hasArgs());
191     }
192 }