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.Collections;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.ListIterator;
23  import java.util.Set;
24  
25  import org.apache.commons.cli2.DisplaySetting;
26  import org.apache.commons.cli2.Option;
27  import org.apache.commons.cli2.OptionException;
28  import org.apache.commons.cli2.Parent;
29  import org.apache.commons.cli2.WriteableCommandLine;
30  import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
31  import org.apache.commons.cli2.resource.ResourceConstants;
32  import org.apache.commons.cli2.resource.ResourceHelper;
33  
34  /**
35   * @author Rob Oxspring
36   *
37   * To change the template for this generated type comment go to
38   * Window>Preferences>Java>Code Generation>Code and Comments
39   */
40  public class CommandTest
41      extends ParentTestCase {
42      public static Command buildStartCommand() {
43          return new Command("start", "Begins the process", Collections.singleton("go"), false, null,
44                             null, 0);
45      }
46  
47      public static Command buildCommitCommand() {
48          return new Command("commit", "Commit the changes to the database", null, true, null, null, 0);
49      }
50  
51      public static Command buildLoginCommand() {
52          return new Command("login", "Initiates a session for the user", null, false,
53                             ArgumentTest.buildUsernameArgument(), null, 0);
54      }
55  
56      /*
57       * (non-Javadoc)
58       *
59       * @see org.apache.commons.cli2.ParentTestCase#testProcessParent()
60       */
61      public void testProcessParent()
62          throws OptionException {
63          final Command option = buildStartCommand();
64          final List args = list("go");
65          final WriteableCommandLine commandLine = commandLine(option, args);
66          final ListIterator iterator = args.listIterator();
67          option.processParent(commandLine, iterator);
68  
69          assertFalse(iterator.hasNext());
70          assertTrue(commandLine.hasOption(option));
71          assertTrue(commandLine.hasOption("start"));
72          assertTrue(commandLine.hasOption("go"));
73          assertTrue(commandLine.getValues(option).isEmpty());
74      }
75  
76      public void testProcessParent_Spare()
77          throws OptionException {
78          final Command option = buildLoginCommand();
79          final List args = list("login", "rob");
80          final WriteableCommandLine commandLine = commandLine(option, args);
81          final ListIterator iterator = args.listIterator();
82          option.processParent(commandLine, iterator);
83  
84          assertEquals("rob", iterator.next());
85          assertFalse(iterator.hasNext());
86          assertTrue(commandLine.hasOption(option));
87          assertTrue(commandLine.hasOption("login"));
88          assertTrue(commandLine.getValues(option).isEmpty());
89      }
90  
91      /*
92       * (non-Javadoc)
93       *
94       * @see org.apache.commons.cli2.OptionTestCase#testCanProcess()
95       */
96      public void testCanProcess() {
97          final Command option = buildStartCommand();
98          assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start"));
99      }
100 
101     public void testCanProcess_BadMatch() {
102         final Command option = buildStartCommand();
103         assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "stop"));
104     }
105 
106     public void testCanProcess_Alias() {
107         final Command option = buildStartCommand();
108         assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "go"));
109     }
110 
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.apache.commons.cli2.OptionTestCase#testPrefixes()
115      */
116     public void testPrefixes() {
117         final Command option = buildStartCommand();
118         assertTrue(option.getPrefixes().isEmpty());
119     }
120 
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.apache.commons.cli2.OptionTestCase#testProcess()
125      */
126     public void testProcess()
127         throws OptionException {
128         final Command option = buildLoginCommand();
129         final List args = list("login", "rob");
130         final WriteableCommandLine commandLine = commandLine(option, args);
131         final ListIterator iterator = args.listIterator();
132         option.process(commandLine, iterator);
133 
134         assertFalse(iterator.hasNext());
135         assertTrue(commandLine.hasOption(option));
136         assertTrue(commandLine.hasOption("login"));
137         assertEquals("rob", commandLine.getValue(option));
138     }
139 
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.apache.commons.cli2.OptionTestCase#testTriggers()
144      */
145     public void testTriggers() {
146         final Command option = buildStartCommand();
147         final Set triggers = option.getTriggers();
148         assertContentsEqual(list("start", "go"), triggers);
149     }
150 
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.apache.commons.cli2.OptionTestCase#testValidate()
155      */
156     public void testValidate() {
157         final Parent option = buildCommitCommand();
158         final WriteableCommandLine commandLine = commandLine(option, list());
159 
160         try {
161             option.validate(commandLine);
162             fail("Missing an option");
163         } catch (OptionException moe) {
164             assertSame(option, moe.getOption());
165         }
166     }
167 
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.apache.commons.cli2.OptionTestCase#testAppendUsage()
172      */
173     public void testAppendUsage() {
174         final Option option = buildStartCommand();
175         final StringBuffer buffer = new StringBuffer();
176         option.appendUsage(buffer, DisplaySetting.ALL, null);
177 
178         assertEquals("[start (go)]", buffer.toString());
179     }
180 
181     public void testNullPreferredName() {
182         try {
183             new Command(null, "", Collections.singleton("go"), false, null, null, 0);
184         } catch (IllegalArgumentException exp) {
185             assertEquals("wrong exception name",
186                          ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT),
187                          exp.getMessage());
188         }
189     }
190 
191     public void testEmotyPreferredName() {
192         try {
193             new Command("", "", Collections.singleton("go"), false, null, null, 0);
194         } catch (IllegalArgumentException exp) {
195             assertEquals("wrong exception name",
196                          ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT),
197                          exp.getMessage());
198         }
199     }
200 
201     public void testAppendUsage_NoOptional() {
202         final Option option = buildStartCommand();
203         final StringBuffer buffer = new StringBuffer();
204         final Set settings = new HashSet(DisplaySetting.ALL);
205         settings.remove(DisplaySetting.DISPLAY_OPTIONAL);
206         option.appendUsage(buffer, settings, null);
207 
208         assertEquals("start (go)", buffer.toString());
209     }
210 
211     public void testAppendUsage_NoAlias() {
212         final Option option = buildStartCommand();
213         final StringBuffer buffer = new StringBuffer();
214         final Set settings = new HashSet(DisplaySetting.ALL);
215         settings.remove(DisplaySetting.DISPLAY_ALIASES);
216         option.appendUsage(buffer, settings, null);
217 
218         assertEquals("[start]", buffer.toString());
219     }
220 
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.apache.commons.cli2.OptionTestCase#testGetPreferredName()
225      */
226     public void testGetPreferredName() {
227         final Option option = buildStartCommand();
228         assertEquals("start", option.getPreferredName());
229     }
230 
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.apache.commons.cli2.OptionTestCase#testGetDescription()
235      */
236     public void testGetDescription() {
237         final Option option = buildLoginCommand();
238         assertEquals("Initiates a session for the user", option.getDescription());
239     }
240 
241     /*
242      * (non-Javadoc)
243      *
244      * @see org.apache.commons.cli2.OptionTestCase#testHelpLines()
245      */
246     public void testHelpLines() {
247         // TODO Auto-generated method stub
248     }
249 }