| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package org.apache.commons.cli.bug; |
| 19 |
|
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
|
| 22 |
import java.io.PrintWriter; |
| 23 |
import java.io.StringWriter; |
| 24 |
import java.sql.ParameterMetaData; |
| 25 |
import java.sql.Types; |
| 26 |
|
| 27 |
import org.apache.commons.cli.HelpFormatter; |
| 28 |
import org.apache.commons.cli.Option; |
| 29 |
import org.apache.commons.cli.OptionGroup; |
| 30 |
import org.apache.commons.cli.Options; |
| 31 |
import org.junit.Before; |
| 32 |
import org.junit.Test; |
| 33 |
|
| 34 |
public class BugCLI162Test { |
| 35 |
/** Constant for the line separator. */ |
| 36 |
private static final String CR = System.lineSeparator(); |
| 37 |
|
| 38 |
// Constants used for options |
| 39 |
private static final String OPT = "-"; |
| 40 |
|
| 41 |
private static final String OPT_COLUMN_NAMES = "l"; |
| 42 |
|
| 43 |
private static final String OPT_CONNECTION = "c"; |
| 44 |
|
| 45 |
private static final String OPT_DESCRIPTION = "e"; |
| 46 |
|
| 47 |
private static final String OPT_DRIVER = "d"; |
| 48 |
|
| 49 |
private static final String OPT_DRIVER_INFO = "n"; |
| 50 |
|
| 51 |
private static final String OPT_FILE_BINDING = "b"; |
| 52 |
|
| 53 |
private static final String OPT_FILE_JDBC = "j"; |
| 54 |
|
| 55 |
private static final String OPT_FILE_SFMD = "f"; |
| 56 |
|
| 57 |
private static final String OPT_HELP = "h"; |
| 58 |
|
| 59 |
private static final String OPT_HELP_LONG = "help"; |
| 60 |
|
| 61 |
private static final String OPT_INTERACTIVE = "i"; |
| 62 |
|
| 63 |
private static final String OPT_JDBC_TO_SFMD = "2"; |
| 64 |
|
| 65 |
private static final String OPT_JDBC_TO_SFMD_L = "jdbc2sfmd"; |
| 66 |
|
| 67 |
private static final String OPT_METADATA = "m"; |
| 68 |
|
| 69 |
private static final String OPT_PARAM_MODES_INT = "o"; |
| 70 |
|
| 71 |
private static final String OPT_PARAM_MODES_NAME = "O"; |
| 72 |
|
| 73 |
private static final String OPT_PARAM_NAMES = "a"; |
| 74 |
|
| 75 |
private static final String OPT_PARAM_TYPES_INT = "y"; |
| 76 |
|
| 77 |
private static final String OPT_PARAM_TYPES_NAME = "Y"; |
| 78 |
|
| 79 |
private static final String OPT_PASSWORD = "p"; |
| 80 |
|
| 81 |
private static final String OPT_PASSWORD_L = "password"; |
| 82 |
|
| 83 |
private static final String OPT_SQL = "s"; |
| 84 |
|
| 85 |
private static final String OPT_SQL_L = "sql"; |
| 86 |
|
| 87 |
private static final String OPT_STACK_TRACE = "t"; |
| 88 |
|
| 89 |
private static final String OPT_TIMING = "g"; |
| 90 |
|
| 91 |
private static final String OPT_TRIM_L = "trim"; |
| 92 |
|
| 93 |
private static final String OPT_USER = "u"; |
| 94 |
|
| 95 |
private static final String OPT_WRITE_TO_FILE = "w"; |
| 96 |
|
| 97 |
private static final String PMODE_IN = "IN"; |
| 98 |
|
| 99 |
private static final String PMODE_INOUT = "INOUT"; |
| 100 |
|
| 101 |
private static final String PMODE_OUT = "OUT"; |
| 102 |
|
| 103 |
private static final String PMODE_UNK = "Unknown"; |
| 104 |
|
| 105 |
private static final String PMODES = PMODE_IN + ", " + PMODE_INOUT + ", " + PMODE_OUT + ", " + PMODE_UNK; |
| 106 |
|
| 107 |
private HelpFormatter formatter; |
| 108 |
|
| 109 |
private StringWriter sw; |
| 110 |
|
| 111 |
@Before |
| 112 |
public void setUp() { |
| 113 |
formatter = new HelpFormatter(); |
| 114 |
sw = new StringWriter(); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void testInfiniteLoop() { |
| 119 |
final Options options = new Options(); |
| 120 |
options.addOption("h", "help", false, "This is a looooong description"); |
| 121 |
// used to hang & crash |
| 122 |
formatter.printHelp(new PrintWriter(sw), 20, "app", null, options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null); |
| 123 |
|
| 124 |
//@formatter:off |
| 125 |
final String expected = "usage: app" + CR + |
| 126 |
" -h,--help This is" + CR + |
| 127 |
" a" + CR + |
| 128 |
" looooon" + CR + |
| 129 |
" g" + CR + |
| 130 |
" descrip" + CR + |
| 131 |
" tion" + CR; |
| 132 |
//@formatter:on |
| 133 |
assertEquals(expected, sw.toString()); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void testLongLineChunking() { |
| 138 |
final Options options = new Options(); |
| 139 |
//@formatter:off |
| 140 |
options.addOption("x", "extralongarg", false, |
| 141 |
"This description has ReallyLongValuesThatAreLongerThanTheWidthOfTheColumns " + |
| 142 |
"and also other ReallyLongValuesThatAreHugerAndBiggerThanTheWidthOfTheColumnsBob, " + |
| 143 |
"yes. "); |
| 144 |
//@formatter:on |
| 145 |
formatter.printHelp(new PrintWriter(sw), 35, this.getClass().getName(), "Header", options, 0, 5, "Footer"); |
| 146 |
//@formatter:off |
| 147 |
final String expected = "usage:" + CR + |
| 148 |
" org.apache.commons.cli.bug.B" + CR + |
| 149 |
" ugCLI162Test" + CR + |
| 150 |
"Header" + CR + |
| 151 |
"-x,--extralongarg This" + CR + |
| 152 |
" description" + CR + |
| 153 |
" has" + CR + |
| 154 |
" ReallyLongVal" + CR + |
| 155 |
" uesThatAreLon" + CR + |
| 156 |
" gerThanTheWid" + CR + |
| 157 |
" thOfTheColumn" + CR + |
| 158 |
" s and also" + CR + |
| 159 |
" other" + CR + |
| 160 |
" ReallyLongVal" + CR + |
| 161 |
" uesThatAreHug" + CR + |
| 162 |
" erAndBiggerTh" + CR + |
| 163 |
" anTheWidthOfT" + CR + |
| 164 |
" heColumnsBob," + CR + |
| 165 |
" yes." + CR + |
| 166 |
"Footer" + CR; |
| 167 |
//@formatter:on |
| 168 |
assertEquals("Long arguments did not split as expected", expected, sw.toString()); |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void testLongLineChunkingIndentIgnored() { |
| 173 |
final Options options = new Options(); |
| 174 |
options.addOption("x", "extralongarg", false, "This description is Long."); |
| 175 |
formatter.printHelp(new PrintWriter(sw), 22, this.getClass().getName(), "Header", options, 0, 5, "Footer"); |
| 176 |
//@formatter:off |
| 177 |
final String expected = "usage:" + CR + |
| 178 |
" org.apache.comm" + CR + |
| 179 |
" ons.cli.bug.Bug" + CR + |
| 180 |
" CLI162Test" + CR + |
| 181 |
"Header" + CR + |
| 182 |
"-x,--extralongarg" + CR + |
| 183 |
" This description is" + CR + |
| 184 |
" Long." + CR + |
| 185 |
"Footer" + CR; |
| 186 |
//@formatter:on |
| 187 |
assertEquals("Long arguments did not split as expected", expected, sw.toString()); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void testPrintHelpLongLines() { |
| 192 |
// Options build |
| 193 |
final Options commandLineOptions; |
| 194 |
commandLineOptions = new Options(); |
| 195 |
commandLineOptions.addOption(OPT_HELP, OPT_HELP_LONG, false, "Prints help and quits"); |
| 196 |
commandLineOptions.addOption(OPT_DRIVER, "driver", true, "JDBC driver class name"); |
| 197 |
//@formatter:off |
| 198 |
commandLineOptions.addOption(OPT_DRIVER_INFO, "info", false, "Prints driver information and properties. If " |
| 199 |
+ OPT |
| 200 |
+ OPT_CONNECTION |
| 201 |
+ " is not specified, all drivers on the classpath are displayed."); |
| 202 |
//@formatter:on |
| 203 |
commandLineOptions.addOption(OPT_CONNECTION, "url", true, "Connection URL"); |
| 204 |
commandLineOptions.addOption(OPT_USER, "user", true, "A database user name"); |
| 205 |
//@formatter:off |
| 206 |
commandLineOptions |
| 207 |
.addOption( |
| 208 |
OPT_PASSWORD, |
| 209 |
OPT_PASSWORD_L, |
| 210 |
true, |
| 211 |
"The database password for the user specified with the " |
| 212 |
+ OPT |
| 213 |
+ OPT_USER |
| 214 |
+ " option. You can obfuscate the password with org.mortbay.jetty.security.Password," |
| 215 |
+ " see http://docs.codehaus.org/display/JETTY/Securing+Passwords"); |
| 216 |
//@formatter:on |
| 217 |
commandLineOptions.addOption(OPT_SQL, OPT_SQL_L, true, "Runs SQL or {call stored_procedure(?, ?)} or {?=call function(?, ?)}"); |
| 218 |
commandLineOptions.addOption(OPT_FILE_SFMD, "sfmd", true, "Writes a SFMD file for the given SQL"); |
| 219 |
commandLineOptions.addOption(OPT_FILE_BINDING, "jdbc", true, "Writes a JDBC binding node file for the given SQL"); |
| 220 |
commandLineOptions.addOption(OPT_FILE_JDBC, "node", true, "Writes a JDBC node file for the given SQL (internal debugging)"); |
| 221 |
commandLineOptions.addOption(OPT_WRITE_TO_FILE, "outfile", true, "Writes the SQL output to the given file"); |
| 222 |
commandLineOptions.addOption(OPT_DESCRIPTION, "description", true, |
| 223 |
"SFMD description. A default description is used if omited. Example: " + OPT + OPT_DESCRIPTION + " \"Runs such and such\""); |
| 224 |
commandLineOptions.addOption(OPT_INTERACTIVE, "interactive", false, |
| 225 |
"Runs in interactive mode, reading and writing from the console, 'go' or '/' sends a statement"); |
| 226 |
commandLineOptions.addOption(OPT_TIMING, "printTiming", false, "Prints timing information"); |
| 227 |
commandLineOptions.addOption(OPT_METADATA, "printMetaData", false, "Prints metadata information"); |
| 228 |
commandLineOptions.addOption(OPT_STACK_TRACE, "printStack", false, "Prints stack traces on errors"); |
| 229 |
//@formatter:off |
| 230 |
Option option = new Option(OPT_COLUMN_NAMES, "columnNames", true, "Column XML names; default names column labels. Example: " |
| 231 |
+ OPT |
| 232 |
+ OPT_COLUMN_NAMES |
| 233 |
+ " \"cname1 cname2\""); |
| 234 |
//@formatter:on |
| 235 |
commandLineOptions.addOption(option); |
| 236 |
//@formatter:off |
| 237 |
option = new Option(OPT_PARAM_NAMES, "paramNames", true, "Parameter XML names; default names are param1, param2, etc. Example: " |
| 238 |
+ OPT |
| 239 |
+ OPT_PARAM_NAMES |
| 240 |
+ " \"pname1 pname2\""); |
| 241 |
//@formatter:on |
| 242 |
commandLineOptions.addOption(option); |
| 243 |
// |
| 244 |
final OptionGroup pOutTypesOptionGroup = new OptionGroup(); |
| 245 |
final String pOutTypesOptionGroupDoc = OPT + OPT_PARAM_TYPES_INT + " and " + OPT + OPT_PARAM_TYPES_NAME + " are mutually exclusive."; |
| 246 |
final String typesClassName = Types.class.getName(); |
| 247 |
//@formatter:off |
| 248 |
option = new Option(OPT_PARAM_TYPES_INT, "paramTypes", true, "Parameter types from " |
| 249 |
+ typesClassName |
| 250 |
+ ". " |
| 251 |
+ pOutTypesOptionGroupDoc |
| 252 |
+ " Example: " |
| 253 |
+ OPT |
| 254 |
+ OPT_PARAM_TYPES_INT |
| 255 |
+ " \"-10 12\""); |
| 256 |
//@formatter:on |
| 257 |
commandLineOptions.addOption(option); |
| 258 |
//@formatter:off |
| 259 |
option = new Option(OPT_PARAM_TYPES_NAME, "paramTypeNames", true, "Parameter " |
| 260 |
+ typesClassName |
| 261 |
+ " names. " |
| 262 |
+ pOutTypesOptionGroupDoc |
| 263 |
+ " Example: " |
| 264 |
+ OPT |
| 265 |
+ OPT_PARAM_TYPES_NAME |
| 266 |
+ " \"CURSOR VARCHAR\""); |
| 267 |
//@formatter:on |
| 268 |
commandLineOptions.addOption(option); |
| 269 |
commandLineOptions.addOptionGroup(pOutTypesOptionGroup); |
| 270 |
// |
| 271 |
final OptionGroup modesOptionGroup = new OptionGroup(); |
| 272 |
final String modesOptionGroupDoc = OPT + OPT_PARAM_MODES_INT + " and " + OPT + OPT_PARAM_MODES_NAME + " are mutually exclusive."; |
| 273 |
//@formatter:off |
| 274 |
option = new Option(OPT_PARAM_MODES_INT, "paramModes", true, "Parameters modes (" |
| 275 |
+ ParameterMetaData.parameterModeIn |
| 276 |
+ "=IN, " |
| 277 |
+ ParameterMetaData.parameterModeInOut |
| 278 |
+ "=INOUT, " |
| 279 |
+ ParameterMetaData.parameterModeOut |
| 280 |
+ "=OUT, " |
| 281 |
+ ParameterMetaData.parameterModeUnknown |
| 282 |
+ "=Unknown" |
| 283 |
+ "). " |
| 284 |
+ modesOptionGroupDoc |
| 285 |
+ " Example for 2 parameters, OUT and IN: " |
| 286 |
+ OPT |
| 287 |
+ OPT_PARAM_MODES_INT |
| 288 |
+ " \"" |
| 289 |
+ ParameterMetaData.parameterModeOut |
| 290 |
+ " " |
| 291 |
+ ParameterMetaData.parameterModeIn |
| 292 |
+ "\""); |
| 293 |
//@formatter:on |
| 294 |
modesOptionGroup.addOption(option); |
| 295 |
//@formatter:off |
| 296 |
option = new Option(OPT_PARAM_MODES_NAME, "paramModeNames", true, "Parameters mode names (" |
| 297 |
+ PMODES |
| 298 |
+ "). " |
| 299 |
+ modesOptionGroupDoc |
| 300 |
+ " Example for 2 parameters, OUT and IN: " |
| 301 |
+ OPT |
| 302 |
+ OPT_PARAM_MODES_NAME |
| 303 |
+ " \"" |
| 304 |
+ PMODE_OUT |
| 305 |
+ " " |
| 306 |
+ PMODE_IN |
| 307 |
+ "\""); |
| 308 |
//@formatter:on |
| 309 |
modesOptionGroup.addOption(option); |
| 310 |
commandLineOptions.addOptionGroup(modesOptionGroup); |
| 311 |
option = new Option(null, OPT_TRIM_L, true, |
| 312 |
"Trims leading and trailing spaces from all column values. Column XML names can be optionally specified to set which columns to trim."); |
| 313 |
option.setOptionalArg(true); |
| 314 |
commandLineOptions.addOption(option); |
| 315 |
option = new Option(OPT_JDBC_TO_SFMD, OPT_JDBC_TO_SFMD_L, true, |
| 316 |
"Converts the JDBC file in the first argument to an SMFD file specified in the second argument."); |
| 317 |
option.setArgs(2); |
| 318 |
commandLineOptions.addOption(option); |
| 319 |
|
| 320 |
formatter.printHelp(new PrintWriter(sw), HelpFormatter.DEFAULT_WIDTH, this.getClass().getName(), null, commandLineOptions, |
| 321 |
HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null); |
| 322 |
//@formatter:off |
| 323 |
final String expected = "usage: org.apache.commons.cli.bug.BugCLI162Test" + CR + |
| 324 |
" -2,--jdbc2sfmd <arg> Converts the JDBC file in the first argument" + CR + |
| 325 |
" to an SMFD file specified in the second" + CR + |
| 326 |
" argument." + CR + |
| 327 |
" -a,--paramNames <arg> Parameter XML names; default names are" + CR + |
| 328 |
" param1, param2, etc. Example: -a \"pname1" + CR + |
| 329 |
" pname2\"" + CR + |
| 330 |
" -b,--jdbc <arg> Writes a JDBC binding node file for the given" + CR + |
| 331 |
" SQL" + CR + |
| 332 |
" -c,--url <arg> Connection URL" + CR + |
| 333 |
" -d,--driver <arg> JDBC driver class name" + CR + |
| 334 |
" -e,--description <arg> SFMD description. A default description is" + CR + |
| 335 |
" used if omited. Example: -e \"Runs such and" + CR + |
| 336 |
" such\"" + CR + |
| 337 |
" -f,--sfmd <arg> Writes a SFMD file for the given SQL" + CR + |
| 338 |
" -g,--printTiming Prints timing information" + CR + |
| 339 |
" -h,--help Prints help and quits" + CR + |
| 340 |
" -i,--interactive Runs in interactive mode, reading and writing" + CR + |
| 341 |
" from the console, 'go' or '/' sends a" + CR + |
| 342 |
" statement" + CR + |
| 343 |
" -j,--node <arg> Writes a JDBC node file for the given SQL" + CR + |
| 344 |
" (internal debugging)" + CR + |
| 345 |
" -l,--columnNames <arg> Column XML names; default names column" + CR + |
| 346 |
" labels. Example: -l \"cname1 cname2\"" + CR + |
| 347 |
" -m,--printMetaData Prints metadata information" + CR + |
| 348 |
" -n,--info Prints driver information and properties. If" + CR + |
| 349 |
" -c is not specified, all drivers on the" + CR + |
| 350 |
" classpath are displayed." + CR + |
| 351 |
" -o,--paramModes <arg> Parameters modes (1=IN, 2=INOUT, 4=OUT," + CR + |
| 352 |
" 0=Unknown). -o and -O are mutually exclusive." + CR + |
| 353 |
" Example for 2 parameters, OUT and IN: -o \"4" + CR + |
| 354 |
" 1\"" + CR + |
| 355 |
" -O,--paramModeNames <arg> Parameters mode names (IN, INOUT, OUT," + CR + |
| 356 |
" Unknown). -o and -O are mutually exclusive." + CR + |
| 357 |
" Example for 2 parameters, OUT and IN: -O \"OUT" + CR + |
| 358 |
" IN\"" + CR + |
| 359 |
" -p,--password <arg> The database password for the user specified" + CR + |
| 360 |
" with the -u option. You can obfuscate the" + CR + |
| 361 |
" password with" + CR + |
| 362 |
" org.mortbay.jetty.security.Password, see" + CR + |
| 363 |
" http://docs.codehaus.org/display/JETTY/Securi" + CR + |
| 364 |
" ng+Passwords" + CR + |
| 365 |
" -s,--sql <arg> Runs SQL or {call stored_procedure(?, ?)} or" + CR + |
| 366 |
" {?=call function(?, ?)}" + CR + |
| 367 |
" -t,--printStack Prints stack traces on errors" + CR + |
| 368 |
" --trim <arg> Trims leading and trailing spaces from all" + CR + |
| 369 |
" column values. Column XML names can be" + CR + |
| 370 |
" optionally specified to set which columns to" + CR + |
| 371 |
" trim." + CR + |
| 372 |
" -u,--user <arg> A database user name" + CR + |
| 373 |
" -w,--outfile <arg> Writes the SQL output to the given file" + CR + |
| 374 |
" -y,--paramTypes <arg> Parameter types from java.sql.Types. -y and" + CR + |
| 375 |
" -Y are mutually exclusive. Example: -y \"-10" + CR + |
| 376 |
" 12\"" + CR + |
| 377 |
" -Y,--paramTypeNames <arg> Parameter java.sql.Types names. -y and -Y are" + CR + |
| 378 |
" mutually exclusive. Example: -Y \"CURSOR" + CR + |
| 379 |
" VARCHAR\"" + CR; |
| 380 |
//@formatter:on |
| 381 |
assertEquals(expected, sw.toString()); |
| 382 |
} |
| 383 |
|
| 384 |
} |