1414import junit .framework .TestCase ;
1515import junit .framework .TestSuite ;
1616
17+ import java .util .Arrays ;
1718import java .util .Properties ;
1819
1920public class ValueTest extends TestCase
@@ -73,6 +74,9 @@ public void setUp()
7374 opts .addOption ( OptionBuilder .hasOptionalArgs ( )
7475 .create ( 'j' ) );
7576
77+ opts .addOption ( OptionBuilder .hasArgs ( ).withValueSeparator ( ',' )
78+ .create ( 'k' ) );
79+
7680 String [] args = new String [] { "-a" ,
7781 "-b" , "foo" ,
7882 "--c" ,
@@ -253,11 +257,14 @@ public void testShortOptionalNArgValues()
253257
254258 public void testLongOptionalNArgValues ()
255259 {
256- String [] args = new String [] { "--hide" , "house" , "hair" , "head"
260+ String [] args = new String [] {
261+ "--hide" , "house" , "hair" , "head"
257262 };
263+
264+ CommandLineParser parser = new PosixParser ();
265+
258266 try
259267 {
260- CommandLineParser parser = new PosixParser ();
261268 CommandLine cmd = parser .parse (opts ,args );
262269 assertTrue ( cmd .hasOption ("hide" ) );
263270 assertEquals ( "house" , cmd .getOptionValue ("hide" ) );
@@ -272,13 +279,15 @@ public void testLongOptionalNArgValues()
272279 }
273280 }
274281
275- public void testPropertyValues ()
282+ public void testPropertyOptionSingularValue ()
276283 {
277284 Properties properties = new Properties ();
278285 properties .setProperty ( "hide" , "seek" );
286+
287+ CommandLineParser parser = new PosixParser ();
288+
279289 try
280290 {
281- CommandLineParser parser = new PosixParser ();
282291 CommandLine cmd = parser .parse (opts , null , properties );
283292 assertTrue ( cmd .hasOption ("hide" ) );
284293 assertEquals ( "seek" , cmd .getOptionValue ("hide" ) );
@@ -288,8 +297,100 @@ public void testPropertyValues()
288297 {
289298 fail ("Cannot setUp() CommandLine: " + e .toString ());
290299 }
300+ }
301+
302+ public void testPropertyOptionFlags ()
303+ {
304+ Properties properties = new Properties ();
305+ properties .setProperty ( "a" , "true" );
306+ properties .setProperty ( "c" , "yes" );
307+ properties .setProperty ( "e" , "1" );
308+
309+ CommandLineParser parser = new PosixParser ();
310+
311+ try
312+ {
313+ CommandLine cmd = parser .parse (opts , null , properties );
314+ assertTrue ( cmd .hasOption ("a" ) );
315+ assertTrue ( cmd .hasOption ("c" ) );
316+ assertTrue ( cmd .hasOption ("e" ) );
317+ }
318+ catch (ParseException e )
319+ {
320+ fail ("Cannot setUp() CommandLine: " + e .toString ());
321+ }
322+
323+ properties = new Properties ();
324+ properties .setProperty ( "a" , "false" );
325+ properties .setProperty ( "c" , "no" );
326+ properties .setProperty ( "e" , "0" );
327+ try
328+ {
329+ CommandLine cmd = parser .parse (opts , null , properties );
330+ assertTrue ( !cmd .hasOption ("a" ) );
331+ assertTrue ( !cmd .hasOption ("c" ) );
332+ assertTrue ( !cmd .hasOption ("e" ) );
333+ }
334+ catch (ParseException e )
335+ {
336+ fail ("Cannot setUp() CommandLine: " + e .toString ());
337+ }
338+
339+ properties = new Properties ();
340+ properties .setProperty ( "a" , "TRUE" );
341+ properties .setProperty ( "c" , "nO" );
342+ properties .setProperty ( "e" , "TrUe" );
343+ try
344+ {
345+ CommandLine cmd = parser .parse (opts , null , properties );
346+ assertTrue ( cmd .hasOption ("a" ) );
347+ assertTrue ( !cmd .hasOption ("c" ) );
348+ assertTrue ( cmd .hasOption ("e" ) );
349+ }
350+ catch (ParseException e )
351+ {
352+ fail ("Cannot setUp() CommandLine: " + e .toString ());
353+ }
354+
355+ properties = new Properties ();
356+ properties .setProperty ( "a" , "just a string" );
357+ properties .setProperty ( "e" , "" );
358+ try
359+ {
360+ CommandLine cmd = parser .parse (opts , null , properties );
361+ assertTrue ( !cmd .hasOption ("a" ) );
362+ assertTrue ( !cmd .hasOption ("c" ) );
363+ assertTrue ( !cmd .hasOption ("e" ) );
364+ }
365+ catch (ParseException e )
366+ {
367+ fail ("Cannot setUp() CommandLine: " + e .toString ());
368+ }
369+
291370 }
292371
372+ public void testPropertyOptionMultipleValues ()
373+ {
374+ Properties properties = new Properties ();
375+ properties .setProperty ( "k" , "one,two" );
376+
377+ CommandLineParser parser = new PosixParser ();
378+
379+ String [] values = new String [] {
380+ "one" , "two"
381+ };
382+ try
383+ {
384+ CommandLine cmd = parser .parse (opts , null , properties );
385+ assertTrue ( cmd .hasOption ("k" ) );
386+ assertTrue ( Arrays .equals ( values , cmd .getOptionValues ('k' ) ) );
387+ }
388+ catch (ParseException e )
389+ {
390+ fail ("Cannot setUp() CommandLine: " + e .toString ());
391+ }
392+ }
393+
293394 public void testPropertyOverrideValues ()
294395 {
295396 String [] args = new String [] {
0 commit comments