1 22 package org.jboss.test.jmx.compliance.openmbean; 23 24 import junit.framework.TestCase; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.util.Arrays ; 31 import java.util.Set ; 32 33 import javax.management.MBeanOperationInfo ; 34 import javax.management.openmbean.OpenMBeanParameterInfo ; 35 import javax.management.openmbean.OpenMBeanParameterInfoSupport ; 36 import javax.management.openmbean.OpenMBeanOperationInfoSupport ; 37 import javax.management.openmbean.OpenType ; 38 import javax.management.openmbean.SimpleType ; 39 40 45 public class OpenMBeanOperationInfoSupportTestCase 46 extends TestCase 47 { 48 50 52 54 57 public OpenMBeanOperationInfoSupportTestCase(String s) 58 { 59 super(s); 60 } 61 62 64 public void testOpenMBeanOperationInfoSupport() 65 throws Exception 66 { 67 OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport ( 68 "name", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 69 assertEquals("name", info.getName()); 70 assertEquals("description", info.getDescription()); 71 assertEquals(0, info.getSignature().length); 72 assertEquals("java.lang.String", info.getReturnType()); 73 assertEquals(SimpleType.STRING, info.getReturnOpenType()); 74 assertEquals(MBeanOperationInfo.ACTION_INFO, info.getImpact()); 75 76 info = new OpenMBeanOperationInfoSupport ( 77 "name", "description", new OpenMBeanParameterInfoSupport [0], 78 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 79 assertEquals("name", info.getName()); 80 assertEquals("description", info.getDescription()); 81 assertEquals(0, info.getSignature().length); 82 assertEquals("java.lang.String", info.getReturnType()); 83 assertEquals(SimpleType.STRING, info.getReturnOpenType()); 84 assertEquals(MBeanOperationInfo.ACTION_INFO, info.getImpact()); 85 86 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 87 { 88 new OpenMBeanParameterInfoSupport ( 89 "name", "description", SimpleType.STRING) 90 }; 91 info = new OpenMBeanOperationInfoSupport ( 92 "name", "description", parms, 93 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 94 assertEquals("name", info.getName()); 95 assertEquals("description", info.getDescription()); 96 assertEquals(1, info.getSignature().length); 97 assertEquals("java.lang.String", info.getReturnType()); 98 assertEquals(SimpleType.STRING, info.getReturnOpenType()); 99 assertEquals(MBeanOperationInfo.ACTION_INFO, info.getImpact()); 100 } 101 102 public void testReturnOpenType() 103 throws Exception 104 { 105 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 106 { 107 new OpenMBeanParameterInfoSupport ( 108 "name", "description", SimpleType.STRING) 109 }; 110 OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport ( 111 "name", "description", parms, 112 SimpleType.BOOLEAN, MBeanOperationInfo.ACTION_INFO); 113 114 assertEquals(SimpleType.BOOLEAN, info.getReturnOpenType()); 115 } 116 117 public void testEquals() 118 throws Exception 119 { 120 OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport ( 121 "name", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 122 123 assertTrue("Null should not be equal", info.equals(null) == false); 124 assertTrue("Only OpenMBeanOperationInfo should be equal", info.equals(new Object ()) == false); 125 126 OpenMBeanOperationInfoSupport info2 = new OpenMBeanOperationInfoSupport ( 127 "name", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 128 129 assertTrue("Different instances of the same data are equal", info.equals(info2)); 130 assertTrue("Different instances of the same data are equal", info2.equals(info)); 131 132 info2 = new OpenMBeanOperationInfoSupport ( 133 "name", "description2", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 134 135 assertTrue("Different instances with different descriptions are equal", info.equals(info2)); 136 assertTrue("Different instances with different descritpions are equal", info2.equals(info)); 137 138 info2 = new OpenMBeanOperationInfoSupport ( 139 "name2", "description", null, SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 140 141 assertTrue("Instances with different names are not equal", info.equals(info2) == false); 142 assertTrue("Instances with different names are not equal", info2.equals(info) == false); 143 144 OpenMBeanParameterInfoSupport param1 = new OpenMBeanParameterInfoSupport ( 145 "name", "description", SimpleType.STRING); 146 OpenMBeanParameterInfoSupport param2 = new OpenMBeanParameterInfoSupport ( 147 "name2", "description", SimpleType.STRING); 148 149 info = new OpenMBeanOperationInfoSupport ( 150 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }, 151 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 152 info2 = new OpenMBeanOperationInfoSupport ( 153 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }, 154 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 155 156 assertTrue("Different instances with the same parameters are equal", info.equals(info2)); 157 assertTrue("Different instances with the same parameters are equal", info2.equals(info)); 158 159 info = new OpenMBeanOperationInfoSupport ( 160 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }, 161 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 162 info2 = new OpenMBeanOperationInfoSupport ( 163 "name", "description", new OpenMBeanParameterInfoSupport [] { param2, param1 }, 164 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 165 166 assertTrue("Different instances with the same signature but different parameters are not equal", 167 info.equals(info2) == false); 168 assertTrue("Different instances with the same signature but different parameters are not equal", 169 info2.equals(info) == false); 170 171 param2 = new OpenMBeanParameterInfoSupport ( 172 "name2", "description", SimpleType.INTEGER); 173 info2 = new OpenMBeanOperationInfoSupport ( 174 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }, 175 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 176 177 assertTrue("Different instances with different signatures are not equal", 178 info.equals(info2) == false); 179 assertTrue("Different instances with different signatures are not equal", 180 info2.equals(info) == false); 181 182 info2 = new OpenMBeanOperationInfoSupport ( 183 "name", "description", new OpenMBeanParameterInfoSupport [] { param1 }, 184 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 185 186 assertTrue("Different instances with different numbers of paramters are not equal", 187 info.equals(info2) == false); 188 assertTrue("Different instances with different numbers of parameters are not equal", 189 info2.equals(info) == false); 190 191 info2 = new OpenMBeanOperationInfoSupport ( 192 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }, 193 SimpleType.INTEGER, MBeanOperationInfo.ACTION_INFO); 194 195 assertTrue("Different instances with different return types are not equal", 196 info.equals(info2) == false); 197 assertTrue("Different instances with different return types are not equal", 198 info2.equals(info) == false); 199 200 info2 = new OpenMBeanOperationInfoSupport ( 201 "name", "description", new OpenMBeanParameterInfoSupport [] { param1, param2 }, 202 SimpleType.STRING, MBeanOperationInfo.ACTION); 203 204 assertTrue("Different instances with different impacts are not equal", 205 info.equals(info2) == false); 206 assertTrue("Different instances with different impacts are not equal", 207 info2.equals(info) == false); 208 } 209 210 public void testHashCode() 211 throws Exception 212 { 213 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 214 { 215 new OpenMBeanParameterInfoSupport ( 216 "name", "description", SimpleType.STRING) 217 }; 218 OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport ( 219 "name", "description", parms, 220 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 221 222 int myHash = "name".hashCode() + Arrays.asList(parms).hashCode() 223 + SimpleType.STRING.hashCode() + MBeanOperationInfo.ACTION_INFO; 224 assertEquals(myHash, info.hashCode()); 225 } 226 227 public void testToString() 228 throws Exception 229 { 230 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 231 { 232 new OpenMBeanParameterInfoSupport ( 233 "name", "description", SimpleType.STRING) 234 }; 235 OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport ( 236 "NAME", "DESCRIPTION", parms, 237 SimpleType.INTEGER, MBeanOperationInfo.ACTION_INFO); 238 239 String toString = info.toString(); 240 241 assertTrue("info.toString() should contain NAME", 242 toString.indexOf("NAME") != -1); 243 assertTrue("info.toString() should contain the parameters", 244 toString.indexOf(Arrays.asList(parms).toString()) != -1); 245 assertTrue("info.toString() should contain the simple type", 246 toString.indexOf(SimpleType.INTEGER.toString()) != -1); 247 } 248 249 public void testSerialization() 250 throws Exception 251 { 252 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 253 { 254 new OpenMBeanParameterInfoSupport ( 255 "name", "description", SimpleType.STRING) 256 }; 257 OpenMBeanOperationInfoSupport info = new OpenMBeanOperationInfoSupport ( 258 "name", "description", parms, 259 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 260 261 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 263 ObjectOutputStream oos = new ObjectOutputStream (baos); 264 oos.writeObject(info); 265 266 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 268 ObjectInputStream ois = new ObjectInputStream (bais); 269 Object result = ois.readObject(); 270 271 assertEquals(info, result); 272 } 273 274 public void testErrors() 275 throws Exception 276 { 277 boolean caught = false; 278 try 279 { 280 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 281 { 282 new OpenMBeanParameterInfoSupport ( 283 "name", "description", SimpleType.STRING) 284 }; 285 new OpenMBeanOperationInfoSupport ( 286 null, "description", parms, 287 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 288 } 289 catch (IllegalArgumentException e) 290 { 291 caught = true; 292 } 293 if (caught == false) 294 fail("Expected IllegalArgumentException for null name"); 295 296 caught = false; 297 try 298 { 299 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 300 { 301 new OpenMBeanParameterInfoSupport ( 302 "name", "description", SimpleType.STRING) 303 }; 304 new OpenMBeanOperationInfoSupport ( 305 "", "description", parms, 306 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 307 } 308 catch (IllegalArgumentException e) 309 { 310 caught = true; 311 } 312 if (caught == false) 313 fail("Expected IllegalArgumentException for empty name"); 314 315 caught = false; 316 try 317 { 318 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 319 { 320 new OpenMBeanParameterInfoSupport ( 321 "name", "description", SimpleType.STRING) 322 }; 323 new OpenMBeanOperationInfoSupport ( 324 "name", null, parms, 325 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 326 } 327 catch (IllegalArgumentException e) 328 { 329 caught = true; 330 } 331 if (caught == false) 332 fail("Expected IllegalArgumentException for null description"); 333 334 caught = false; 335 try 336 { 337 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 338 { 339 new OpenMBeanParameterInfoSupport ( 340 "name", "description", SimpleType.STRING) 341 }; 342 new OpenMBeanOperationInfoSupport ( 343 "name", "", parms, 344 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 345 } 346 catch (IllegalArgumentException e) 347 { 348 caught = true; 349 } 350 if (caught == false) 351 fail("Expected IllegalArgumentException for empty description"); 352 353 caught = false; 354 try 355 { 356 new OpenMBeanOperationInfoSupport ( 357 "name", "description", new MyOpenMBeanParameterInfo[] { new MyOpenMBeanParameterInfo() }, 358 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO); 359 } 360 catch (ArrayStoreException e) 361 { 362 caught = true; 363 } 364 if (caught == false) 365 fail("Expected ArrayStoreException for non MBeanParameterInfo array"); 366 367 caught = false; 368 try 369 { 370 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 371 { 372 new OpenMBeanParameterInfoSupport ( 373 "name", "description", SimpleType.STRING) 374 }; 375 new OpenMBeanOperationInfoSupport ( 376 "name", "description", parms, 377 null, MBeanOperationInfo.ACTION_INFO); 378 } 379 catch (IllegalArgumentException e) 380 { 381 caught = true; 382 } 383 if (caught == false) 384 fail("Expected IllegalArgumentException for null return type"); 385 386 caught = false; 387 try 388 { 389 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 390 { 391 new OpenMBeanParameterInfoSupport ( 392 "name", "description", SimpleType.STRING) 393 }; 394 new OpenMBeanOperationInfoSupport ( 395 "name", "description", parms, 396 SimpleType.VOID, MBeanOperationInfo.ACTION_INFO); 397 } 398 catch (IllegalArgumentException e) 399 { 400 caught = true; 401 } 402 if (caught == true) 403 fail("Didn't expect IllegalArgumentException for VOID return type"); 404 405 caught = false; 406 try 407 { 408 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 409 { 410 new OpenMBeanParameterInfoSupport ( 411 "name", "description", SimpleType.STRING) 412 }; 413 new OpenMBeanOperationInfoSupport ( 414 "name", "description", parms, 415 SimpleType.STRING, 1234567); 416 } 417 catch (IllegalArgumentException e) 418 { 419 caught = true; 420 } 421 if (caught == false) 422 fail("Expected IllegalArgumentExecption for invalid action"); 423 424 caught = false; 425 try 426 { 427 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 428 { 429 new OpenMBeanParameterInfoSupport ( 430 "name", "description", SimpleType.STRING) 431 }; 432 new OpenMBeanOperationInfoSupport ( 433 "name", "description", parms, 434 SimpleType.STRING, MBeanOperationInfo.UNKNOWN); 435 } 436 catch (IllegalArgumentException e) 437 { 438 caught = true; 439 } 440 if (caught == false) 441 fail("Expected IllegalArgumentExecption for UNKNOWN action"); 442 } 443 444 public static class MyOpenMBeanParameterInfo 445 implements OpenMBeanParameterInfo 446 { 447 public boolean equals(Object o) { return false; } 448 public Object getDefaultValue() { return null; } 449 public String getDescription() { return null; } 450 public Set getLegalValues() { return null; } 451 public Comparable getMaxValue() { return null; } 452 public Comparable getMinValue() { return null; } 453 public String getName() { return null; } 454 public OpenType getOpenType() { return null; } 455 public boolean hasDefaultValue() { return false; } 456 public boolean hasLegalValues() { return false; } 457 public int hashCode() { return 0; } 458 public boolean hasMaxValue() { return false; } 459 public boolean hasMinValue() { return false; } 460 public boolean isValue(Object o) { return false; } 461 public String toString() { return null; } 462 } 463 } 464 | Popular Tags |