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    *      https://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  
18  package org.apache.commons.cli.bug;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.commons.cli.HelpFormatter;
29  import org.apache.commons.cli.Option;
30  import org.apache.commons.cli.OptionGroup;
31  import org.apache.commons.cli.Options;
32  import org.junit.jupiter.api.Test;
33  
34  class BugCLI266Test {
35  
36      private final List<String> insertedOrder = Arrays.asList("h", "d", "f", "x", "s", "p", "t", "w", "o");
37      private final List<String> sortOrder = Arrays.asList("d", "f", "h", "o", "p", "s", "t", "w", "x");
38  
39      private void buildOptionsGroup(final Options options) {
40          final OptionGroup optionGroup1 = new OptionGroup();
41          final OptionGroup optionGroup2 = new OptionGroup();
42          optionGroup1.setRequired(true);
43          optionGroup2.setRequired(true);
44  
45          //@formatter:off
46          optionGroup1.addOption(Option.builder("d")
47          .longOpt("db")
48          .hasArg()
49          .argName("table-name").get());
50          optionGroup1.addOption(Option.builder("f")
51          .longOpt("flat-file")
52          .hasArg()
53          .argName("input.csv").get());
54          //@formatter:on
55          options.addOptionGroup(optionGroup1);
56          //@formatter:off
57          optionGroup2.addOption(Option.builder("x")
58          .hasArg()
59          .argName("arg1").get());
60          optionGroup2.addOption(Option.builder("s").get());
61          optionGroup2.addOption(Option.builder("p")
62          .hasArg()
63          .argName("arg1").get());
64          //@formatter:on
65          options.addOptionGroup(optionGroup2);
66      }
67  
68      private Options getOptions() {
69          final Options options = new Options();
70          //@formatter:off
71          final Option help = Option.builder("h")
72          .longOpt("help")
73          .desc("Prints this help message").get();
74          //@formatter:on
75          options.addOption(help);
76  
77          buildOptionsGroup(options);
78  
79          //@formatter:off
80          final Option t = Option.builder("t")
81          .required()
82          .hasArg()
83          .argName("file").get();
84          final Option w = Option.builder("w")
85          .required()
86          .hasArg()
87          .argName("word").get();
88          final Option o = Option.builder("o")
89          .hasArg()
90          .argName("directory").get();
91          //@formatter:on
92          options.addOption(t);
93          options.addOption(w);
94          options.addOption(o);
95          return options;
96      }
97  
98      @Test
99      void testOptionComparatorDefaultOrder() {
100         final HelpFormatter formatter = new HelpFormatter();
101         final List<Option> options = new ArrayList<>(getOptions().getOptions());
102         Collections.sort(options, formatter.getOptionComparator());
103         int i = 0;
104         for (final Option o : options) {
105             assertEquals(o.getOpt(), sortOrder.get(i));
106             i++;
107         }
108     }
109 
110     @Test
111     void testOptionComparatorInsertedOrder() {
112         final Collection<Option> options = getOptions().getOptions();
113         int i = 0;
114         for (final Option o : options) {
115             assertEquals(o.getOpt(), insertedOrder.get(i));
116             i++;
117         }
118     }
119 }