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.builder;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.commons.cli2.option.ArgumentImpl;
27  import org.apache.commons.cli2.resource.ResourceConstants;
28  import org.apache.commons.cli2.resource.ResourceHelper;
29  import org.apache.commons.cli2.validation.DateValidator;
30  import org.apache.commons.cli2.validation.Validator;
31  
32  public class ArgumentBuilderTest
33      extends TestCase {
34      private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
35      private ArgumentBuilder argumentBuilder;
36  
37      /*
38       * @see TestCase#setUp()
39       */
40      protected void setUp()
41          throws Exception {
42          this.argumentBuilder = new ArgumentBuilder();
43      }
44  
45      public void testConsumeRemaining() {
46          this.argumentBuilder.withConsumeRemaining("--");
47          this.argumentBuilder.withName("arg");
48  
49          ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
50  
51          assertEquals("incorrect consume remaining token", "--", arg.getConsumeRemaining());
52      }
53  
54      public void testNullConsumeRemaining() {
55          try {
56              this.argumentBuilder.withConsumeRemaining(null);
57              fail("cannot use null consume remaining token");
58          } catch (IllegalArgumentException exp) {
59              assertEquals("wrong exception message",
60                           resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING),
61                           exp.getMessage());
62          }
63      }
64  
65      public void testEmptyConsumeRemaining() {
66          try {
67              this.argumentBuilder.withConsumeRemaining("");
68              fail("cannot use empty string consume remaining token");
69          } catch (IllegalArgumentException exp) {
70              assertEquals("wrong exception message",
71                           resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING),
72                           exp.getMessage());
73          }
74      }
75  
76      public void testDefault() {
77          this.argumentBuilder.withDefault("defaultString");
78          this.argumentBuilder.withName("arg");
79  
80          ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
81  
82          assertEquals("incorrect number of default values", 1, arg.getDefaultValues().size());
83          assertEquals("incorrect default value", "defaultString", arg.getDefaultValues().get(0));
84      }
85  
86      public void testDefaultX2() {
87          this.argumentBuilder.withDefault("defaultString1");
88          this.argumentBuilder.withDefault("defaultString2");
89          this.argumentBuilder.withName("arg");
90  
91          ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
92  
93          assertEquals("incorrect number of default values", 2, arg.getDefaultValues().size());
94          assertEquals("incorrect default value-1", "defaultString1", arg.getDefaultValues().get(0));
95          assertEquals("incorrect default value-2", "defaultString2", arg.getDefaultValues().get(1));
96      }
97  
98      public void testNullDefault() {
99          try {
100             this.argumentBuilder.withDefault(null);
101             fail("cannot use null default");
102         } catch (IllegalArgumentException exp) {
103             assertEquals("wrong exception message",
104                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT),
105                          exp.getMessage());
106         }
107     }
108 
109     public void testDefaults() {
110         final List defaults = new ArrayList();
111         defaults.add("one");
112         defaults.add("two");
113 
114         this.argumentBuilder.withDefaults(defaults);
115         this.argumentBuilder.withName("arg");
116 
117         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
118 
119         assertEquals("incorrect number of default values", 2, arg.getDefaultValues().size());
120         assertEquals("incorrect default value-1", "one", arg.getDefaultValues().get(0));
121         assertEquals("incorrect default value-2", "two", arg.getDefaultValues().get(1));
122         assertEquals("incorrect default values list", defaults, arg.getDefaultValues());
123 
124     }
125 
126     public void testNullDefaults() {
127         try {
128             this.argumentBuilder.withDefaults(null);
129             fail("cannot use null defaults");
130         } catch (IllegalArgumentException exp) {
131             assertEquals("wrong exception message",
132                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS),
133                          exp.getMessage());
134         }
135     }
136 
137     public void testId() {
138         this.argumentBuilder.withId(1);
139         this.argumentBuilder.withName("arg");
140 
141         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
142 
143         assertEquals("incorrect id", 1, arg.getId());
144     }
145 
146     public void testInitialSeparator() {
147         this.argumentBuilder.withInitialSeparator(',');
148         this.argumentBuilder.withName("arg");
149 
150         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
151 
152         assertEquals("incorrect initial separator", ',', arg.getInitialSeparator());
153     }
154 
155     public void testMaximum() {
156         this.argumentBuilder.withMaximum(1);
157         this.argumentBuilder.withName("arg");
158 
159         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
160 
161         assertEquals("incorrect maximum", 1, arg.getMaximum());
162     }
163 
164     public void testNegativeMaximum() {
165         try {
166             this.argumentBuilder.withMaximum(-1);
167             fail("cannot use negative maximum");
168         } catch (IllegalArgumentException exp) {
169             assertEquals("wrong exception message",
170                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM),
171                          exp.getMessage());
172         }
173     }
174 
175     public void testMinimum() {
176         this.argumentBuilder.withMinimum(1);
177         this.argumentBuilder.withName("arg");
178 
179         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
180 
181         assertEquals("incorrect maximum", 1, arg.getMinimum());
182     }
183 
184     public void testNegativeMinimum() {
185         try {
186             this.argumentBuilder.withMinimum(-1);
187             fail("cannot use negative minimum");
188         } catch (IllegalArgumentException exp) {
189             assertEquals("wrong exception message",
190                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM),
191                          exp.getMessage());
192         }
193     }
194 
195     public void testName() {
196         this.argumentBuilder.withName("arg");
197 
198         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
199 
200         assertEquals("incorrect preferred name", "arg", arg.getPreferredName());
201     }
202 
203     public void testNullName() {
204         try {
205             this.argumentBuilder.withName(null);
206             fail("cannot use null name");
207         } catch (IllegalArgumentException exp) {
208             assertEquals("wrong exception message",
209                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_NAME),
210                          exp.getMessage());
211         }
212     }
213 
214     public void testEmptyName() {
215         try {
216             this.argumentBuilder.withName("");
217             fail("cannot use empty name");
218         } catch (IllegalArgumentException exp) {
219             assertEquals("wrong exception message",
220                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME),
221                          exp.getMessage());
222         }
223     }
224 
225     public void testSubsequentSeparator() {
226         this.argumentBuilder.withSubsequentSeparator(':');
227         this.argumentBuilder.withName("arg");
228 
229         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
230 
231         assertEquals("incorrect subsequent separator", ':', arg.getSubsequentSeparator());
232     }
233 
234     public void testValidator() {
235         Validator validator = DateValidator.getDateInstance();
236         this.argumentBuilder.withValidator(validator);
237         this.argumentBuilder.withName("arg");
238 
239         ArgumentImpl arg = (ArgumentImpl) this.argumentBuilder.create();
240 
241         assertEquals("incorrect validator", validator, arg.getValidator());
242     }
243 
244     public void testNullValidator() {
245         try {
246             this.argumentBuilder.withValidator(null);
247             fail("cannot use null validator");
248         } catch (IllegalArgumentException exp) {
249             assertEquals("wrong exception message",
250                          resources.getMessage(ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR),
251                          exp.getMessage());
252         }
253     }
254 }