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  
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  public 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 firstGroup = new OptionGroup();
41          final OptionGroup secondGroup = new OptionGroup();
42          firstGroup.setRequired(true);
43          secondGroup.setRequired(true);
44  
45          //@formatter:off
46          firstGroup.addOption(Option.builder("d")
47                  .longOpt("db")
48                  .hasArg()
49                  .argName("table-name")
50                  .build());
51          firstGroup.addOption(Option.builder("f")
52                  .longOpt("flat-file")
53                  .hasArg()
54                  .argName("input.csv")
55                  .build());
56          //@formatter:on
57          options.addOptionGroup(firstGroup);
58          //@formatter:off
59          secondGroup.addOption(Option.builder("x")
60                  .hasArg()
61                  .argName("arg1")
62                  .build());
63          secondGroup.addOption(Option.builder("s")
64                  .build());
65          secondGroup.addOption(Option.builder("p")
66                  .hasArg()
67                  .argName("arg1")
68                  .build());
69          //@formatter:on
70          options.addOptionGroup(secondGroup);
71      }
72  
73      private Options getOptions() {
74          final Options options = new Options();
75          //@formatter:off
76          final Option help = Option.builder("h")
77                  .longOpt("help")
78                  .desc("Prints this help message")
79                  .build();
80          //@formatter:on
81          options.addOption(help);
82  
83          buildOptionsGroup(options);
84  
85          //@formatter:off
86          final Option t = Option.builder("t")
87                  .required()
88                  .hasArg()
89                  .argName("file")
90                  .build();
91          final Option w = Option.builder("w")
92                  .required()
93                  .hasArg()
94                  .argName("word")
95                  .build();
96          final Option o = Option.builder("o")
97                  .hasArg()
98                  .argName("directory")
99                  .build();
100         //@formatter:on
101         options.addOption(t);
102         options.addOption(w);
103         options.addOption(o);
104         return options;
105     }
106 
107     @Test
108     public void testOptionComparatorDefaultOrder() {
109         final HelpFormatter formatter = new HelpFormatter();
110         final List<Option> options = new ArrayList<>(getOptions().getOptions());
111         Collections.sort(options, formatter.getOptionComparator());
112         int i = 0;
113         for (final Option o : options) {
114             assertEquals(o.getOpt(), sortOrder.get(i));
115             i++;
116         }
117     }
118 
119     @Test
120     public void testOptionComparatorInsertedOrder() {
121         final Collection<Option> options = getOptions().getOptions();
122         int i = 0;
123         for (final Option o : options) {
124             assertEquals(o.getOpt(), insertedOrder.get(i));
125             i++;
126         }
127     }
128 }