|
21 | 21 | import java.io.StringReader; |
22 | 22 | import java.io.StringWriter; |
23 | 23 | import java.util.Collections; |
| 24 | +import java.util.Comparator; |
24 | 25 | import java.util.HashSet; |
25 | 26 | import java.util.Iterator; |
26 | 27 | import java.util.List; |
@@ -168,6 +169,75 @@ public void testPrint() throws IOException { |
168 | 169 | assertNull(reader.readLine()); |
169 | 170 | } |
170 | 171 |
|
| 172 | + public void testComparator() throws IOException { |
| 173 | + final StringWriter writer = new StringWriter(); |
| 174 | + final PrintWriter pw = new PrintWriter(writer); |
| 175 | + helpFormatter.setPrintWriter(pw); |
| 176 | + final Comparator comparator = new OptionComparator(); |
| 177 | + helpFormatter.setComparator(comparator); |
| 178 | + helpFormatter.print(); |
| 179 | + |
| 180 | + // test comparator |
| 181 | + assertEquals("invalid comparator", comparator, helpFormatter.getComparator()); |
| 182 | + final BufferedReader reader = |
| 183 | + new BufferedReader(new StringReader(writer.toString())); |
| 184 | + assertEquals( |
| 185 | + "+------------------------------------------------------------------------------+", |
| 186 | + reader.readLine()); |
| 187 | + assertEquals( |
| 188 | + "|*Jakarta Commons CLI *|", |
| 189 | + reader.readLine()); |
| 190 | + assertEquals( |
| 191 | + "+------------------------------------------------------------------------------+", |
| 192 | + reader.readLine()); |
| 193 | + assertEquals( |
| 194 | + "|*Usage: *|", |
| 195 | + reader.readLine()); |
| 196 | + assertEquals( |
| 197 | + "|*ant [--verbose --projecthelp --help --diagnostics] [<target1> [<target2> *|", |
| 198 | + reader.readLine()); |
| 199 | + assertEquals( |
| 200 | + "|*...]] *|", |
| 201 | + reader.readLine()); |
| 202 | + assertEquals( |
| 203 | + "+------------------------------------------------------------------------------+", |
| 204 | + reader.readLine()); |
| 205 | + assertEquals( |
| 206 | + "|*options *-* *|", |
| 207 | + reader.readLine()); |
| 208 | + assertEquals( |
| 209 | + "|* --verbose *-*print the version information and exit *|", |
| 210 | + reader.readLine()); |
| 211 | + assertEquals( |
| 212 | + "|* --projecthelp *-*print project help information *|", |
| 213 | + reader.readLine()); |
| 214 | + assertEquals( |
| 215 | + "|* --help (-?,-h) *-*Displays the help *|", |
| 216 | + reader.readLine()); |
| 217 | + assertEquals( |
| 218 | + "|* --diagnostics *-*print information that might be helpful to diagnose *|", |
| 219 | + reader.readLine()); |
| 220 | + assertEquals( |
| 221 | + "|* *-*or report problems. *|", |
| 222 | + reader.readLine()); |
| 223 | + assertEquals( |
| 224 | + "|* target [target ...]*-*The targets ant should build *|", |
| 225 | + reader.readLine()); |
| 226 | + assertEquals( |
| 227 | + "+------------------------------------------------------------------------------+", |
| 228 | + reader.readLine()); |
| 229 | + assertEquals( |
| 230 | + "|*Copyright 2003 *|", |
| 231 | + reader.readLine()); |
| 232 | + assertEquals( |
| 233 | + "|*Apache Software Foundation *|", |
| 234 | + reader.readLine()); |
| 235 | + assertEquals( |
| 236 | + "+------------------------------------------------------------------------------+", |
| 237 | + reader.readLine()); |
| 238 | + assertNull(reader.readLine()); |
| 239 | + } |
| 240 | + |
171 | 241 | public void testPrintHelp() throws IOException { |
172 | 242 | final StringWriter writer = new StringWriter(); |
173 | 243 | helpFormatter.setPrintWriter(new PrintWriter(writer)); |
@@ -530,3 +600,13 @@ public void testGutters() throws IOException { |
530 | 600 | assertNull(reader.readLine()); |
531 | 601 | } |
532 | 602 | } |
| 603 | + |
| 604 | + |
| 605 | +class OptionComparator implements Comparator { |
| 606 | + |
| 607 | + public int compare(Object o1, Object o2) { |
| 608 | + Option opt1 = (Option) o1; |
| 609 | + Option opt2 = (Option) o2; |
| 610 | + return -opt1.getPreferredName().compareTo(opt2.getPreferredName()); |
| 611 | + } |
| 612 | +} |
0 commit comments