View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.cli2.bug;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.commons.cli2.Argument;
24  import org.apache.commons.cli2.Group;
25  import org.apache.commons.cli2.OptionException;
26  import org.apache.commons.cli2.builder.ArgumentBuilder;
27  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
28  import org.apache.commons.cli2.builder.GroupBuilder;
29  import org.apache.commons.cli2.commandline.Parser;
30  import org.apache.commons.cli2.option.SourceDestArgument;
31  
32  /**
33   * The first is a loop in Parser.parse() if I set a non-declared option. This 
34   * code goes into a loop in Parser.java method parse this 'while' loop runs 
35   * endless
36   * 
37   * @author Steve Alberty
38   */
39  public class BugLoopingOptionLookAlikeTest extends TestCase {
40  
41      public void testLoopingOptionLookAlike() {
42          final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
43          final ArgumentBuilder abuilder = new ArgumentBuilder();
44          final GroupBuilder gbuilder = new GroupBuilder();
45          final Group options = gbuilder
46              .withName("ant")
47              .withOption(obuilder.withShortName("help").withDescription("print this message").create())
48              .withOption(obuilder.withShortName("projecthelp").withDescription("print project help information").create())
49              .withOption(abuilder.withName("target").create())
50              .create();
51          
52          final Parser parser = new Parser();
53          parser.setGroup(options);
54          try {
55              parser.parse(new String[] { "-abcdef",
56                      "testfile.txt ", });
57              fail("OptionException");
58          } catch (OptionException e) {
59              assertEquals("Unexpected -abcdef while processing ant",e.getMessage());
60          }
61      }
62      
63      public void testLoopingOptionLookAlike2() {
64          final ArgumentBuilder abuilder = new ArgumentBuilder();
65          final GroupBuilder gbuilder = new GroupBuilder();
66          final Argument inputfile_opt = abuilder.withName("input").withMinimum(1).withMaximum(1).create();
67          final Argument outputfile_opt = abuilder.withName("output").withMinimum(1).withMaximum(1).create();
68          final Argument targets = new SourceDestArgument(inputfile_opt, outputfile_opt);
69          final Group options = gbuilder.withOption(targets).create();
70          final Parser parser = new Parser();
71          parser.setGroup(options);
72          try {
73              parser.parse(new String[] { "testfile.txt", "testfile.txt", "testfile.txt", "testfile.txt" });
74              fail("OptionException");
75          } catch (OptionException e) {
76              assertEquals("Unexpected testfile.txt while processing ", e.getMessage());
77          }
78      }    
79  }