Skip to content

Commit cfbb46a

Browse files
committed
Improved the test coverage of TypeHandler and PatternOptionBuilder
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@679851 13f79535-47bb-0310-9956-ffa450edef68
1 parent e2f25af commit cfbb46a

3 files changed

Lines changed: 190 additions & 123 deletions

File tree

src/java/org/apache/commons/cli/PatternOptionBuilder.java

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
*
4242
* <p>
4343
* For example, the following allows command line flags of '-v -p string-value -f /dir/file'.
44+
* The exclamation mark precede a mandatory option.
4445
* </p>
45-
* <code>Options options = PatternOptionBuilder.parsePattern("vp:f/");</code>
46+
* <code>Options options = PatternOptionBuilder.parsePattern("vp:!f/");</code>
4647
*
4748
* <p>
4849
* TODO These need to break out to OptionType and also
@@ -86,93 +87,77 @@ public class PatternOptionBuilder {
8687
public static final Class URL_VALUE = URL.class;
8788

8889
/**
89-
* <p>Retrieve the class that <code>ch</code> represents.</p>
90+
* Retrieve the class that <code>ch</code> represents.
9091
*
9192
* @param ch the specified character
9293
* @return The class that <code>ch</code> represents
9394
*/
9495
public static Object getValueClass(char ch)
9596
{
96-
if (ch == '@')
97+
switch (ch)
9798
{
98-
return PatternOptionBuilder.OBJECT_VALUE;
99-
}
100-
else if (ch == ':')
101-
{
102-
return PatternOptionBuilder.STRING_VALUE;
103-
}
104-
else if (ch == '%')
105-
{
106-
return PatternOptionBuilder.NUMBER_VALUE;
107-
}
108-
else if (ch == '+')
109-
{
110-
return PatternOptionBuilder.CLASS_VALUE;
111-
}
112-
else if (ch == '#')
113-
{
114-
return PatternOptionBuilder.DATE_VALUE;
115-
}
116-
else if (ch == '<')
117-
{
118-
return PatternOptionBuilder.EXISTING_FILE_VALUE;
119-
}
120-
else if (ch == '>')
121-
{
122-
return PatternOptionBuilder.FILE_VALUE;
123-
}
124-
else if (ch == '*')
125-
{
126-
return PatternOptionBuilder.FILES_VALUE;
127-
}
128-
else if (ch == '/')
129-
{
130-
return PatternOptionBuilder.URL_VALUE;
99+
case '@':
100+
return PatternOptionBuilder.OBJECT_VALUE;
101+
case ':':
102+
return PatternOptionBuilder.STRING_VALUE;
103+
case '%':
104+
return PatternOptionBuilder.NUMBER_VALUE;
105+
case '+':
106+
return PatternOptionBuilder.CLASS_VALUE;
107+
case '#':
108+
return PatternOptionBuilder.DATE_VALUE;
109+
case '<':
110+
return PatternOptionBuilder.EXISTING_FILE_VALUE;
111+
case '>':
112+
return PatternOptionBuilder.FILE_VALUE;
113+
case '*':
114+
return PatternOptionBuilder.FILES_VALUE;
115+
case '/':
116+
return PatternOptionBuilder.URL_VALUE;
131117
}
132118

133119
return null;
134120
}
135121

136122
/**
137-
* <p>Returns whether <code>ch</code> is a value code, i.e.
138-
* whether it represents a class in a pattern.</p>
123+
* Returns whether <code>ch</code> is a value code, i.e.
124+
* whether it represents a class in a pattern.
139125
*
140126
* @param ch the specified character
141127
* @return true if <code>ch</code> is a value code, otherwise false.
142128
*/
143129
public static boolean isValueCode(char ch)
144130
{
145-
if ((ch != '@') && (ch != ':') && (ch != '%') && (ch != '+')
146-
&& (ch != '#') && (ch != '<') && (ch != '>') && (ch != '*')
147-
&& (ch != '/') && (ch != '!'))
148-
{
149-
return false;
150-
}
151-
152-
return true;
131+
return ch == '@'
132+
|| ch == ':'
133+
|| ch == '%'
134+
|| ch == '+'
135+
|| ch == '#'
136+
|| ch == '<'
137+
|| ch == '>'
138+
|| ch == '*'
139+
|| ch == '/'
140+
|| ch == '!';
153141
}
154142

155143
/**
156-
* <p>Returns the {@link Options} instance represented by
157-
* <code>pattern</code>.</p>
144+
* Returns the {@link Options} instance represented by
145+
* <code>pattern</code>.
158146
*
159147
* @param pattern the pattern string
160148
* @return The {@link Options} instance
161149
*/
162150
public static Options parsePattern(String pattern)
163151
{
164-
int sz = pattern.length();
165-
166152
char opt = ' ';
167-
char ch = ' ';
168153
boolean required = false;
169154
Object type = null;
170155

171156
Options options = new Options();
172157

173-
for (int i = 0; i < sz; i++)
158+
for (int i = 0; i < pattern.length(); i++)
174159
{
175-
ch = pattern.charAt(i);
160+
char ch = pattern.charAt(i);
176161

177162
// a value code comes after an option and specifies
178163
// details about it

src/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
public class TypeHandler {
3636

3737
/**
38-
* <p>Returns the <code>Object</code> of type <code>obj</code>
39-
* with the value of <code>str</code>.</p>
38+
* Returns the <code>Object</code> of type <code>obj</code>
39+
* with the value of <code>str</code>.
4040
*
4141
* @param str the command line value
4242
* @param obj the type of argument
@@ -49,8 +49,8 @@ public static Object createValue(String str, Object obj)
4949
}
5050

5151
/**
52-
* <p>Returns the <code>Object</code> of type <code>clazz</code>
53-
* with the value of <code>str</code>.</p>
52+
* Returns the <code>Object</code> of type <code>clazz</code>
53+
* with the value of <code>str</code>.
5454
*
5555
* @param str the command line value
5656
* @param clazz the type of argument
@@ -102,7 +102,7 @@ else if (PatternOptionBuilder.URL_VALUE == clazz)
102102
}
103103

104104
/**
105-
* <p>Create an Object from the classname and empty constructor.</p>
105+
* Create an Object from the classname and empty constructor.
106106
*
107107
* @param str the argument value
108108
* @return the initialised object, or null if it couldn't create
@@ -118,7 +118,7 @@ public static Object createObject(String str)
118118
}
119119
catch (ClassNotFoundException cnfe)
120120
{
121-
System.err.println("Unable to find: " + str);
121+
System.err.println("Unable to find the class: " + str);
122122

123123
return null;
124124
}
@@ -129,27 +129,17 @@ public static Object createObject(String str)
129129
{
130130
instance = cl.newInstance();
131131
}
132-
catch (InstantiationException cnfe)
132+
catch (Exception e)
133133
{
134-
System.err.println("InstantiationException; Unable to create: "
135-
+ str);
136-
137-
return null;
138-
}
139-
catch (IllegalAccessException cnfe)
140-
{
141-
System.err.println("IllegalAccessException; Unable to create: "
142-
+ str);
143-
144-
return null;
134+
System.err.println(e.getClass().getName() + "; Unable to create an instance of: " + str);
145135
}
146136

147137
return instance;
148138
}
149139

150140
/**
151-
* <p>Create a number from a String. If a . is present, it creates a
152-
* Double, otherwise a Long. </p>
141+
* Create a number from a String. If a . is present, it creates a
142+
* Double, otherwise a Long.
153143
*
154144
* @param str the value
155145
* @return the number represented by <code>str</code>, if <code>str</code>
@@ -159,16 +149,13 @@ public static Number createNumber(String str)
159149
{
160150
try
161151
{
162-
if( str != null )
152+
if( str.indexOf('.') != -1 )
153+
{
154+
return Double.valueOf(str);
155+
}
156+
else
163157
{
164-
if( str.indexOf('.') != -1 )
165-
{
166-
return Double.valueOf(str);
167-
}
168-
else
169-
{
170-
return Long.valueOf(str);
171-
}
158+
return Long.valueOf(str);
172159
}
173160
}
174161
catch (NumberFormatException nfe)
@@ -180,7 +167,7 @@ public static Number createNumber(String str)
180167
}
181168

182169
/**
183-
* <p>Returns the class whose name is <code>str</code>.</p>
170+
* Returns the class whose name is <code>str</code>.
184171
*
185172
* @param str the class name
186173
* @return The class if it is found, otherwise return null
@@ -200,7 +187,7 @@ public static Class createClass(String str)
200187
}
201188

202189
/**
203-
* <p>Returns the date represented by <code>str</code>.</p>
190+
* Returns the date represented by <code>str</code>.
204191
*
205192
* @param str the date string
206193
* @return The date if <code>str</code> is a valid date string,
@@ -212,7 +199,7 @@ public static Date createDate(String str)
212199
}
213200

214201
/**
215-
* <p>Returns the URL represented by <code>str</code>.</p>
202+
* Returns the URL represented by <code>str</code>.
216203
*
217204
* @param str the URL string
218205
* @return The URL is <code>str</code> is well-formed, otherwise
@@ -224,16 +211,16 @@ public static URL createURL(String str)
224211
{
225212
return new URL(str);
226213
}
227-
catch (MalformedURLException mue)
214+
catch (MalformedURLException e)
228215
{
229-
System.err.println("Unable to parse: " + str);
216+
System.err.println("Unable to parse the URL: " + str);
230217

231218
return null;
232219
}
233220
}
234221

235222
/**
236-
* <p>Returns the File represented by <code>str</code>.</p>
223+
* Returns the File represented by <code>str</code>.
237224
*
238225
* @param str the File location
239226
* @return The file represented by <code>str</code>.
@@ -244,7 +231,7 @@ public static File createFile(String str)
244231
}
245232

246233
/**
247-
* <p>Returns the File[] represented by <code>str</code>.</p>
234+
* Returns the File[] represented by <code>str</code>.
248235
*
249236
* @param str the paths to the files
250237
* @return The File[] represented by <code>str</code>.

0 commit comments

Comments
 (0)