1 10 11 package org.apache.commons.cli; 12 13 import java.util.Arrays ; 14 15 import junit.framework.Test; 16 import junit.framework.TestCase; 17 import junit.framework.TestSuite; 18 19 public class ValuesTest extends TestCase 20 { 21 22 private CommandLine _cmdline = null; 23 private Option _option = null; 24 25 public static Test suite() { 26 return new TestSuite( ValuesTest.class ); 27 } 28 29 public ValuesTest( String name ) 30 { 31 super( name ); 32 } 33 34 public void setUp() 35 { 36 Options opts = new Options(); 37 38 opts.addOption("a", 39 false, 40 "toggle -a"); 41 42 opts.addOption("b", 43 true, 44 "set -b"); 45 46 opts.addOption("c", 47 "c", 48 false, 49 "toggle -c"); 50 51 opts.addOption("d", 52 "d", 53 true, 54 "set -d"); 55 56 opts.addOption( OptionBuilder.withLongOpt( "e" ) 57 .hasArgs() 58 .withDescription( "set -e ") 59 .create( 'e' ) ); 60 61 opts.addOption("f", 62 "f", 63 false, 64 "jk"); 65 66 opts.addOption( OptionBuilder.withLongOpt( "g" ) 67 .hasArgs( 2 ) 68 .withDescription( "set -g") 69 .create( 'g' ) ); 70 71 opts.addOption( OptionBuilder.withLongOpt( "h" ) 72 .hasArgs( 2 ) 73 .withDescription( "set -h") 74 .create( 'h' ) ); 75 76 opts.addOption( OptionBuilder.withLongOpt( "i" ) 77 .withDescription( "set -i") 78 .create( 'i' ) ); 79 80 opts.addOption( OptionBuilder.withLongOpt( "j" ) 81 .hasArgs( ) 82 .withDescription( "set -j") 83 .withValueSeparator( '=' ) 84 .create( 'j' ) ); 85 86 opts.addOption( OptionBuilder.withLongOpt( "k" ) 87 .hasArgs( ) 88 .withDescription( "set -k") 89 .withValueSeparator( '=' ) 90 .create( 'k' ) ); 91 92 _option = OptionBuilder.withLongOpt( "m" ) 93 .hasArgs( ) 94 .withDescription( "set -m") 95 .withValueSeparator( ) 96 .create( 'm' ); 97 98 opts.addOption( _option ); 99 100 String [] args = new String [] { "-a", 101 "-b", "foo", 102 "--c", 103 "--d", "bar", 104 "-e", "one", "two", 105 "-f", 106 "arg1", "arg2", 107 "-g", "val1", "val2" , "arg3", 108 "-h", "val1", "-i", 109 "-h", "val2", 110 "-jkey=value", 111 "-j", "key=value", 112 "-kkey1=value1", 113 "-kkey2=value2", 114 "-mkey=value"}; 115 116 CommandLineParser parser = new PosixParser(); 117 118 try 119 { 120 _cmdline = parser.parse(opts,args); 121 } 122 catch (ParseException e) 123 { 124 fail("Cannot setUp() CommandLine: " + e.toString()); 125 } 126 } 127 128 public void tearDown() 129 { 130 131 } 132 133 public void testShortArgs() 134 { 135 assertTrue( _cmdline.hasOption("a") ); 136 assertTrue( _cmdline.hasOption("c") ); 137 138 assertNull( _cmdline.getOptionValues("a") ); 139 assertNull( _cmdline.getOptionValues("c") ); 140 } 141 142 public void testShortArgsWithValue() 143 { 144 assertTrue( _cmdline.hasOption("b") ); 145 assertTrue( _cmdline.getOptionValue("b").equals("foo")); 146 assertTrue( _cmdline.getOptionValues("b").length == 1); 147 148 assertTrue( _cmdline.hasOption("d") ); 149 assertTrue( _cmdline.getOptionValue("d").equals("bar")); 150 assertTrue( _cmdline.getOptionValues("d").length == 1); 151 } 152 153 public void testMultipleArgValues() 154 { 155 String [] result = _cmdline.getOptionValues("e"); 156 String [] values = new String [] { "one", "two" }; 157 assertTrue( _cmdline.hasOption("e") ); 158 assertTrue( _cmdline.getOptionValues("e").length == 2); 159 assertTrue( Arrays.equals( values, _cmdline.getOptionValues("e") ) ); 160 } 161 162 public void testTwoArgValues() 163 { 164 String [] result = _cmdline.getOptionValues("g"); 165 String [] values = new String [] { "val1", "val2" }; 166 assertTrue( _cmdline.hasOption("g") ); 167 assertTrue( _cmdline.getOptionValues("g").length == 2); 168 assertTrue( Arrays.equals( values, _cmdline.getOptionValues("g") ) ); 169 } 170 171 public void testComplexValues() 172 { 173 String [] result = _cmdline.getOptionValues("h"); 174 String [] values = new String [] { "val1", "val2" }; 175 assertTrue( _cmdline.hasOption("i") ); 176 assertTrue( _cmdline.hasOption("h") ); 177 assertTrue( _cmdline.getOptionValues("h").length == 2); 178 assertTrue( Arrays.equals( values, _cmdline.getOptionValues("h") ) ); 179 } 180 181 public void testExtraArgs() 182 { 183 String [] args = new String [] { "arg1", "arg2", "arg3" }; 184 assertTrue( _cmdline.getArgs().length == 3 ); 185 assertTrue( Arrays.equals( args, _cmdline.getArgs() ) ); 186 } 187 188 public void testCharSeparator() 189 { 190 String [] values = new String [] { "key", "value", "key", "value" }; 193 assertTrue( _cmdline.hasOption( "j" ) ); 194 assertTrue( _cmdline.hasOption( 'j' ) ); 195 assertTrue( _cmdline.getOptionValues( "j" ).length == 4); 196 assertTrue( _cmdline.getOptionValues( 'j' ).length == 4); 197 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "j" ) ) ); 198 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'j' ) ) ); 199 200 values = new String [] { "key1", "value1", "key2", "value2" }; 201 assertTrue( _cmdline.hasOption( "k" ) ); 202 assertTrue( _cmdline.hasOption( 'k' ) ); 203 assertTrue( _cmdline.getOptionValues( "k" ).length == 4 ); 204 assertTrue( _cmdline.getOptionValues( 'k' ).length == 4 ); 205 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "k" ) ) ); 206 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'k' ) ) ); 207 208 values = new String [] { "key", "value" }; 209 assertTrue( _cmdline.hasOption( "m" ) ); 210 assertTrue( _cmdline.hasOption( 'm' ) ); 211 assertTrue( _cmdline.getOptionValues( "m" ).length == 2); 212 assertTrue( _cmdline.getOptionValues( 'm' ).length == 2); 213 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "m" ) ) ); 214 assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'm' ) ) ); 215 } 216 217 222 248 } 249
| Popular Tags
|