Author: britter
Date: Sun Mar 29 09:34:51 2015
New Revision: 1669880
URL: http://svn.apache.org/r1669880
Log:
Tests sould not write to std out, but instead should verify return values for
correctness
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI162Test.java
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI162Test.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI162Test.java?rev=1669880&r1=1669879&r2=1669880&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI162Test.java
(original)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI162Test.java
Sun Mar 29 09:34:51 2015
@@ -28,20 +28,39 @@ import org.apache.commons.cli.HelpFormat
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
+import org.junit.Before;
import org.junit.Test;
public class BugCLI162Test
{
/** Constant for the line separator.*/
private static final String CR = System.getProperty("line.separator");
+
+ private HelpFormatter formatter;
+ private StringWriter sw;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ formatter = new HelpFormatter();
+ sw = new StringWriter();
+ }
@Test
public void testInfiniteLoop() {
Options options = new Options();
options.addOption("h", "help", false, "This is a looooong
description");
- HelpFormatter formatter = new HelpFormatter();
- formatter.setWidth(20);
- formatter.printHelp("app", options); // used to hang & crash
+ // used to hang & crash
+ formatter.printHelp(new PrintWriter(sw), 20, "app", null, options,
HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
+
+ String expected = "usage: app" + CR +
+ " -h,--help This is" + CR +
+ " a" + CR +
+ " looooon" + CR +
+ " g" + CR +
+ " descrip" + CR +
+ " tion" + CR;
+ assertEquals(expected, sw.toString());
}
@Test
@@ -229,7 +248,66 @@ public class BugCLI162Test
"Converts the JDBC file in the first argument to an SMFD file
specified in the second argument.");
option.setArgs(2);
commandLineOptions.addOption(option);
- new HelpFormatter().printHelp(this.getClass().getName(),
commandLineOptions);
+
+ formatter.printHelp(new PrintWriter(sw), HelpFormatter.DEFAULT_WIDTH,
this.getClass().getName(), null, commandLineOptions,
HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
+ String expected = "usage: org.apache.commons.cli.bug.BugCLI162Test" +
CR +
+ " -2,--jdbc2sfmd <arg> Converts the JDBC file in the
first argument" + CR +
+ " to an SMFD file specified in the
second" + CR +
+ " argument." + CR +
+ " -a,--paramNames <arg> Parameter XML names; default
names are" + CR +
+ " param1, param2, etc. Example: -a
\"pname1" + CR +
+ " pname2\"" + CR +
+ " -b,--jdbc <arg> Writes a JDBC binding node file
for the given" + CR +
+ " SQL" + CR +
+ " -c,--url <arg> Connection URL" + CR +
+ " -d,--driver <arg> JDBC driver class name" + CR +
+ " -e,--description <arg> SFMD description. A default
description is" + CR +
+ " used if omited. Example: -e
\"Runs such and" + CR +
+ " such\"" + CR +
+ " -f,--sfmd <arg> Writes a SFMD file for the given
SQL" + CR +
+ " -g,--printTiming Prints timing information" + CR +
+ " -h,--help Prints help and quits" + CR +
+ " -i,--interactive Runs in interactive mode,
reading and writing" + CR +
+ " from the console, 'go' or '/'
sends a" + CR +
+ " statement" + CR +
+ " -j,--node <arg> Writes a JDBC node file for the
given SQL" + CR +
+ " (internal debugging)" + CR +
+ " -l,--columnNames <arg> Column XML names; default names
column" + CR +
+ " labels. Example: -l \"cname1
cname2\"" + CR +
+ " -m,--printMetaData Prints metadata information" +
CR +
+ " -n,--info Prints driver information and
properties. If" + CR +
+ " -c is not specified, all drivers
on the" + CR +
+ " classpath are displayed." + CR +
+ " -o,--paramModes <arg> Parameters modes (1=IN, 2=INOUT,
4=OUT," + CR +
+ " 0=Unknown). -o and -O are
mutually exclusive." + CR +
+ " Example for 2 parameters, OUT
and IN: -o \"4" + CR +
+ " 1\"" + CR +
+ " -O,--paramModeNames <arg> Parameters mode names (IN,
INOUT, OUT," + CR +
+ " Unknown). -o and -O are mutually
exclusive." + CR +
+ " Example for 2 parameters, OUT
and IN: -O \"OUT" + CR +
+ " IN\"" + CR +
+ " -p,--password <arg> The database password for the
user specified" + CR +
+ " with the -u option. You can
obfuscate the" + CR +
+ " password with" + CR +
+ "
org.mortbay.jetty.security.Password, see" + CR +
+ "
http://docs.codehaus.org/display/JETTY/Securi" + CR +
+ " ng+Passwords" + CR +
+ " -s,--sql <arg> Runs SQL or {call
stored_procedure(?, ?)} or" + CR +
+ " {?=call function(?, ?)}" + CR +
+ " -t,--printStack Prints stack traces on errors" +
CR +
+ " --trim <arg> Trims leading and trailing
spaces from all" + CR +
+ " column values. Column XML names
can be" + CR +
+ " optionally specified to set
which columns to" + CR +
+ " trim." + CR +
+ " -u,--user <arg> A database user name" + CR +
+ " -w,--outfile <arg> Writes the SQL output to the
given file" + CR +
+ " -y,--paramTypes <arg> Parameter types from
java.sql.Types. -y and" + CR +
+ " -Y are mutually exclusive.
Example: -y \"-10" + CR +
+ " 12\"" + CR +
+ " -Y,--paramTypeNames <arg> Parameter java.sql.Types names.
-y and -Y are" + CR +
+ " mutually exclusive. Example: -Y
\"CURSOR" + CR +
+ " VARCHAR\"" + CR;
+ assertEquals(expected, sw.toString());
}
@Test
@@ -239,8 +317,7 @@ public class BugCLI162Test
"This description has
ReallyLongValuesThatAreLongerThanTheWidthOfTheColumns " +
"and also other
ReallyLongValuesThatAreHugerAndBiggerThanTheWidthOfTheColumnsBob, " +
"yes. ");
- HelpFormatter formatter = new HelpFormatter();
- StringWriter sw = new StringWriter();
+
formatter.printHelp(new PrintWriter(sw), 35,
this.getClass().getName(), "Header", options, 0, 5, "Footer");
String expected = "usage:" + CR +
" org.apache.commons.cli.bug.B" + CR +
@@ -269,10 +346,8 @@ public class BugCLI162Test
public void testLongLineChunkingIndentIgnored() {
Options options = new Options();
options.addOption("x", "extralongarg", false, "This description is
Long." );
- HelpFormatter formatter = new HelpFormatter();
- StringWriter sw = new StringWriter();
+
formatter.printHelp(new PrintWriter(sw), 22,
this.getClass().getName(), "Header", options, 0, 5, "Footer");
- System.err.println(sw.toString());
String expected = "usage:" + CR +
" org.apache.comm" + CR +
" ons.cli.bug.Bug" + CR +