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  public class GnuParseTest extends TestCase
24  {
25      private Options _options = null;
26      private Parser _parser = null;
27  
28      public static Test suite() { 
29          return new TestSuite( GnuParseTest.class ); 
30      }
31  
32      public GnuParseTest( String name )
33      {
34          super( name );
35      }
36  
37      public void setUp()
38      {
39          _options = new Options()
40              .addOption("a",
41                         "enable-a",
42                         false,
43                         "turn [a] on or off")
44              .addOption("b",
45                         "bfile",
46                         true,
47                         "set the value of [b]")
48              .addOption("c",
49                         "copt",
50                         false,
51                         "turn [c] on or off");
52  
53          _parser = new GnuParser( );
54      }
55  
56      public void tearDown()
57      {
58  
59      }
60  
61      public void testSimpleShort()
62      {
63          String[] args = new String[] { "-a",
64                                         "-b", "toast",
65                                         "foo", "bar" };
66  
67          try
68          {
69              CommandLine cl = _parser.parse(_options, args);
70              
71              assertTrue( "Confirm -a is set", cl.hasOption("a") );
72              assertTrue( "Confirm -b is set", cl.hasOption("b") );
73              assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
74              assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
75          }
76          catch (ParseException e)
77          {
78              fail( e.toString() );
79          }
80      }
81  
82      public void testSimpleLong()
83      {
84          String[] args = new String[] { "--enable-a",
85                                         "--bfile", "toast",
86                                         "foo", "bar" };
87  
88          try
89          {
90              CommandLine cl = _parser.parse(_options, args);
91              
92              assertTrue( "Confirm -a is set", cl.hasOption("a") );
93              assertTrue( "Confirm -b is set", cl.hasOption("b") );
94              assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
95              assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2);
96          } 
97          catch (ParseException e)
98          {
99              fail( e.toString() );
100         }
101     }
102 
103     public void testExtraOption()
104     {
105         String[] args = new String[] { "-a", "-d", "-b", "toast",
106                                        "foo", "bar" };
107 
108         boolean caught = false;
109 
110         try
111         {
112             CommandLine cl = _parser.parse(_options, args);
113             
114             assertTrue( "Confirm -a is set", cl.hasOption("a") );
115             assertTrue( "Confirm -b is set", cl.hasOption("b") );
116             assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") );
117             assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3);
118         }
119         catch (UnrecognizedOptionException e)
120         {
121             caught = true;
122         }
123         catch (ParseException e)
124         {
125             fail( e.toString() );
126         }
127         assertTrue( "Confirm UnrecognizedOptionException caught", caught );
128     }
129 
130     public void testMissingArg()
131     {
132 
133         String[] args = new String[] { "-b" };
134 
135         boolean caught = false;
136 
137         try
138         {
139             CommandLine cl = _parser.parse(_options, args);
140         }
141         catch (MissingArgumentException e)
142         {
143             caught = true;
144         }
145         catch (ParseException e)
146         {
147             fail( e.toString() );
148         }
149 
150         assertTrue( "Confirm MissingArgumentException caught", caught );
151     }
152 
153     public void testStop()
154     {
155         String[] args = new String[] { "-c",
156                                        "foober",
157                                        "-b",
158                                        "toast" };
159 
160         try
161         {
162             CommandLine cl = _parser.parse(_options, args, true);
163             assertTrue( "Confirm -c is set", cl.hasOption("c") );
164             assertTrue( "Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
165         }
166         catch (ParseException e)
167         {
168             fail( e.toString() );
169         }
170     }
171 
172     public void testMultiple()
173     {
174         String[] args = new String[] { "-c",
175                                        "foobar",
176                                        "-b",
177                                        "toast" };
178 
179         try
180         {
181             CommandLine cl = _parser.parse(_options, args, true);
182             assertTrue( "Confirm -c is set", cl.hasOption("c") );
183             assertTrue( "Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
184 
185             cl = _parser.parse(_options, cl.getArgs() );
186 
187             assertTrue( "Confirm -c is not set", ! cl.hasOption("c") );
188             assertTrue( "Confirm -b is set", cl.hasOption("b") );
189             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
190             assertTrue( "Confirm  1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
191             assertTrue( "Confirm  value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") );
192         }
193         catch (ParseException e)
194         {
195             fail( e.toString() );
196         }
197     }
198 
199     public void testMultipleWithLong()
200     {
201         String[] args = new String[] { "--copt",
202                                        "foobar",
203                                        "--bfile", "toast" };
204 
205         try
206         {
207             CommandLine cl = _parser.parse(_options,args,
208                                             true);
209             assertTrue( "Confirm -c is set", cl.hasOption("c") );
210             assertTrue( "Confirm  3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3);
211 
212             cl = _parser.parse(_options, cl.getArgs() );
213 
214             assertTrue( "Confirm -c is not set", ! cl.hasOption("c") );
215             assertTrue( "Confirm -b is set", cl.hasOption("b") );
216             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") );
217             assertTrue( "Confirm  1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
218             assertTrue( "Confirm  value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") );
219         }
220         catch (ParseException e)
221         {
222             fail( e.toString() );
223         }
224     }
225 
226     public void testDoubleDash()
227     {
228         String[] args = new String[] { "--copt",
229                                        "--",
230                                        "-b", "toast" };
231 
232         try
233         {
234             CommandLine cl = _parser.parse(_options, args);
235 
236             assertTrue( "Confirm -c is set", cl.hasOption("c") );
237             assertTrue( "Confirm -b is not set", ! cl.hasOption("b") );
238             assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2);
239 
240         }
241         catch (ParseException e)
242         {
243             fail( e.toString() );
244         }
245     }
246 
247     public void testSingleDash()
248     {
249         String[] args = new String[] { "--copt",
250                                        "-b", "-",
251                                        "-a",
252                                        "-" };
253 
254         try
255         {
256             CommandLine cl = _parser.parse(_options, args);
257 
258             assertTrue( "Confirm -a is set", cl.hasOption("a") );
259             assertTrue( "Confirm -b is set", cl.hasOption("b") );
260             assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("-") );
261             assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1);
262             assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") );
263         }
264         catch (ParseException e)
265         {
266             fail( e.toString() );
267         }
268         
269     }
270 }