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;
18  
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.commons.cli2.builder.ArgumentBuilder;
26  import org.apache.commons.cli2.builder.SwitchBuilder;
27  import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
28  
29  /**
30   * Tests the interaction of command line values and defaults supplied in different ways.
31   *
32   * Tests marked _Parsed involve values parsed from a command line.
33   *
34   * Tests marked _Method involve defaults supplied in the query method.
35   *
36   * Tests marked _Option involce defaults specified in the model.
37   *
38   * @author Rob Oxspring
39   */
40  public class CommandLineDefaultsTest extends TestCase {
41  
42      /*
43       * utils to grab the default from the method
44       */
45  
46      private Object methodSwitch(WriteableCommandLine cl, Option o, Boolean bool) {
47          return cl.getSwitch(o, bool);
48      }
49  
50      private Object methodSwitchNull(WriteableCommandLine cl, Option o) {
51          return methodSwitch(cl, o, null);
52      }
53  
54      private Object methodSwitchOff(WriteableCommandLine cl, Option o) {
55          return methodSwitch(cl, o, Boolean.FALSE);
56      }
57  
58      private Object methodSwitchOn(WriteableCommandLine cl, Option o) {
59          return methodSwitch(cl, o, Boolean.TRUE);
60      }
61  
62      private Object methodValueMissing(WriteableCommandLine cl, Option o) {
63          return cl.getValue(o);
64      }
65  
66      private Object methodValuePresent(WriteableCommandLine cl, Option o) {
67          return cl.getValue(o, "method");
68      }
69  
70      /*
71       * utils to grab the default from the option model
72       */
73  
74      private Option optionSwitch(Boolean bool) {
75          return new SwitchBuilder().withName("switch").withSwitchDefault(bool)
76                  .create();
77      }
78  
79      private Option optionSwitchNull() {
80          return optionSwitch(null);
81      }
82  
83      private Option optionSwitchOff() {
84          return optionSwitch(Boolean.FALSE);
85      }
86  
87      private Option optionSwitchOn() {
88          return optionSwitch(Boolean.TRUE);
89      }
90  
91      private Option optionValueMissing() {
92          return new ArgumentBuilder().create();
93      }
94  
95      private Option optionValuePresent() {
96          return new ArgumentBuilder().withDefaults(
97                  Arrays.asList(new String[] { "option" })).create();
98      }
99  
100     /*
101      * utils to grab the input from the command line
102      */
103 
104     private WriteableCommandLine parsedSwitch(Option o, Boolean bool) {
105         final List args;
106         if (bool == null) {
107             args = Collections.EMPTY_LIST;
108         } else {
109             args = Collections
110                     .singletonList(String.valueOf(bool).toLowerCase());
111         }
112         WriteableCommandLine cl = new WriteableCommandLineImpl(o, args);
113         o.defaults(cl);
114         if (bool != null) {
115             cl.addSwitch(o, bool.booleanValue());
116         }
117         return cl;
118     }
119 
120     private WriteableCommandLine parsedSwitchNull(Option o) {
121         return parsedSwitch(o, null);
122     }
123 
124     private WriteableCommandLine parsedSwitchOn(Option o) {
125         return parsedSwitch(o, Boolean.TRUE);
126     }
127 
128     private WriteableCommandLine parsedValueMissing(Option o) {
129         WriteableCommandLine cl = new WriteableCommandLineImpl(o,
130                 Collections.EMPTY_LIST);
131         o.defaults(cl);
132         return cl;
133     }
134 
135     private WriteableCommandLine parsedValuePresent(Option o) {
136         WriteableCommandLine cl = new WriteableCommandLineImpl(o, Arrays
137                 .asList(new String[] { "parsed" }));
138         o.defaults(cl);
139         cl.addValue(o, "parsed");
140         return cl;
141     }
142 
143     /*
144      * tests
145      */
146 
147     public void testSwitch_Method() {
148         final Option o = optionSwitchNull();
149         final WriteableCommandLine cl = parsedSwitchNull(o);
150         final Object v = methodSwitchOn(cl, o);
151         assertEquals(Boolean.TRUE, v);
152     }
153 
154     public void testSwitch_Method_Option() {
155         final Option o = optionSwitchOff();
156         final WriteableCommandLine cl = parsedSwitchNull(o);
157         final Object v = methodSwitchOn(cl, o);
158         assertEquals(Boolean.TRUE, v);
159     }
160 
161     public void testSwitch_Option() {
162         final Option o = optionSwitchOn();
163         final WriteableCommandLine cl = parsedSwitchNull(o);
164         final Object v = methodSwitchNull(cl, o);
165         assertEquals(Boolean.TRUE, v);
166     }
167 
168     public void testSwitch_Parsed() {
169         final Option o = optionSwitchNull();
170         final WriteableCommandLine cl = parsedSwitchOn(o);
171         final Object v = methodSwitchNull(cl, o);
172         assertEquals(Boolean.TRUE, v);
173     }
174 
175     public void testSwitch_Parsed_Method() {
176         final Option o = optionSwitchOff();
177         final WriteableCommandLine cl = parsedSwitchOn(o);
178         final Object v = methodSwitchNull(cl, o);
179         assertEquals(Boolean.TRUE, v);
180     }
181 
182     public void testSwitch_Parsed_Method_Option() {
183         final Option o = optionSwitchOff();
184         final WriteableCommandLine cl = parsedSwitchOn(o);
185         final Object v = methodSwitchOff(cl, o);
186         assertEquals(Boolean.TRUE, v);
187     }
188 
189     public void testSwitch_Parsed_Option() {
190         final Option o = optionSwitchOff();
191         final WriteableCommandLine cl = parsedSwitchOn(o);
192         final Object v = methodSwitchNull(cl, o);
193         assertEquals(Boolean.TRUE, v);
194     }
195 
196     public void testValues() {
197         final Option o = optionValueMissing();
198         final WriteableCommandLine cl = parsedValueMissing(o);
199         final Object v = methodValueMissing(cl, o);
200         assertNull(v);
201     }
202 
203     public void testValues_Method() {
204         final Option o = optionValueMissing();
205         final WriteableCommandLine cl = parsedValueMissing(o);
206         final Object v = methodValuePresent(cl, o);
207         assertEquals("method", v);
208     }
209 
210     public void testValues_Method_Option() {
211         final Option o = optionValuePresent();
212         final WriteableCommandLine cl = parsedValueMissing(o);
213         final Object v = methodValuePresent(cl, o);
214         assertEquals("method", v);
215     }
216 
217     public void testValues_Option() {
218         final Option o = optionValuePresent();
219         final WriteableCommandLine cl = parsedValueMissing(o);
220         final Object v = methodValueMissing(cl, o);
221         assertEquals("option", v);
222     }
223 
224     public void testValues_Parsed() {
225         final Option o = optionValueMissing();
226         final WriteableCommandLine cl = parsedValuePresent(o);
227         final Object v = methodValueMissing(cl, o);
228         assertEquals("parsed", v);
229     }
230 
231     public void testValues_Parsed_Method() {
232         final Option o = optionValueMissing();
233         final WriteableCommandLine cl = parsedValuePresent(o);
234         final Object v = methodValuePresent(cl, o);
235         assertEquals("parsed", v);
236     }
237 
238     public void testValues_Parsed_Method_Option() {
239         final Option o = optionValuePresent();
240         final WriteableCommandLine cl = parsedValuePresent(o);
241         final Object v = methodValuePresent(cl, o);
242         assertEquals("parsed", v);
243     }
244 
245     public void testValues_Parsed_Option() {
246         final Option o = optionValuePresent();
247         final WriteableCommandLine cl = parsedValuePresent(o);
248         final Object v = methodValueMissing(cl, o);
249         assertEquals("parsed", v);
250     }
251 }