1 8 9 package test.mx4j.tools.i18n; 10 11 import java.util.HashMap ; 12 import java.util.Locale ; 13 import java.util.Map ; 14 import java.util.MissingResourceException ; 15 import javax.management.MBeanAttributeInfo ; 16 import javax.management.MBeanConstructorInfo ; 17 import javax.management.MBeanFeatureInfo ; 18 import javax.management.MBeanInfo ; 19 import javax.management.MBeanOperationInfo ; 20 import javax.management.MBeanParameterInfo ; 21 22 import junit.framework.TestCase; 23 import mx4j.tools.i18n.I18NStandardMBean; 24 25 28 public class I18NStandardMBeanTest extends TestCase 29 { 30 public I18NStandardMBeanTest(String s) 31 { 32 super(s); 33 } 34 35 public void setUp() 36 { 37 I18NStandardMBean.setDefaultLocale(null); 38 setLocaleProp(""); } 40 41 45 public void testSubclassSource() throws Exception 46 { 47 setDefaultLocale(Locale.ITALY); I18NStandardMBean mbean = 50 new I18NStandardMBeanSupport.SubclassNameOnly(); 51 MBeanInfo info = mbean.getMBeanInfo(); 52 assertEquals(info.getDescription(), 53 "The default subclassed name only MBean description"); 54 55 setDefaultLocale(Locale.ENGLISH); 57 mbean = new I18NStandardMBeanSupport.SubclassNameOnly(Locale.FRENCH); 58 info = mbean.getMBeanInfo(); 59 assertEquals(info.getDescription(), 60 "The French subclassed name only MBean description"); 61 62 mbean = new I18NStandardMBeanSupport.SubclassNameOnly(); 64 info = mbean.getMBeanInfo(); 65 assertEquals(info.getDescription(), 66 "The English subclassed name only MBean description"); 67 68 setLocaleProp("fr_FR"); 70 mbean = new I18NStandardMBeanSupport.SubclassNameOnly(); 71 info = mbean.getMBeanInfo(); 72 assertEquals(info.getDescription(), 73 "The French subclassed name only MBean description"); 74 75 I18NStandardMBean.setDefaultLocale(Locale.ENGLISH); 77 mbean = new I18NStandardMBeanSupport.SubclassNameOnly(); 78 info = mbean.getMBeanInfo(); 79 assertEquals(info.getDescription(), 80 "The English subclassed name only MBean description"); 81 } 82 83 87 public void testImplementationSource() throws Exception 88 { 89 I18NStandardMBeanSupport.ImplementationNameOnly impl = 90 new I18NStandardMBeanSupport.ImplementationNameOnly(); 91 92 setDefaultLocale(Locale.ITALY); I18NStandardMBean mbean = 95 new I18NStandardMBean(impl, 96 I18NStandardMBeanSupport.FullManagement.class); 97 MBeanInfo info = mbean.getMBeanInfo(); 98 assertEquals(info.getDescription(), 99 "The default direct implementation name only MBean description"); 100 101 setDefaultLocale(Locale.ENGLISH); 103 mbean = 104 new I18NStandardMBean(impl, 105 I18NStandardMBeanSupport.FullManagement.class, 106 Locale.FRENCH); 107 info = mbean.getMBeanInfo(); 108 assertEquals(info.getDescription(), 109 "The French direct implementation name only MBean description"); 110 111 mbean = 113 new I18NStandardMBean(impl, 114 I18NStandardMBeanSupport.FullManagement.class); 115 info = mbean.getMBeanInfo(); 116 assertEquals(info.getDescription(), 117 "The English direct implementation name only MBean description"); 118 119 setLocaleProp("fr_FR"); 121 mbean = 122 new I18NStandardMBean(impl, 123 I18NStandardMBeanSupport.FullManagement.class); 124 info = mbean.getMBeanInfo(); 125 assertEquals(info.getDescription(), 126 "The French direct implementation name only MBean description"); 127 128 I18NStandardMBean.setDefaultLocale(Locale.ENGLISH); 130 mbean = 131 new I18NStandardMBean(impl, 132 I18NStandardMBeanSupport.FullManagement.class); 133 info = mbean.getMBeanInfo(); 134 assertEquals(info.getDescription(), 135 "The English direct implementation name only MBean description"); 136 } 137 138 143 public void testAllKeys() throws Exception 144 { 145 I18NStandardMBean mbean = 146 new I18NStandardMBeanSupport.SubclassComplete(); 147 MBeanInfo info = mbean.getMBeanInfo(); 148 info = mbean.getMBeanInfo(); assertEquals(info.getDescription(), 150 "The default subclassed complete MBean description"); 151 MBeanAttributeInfo attrInfo = info.getAttributes()[0]; 152 assertEquals(attrInfo.getDescription(), "The attribute description"); 153 MBeanOperationInfo opInfo = info.getOperations()[0]; 154 assertEquals(opInfo.getDescription(), "The operation description"); 155 MBeanParameterInfo paramInfo = opInfo.getSignature()[0]; 156 checkParam(paramInfo, 157 "The first parameter for the operation", 158 "parameter1"); 159 paramInfo = opInfo.getSignature()[1]; 160 checkParam(paramInfo, 161 "The second parameter for the operation", 162 "parameter2"); 163 164 MBeanConstructorInfo [] cstrs = info.getConstructors(); 165 assertEquals(cstrs.length, 2); 166 168 Map mapCstrForDesc = makeFeatureMap(cstrs); 169 170 MBeanConstructorInfo cstr = 172 (MBeanConstructorInfo )mapCstrForDesc.get("The no-args constructor"); 173 assertNotNull(cstr); 174 assertEquals(cstr.getSignature().length, 0); 175 176 cstr = 178 (MBeanConstructorInfo )mapCstrForDesc.get("The Locale specific constructor"); 179 assertNotNull(cstr); 180 assertEquals(cstr.getSignature().length, 1); 181 paramInfo = cstr.getSignature()[0]; 182 checkParam(paramInfo, "The locale", "locale"); 183 } 184 185 188 public void testOverloadedConstructors() throws Exception 189 { 190 I18NStandardMBean mbean = 191 new I18NStandardMBeanSupport.SubclassOverload(); 192 MBeanInfo info = mbean.getMBeanInfo(); 193 info = mbean.getMBeanInfo(); MBeanConstructorInfo [] cstrs = info.getConstructors(); 196 assertEquals(cstrs.length, 6); 197 Map mapCstrForDesc = makeFeatureMap(cstrs); 198 199 MBeanConstructorInfo cstr = 201 (MBeanConstructorInfo )mapCstrForDesc.get("The no-args constructor"); 202 assertNotNull(cstr); 203 assertEquals(cstr.getSignature().length, 0); 204 205 cstr = 207 (MBeanConstructorInfo )mapCstrForDesc.get("The Object constructor"); 208 assertNotNull(cstr); 209 assertEquals(cstr.getSignature().length, 1); 210 checkParam(cstr.getSignature()[0], "an object", "obj"); 211 212 cstr = (MBeanConstructorInfo )mapCstrForDesc.get("The int constructor"); 214 assertNotNull(cstr); 215 assertEquals(cstr.getSignature().length, 1); 216 checkParam(cstr.getSignature()[0], "a number", "value"); 217 218 cstr = 220 (MBeanConstructorInfo )mapCstrForDesc.get("The int[] constructor"); 221 assertNotNull(cstr); 222 assertEquals(cstr.getSignature().length, 1); 223 checkParam(cstr.getSignature()[0], "an array of int", "intArray"); 224 225 cstr = 227 (MBeanConstructorInfo )mapCstrForDesc.get("The Object[] constructor"); 228 assertNotNull(cstr); 229 assertEquals(cstr.getSignature().length, 1); 230 checkParam(cstr.getSignature()[0], "an array of Object", "objArray"); 231 232 cstr = 234 (MBeanConstructorInfo )mapCstrForDesc.get("A two parameter int,Object constructor"); 235 assertNotNull(cstr); 236 assertEquals(cstr.getSignature().length, 2); 237 checkParam(cstr.getSignature()[0], "a number", "a"); 238 checkParam(cstr.getSignature()[1], "an object", "b"); 239 } 240 241 244 public void testAmbiguousConstructors() throws Exception 245 { 246 System.out.println("****"); 247 I18NStandardMBean mbean = 248 new I18NStandardMBeanSupport.SubclassAmbiguousConstructors(); 249 MBeanInfo info = mbean.getMBeanInfo(); 250 info = mbean.getMBeanInfo(); 253 MBeanConstructorInfo [] cstrs = info.getConstructors(); 254 assertEquals(cstrs.length, 7); 255 Map mapCstrForDesc = makeFeatureMap(cstrs); 256 assertEquals(mapCstrForDesc.size(), 3); 258 MBeanConstructorInfo cstr = 260 (MBeanConstructorInfo )mapCstrForDesc.get("The no-args constructor"); 261 assertNotNull(cstr); 262 assertEquals(cstr.getSignature().length, 0); 263 264 cstr = 266 (MBeanConstructorInfo )mapCstrForDesc.get("ambiguous constructor"); 267 assertNotNull(cstr); 268 269 cstr = 271 (MBeanConstructorInfo )mapCstrForDesc.get("The two parameter constructor"); 272 assertNotNull(cstr); 273 assertEquals(cstr.getSignature().length, 2); 274 checkParam(cstr.getSignature()[0], "a number", "a"); 275 checkParam(cstr.getSignature()[1], "another number", "b"); 276 } 277 278 281 public void testOverloadedOperations() throws Exception 282 { 283 I18NStandardMBean mbean = 284 new I18NStandardMBeanSupport.SubclassOverload(); 285 MBeanInfo info = mbean.getMBeanInfo(); 286 info = mbean.getMBeanInfo(); MBeanOperationInfo [] ops = info.getOperations(); 288 assertEquals(ops.length, 8); 290 Map mapOpsForDesc = makeFeatureMap(ops); 291 292 MBeanOperationInfo op = 294 (MBeanOperationInfo )mapOpsForDesc.get("a no parameter operation"); 295 assertNotNull(op); 296 assertEquals(op.getSignature().length, 0); 297 298 op = (MBeanOperationInfo )mapOpsForDesc.get("an int operation"); 300 assertNotNull(op); 301 assertEquals(op.getSignature().length, 1); 302 checkParam(op.getSignature()[0], "a number", "value"); 303 304 op = (MBeanOperationInfo )mapOpsForDesc.get("an Object operation"); 306 assertNotNull(op); 307 assertEquals(op.getSignature().length, 1); 308 checkParam(op.getSignature()[0], "an object", "obj"); 309 310 op = (MBeanOperationInfo )mapOpsForDesc.get("an int[] operation"); 312 assertNotNull(op); 313 assertEquals(op.getSignature().length, 1); 314 checkParam(op.getSignature()[0], "an array of int", "intArray"); 315 316 op = (MBeanOperationInfo )mapOpsForDesc.get("an Object[] operation"); 318 assertNotNull(op); 319 assertEquals(op.getSignature().length, 1); 320 checkParam(op.getSignature()[0], "an array of Object", "objArray"); 321 322 op = 324 (MBeanOperationInfo )mapOpsForDesc.get("a two parameter int,Object operation"); 325 assertNotNull(op); 326 assertEquals(op.getSignature().length, 2); 327 checkParam(op.getSignature()[0], "a number", "a"); 328 checkParam(op.getSignature()[1], "an object", "obj"); 329 330 op = 332 (MBeanOperationInfo )mapOpsForDesc.get("a three parameter int,int,int operation"); 333 assertNotNull(op); 334 assertEquals(op.getSignature().length, 3); 335 checkParam(op.getSignature()[0], "a number", "i"); 336 checkParam(op.getSignature()[1], "another number", "j"); 337 checkParam(op.getSignature()[2], "yet another number", "k"); 338 339 op = 341 (MBeanOperationInfo )mapOpsForDesc.get("a three parameter int,int,String operation"); 342 assertNotNull(op); 343 assertEquals(op.getSignature().length, 3); 344 checkParam(op.getSignature()[0], "a number", "i"); 345 checkParam(op.getSignature()[1], "another number", "j"); 346 checkParam(op.getSignature()[2], "a String", "s"); 347 348 } 349 350 353 public void testAmbiguousOperations() throws Exception 354 { 355 I18NStandardMBean mbean = 356 new I18NStandardMBeanSupport.SubclassAmbiguousOperation(); 357 MBeanInfo info = mbean.getMBeanInfo(); 358 info = mbean.getMBeanInfo(); MBeanOperationInfo [] ops = info.getOperations(); 361 assertEquals(ops.length, 8); 362 Map mapOpsForDesc = makeFeatureMap(ops); 363 assertEquals(mapOpsForDesc.size(), 3); 365 MBeanOperationInfo op = 367 (MBeanOperationInfo )mapOpsForDesc.get("a no parameter operation"); 368 assertNotNull(op); 369 assertEquals(op.getSignature().length, 0); 370 371 op = 373 (MBeanOperationInfo )mapOpsForDesc.get("a two parameter int,Object operation"); 374 assertNotNull(op); 375 376 op = 378 (MBeanOperationInfo )mapOpsForDesc.get("a two parameter int,Object operation"); 379 assertNotNull(op); 380 assertEquals(op.getSignature().length, 2); 381 checkParam(op.getSignature()[0], "a number", "a"); 382 checkParam(op.getSignature()[1], "an object", "obj"); 383 384 } 385 386 389 public void testPartialDesciptions() throws Exception 390 { 391 setDefaultLocale(Locale.ITALY); I18NStandardMBean mbean = 393 new I18NStandardMBeanSupport.SubclassNameOnly(); 394 MBeanInfo info = mbean.getMBeanInfo(); 395 info = mbean.getMBeanInfo(); assertEquals(info.getDescription(), 397 "The default subclassed name only MBean description"); 398 MBeanAttributeInfo attrInfo = info.getAttributes()[0]; 399 assertEquals(attrInfo.getDescription(), "??(attr.Attrib)"); 400 MBeanOperationInfo opInfo = info.getOperations()[0]; 401 assertEquals(opInfo.getDescription(), "??(op.operation)"); 402 MBeanParameterInfo paramInfo = opInfo.getSignature()[0]; 403 checkParam(paramInfo, "??(op.operation.param.1)", "param1"); 404 paramInfo = opInfo.getSignature()[1]; 405 checkParam(paramInfo, "??(op.operation.param.2)", "param2"); 406 407 MBeanConstructorInfo [] cstrs = info.getConstructors(); 408 assertEquals(cstrs.length, 2); 409 dumpConstructors(info); 410 411 Map mapCstrForDesc = makeFeatureMap(cstrs); 412 assertEquals(mapCstrForDesc.size(), 1); MBeanConstructorInfo cstr = 414 (MBeanConstructorInfo )mapCstrForDesc.get("ambiguous constructor"); 415 assertNotNull(cstr); 416 } 417 418 421 public void testMissingBundle() throws Exception 422 { 423 try 424 { 425 I18NStandardMBean mbean = 426 new I18NStandardMBeanSupport.SubclassNoBundle(); 427 assertTrue(false); 428 } 429 catch (MissingResourceException e) 430 { 431 } 432 433 try 434 { 435 I18NStandardMBeanSupport.ImplementationNoBundle impl = 436 new I18NStandardMBeanSupport.ImplementationNoBundle(); 437 I18NStandardMBean mbean = 438 new I18NStandardMBean(impl, 439 I18NStandardMBeanSupport.FullManagement.class); 440 MBeanInfo info = mbean.getMBeanInfo(); 441 assertTrue(false); 442 } 443 catch (MissingResourceException e) 444 { 445 } 446 } 447 448 private Map makeFeatureMap(MBeanFeatureInfo [] features) 450 { 451 Map mapFeatureForDesc = new HashMap (); for (int i = 0; i < features.length; i++) 453 { 454 mapFeatureForDesc.put(features[i].getDescription(), features[i]); 455 } 456 return mapFeatureForDesc; 457 } 458 459 private void dumpConstructors(MBeanInfo info) 460 { 461 MBeanConstructorInfo [] consts = info.getConstructors(); 462 System.out.println("NB cons = " + consts.length); 463 for (int i = 0; i < consts.length; i++) 464 { 465 System.out.println("Cons " + i + ":" + consts[i].getDescription()); 466 MBeanParameterInfo [] params = consts[i].getSignature(); 467 for (int j = 0; j < params.length; j++) 468 { 469 MBeanParameterInfo p = params[j]; 470 System.out.println(" Param " 471 + j 472 + ":" 473 + p.getDescription() 474 + " name=" 475 + p.getName() 476 + " type=" 477 + p.getType()); 478 } 479 } 480 } 481 482 private void dumpOperations(MBeanInfo info) 483 { 484 MBeanOperationInfo [] ops = info.getOperations(); 485 System.out.println("NB operations = " + ops.length); 486 for (int i = 0; i < ops.length; i++) 487 { 488 System.out.println("Op " + i + ":" + ops[i].getDescription()); 489 MBeanParameterInfo [] params = ops[i].getSignature(); 490 for (int j = 0; j < params.length; j++) 491 { 492 MBeanParameterInfo p = params[j]; 493 System.out.println(" Param " 494 + j 495 + ":" 496 + p.getDescription() 497 + " name=" 498 + p.getName() 499 + " type=" 500 + p.getType()); 501 } 502 } 503 } 504 505 private void setLocaleProp(String s) 506 { 507 System.setProperty("mx4j.descriptionLocale", s); 508 } 509 510 private void setDefaultLocale(Locale locale) 511 { 512 Locale.setDefault(locale); 513 } 514 515 private void checkParam(MBeanParameterInfo paramInfo, 516 String description, 517 String name) 518 { 519 assertEquals(paramInfo.getDescription(), description); 520 assertEquals(paramInfo.getName(), name); 521 } 522 523 } 524 | Popular Tags |