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