1 10 11 package org.apache.commons.cli; 12 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 public class GnuParseTest extends TestCase 18 { 19 private Options _options = null; 20 private CommandLineParser _parser = null; 21 22 public static Test suite() { 23 return new TestSuite( GnuParseTest.class ); 24 } 25 26 public GnuParseTest( String name ) 27 { 28 super( name ); 29 } 30 31 public void setUp() 32 { 33 _options = new Options() 34 .addOption("a", 35 "enable-a", 36 false, 37 "turn [a] on or off") 38 .addOption("b", 39 "bfile", 40 true, 41 "set the value of [b]") 42 .addOption("c", 43 "copt", 44 false, 45 "turn [c] on or off"); 46 47 _parser = new GnuParser( ); 48 } 49 50 public void tearDown() 51 { 52 53 } 54 55 public void testSimpleShort() 56 { 57 String [] args = new String [] { "-a", 58 "-b", "toast", 59 "foo", "bar" }; 60 61 try 62 { 63 CommandLine cl = _parser.parse(_options, args); 64 65 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 66 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 67 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 68 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); 69 } 70 catch (ParseException e) 71 { 72 fail( e.toString() ); 73 } 74 } 75 76 public void testSimpleLong() 77 { 78 String [] args = new String [] { "--enable-a", 79 "--bfile", "toast", 80 "foo", "bar" }; 81 82 try 83 { 84 CommandLine cl = _parser.parse(_options, args); 85 86 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 87 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 88 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 89 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 2); 90 } 91 catch (ParseException e) 92 { 93 fail( e.toString() ); 94 } 95 } 96 97 public void testExtraOption() 98 { 99 String [] args = new String [] { "-a", "-d", "-b", "toast", 100 "foo", "bar" }; 101 102 boolean caught = false; 103 104 try 105 { 106 CommandLine cl = _parser.parse(_options, args); 107 108 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 109 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 110 assertTrue( "confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 111 assertTrue( "Confirm size of extra args", cl.getArgList().size() == 3); 112 } 113 catch (UnrecognizedOptionException e) 114 { 115 caught = true; 116 } 117 catch (ParseException e) 118 { 119 fail( e.toString() ); 120 } 121 assertTrue( "Confirm UnrecognizedOptionException caught", caught ); 122 } 123 124 public void testMissingArg() 125 { 126 127 String [] args = new String [] { "-b" }; 128 129 boolean caught = false; 130 131 try 132 { 133 CommandLine cl = _parser.parse(_options, args); 134 } 135 catch (MissingArgumentException e) 136 { 137 caught = true; 138 } 139 catch (ParseException e) 140 { 141 fail( e.toString() ); 142 } 143 144 assertTrue( "Confirm MissingArgumentException caught", caught ); 145 } 146 147 public void testStop() 148 { 149 String [] args = new String [] { "-c", 150 "foober", 151 "-b", 152 "toast" }; 153 154 try 155 { 156 CommandLine cl = _parser.parse(_options, args, true); 157 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 158 assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); 159 } 160 catch (ParseException e) 161 { 162 fail( e.toString() ); 163 } 164 } 165 166 public void testMultiple() 167 { 168 String [] args = new String [] { "-c", 169 "foobar", 170 "-b", 171 "toast" }; 172 173 try 174 { 175 CommandLine cl = _parser.parse(_options, args, true); 176 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 177 assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); 178 179 cl = _parser.parse(_options, cl.getArgs() ); 180 181 assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); 182 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 183 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 184 assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); 185 assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); 186 } 187 catch (ParseException e) 188 { 189 fail( e.toString() ); 190 } 191 } 192 193 public void testMultipleWithLong() 194 { 195 String [] args = new String [] { "--copt", 196 "foobar", 197 "--bfile", "toast" }; 198 199 try 200 { 201 CommandLine cl = _parser.parse(_options,args, 202 true); 203 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 204 assertTrue( "Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); 205 206 cl = _parser.parse(_options, cl.getArgs() ); 207 208 assertTrue( "Confirm -c is not set", ! cl.hasOption("c") ); 209 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 210 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("toast") ); 211 assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); 212 assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar") ); 213 } 214 catch (ParseException e) 215 { 216 fail( e.toString() ); 217 } 218 } 219 220 public void testDoubleDash() 221 { 222 String [] args = new String [] { "--copt", 223 "--", 224 "-b", "toast" }; 225 226 try 227 { 228 CommandLine cl = _parser.parse(_options, args); 229 230 assertTrue( "Confirm -c is set", cl.hasOption("c") ); 231 assertTrue( "Confirm -b is not set", ! cl.hasOption("b") ); 232 assertTrue( "Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); 233 234 } 235 catch (ParseException e) 236 { 237 fail( e.toString() ); 238 } 239 } 240 241 public void testSingleDash() 242 { 243 String [] args = new String [] { "--copt", 244 "-b", "-", 245 "-a", 246 "-" }; 247 248 try 249 { 250 CommandLine cl = _parser.parse(_options, args); 251 252 assertTrue( "Confirm -a is set", cl.hasOption("a") ); 253 assertTrue( "Confirm -b is set", cl.hasOption("b") ); 254 assertTrue( "Confirm arg of -b", cl.getOptionValue("b").equals("-") ); 255 assertTrue( "Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); 256 assertTrue( "Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-") ); 257 } 258 catch (ParseException e) 259 { 260 fail( e.toString() ); 261 } 262 263 } 264 } 265 | Popular Tags |