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  package org.apache.commons.cli2.option;
18  
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.ListIterator;
22  import java.util.Set;
23  
24  import org.apache.commons.cli2.DisplaySetting;
25  import org.apache.commons.cli2.Option;
26  import org.apache.commons.cli2.OptionException;
27  import org.apache.commons.cli2.Parent;
28  import org.apache.commons.cli2.WriteableCommandLine;
29  import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
30  
31  /**
32   * @author roberto
33   *
34   * To change the template for this generated type comment go to
35   * Window>Preferences>Java>Code Generation>Code and Comments
36   */
37  public class DefaultOptionTest extends ParentTestCase {
38  
39      public static DefaultOption buildHelpOption() {
40          final Set aliases = new HashSet(list("-h", "-?"));
41          return new DefaultOption(
42              "-",
43              "--",
44              true,
45              "--help",
46              "Displays the help",
47              aliases,
48              aliases,
49              false,
50              null,
51              null,
52              'h');
53      }
54  
55      public static DefaultOption buildXOption() {
56          return new DefaultOption(
57              "-",
58              "--",
59              true,
60              "-X",
61              "This is needed",
62              null,
63              null,
64              true,
65              null,
66              null,
67              'X');
68      }
69  
70      /*
71       * (non-Javadoc)
72       *
73       * @see org.apache.commons.cli2.ParentTestCase#testProcessParent()
74       */
75      public void testProcessParent() throws OptionException {
76          final DefaultOption option = buildHelpOption();
77          final List args = list("--help");
78          final WriteableCommandLine commandLine = commandLine(option, args);
79          final ListIterator iterator = args.listIterator();
80          option.processParent(commandLine, iterator);
81  
82          assertFalse(iterator.hasNext());
83          assertTrue(commandLine.hasOption(option));
84          assertTrue(commandLine.hasOption("--help"));
85          assertTrue(commandLine.hasOption("-?"));
86          assertTrue(commandLine.getValues(option).isEmpty());
87      }
88  
89      public void testProcessParent_Burst() throws OptionException {
90          final DefaultOption option = buildHelpOption();
91          final List args = list("-help");
92          final WriteableCommandLine commandLine = commandLine(option, args);
93          final ListIterator iterator = args.listIterator();
94          option.processParent(commandLine, iterator);
95  
96          assertEquals("-elp", iterator.next());
97          assertFalse(iterator.hasNext());
98          assertTrue(commandLine.hasOption(option));
99          assertTrue(commandLine.hasOption("--help"));
100         assertTrue(commandLine.hasOption("-?"));
101         assertTrue(commandLine.getValues(option).isEmpty());
102     }
103 
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.apache.commons.cli2.OptionTestCase#testCanProcess()
108      */
109     public void testCanProcess() {
110         final DefaultOption option = buildHelpOption();
111         assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-?"));
112     }
113 
114     public void testCanProcess_BadMatch() {
115         final DefaultOption option = buildHelpOption();
116         assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-H"));
117     }
118 
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.apache.commons.cli2.OptionTestCase#testPrefixes()
123      */
124     public void testPrefixes() {
125         final DefaultOption option = buildHelpOption();
126         assertContentsEqual(list("-", "--"), option.getPrefixes());
127     }
128 
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.apache.commons.cli2.OptionTestCase#testProcess()
133      */
134     public void testProcess() {
135         // TODO Auto-generated method stub
136 
137     }
138 
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.apache.commons.cli2.OptionTestCase#testTriggers()
143      */
144     public void testTriggers() {
145         final DefaultOption option = buildHelpOption();
146         assertContentsEqual(list("-?", "-h", "--help"), option.getTriggers());
147     }
148 
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.apache.commons.cli2.OptionTestCase#testValidate()
153      */
154     public void testValidate() {
155         final Parent option = buildXOption();
156         final WriteableCommandLine commandLine = commandLine(option, list());
157 
158         try {
159             option.validate(commandLine);
160             fail("Missing an option");
161         }
162         catch (OptionException moe) {
163             assertSame(option, moe.getOption());
164         }
165     }
166 
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage()
171      */
172     public void testAppendUsage() {
173         final Option option = buildHelpOption();
174         final StringBuffer buffer = new StringBuffer();
175         option.appendUsage(buffer, DisplaySetting.ALL, null);
176 
177         assertEquals("[--help (-?,-h)]", buffer.toString());
178     }
179 
180     public void testAppendUsage_NoOptional() {
181         final Option option = buildHelpOption();
182         final StringBuffer buffer = new StringBuffer();
183         final Set settings = new HashSet(DisplaySetting.ALL);
184         settings.remove(DisplaySetting.DISPLAY_OPTIONAL);
185         option.appendUsage(buffer, settings, null);
186 
187         assertEquals("--help (-?,-h)", buffer.toString());
188     }
189 
190     public void testAppendUsage_NoAlias() {
191         final Option option = buildHelpOption();
192         final StringBuffer buffer = new StringBuffer();
193         final Set settings = new HashSet(DisplaySetting.ALL);
194         settings.remove(DisplaySetting.DISPLAY_ALIASES);
195         option.appendUsage(buffer, settings, null);
196 
197         assertEquals("[--help]", buffer.toString());
198     }
199 
200     /*
201      * (non-Javadoc)
202      *
203      * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName()
204      */
205     public void testGetPreferredName() {
206         final Option option = buildHelpOption();
207         assertEquals("--help", option.getPreferredName());
208     }
209 
210     /*
211      * (non-Javadoc)
212      *
213      * @see org.apache.commons.cli2.OptionTestCase#testGetDescription()
214      */
215     public void testGetDescription() {
216         final Option option = buildHelpOption();
217         assertEquals("Displays the help", option.getDescription());
218     }
219     /*
220      * (non-Javadoc)
221      *
222      * @see org.apache.commons.cli2.OptionTestCase#testHelpLines()
223      */
224     public void testHelpLines() {
225         // TODO Auto-generated method stub
226     }
227 }