2424
2525import junit .framework .TestCase ;
2626
27+ import org .apache .commons .cli2 .Group ;
2728import org .apache .commons .cli2 .Option ;
2829import org .apache .commons .cli2 .OptionException ;
2930import org .apache .commons .cli2 .builder .DefaultOptionBuilder ;
3435public class HelpFormatterTest extends TestCase {
3536 private HelpFormatter helpFormatter ;
3637 private Option verbose ;
38+ private Group options ;
3739
3840 public void setUp () {
3941 helpFormatter = new HelpFormatter ("|*" , "*-*" , "*|" , 80 );
@@ -49,23 +51,24 @@ public void setUp() {
4951 .withDescription ("print the version information and exit" )
5052 .create ();
5153
52- helpFormatter .setGroup (
53- new GroupBuilder ()
54- .withName ("options" )
55- .withOption (DefaultOptionTest .buildHelpOption ())
56- .withOption (ArgumentTest .buildTargetsArgument ())
57- .withOption (
58- new DefaultOptionBuilder ()
59- .withLongName ("diagnostics" )
60- .withDescription ("print information that might be helpful to diagnose or report problems." )
61- .create ())
62- .withOption (
63- new DefaultOptionBuilder ()
64- .withLongName ("projecthelp" )
65- .withDescription ("print project help information" )
66- .create ())
67- .withOption (verbose )
68- .create ());
54+ options = new GroupBuilder ()
55+ .withName ("options" )
56+ .withOption (DefaultOptionTest .buildHelpOption ())
57+ .withOption (ArgumentTest .buildTargetsArgument ())
58+ .withOption (
59+ new DefaultOptionBuilder ()
60+ .withLongName ("diagnostics" )
61+ .withDescription ("print information that might be helpful to diagnose or report problems." )
62+ .create ())
63+ .withOption (
64+ new DefaultOptionBuilder ()
65+ .withLongName ("projecthelp" )
66+ .withDescription ("print project help information" )
67+ .create ())
68+ .withOption (verbose )
69+ .create ();
70+
71+ helpFormatter .setGroup (options );
6972 }
7073
7174 public void testPrint () throws IOException {
@@ -190,6 +193,27 @@ public void testPrintHelp_WithException() throws IOException {
190193 assertNull (reader .readLine ());
191194 }
192195
196+ public void testPrintHelp_TooNarrow () throws IOException {
197+ final StringWriter writer = new StringWriter ();
198+ helpFormatter = new HelpFormatter ("<" ,"=" ,">" ,4 );
199+ helpFormatter .setGroup (options );
200+ helpFormatter .setPrintWriter (new PrintWriter (writer ));
201+ helpFormatter .printHelp ();
202+ System .out .println (writer );
203+ final BufferedReader reader =
204+ new BufferedReader (new StringReader (writer .toString ()));
205+ assertEquals (
206+ "<options = >" ,
207+ reader .readLine ());
208+ assertEquals (
209+ "< --help (-?,-h) =D>" ,
210+ reader .readLine ());
211+ assertEquals (
212+ "< =i>" ,
213+ reader .readLine ());
214+ // lots more lines unchecked
215+ }
216+
193217 public void testPrintException () throws IOException {
194218 final StringWriter writer = new StringWriter ();
195219 helpFormatter .setPrintWriter (new PrintWriter (writer ));
@@ -322,6 +346,16 @@ public void testWrap_NewLine() {
322346 assertEquals ("" , i .next ());
323347 assertFalse (i .hasNext ());
324348 }
349+
350+ public void testWrap_Below1Length () {
351+ try {
352+ HelpFormatter .wrap ("Apache Software Foundation" ,-1 );
353+ fail ("IllegalArgumentException" );
354+ }
355+ catch (IllegalArgumentException e ) {
356+ assertEquals ("width must be positive" ,e .getMessage ());
357+ }
358+ }
325359
326360 public void testPad () throws IOException {
327361 final StringWriter writer = new StringWriter ();
@@ -340,4 +374,10 @@ public void testPad_TooLong() throws IOException {
340374 HelpFormatter .pad ("hello world" , 10 , writer );
341375 assertEquals ("hello world" , writer .toString ());
342376 }
377+
378+ public void testPad_TooShort () throws IOException {
379+ final StringWriter writer = new StringWriter ();
380+ HelpFormatter .pad ("hello world" , -5 , writer );
381+ assertEquals ("hello world" , writer .toString ());
382+ }
343383}
0 commit comments