Skip to content

Commit b207a2b

Browse files
committed
Tests sould not write to std out, but instead should verify return values for correctness
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1669880 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6404901 commit b207a2b

1 file changed

Lines changed: 84 additions & 9 deletions

File tree

src/test/java/org/apache/commons/cli/bug/BugCLI162Test.java

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,39 @@
2828
import org.apache.commons.cli.Option;
2929
import org.apache.commons.cli.OptionGroup;
3030
import org.apache.commons.cli.Options;
31+
import org.junit.Before;
3132
import org.junit.Test;
3233

3334
public class BugCLI162Test
3435
{
3536
/** Constant for the line separator.*/
3637
private static final String CR = System.getProperty("line.separator");
38+
39+
private HelpFormatter formatter;
40+
private StringWriter sw;
41+
42+
@Before
43+
public void setUp() throws Exception
44+
{
45+
formatter = new HelpFormatter();
46+
sw = new StringWriter();
47+
}
3748

3849
@Test
3950
public void testInfiniteLoop() {
4051
Options options = new Options();
4152
options.addOption("h", "help", false, "This is a looooong description");
42-
HelpFormatter formatter = new HelpFormatter();
43-
formatter.setWidth(20);
44-
formatter.printHelp("app", options); // used to hang & crash
53+
// used to hang & crash
54+
formatter.printHelp(new PrintWriter(sw), 20, "app", null, options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
55+
56+
String expected = "usage: app" + CR +
57+
" -h,--help This is" + CR +
58+
" a" + CR +
59+
" looooon" + CR +
60+
" g" + CR +
61+
" descrip" + CR +
62+
" tion" + CR;
63+
assertEquals(expected, sw.toString());
4564
}
4665

4766
@Test
@@ -229,7 +248,66 @@ public void testPrintHelpLongLines() {
229248
"Converts the JDBC file in the first argument to an SMFD file specified in the second argument.");
230249
option.setArgs(2);
231250
commandLineOptions.addOption(option);
232-
new HelpFormatter().printHelp(this.getClass().getName(), commandLineOptions);
251+
252+
formatter.printHelp(new PrintWriter(sw), HelpFormatter.DEFAULT_WIDTH, this.getClass().getName(), null, commandLineOptions, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
253+
String expected = "usage: org.apache.commons.cli.bug.BugCLI162Test" + CR +
254+
" -2,--jdbc2sfmd <arg> Converts the JDBC file in the first argument" + CR +
255+
" to an SMFD file specified in the second" + CR +
256+
" argument." + CR +
257+
" -a,--paramNames <arg> Parameter XML names; default names are" + CR +
258+
" param1, param2, etc. Example: -a \"pname1" + CR +
259+
" pname2\"" + CR +
260+
" -b,--jdbc <arg> Writes a JDBC binding node file for the given" + CR +
261+
" SQL" + CR +
262+
" -c,--url <arg> Connection URL" + CR +
263+
" -d,--driver <arg> JDBC driver class name" + CR +
264+
" -e,--description <arg> SFMD description. A default description is" + CR +
265+
" used if omited. Example: -e \"Runs such and" + CR +
266+
" such\"" + CR +
267+
" -f,--sfmd <arg> Writes a SFMD file for the given SQL" + CR +
268+
" -g,--printTiming Prints timing information" + CR +
269+
" -h,--help Prints help and quits" + CR +
270+
" -i,--interactive Runs in interactive mode, reading and writing" + CR +
271+
" from the console, 'go' or '/' sends a" + CR +
272+
" statement" + CR +
273+
" -j,--node <arg> Writes a JDBC node file for the given SQL" + CR +
274+
" (internal debugging)" + CR +
275+
" -l,--columnNames <arg> Column XML names; default names column" + CR +
276+
" labels. Example: -l \"cname1 cname2\"" + CR +
277+
" -m,--printMetaData Prints metadata information" + CR +
278+
" -n,--info Prints driver information and properties. If" + CR +
279+
" -c is not specified, all drivers on the" + CR +
280+
" classpath are displayed." + CR +
281+
" -o,--paramModes <arg> Parameters modes (1=IN, 2=INOUT, 4=OUT," + CR +
282+
" 0=Unknown). -o and -O are mutually exclusive." + CR +
283+
" Example for 2 parameters, OUT and IN: -o \"4" + CR +
284+
" 1\"" + CR +
285+
" -O,--paramModeNames <arg> Parameters mode names (IN, INOUT, OUT," + CR +
286+
" Unknown). -o and -O are mutually exclusive." + CR +
287+
" Example for 2 parameters, OUT and IN: -O \"OUT" + CR +
288+
" IN\"" + CR +
289+
" -p,--password <arg> The database password for the user specified" + CR +
290+
" with the -u option. You can obfuscate the" + CR +
291+
" password with" + CR +
292+
" org.mortbay.jetty.security.Password, see" + CR +
293+
" http://docs.codehaus.org/display/JETTY/Securi" + CR +
294+
" ng+Passwords" + CR +
295+
" -s,--sql <arg> Runs SQL or {call stored_procedure(?, ?)} or" + CR +
296+
" {?=call function(?, ?)}" + CR +
297+
" -t,--printStack Prints stack traces on errors" + CR +
298+
" --trim <arg> Trims leading and trailing spaces from all" + CR +
299+
" column values. Column XML names can be" + CR +
300+
" optionally specified to set which columns to" + CR +
301+
" trim." + CR +
302+
" -u,--user <arg> A database user name" + CR +
303+
" -w,--outfile <arg> Writes the SQL output to the given file" + CR +
304+
" -y,--paramTypes <arg> Parameter types from java.sql.Types. -y and" + CR +
305+
" -Y are mutually exclusive. Example: -y \"-10" + CR +
306+
" 12\"" + CR +
307+
" -Y,--paramTypeNames <arg> Parameter java.sql.Types names. -y and -Y are" + CR +
308+
" mutually exclusive. Example: -Y \"CURSOR" + CR +
309+
" VARCHAR\"" + CR;
310+
assertEquals(expected, sw.toString());
233311
}
234312

235313
@Test
@@ -239,8 +317,7 @@ public void testLongLineChunking() {
239317
"This description has ReallyLongValuesThatAreLongerThanTheWidthOfTheColumns " +
240318
"and also other ReallyLongValuesThatAreHugerAndBiggerThanTheWidthOfTheColumnsBob, " +
241319
"yes. ");
242-
HelpFormatter formatter = new HelpFormatter();
243-
StringWriter sw = new StringWriter();
320+
244321
formatter.printHelp(new PrintWriter(sw), 35, this.getClass().getName(), "Header", options, 0, 5, "Footer");
245322
String expected = "usage:" + CR +
246323
" org.apache.commons.cli.bug.B" + CR +
@@ -269,10 +346,8 @@ public void testLongLineChunking() {
269346
public void testLongLineChunkingIndentIgnored() {
270347
Options options = new Options();
271348
options.addOption("x", "extralongarg", false, "This description is Long." );
272-
HelpFormatter formatter = new HelpFormatter();
273-
StringWriter sw = new StringWriter();
349+
274350
formatter.printHelp(new PrintWriter(sw), 22, this.getClass().getName(), "Header", options, 0, 5, "Footer");
275-
System.err.println(sw.toString());
276351
String expected = "usage:" + CR +
277352
" org.apache.comm" + CR +
278353
" ons.cli.bug.Bug" + CR +

0 commit comments

Comments
 (0)