1 23 24 29 30 package com.sun.cli.jmx.support; 31 32 import junit.framework.TestCase; 33 34 import com.sun.cli.util.stringifier.*; 35 36 import com.sun.cli.jmx.support.ParseResult; 37 import com.sun.cli.jmx.support.ParseResultStringifier; 38 39 public final class ArgParserTest extends TestCase 40 { 41 void 42 dm( Object o ) 43 { 44 System.out.println( o.toString() ); 45 } 46 47 public 48 ArgParserTest() 49 { 50 } 51 52 public void 53 testCreate() 54 { 55 new ArgParserImpl(); 56 } 57 58 public void 59 testEmpty() 60 throws ArgParserException 61 { 62 final ArgParserImpl ap = new ArgParserImpl( ); 63 64 final ParseResult [] results = ap.Parse( "", false ); 65 66 assertEquals( 0, results.length ); 67 } 68 69 ParseResult 70 stringWithCast( String str ) 71 { 72 return( new ParseResult( ParseResult.LITERAL_STRING, str, "String" ) ); 73 } 74 75 ParseResult 76 stringWithoutCast( String str ) 77 { 78 return( new ParseResult( ParseResult.OTHER, str ) ); 79 } 80 ParseResult 81 otherWithCast( String str, String cast ) 82 { 83 return( new ParseResult( ParseResult.OTHER, str, cast ) ); 84 } 85 ParseResult 86 otherWithoutCast( String str ) 87 { 88 return( new ParseResult( ParseResult.OTHER, str) ); 89 } 90 91 String 92 quoteString( String s ) 93 { 94 return( '\"' + s + '\"' ); 95 } 96 97 98 public void 99 testSingleNonNamedStringWithoutCast() 100 throws ArgParserException 101 { 102 final ParseResult [] results = new ArgParserImpl().Parse( "hello", false ); 103 104 assertEquals( 1, results.length ); 105 assertEquals( stringWithoutCast( "hello" ), results[ 0 ] ); 106 } 107 108 109 public void 110 testSingleNonNamedStringWithCast() 111 throws ArgParserException 112 { 113 final ParseResult [] results = new ArgParserImpl().Parse( "(String)hello", false ); 114 115 assertEquals( 1, results.length ); 116 assertEquals( stringWithCast( "hello" ), results[ 0 ] ); 117 } 118 119 public void 120 testEmptyStringWithCast() 121 throws ArgParserException 122 { 123 final ParseResult [] results = new ArgParserImpl().Parse( "(String)", false ); 124 125 assertEquals( 1, results.length ); 126 assertEquals( stringWithCast( "" ), results[ 0 ] ); 127 } 128 129 public void 130 testMultipleStringsWithCast() 131 throws ArgParserException 132 { 133 final String input = "(String)s1,\"s2\",(String)s3"; 134 final ParseResult [] results = new ArgParserImpl().Parse( input, false ); 135 136 assertEquals( 3, results.length ); 137 final ParseResult [] expected = 138 { 139 stringWithCast( "s1" ), 140 stringWithCast( "s2" ), 141 stringWithCast( "s3" ), 142 }; 143 assertEquals( expected[ 0 ], results[ 0 ] ); 144 assertEquals( expected[ 1 ], results[ 1 ] ); 145 assertEquals( expected[ 2 ], results[ 2 ] ); 146 } 147 148 final static String TAB_STR = "\\t"; 149 final static String NEWLINE_STR = "\\n"; 150 final static String RETURN_STR = "\\r"; 151 final static String ESCAPE_CHAR_STR = "\\\\"; 152 153 public void 154 testEscapeCharsInUnquotedString() 155 throws ArgParserException 156 { 157 final String input = TAB_STR + NEWLINE_STR + RETURN_STR + ESCAPE_CHAR_STR; 158 final ParseResult [] results = new ArgParserImpl().Parse( input, false ); 159 160 assertEquals( 1, results.length ); 161 assertEquals( stringWithoutCast( "\t\n\r\\" ), results[ 0 ] ); 162 } 163 164 public void 165 testEscapeCharsInQuotedString() 166 throws ArgParserException 167 { 168 final String input = TAB_STR + NEWLINE_STR + RETURN_STR + ESCAPE_CHAR_STR; 169 final ParseResult [] results = new ArgParserImpl().Parse( quoteString( input ), false ); 170 171 assertEquals( 1, results.length ); 172 assertEquals( stringWithCast( "\t\n\r\\" ), results[ 0 ] ); 173 } 174 175 public void 176 testEmptyArray() 177 throws ArgParserException 178 { 179 final String input = "{}"; 180 final ParseResult [] results = new ArgParserImpl().Parse( input, false ); 181 182 assertEquals( 1, results.length ); 183 final ParseResult expected = 184 new ParseResult( ParseResult.ARRAY, new ParseResult [ 0 ] ); 185 assertEquals( expected, results[ 0 ] ); 186 } 187 188 public void 189 testMultiArray() 190 throws ArgParserException 191 { 192 final String input = "{\"hello\",1,(String)there}"; 193 final ParseResult [] results = new ArgParserImpl().Parse( input, false ); 194 195 assertEquals( 1, results.length ); 196 197 final ParseResult [] expectedArrayElements = 198 { 199 stringWithCast( "hello" ), 200 otherWithoutCast( "1" ), 201 stringWithCast( "there" ), 202 }; 203 final ParseResult expected = new ParseResult( ParseResult.ARRAY, expectedArrayElements ); 204 205 assertEquals( 1, results.length ); 206 assertEquals( expected, results[ 0 ] ); 207 } 208 209 private void 210 testExpr( boolean namedArgs, String expr ) 211 throws Exception 212 { 213 boolean success = false; 214 215 final ParseResult [] results = new ArgParserImpl().Parse( expr, namedArgs ); 216 } 217 218 public void 219 testNonNamedSuccessCases( ) 220 throws Exception 221 { 222 boolean success; 224 225 final String [] expressions = new String [] 226 { 227 "1", 228 "999.0003777", 229 "\"\"", ")", "}", "=", "(boolean)true", 234 "(Boolean)false", 235 "hello", 236 "(String)hello", 237 "\\(String)", 238 "\",,,,,,,,,\"", 239 "\"hello\"", 240 "1,2,3,4,5,6,\"hello\\,,,,,goodbye\",hello\\,goodbye", 241 "{}", 242 "{,,,,}", 243 "{1}", 244 "{1,2}", 245 "{1,2,3,4,5,6,hello}", 246 "{1,2,3,4,5,6,{hello,world}}", 247 "(Object){(int)1,(long)2,(Integer)3,4,5,6,(String){(String)\"hello\",world}}" 248 }; 249 final int numExprs = expressions.length; 250 251 final String testName = "argName"; 252 253 String togetherList = ""; 254 String togetherNamed = ""; 255 for( int i = 0; i < numExprs; ++i ) 256 { 257 final String testString = expressions[ i ]; 258 259 try 260 { 261 testExpr( false, testString ); 262 } 263 catch( Exception e ) 264 { 265 fail( "ERROR: case failed: " + expressions[ i ] ); 266 } 267 268 togetherList = togetherList + testString + ","; 269 } 270 271 testExpr( false, togetherList ); 272 testExpr( false, "{" + togetherList + "}" ); 273 } 274 275 276 public void 277 testNamedSuccessCases( ) 278 throws Exception 279 { 280 boolean success; 282 283 final String [] expressions = new String [] 284 { 285 "test=hello\\,world", 286 "hello=1,world=there", 287 "arg1=1,arg2=2,arg3=3,argString=(String)hello\\,world", 288 "count=1", 289 "name=server1,cluster=cluster1,type=instance", 290 "empty1=,empty2=,empty3=" 291 }; 292 final int numExprs = expressions.length; 293 294 String togetherList = ""; 295 for( int i = 0; i < numExprs; ++i ) 296 { 297 final String testString = expressions[ i ]; 298 299 try 300 { 301 testExpr( true, testString ); 302 } 303 catch( Exception e ) 304 { 305 fail( "ERROR: case failed: " + expressions[ i ] + "\n" + e.toString() ); 306 } 307 308 togetherList = togetherList + testString + ","; 309 } 310 311 testExpr( false, togetherList ); 312 } 313 314 public void 315 TestFailureCases() 316 throws Exception 317 { 318 boolean success; 319 320 final String [] failureExpressions = new String [] 321 { 322 "{", 323 "(", 324 "()", 325 "(a-b-c)", 326 "(.abc)", 327 "({", 328 "(int)", 329 "\"\"\"", 330 "\"", 331 }; 332 final int numFailureExprs = failureExpressions.length; 333 334 String together = "{"; 335 for( int i = 0; i < numFailureExprs; ++i ) 336 { 337 try 338 { 339 testExpr( false, failureExpressions[ i ] ); 340 } 341 catch( Exception e ) 342 { 343 fail( "ERROR: case failed: " + failureExpressions[ i ] + "\n" + e.toString() ); 344 } 345 346 together = together + failureExpressions[ i ] + ","; 347 } 348 together = together + "}"; 349 350 try 351 { 352 testExpr( false, together ); 353 } 354 catch( Exception e ) 355 { 356 fail( "ERROR: case failed: " + together + "\n" + e.toString() ); 357 } 358 } 359 } 360 361 | Popular Tags |