001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2;
018
019import java.util.Arrays;
020import java.util.List;
021import java.util.Set;
022
023import junit.framework.TestCase;
024
025import org.apache.commons.cli2.builder.ArgumentBuilder;
026import org.apache.commons.cli2.builder.DefaultOptionBuilder;
027import org.apache.commons.cli2.builder.GroupBuilder;
028import org.apache.commons.cli2.commandline.Parser;
029
030/**
031 * @author Rob Oxspring
032 */
033public class PrecedenceTest extends TestCase {
034    private final String[] args = new String[] { "-file" };
035
036    public void testSimple() throws OptionException {
037        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
038
039        final Group options =
040            new GroupBuilder()
041                .withOption(oBuilder.withShortName("file").create())
042                .create();
043
044        final CommandLine cl = buildCommandLine(options, args);
045        assertEquals(new String[] { "-file" }, cl);
046    }
047
048    public void testArgument() throws OptionException {
049        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
050        final ArgumentBuilder aBuilder = new ArgumentBuilder();
051
052        final Group options =
053            new GroupBuilder()
054                .withOption(
055                    oBuilder
056                        .withShortName("f")
057                        .withArgument(aBuilder.create())
058                        .create())
059                .create();
060
061        final CommandLine cl = buildCommandLine(options, args);
062        assertEquals(new String[] { "-f" }, cl);
063    }
064
065    public void testBurst() throws OptionException {
066        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
067        final GroupBuilder gBuilder = new GroupBuilder();
068        final Group options =
069            gBuilder
070                .withOption(oBuilder.withShortName("f").create())
071                .withOption(oBuilder.withShortName("i").create())
072                .withOption(oBuilder.withShortName("l").create())
073                .withOption(oBuilder.withShortName("e").create())
074                .create();
075
076        final CommandLine cl = buildCommandLine(options, args);
077        assertEquals(new String[] { "-f", "-i", "-l", "-e" }, cl);
078    }
079
080    public void testChildren() throws OptionException {
081        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
082        final GroupBuilder gBuilder = new GroupBuilder();
083
084        final Group children =
085            gBuilder
086                .withOption(oBuilder.withShortName("i").create())
087                .withOption(oBuilder.withShortName("l").create())
088                .withOption(oBuilder.withShortName("e").create())
089                .create();
090        final Group options =
091            gBuilder
092                .withOption(
093                    oBuilder
094                        .withShortName("f")
095                        .withChildren(children)
096                        .create())
097                .create();
098
099        final CommandLine cl = buildCommandLine(options, args);
100        assertEquals(new String[] { "-f", "-i", "-l", "-e" }, cl);
101    }
102
103    public void XtestSimpleVsArgument() throws OptionException {
104        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
105        final GroupBuilder gBuilder = new GroupBuilder();
106        final ArgumentBuilder aBuilder = new ArgumentBuilder();
107
108        final Group options =
109            gBuilder
110                .withOption(oBuilder.withShortName("file").create())
111                .withOption(
112                    oBuilder
113                        .withShortName("f")
114                        .withArgument(aBuilder.create())
115                        .create())
116                .create();
117
118        final CommandLine cl = buildCommandLine(options, args);
119        assertEquals(new String[] { "-f" }, cl);
120    }
121
122    public void XtestSimpleVsBurst() throws OptionException {
123        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
124        final GroupBuilder gBuilder = new GroupBuilder();
125        final Group options =
126            gBuilder
127                .withOption(oBuilder.withShortName("file").create())
128                .withOption(oBuilder.withShortName("f").create())
129                .withOption(oBuilder.withShortName("i").create())
130                .withOption(oBuilder.withShortName("l").create())
131                .withOption(oBuilder.withShortName("e").create())
132                .create();
133
134        final CommandLine cl = buildCommandLine(options, args);
135        assertEquals(new String[] { "-f", "-i", "-l", "-e" }, cl);
136    }
137
138    public void XtestSimpleVsChildren() throws OptionException {
139        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
140        final GroupBuilder gBuilder = new GroupBuilder();
141
142        final Group children =
143            gBuilder
144                .withOption(
145                    oBuilder.withShortName("i").withLongName("ci").create())
146                .withOption(
147                    oBuilder.withShortName("l").withLongName("cl").create())
148                .withOption(
149                    oBuilder.withShortName("e").withLongName("ce").create())
150                .create();
151
152        final Group options =
153            gBuilder
154                .withOption(oBuilder.withShortName("file").create())
155                .withOption(
156                    oBuilder
157                        .withShortName("f")
158                        .withChildren(children)
159                        .create())
160                .create();
161
162        final CommandLine cl = buildCommandLine(options, args);
163        assertEquals(
164            new String[] { "-f", "-i", "--ci", "-l", "--cl", "-e", "--ce" },
165            cl);
166    }
167
168    public void testArgumentVsBurst() throws OptionException {
169        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
170        final GroupBuilder gBuilder = new GroupBuilder();
171        final ArgumentBuilder aBuilder = new ArgumentBuilder();
172
173        final Group options =
174            gBuilder
175                .withOption(
176                    oBuilder
177                        .withShortName("f")
178                        .withArgument(aBuilder.create())
179                        .create())
180                .withOption(oBuilder.withShortName("i").create())
181                .withOption(oBuilder.withShortName("l").create())
182                .withOption(oBuilder.withShortName("e").create())
183                .create();
184
185        final CommandLine cl = buildCommandLine(options, args);
186        assertEquals(new String[] { "-f" }, cl);
187    }
188
189    public void testArgumentVsChildren() throws OptionException {
190        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
191        final GroupBuilder gBuilder = new GroupBuilder();
192        final ArgumentBuilder aBuilder = new ArgumentBuilder();
193
194        final Group children =
195            gBuilder
196                .withOption(oBuilder.withShortName("i").create())
197                .withOption(oBuilder.withShortName("l").create())
198                .withOption(oBuilder.withShortName("e").create())
199                .create();
200        final Group options =
201            gBuilder
202                .withOption(
203                    oBuilder
204                        .withShortName("f")
205                        .withChildren(children)
206                        .withArgument(aBuilder.create())
207                        .create())
208                .create();
209
210        final CommandLine cl = buildCommandLine(options, args);
211        assertEquals(new String[] { "-f" }, cl);
212    }
213
214    public void testBurstVsChildren() throws OptionException {
215        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
216        final GroupBuilder gBuilder = new GroupBuilder();
217
218        final Group children =
219            gBuilder
220                .withOption(
221                    oBuilder.withShortName("i").withLongName("ci").create())
222                .withOption(
223                    oBuilder.withShortName("l").withLongName("cl").create())
224                .withOption(
225                    oBuilder.withShortName("e").withLongName("ce").create())
226                .create();
227
228        final Group options =
229            gBuilder
230                .withOption(
231                    oBuilder
232                        .withShortName("f")
233                        .withChildren(children)
234                        .create())
235                .withOption(
236                    oBuilder.withShortName("i").withLongName("bi").create())
237                .withOption(
238                    oBuilder.withShortName("l").withLongName("bl").create())
239                .withOption(
240                    oBuilder.withShortName("e").withLongName("be").create())
241                .create();
242
243        final CommandLine cl = buildCommandLine(options, args);
244        assertEquals(
245            new String[] { "-f", "-i", "--ci", "-l", "--cl", "-e", "--ce" },
246            cl);
247    }
248
249    public void XtestSimpleVsArgumentVsBurst() throws OptionException {
250        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
251        final GroupBuilder gBuilder = new GroupBuilder();
252        final ArgumentBuilder aBuilder = new ArgumentBuilder();
253
254        final Group options =
255            gBuilder
256                .withOption(oBuilder.withShortName("file").create())
257                .withOption(
258                    oBuilder
259                        .withShortName("f")
260                        .withArgument(aBuilder.create())
261                        .create())
262                .withOption(oBuilder.withShortName("i").create())
263                .withOption(oBuilder.withShortName("l").create())
264                .withOption(oBuilder.withShortName("e").create())
265                .create();
266
267        final CommandLine cl = buildCommandLine(options, args);
268        assertEquals(new String[] { "-f" }, cl);
269    }
270
271    public void XtestSimpleVsArgumentVsChildren() throws OptionException {
272        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
273        final GroupBuilder gBuilder = new GroupBuilder();
274        final ArgumentBuilder aBuilder = new ArgumentBuilder();
275
276        final Group children =
277            gBuilder
278                .withOption(
279                    oBuilder.withShortName("i").withLongName("ci").create())
280                .withOption(
281                    oBuilder.withShortName("l").withLongName("cl").create())
282                .withOption(
283                    oBuilder.withShortName("e").withLongName("ce").create())
284                .create();
285
286        final Group options =
287            gBuilder
288                .withOption(oBuilder.withShortName("file").create())
289                .withOption(
290                    oBuilder
291                        .withShortName("f")
292                        .withChildren(children)
293                        .withArgument(aBuilder.create())
294                        .create())
295                .create();
296
297        final CommandLine cl = buildCommandLine(options, args);
298        assertEquals(new String[] { "-f" }, cl);
299    }
300
301    public void XtestSimpleVsBurstVsChildren() throws OptionException {
302        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
303        final GroupBuilder gBuilder = new GroupBuilder();
304
305        final Group children =
306            gBuilder
307                .withOption(
308                    oBuilder.withShortName("i").withLongName("ci").create())
309                .withOption(
310                    oBuilder.withShortName("l").withLongName("cl").create())
311                .withOption(
312                    oBuilder.withShortName("e").withLongName("ce").create())
313                .create();
314
315        final Group options =
316            gBuilder
317                .withOption(oBuilder.withShortName("file").create())
318                .withOption(
319                    oBuilder
320                        .withShortName("f")
321                        .withChildren(children)
322                        .create())
323                .withOption(oBuilder.withShortName("i").create())
324                .withOption(oBuilder.withShortName("l").create())
325                .withOption(oBuilder.withShortName("e").create())
326                .create();
327
328        final CommandLine cl = buildCommandLine(options, args);
329        assertEquals(new String[] { "-f", "-i", "-l", "-e" }, cl);
330    }
331
332    public void testArgumentVsBurstVsChildren() throws OptionException {
333        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
334        final GroupBuilder gBuilder = new GroupBuilder();
335        final ArgumentBuilder aBuilder = new ArgumentBuilder();
336
337        final Group children =
338            gBuilder
339                .withOption(
340                    oBuilder.withShortName("i").withLongName("ci").create())
341                .withOption(
342                    oBuilder.withShortName("l").withLongName("cl").create())
343                .withOption(
344                    oBuilder.withShortName("e").withLongName("ce").create())
345                .create();
346
347        final Group options =
348            gBuilder
349                .withOption(
350                    oBuilder
351                        .withShortName("f")
352                        .withChildren(children)
353                        .withArgument(aBuilder.create())
354                        .create())
355                .withOption(oBuilder.withShortName("i").create())
356                .withOption(oBuilder.withShortName("l").create())
357                .withOption(oBuilder.withShortName("e").create())
358                .create();
359
360        final CommandLine cl = buildCommandLine(options, args);
361        assertEquals(new String[] { "-f" }, cl);
362    }
363
364    public void XtestSimpleVsArgumentVsBurstVsChildren()
365        throws OptionException {
366        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
367        final GroupBuilder gBuilder = new GroupBuilder();
368        final ArgumentBuilder aBuilder = new ArgumentBuilder();
369
370        final Group children =
371            gBuilder
372                .withOption(
373                    oBuilder.withShortName("i").withLongName("ci").create())
374                .withOption(
375                    oBuilder.withShortName("l").withLongName("cl").create())
376                .withOption(
377                    oBuilder.withShortName("e").withLongName("ce").create())
378                .create();
379
380        final Group options =
381            gBuilder
382                .withOption(oBuilder.withShortName("file").create())
383                .withOption(
384                    oBuilder
385                        .withShortName("f")
386                        .withChildren(children)
387                        .withArgument(aBuilder.create())
388                        .create())
389                .withOption(oBuilder.withShortName("i").create())
390                .withOption(oBuilder.withShortName("l").create())
391                .withOption(oBuilder.withShortName("e").create())
392                .create();
393
394        final CommandLine cl = buildCommandLine(options, args);
395        assertEquals(new String[] { "-f" }, cl);
396    }
397
398    public CommandLine buildCommandLine(final Group group, final String[] arguments)
399        throws OptionException {
400        Parser p = new Parser();
401        p.setGroup(group);
402        return p.parse(arguments);
403    }
404
405    public void assertEquals(final String options[], final CommandLine line) {
406        final List expected = Arrays.asList(options);
407        final Set actual = line.getOptionTriggers();
408
409        assertTrue(expected.containsAll(actual));
410        assertTrue(actual.containsAll(expected));
411    }
412
413}