1818
1919package org .apache .commons .cli .bug ;
2020
21- import org .apache .commons .cli .CommandLine ;
22- import org .apache .commons .cli .CommandLineParser ;
21+ import java .io .IOException ;
22+ import java .sql .ParameterMetaData ;
23+ import java .sql .Types ;
24+
25+ import junit .framework .TestCase ;
26+
2327import org .apache .commons .cli .HelpFormatter ;
24- import org .apache .commons .cli .MissingArgumentException ;
2528import org .apache .commons .cli .Option ;
29+ import org .apache .commons .cli .OptionGroup ;
2630import org .apache .commons .cli .Options ;
27- import org .apache .commons .cli .PosixParser ;
28-
29- import junit .framework .TestCase ;
31+ import org .apache .commons .cli .ParseException ;
3032
3133public class BugCLI162Test extends TestCase {
3234
@@ -46,5 +48,196 @@ public void testInfiniteLoop() {
4648 assertTrue (re .getMessage ().startsWith ("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: " ));
4749 }
4850 }
51+
52+ private void testPrintHelp (Options options ) throws ParseException , IOException {
53+ new HelpFormatter ().printHelp (this .getClass ().getName (), options );
54+ }
55+
56+ public void testPrintHelpLongLines () throws ParseException , IOException {
57+ // Constants used for options
58+ final String OPT = "-" ;
59+
60+ final String OPT_COLUMN_NAMES = "l" ;
61+
62+ final String OPT_CONNECTION = "c" ;
63+
64+ final String OPT_DESCRIPTION = "e" ;
65+
66+ final String OPT_DRIVER = "d" ;
67+
68+ final String OPT_DRIVER_INFO = "n" ;
69+
70+ final String OPT_FILE_BINDING = "b" ;
71+
72+ final String OPT_FILE_JDBC = "j" ;
73+
74+ final String OPT_FILE_SFMD = "f" ;
75+
76+ final String OPT_HELP = "h" ;
77+
78+ final String OPT_HELP_ = "help" ;
79+
80+ final String OPT_INTERACTIVE = "i" ;
81+
82+ final String OPT_JDBC_TO_SFMD = "2" ;
83+
84+ final String OPT_JDBC_TO_SFMD_L = "jdbc2sfmd" ;
85+
86+ final String OPT_METADATA = "m" ;
87+
88+ final String OPT_PARAM_MODES_INT = "o" ;
89+
90+ final String OPT_PARAM_MODES_NAME = "O" ;
91+
92+ final String OPT_PARAM_NAMES = "a" ;
93+
94+ final String OPT_PARAM_TYPES_INT = "y" ;
95+
96+ final String OPT_PARAM_TYPES_NAME = "Y" ;
97+
98+ final String OPT_PASSWORD = "p" ;
99+
100+ final String OPT_PASSWORD_L = "password" ;
101+
102+ final String OPT_SQL = "s" ;
103+
104+ final String OPT_SQL_L = "sql" ;
105+
106+ final String OPT_SQL_SPLIT_DEFAULT = "###" ;
107+
108+ final String OPT_SQL_SPLIT_L = "splitSql" ;
109+
110+ final String OPT_STACK_TRACE = "t" ;
111+
112+ final String OPT_TIMING = "g" ;
113+
114+ final String OPT_TRIM_L = "trim" ;
115+
116+ final String OPT_USER = "u" ;
117+
118+ final String OPT_WRITE_TO_FILE = "w" ;
119+
120+ final String _PMODE_IN = "IN" ;
121+
122+ final String _PMODE_INOUT = "INOUT" ;
123+
124+ final String _PMODE_OUT = "OUT" ;
125+
126+ final String _PMODE_UNK = "Unknown" ;
127+
128+ final String PMODES = _PMODE_IN + ", " + _PMODE_INOUT + ", " + _PMODE_OUT + ", " + _PMODE_UNK ;
129+
130+ // Options build
131+ Options commandLineOptions ;
132+ commandLineOptions = new Options ();
133+ commandLineOptions .addOption (OPT_HELP , OPT_HELP_ , false , "Prints help and quits" );
134+ commandLineOptions .addOption (OPT_DRIVER , "driver" , true , "JDBC driver class name" );
135+ commandLineOptions .addOption (OPT_DRIVER_INFO , "info" , false , "Prints driver information and properties. If "
136+ + OPT
137+ + OPT_CONNECTION
138+ + " is not specified, all drivers on the classpath are displayed." );
139+ commandLineOptions .addOption (OPT_CONNECTION , "url" , true , "Connection URL" );
140+ commandLineOptions .addOption (OPT_USER , "user" , true , "A database user name" );
141+ commandLineOptions
142+ .addOption (
143+ OPT_PASSWORD ,
144+ OPT_PASSWORD_L ,
145+ true ,
146+ "The database password for the user specified with the "
147+ + OPT
148+ + OPT_USER
149+ + " option. You can obfuscate the password with org.mortbay.jetty.security.Password, see http://docs.codehaus.org/display/JETTY/Securing+Passwords" );
150+ commandLineOptions .addOption (OPT_SQL , OPT_SQL_L , true , "Runs SQL or {call stored_procedure(?, ?)} or {?=call function(?, ?)}" );
151+ commandLineOptions .addOption (OPT_FILE_SFMD , "sfmd" , true , "Writes a SFMD file for the given SQL" );
152+ commandLineOptions .addOption (OPT_FILE_BINDING , "jdbc" , true , "Writes a JDBC binding node file for the given SQL" );
153+ commandLineOptions .addOption (OPT_FILE_JDBC , "node" , true , "Writes a JDBC node file for the given SQL (internal debugging)" );
154+ commandLineOptions .addOption (OPT_WRITE_TO_FILE , "outfile" , true , "Writes the SQL output to the given file" );
155+ commandLineOptions .addOption (OPT_DESCRIPTION , "description" , true ,
156+ "SFMD description. A default description is used if omited. Example: " + OPT + OPT_DESCRIPTION + " \" Runs such and such\" " );
157+ commandLineOptions .addOption (OPT_INTERACTIVE , "interactive" , false ,
158+ "Runs in interactive mode, reading and writing from the console, 'go' or '/' sends a statement" );
159+ commandLineOptions .addOption (OPT_TIMING , "printTiming" , false , "Prints timing information" );
160+ commandLineOptions .addOption (OPT_METADATA , "printMetaData" , false , "Prints metadata information" );
161+ commandLineOptions .addOption (OPT_STACK_TRACE , "printStack" , false , "Prints stack traces on errors" );
162+ Option option = new Option (OPT_COLUMN_NAMES , "columnNames" , true , "Column XML names; default names column labels. Example: "
163+ + OPT
164+ + OPT_COLUMN_NAMES
165+ + " \" cname1 cname2\" " );
166+ commandLineOptions .addOption (option );
167+ option = new Option (OPT_PARAM_NAMES , "paramNames" , true , "Parameter XML names; default names are param1, param2, etc. Example: "
168+ + OPT
169+ + OPT_PARAM_NAMES
170+ + " \" pname1 pname2\" " );
171+ commandLineOptions .addOption (option );
172+ //
173+ OptionGroup pOutTypesOptionGroup = new OptionGroup ();
174+ String pOutTypesOptionGroupDoc = OPT + OPT_PARAM_TYPES_INT + " and " + OPT + OPT_PARAM_TYPES_NAME + " are mutually exclusive." ;
175+ final String typesClassName = Types .class .getName ();
176+ option = new Option (OPT_PARAM_TYPES_INT , "paramTypes" , true , "Parameter types from "
177+ + typesClassName
178+ + ". "
179+ + pOutTypesOptionGroupDoc
180+ + " Example: "
181+ + OPT
182+ + OPT_PARAM_TYPES_INT
183+ + " \" -10 12\" " );
184+ commandLineOptions .addOption (option );
185+ option = new Option (OPT_PARAM_TYPES_NAME , "paramTypeNames" , true , "Parameter "
186+ + typesClassName
187+ + " names. "
188+ + pOutTypesOptionGroupDoc
189+ + " Example: "
190+ + OPT
191+ + OPT_PARAM_TYPES_NAME
192+ + " \" CURSOR VARCHAR\" " );
193+ commandLineOptions .addOption (option );
194+ commandLineOptions .addOptionGroup (pOutTypesOptionGroup );
195+ //
196+ OptionGroup modesOptionGroup = new OptionGroup ();
197+ String modesOptionGroupDoc = OPT + OPT_PARAM_MODES_INT + " and " + OPT + OPT_PARAM_MODES_NAME + " are mutually exclusive." ;
198+ option = new Option (OPT_PARAM_MODES_INT , "paramModes" , true , "Parameters modes ("
199+ + ParameterMetaData .parameterModeIn
200+ + "=IN, "
201+ + ParameterMetaData .parameterModeInOut
202+ + "=INOUT, "
203+ + ParameterMetaData .parameterModeOut
204+ + "=OUT, "
205+ + ParameterMetaData .parameterModeUnknown
206+ + "=Unknown"
207+ + "). "
208+ + modesOptionGroupDoc
209+ + " Example for 2 parameters, OUT and IN: "
210+ + OPT
211+ + OPT_PARAM_MODES_INT
212+ + " \" "
213+ + ParameterMetaData .parameterModeOut
214+ + " "
215+ + ParameterMetaData .parameterModeIn
216+ + "\" " );
217+ modesOptionGroup .addOption (option );
218+ option = new Option (OPT_PARAM_MODES_NAME , "paramModeNames" , true , "Parameters mode names ("
219+ + PMODES
220+ + "). "
221+ + modesOptionGroupDoc
222+ + " Example for 2 parameters, OUT and IN: "
223+ + OPT
224+ + OPT_PARAM_MODES_NAME
225+ + " \" "
226+ + _PMODE_OUT
227+ + " "
228+ + _PMODE_IN
229+ + "\" " );
230+ modesOptionGroup .addOption (option );
231+ commandLineOptions .addOptionGroup (modesOptionGroup );
232+ option = new Option (null , OPT_TRIM_L , true ,
233+ "Trims leading and trailing spaces from all column values. Column XML names can be optionally specified to set which columns to trim." );
234+ option .setOptionalArg (true );
235+ commandLineOptions .addOption (option );
236+ option = new Option (OPT_JDBC_TO_SFMD , OPT_JDBC_TO_SFMD_L , true ,
237+ "Converts the JDBC file in the first argument to an SMFD file specified in the second argument." );
238+ option .setArgs (2 );
239+ commandLineOptions .addOption (option );
240+ this .testPrintHelp (commandLineOptions );
241+ }
49242
50243}
0 commit comments