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 java.util.ArrayList;
20  import java.util.Collection;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  /***
27   * @author Rob Oxspring roxspring@apache.org
28   * @version $Revision: 544360 $
29   */
30  public class OptionsTest extends TestCase
31  {
32  
33      public static Test suite() 
34      { 
35          return new TestSuite ( OptionsTest.class ); 
36      }
37  
38      public OptionsTest( String name )
39      {
40          super( name );
41      }
42  
43      public void setUp()
44      {
45      }
46  
47      public void tearDown()
48      {
49      }
50      
51      public void testHelpOptions(){
52          
53          Option longOnly1 = OptionBuilder
54              .withLongOpt("long-only1")
55              .create();
56          
57          Option longOnly2 = OptionBuilder
58              .withLongOpt("long-only2")
59              .create();
60                  
61          Option shortOnly1 = OptionBuilder
62              .create("1");
63                  
64          Option shortOnly2 = OptionBuilder
65              .create("2");
66                  
67          Option bothA = OptionBuilder
68              .withLongOpt("bothA")
69              .create("a");
70                  
71          Option bothB = OptionBuilder
72              .withLongOpt("bothB")
73              .create("b");
74          
75          Options options = new Options();
76          options.addOption(longOnly1);
77          options.addOption(longOnly2);
78          options.addOption(shortOnly1);
79          options.addOption(shortOnly2);
80          options.addOption(bothA);
81          options.addOption(bothB);
82          
83          Collection allOptions = new ArrayList();
84          allOptions.add(longOnly1);
85          allOptions.add(longOnly2);
86          allOptions.add(shortOnly1);
87          allOptions.add(shortOnly2);
88          allOptions.add(bothA);
89          allOptions.add(bothB);
90          
91          Collection helpOptions = options.helpOptions();
92          
93          assertTrue("Everything in all should be in help",helpOptions.containsAll(allOptions));
94          assertTrue("Everything in help should be in all",allOptions.containsAll(helpOptions));        
95      }
96  
97      public void testMissingOptionException() throws ParseException {
98          Options options = new Options();
99          options.addOption(OptionBuilder.isRequired().create("f"));
100         try {
101             new PosixParser().parse(options, new String[0]);
102             fail("Expected MissingOptionException to be thrown");
103         } catch (MissingOptionException e) {
104             assertEquals("Missing required option: f", e.getMessage());
105         }
106     }
107 
108     public void testMissingOptionsException() throws ParseException {
109         Options options = new Options();
110         options.addOption(OptionBuilder.isRequired().create("f"));
111         options.addOption(OptionBuilder.isRequired().create("x"));
112         try {
113             new PosixParser().parse(options, new String[0]);
114             fail("Expected MissingOptionException to be thrown");
115         } catch (MissingOptionException e) {
116             assertEquals("Missing required options: fx", e.getMessage());
117         }
118     }
119 
120 }
121