Skip to content

Commit 9a3f8e0

Browse files
author
John Keyes
committed
- added comparator test
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@278885 13f79535-47bb-0310-9956-ffa450edef68
1 parent ae8e1d8 commit 9a3f8e0

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

src/test/org/apache/commons/cli2/util/HelpFormatterTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.StringReader;
2222
import java.io.StringWriter;
2323
import java.util.Collections;
24+
import java.util.Comparator;
2425
import java.util.HashSet;
2526
import java.util.Iterator;
2627
import java.util.List;
@@ -168,6 +169,75 @@ public void testPrint() throws IOException {
168169
assertNull(reader.readLine());
169170
}
170171

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+
171241
public void testPrintHelp() throws IOException {
172242
final StringWriter writer = new StringWriter();
173243
helpFormatter.setPrintWriter(new PrintWriter(writer));
@@ -530,3 +600,13 @@ public void testGutters() throws IOException {
530600
assertNull(reader.readLine());
531601
}
532602
}
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

Comments
 (0)