|
15 | 15 | */ |
16 | 16 | package org.apache.commons.cli; |
17 | 17 |
|
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.PrintStream; |
| 20 | +import java.io.StringWriter; |
| 21 | + |
18 | 22 | import junit.framework.Test; |
19 | 23 | import junit.framework.TestCase; |
20 | 24 | import junit.framework.TestSuite; |
@@ -274,12 +278,30 @@ public void test13666() { |
274 | 278 | .hasArg() |
275 | 279 | .create( 'd' ); |
276 | 280 | options.addOption( dir ); |
277 | | - try { |
278 | | - HelpFormatter formatter = new HelpFormatter(); |
279 | | - formatter.printHelp( "dir", options ); |
| 281 | + |
| 282 | + |
| 283 | + final PrintStream oldSystemOut = System.out; |
| 284 | + try{ |
| 285 | + final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 286 | + final PrintStream print = new PrintStream(bytes); |
| 287 | + |
| 288 | + // capture this platform's eol symbol |
| 289 | + print.println(); |
| 290 | + final String eol = bytes.toString(); |
| 291 | + bytes.reset(); |
| 292 | + |
| 293 | + System.setOut(new PrintStream(bytes)); |
| 294 | + try { |
| 295 | + HelpFormatter formatter = new HelpFormatter(); |
| 296 | + formatter.printHelp( "dir", options ); |
| 297 | + } |
| 298 | + catch( Exception exp ) { |
| 299 | + fail( "Unexpected Exception: " + exp.getMessage() ); |
| 300 | + } |
| 301 | + assertEquals("usage: dir"+eol+" -d <arg> dir"+eol,bytes.toString()); |
280 | 302 | } |
281 | | - catch( Exception exp ) { |
282 | | - fail( "Unexpected Exception: " + exp.getMessage() ); |
| 303 | + finally { |
| 304 | + System.setOut(oldSystemOut); |
283 | 305 | } |
284 | 306 | } |
285 | 307 |
|
|
0 commit comments