|
20 | 20 | import java.io.PrintWriter; |
21 | 21 | import java.io.StringReader; |
22 | 22 | import java.io.StringWriter; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashSet; |
23 | 25 | import java.util.Iterator; |
24 | 26 | import java.util.List; |
| 27 | +import java.util.Set; |
25 | 28 |
|
26 | 29 | import junit.framework.TestCase; |
27 | 30 |
|
| 31 | +import org.apache.commons.cli2.DisplaySetting; |
28 | 32 | import org.apache.commons.cli2.Group; |
29 | 33 | import org.apache.commons.cli2.Option; |
30 | 34 | import org.apache.commons.cli2.OptionException; |
@@ -74,10 +78,36 @@ public void setUp() { |
74 | 78 |
|
75 | 79 | public void testPrint() throws IOException { |
76 | 80 | final StringWriter writer = new StringWriter(); |
77 | | - helpFormatter.setPrintWriter(new PrintWriter(writer)); |
| 81 | + final PrintWriter pw = new PrintWriter(writer); |
| 82 | + helpFormatter.setPrintWriter(pw); |
78 | 83 | helpFormatter.print(); |
79 | 84 |
|
80 | | - //System.out.println(writer.toString()); |
| 85 | + // test group |
| 86 | + assertEquals("incorrect group", this.options, helpFormatter.getGroup()); |
| 87 | + |
| 88 | + // test pagewidth |
| 89 | + assertEquals("incorrect page width", 76, helpFormatter.getPageWidth()); |
| 90 | + |
| 91 | + // test pw |
| 92 | + assertEquals("incorrect print writer", pw, helpFormatter.getPrintWriter()); |
| 93 | + |
| 94 | + // test divider |
| 95 | + assertEquals("incorrect divider", |
| 96 | + "+------------------------------------------------------------------------------+", |
| 97 | + helpFormatter.getDivider()); |
| 98 | + |
| 99 | + // test header |
| 100 | + assertEquals("incorrect header", "Jakarta Commons CLI", helpFormatter.getHeader()); |
| 101 | + |
| 102 | + // test footer |
| 103 | + assertEquals("incorrect footer", "Copyright 2003\nApache Software Foundation", |
| 104 | + helpFormatter.getFooter()); |
| 105 | + |
| 106 | + // test gutters |
| 107 | + assertEquals("incorrect left gutter", "|*", helpFormatter.getGutterLeft()); |
| 108 | + assertEquals("incorrect right gutter", "*|", helpFormatter.getGutterRight()); |
| 109 | + assertEquals("incorrect center gutter", "*-*", helpFormatter.getGutterCenter()); |
| 110 | + |
81 | 111 |
|
82 | 112 | final BufferedReader reader = |
83 | 113 | new BufferedReader(new StringReader(writer.toString())); |
@@ -397,4 +427,106 @@ public void testPad_TooShort() throws IOException { |
397 | 427 | HelpFormatter.pad("hello world", -5, writer); |
398 | 428 | assertEquals("hello world", writer.toString()); |
399 | 429 | } |
| 430 | + |
| 431 | + public void testGutters() throws IOException { |
| 432 | + helpFormatter = new HelpFormatter(null, null, null, 80); |
| 433 | + helpFormatter.setShellCommand("ant"); |
| 434 | + final Set lusage = new HashSet(); |
| 435 | + lusage.add(DisplaySetting.DISPLAY_ALIASES); |
| 436 | + lusage.add(DisplaySetting.DISPLAY_GROUP_NAME); |
| 437 | + helpFormatter.setLineUsageSettings(lusage); |
| 438 | + |
| 439 | + // test line usage |
| 440 | + assertEquals("incorrect line usage", lusage, helpFormatter.getLineUsageSettings()); |
| 441 | + |
| 442 | + final Set fusage = new HashSet(); |
| 443 | + fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN); |
| 444 | + fusage.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT); |
| 445 | + fusage.add(DisplaySetting.DISPLAY_GROUP_OUTER); |
| 446 | + fusage.add(DisplaySetting.DISPLAY_GROUP_EXPANDED); |
| 447 | + fusage.add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED); |
| 448 | + fusage.add(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED); |
| 449 | + fusage.add(DisplaySetting.DISPLAY_SWITCH_ENABLED); |
| 450 | + fusage.add(DisplaySetting.DISPLAY_SWITCH_DISABLED); |
| 451 | + fusage.add(DisplaySetting.DISPLAY_PROPERTY_OPTION); |
| 452 | + fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN); |
| 453 | + fusage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT); |
| 454 | + fusage.add(DisplaySetting.DISPLAY_OPTIONAL); |
| 455 | + helpFormatter.setFullUsageSettings(fusage); |
| 456 | + |
| 457 | + // test line usage |
| 458 | + assertEquals("incorrect full usage", fusage, helpFormatter.getFullUsageSettings()); |
| 459 | + |
| 460 | + final Set dsettings = new HashSet(); |
| 461 | + dsettings.add(DisplaySetting.DISPLAY_GROUP_NAME); |
| 462 | + dsettings.add(DisplaySetting.DISPLAY_GROUP_EXPANDED); |
| 463 | + dsettings.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT); |
| 464 | + |
| 465 | + helpFormatter.setDisplaySettings(dsettings); |
| 466 | + |
| 467 | + verbose = |
| 468 | + new DefaultOptionBuilder() |
| 469 | + .withLongName("verbose") |
| 470 | + .withDescription("print the version information and exit") |
| 471 | + .create(); |
| 472 | + |
| 473 | + options = new GroupBuilder() |
| 474 | + .withName("options") |
| 475 | + .withOption(DefaultOptionTest.buildHelpOption()) |
| 476 | + .withOption(ArgumentTest.buildTargetsArgument()) |
| 477 | + .withOption( |
| 478 | + new DefaultOptionBuilder() |
| 479 | + .withLongName("diagnostics") |
| 480 | + .withDescription("print information that might be helpful to diagnose or report problems.") |
| 481 | + .create()) |
| 482 | + .withOption( |
| 483 | + new DefaultOptionBuilder() |
| 484 | + .withLongName("projecthelp") |
| 485 | + .withDescription("print project help information") |
| 486 | + .create()) |
| 487 | + .withOption(verbose) |
| 488 | + .create(); |
| 489 | + |
| 490 | + helpFormatter.setGroup(options); |
| 491 | + |
| 492 | + // test default gutters |
| 493 | + assertEquals("incorrect left gutter", HelpFormatter.DEFAULT_GUTTER_LEFT, helpFormatter.getGutterLeft()); |
| 494 | + assertEquals("incorrect right gutter", HelpFormatter.DEFAULT_GUTTER_RIGHT, helpFormatter.getGutterRight()); |
| 495 | + assertEquals("incorrect center gutter", HelpFormatter.DEFAULT_GUTTER_CENTER, helpFormatter.getGutterCenter()); |
| 496 | + |
| 497 | + final StringWriter writer = new StringWriter(); |
| 498 | + helpFormatter.setPrintWriter(new PrintWriter(writer)); |
| 499 | + helpFormatter.print(); |
| 500 | + |
| 501 | + final BufferedReader reader = |
| 502 | + new BufferedReader(new StringReader(writer.toString())); |
| 503 | + assertEquals( |
| 504 | + "Usage: ", |
| 505 | + reader.readLine()); |
| 506 | + assertEquals( |
| 507 | + "ant [--help --diagnostics --projecthelp --verbose] [<target1> [<target2> ...]] ", |
| 508 | + reader.readLine()); |
| 509 | + assertEquals( |
| 510 | + "options ", |
| 511 | + reader.readLine()); |
| 512 | + assertEquals( |
| 513 | + " --help (-?,-h) Displays the help ", |
| 514 | + reader.readLine()); |
| 515 | + assertEquals( |
| 516 | + " --diagnostics print information that might be helpful to diagnose or ", |
| 517 | + reader.readLine()); |
| 518 | + assertEquals( |
| 519 | + " report problems. ", |
| 520 | + reader.readLine()); |
| 521 | + assertEquals( |
| 522 | + " --projecthelp print project help information ", |
| 523 | + reader.readLine()); |
| 524 | + assertEquals( |
| 525 | + " --verbose print the version information and exit ", |
| 526 | + reader.readLine()); |
| 527 | + assertEquals( |
| 528 | + " target [target ...] The targets ant should build ", |
| 529 | + reader.readLine()); |
| 530 | + assertNull(reader.readLine()); |
| 531 | + } |
400 | 532 | } |
0 commit comments