Skip to content

Commit 1c6da3c

Browse files
committed
Full test coverage for HelpFormatter.setOptionComparator()
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661641 13f79535-47bb-0310-9956-ffa450edef68
1 parent b736fcf commit 1c6da3c

1 file changed

Lines changed: 39 additions & 22 deletions

File tree

src/test/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ public void testAutomaticUsage() throws Exception
180180

181181
// This test ensures the options are properly sorted
182182
// See https://issues.apache.org/jira/browse/CLI-131
183-
public void testPrintUsage() {
183+
public void testPrintUsage()
184+
{
184185
Option optionA = new Option("a", "first");
185186
Option optionB = new Option("b", "second");
186187
Option optionC = new Option("c", "third");
@@ -197,29 +198,45 @@ public void testPrintUsage() {
197198
}
198199

199200
// uses the test for CLI-131 to implement CLI-155
200-
public void testPrintSortedUsage() {
201-
Option optionA = new Option("a", "first");
202-
Option optionB = new Option("b", "second");
203-
Option optionC = new Option("c", "third");
201+
public void testPrintSortedUsage()
202+
{
204203
Options opts = new Options();
205-
opts.addOption(optionA);
206-
opts.addOption(optionB);
207-
opts.addOption(optionC);
204+
opts.addOption(new Option("a", "first"));
205+
opts.addOption(new Option("b", "second"));
206+
opts.addOption(new Option("c", "third"));
207+
208208
HelpFormatter helpFormatter = new HelpFormatter();
209-
helpFormatter.setOptionComparator(
210-
new Comparator() {
211-
public int compare(Object o1, Object o2) {
212-
// reverses the fuctionality of the default comparator
213-
Option opt1 = (Option)o1;
214-
Option opt2 = (Option)o2;
215-
return opt2.getKey().compareToIgnoreCase(opt1.getKey());
216-
}
217-
} );
218-
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
219-
PrintWriter printWriter = new PrintWriter(bytesOut);
220-
helpFormatter.printUsage(printWriter, 80, "app", opts);
221-
printWriter.close();
222-
assertEquals("usage: app [-c] [-b] [-a]" + EOL, bytesOut.toString());
209+
helpFormatter.setOptionComparator(new Comparator()
210+
{
211+
public int compare(Object o1, Object o2)
212+
{
213+
// reverses the fuctionality of the default comparator
214+
Option opt1 = (Option) o1;
215+
Option opt2 = (Option) o2;
216+
return opt2.getKey().compareToIgnoreCase(opt1.getKey());
217+
}
218+
});
219+
220+
StringWriter out = new StringWriter();
221+
helpFormatter.printUsage(new PrintWriter(out), 80, "app", opts);
222+
223+
assertEquals("usage: app [-c] [-b] [-a]" + EOL, out.toString());
224+
}
225+
226+
public void testPrintSortedUsageWithNullComparator()
227+
{
228+
Options opts = new Options();
229+
opts.addOption(new Option("a", "first"));
230+
opts.addOption(new Option("b", "second"));
231+
opts.addOption(new Option("c", "third"));
232+
233+
HelpFormatter helpFormatter = new HelpFormatter();
234+
helpFormatter.setOptionComparator(null);
235+
236+
StringWriter out = new StringWriter();
237+
helpFormatter.printUsage(new PrintWriter(out), 80, "app", opts);
238+
239+
assertEquals("usage: app [-a] [-b] [-c]" + EOL, out.toString());
223240
}
224241

225242
public void testPrintOptionGroupUsage()

0 commit comments

Comments
 (0)