1 8 9 package com.sleepycat.je.jmx; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.io.ObjectOutputStream ; 15 import java.io.Serializable ; 16 import java.lang.reflect.Method ; 17 import java.util.Arrays ; 18 import java.util.List ; 19 20 import javax.management.Attribute ; 21 import javax.management.DynamicMBean ; 22 import javax.management.JMException ; 23 import javax.management.MBeanAttributeInfo ; 24 import javax.management.MBeanConstructorInfo ; 25 import javax.management.MBeanException ; 26 import javax.management.MBeanInfo ; 27 import javax.management.MBeanNotificationInfo ; 28 import javax.management.MBeanOperationInfo ; 29 import javax.management.MBeanParameterInfo ; 30 31 import jmx.JEApplicationMBean; 32 import junit.framework.TestCase; 33 34 import com.sleepycat.bind.tuple.IntegerBinding; 35 import com.sleepycat.je.BtreeStats; 36 import com.sleepycat.je.Database; 37 import com.sleepycat.je.DatabaseConfig; 38 import com.sleepycat.je.DatabaseEntry; 39 import com.sleepycat.je.DatabaseException; 40 import com.sleepycat.je.DbInternal; 41 import com.sleepycat.je.Environment; 42 import com.sleepycat.je.EnvironmentConfig; 43 import com.sleepycat.je.util.TestUtils; 44 45 48 public class MBeanTest extends TestCase { 49 50 private static final boolean DEBUG = true; 51 private File envHome; 52 private String environmentDir; 53 54 public MBeanTest() { 55 environmentDir = System.getProperty(TestUtils.DEST_DIR); 56 envHome = new File (environmentDir); 57 } 58 59 public void setUp() 60 throws IOException { 61 62 TestUtils.removeLogFiles("Setup", envHome, false); 63 } 64 65 public void tearDown() 66 throws Exception { 67 68 TestUtils.removeLogFiles("tearDown", envHome, true); 69 } 70 71 75 public void testNoOpenMBean() 76 throws Throwable { 77 78 Environment env = null; 79 try { 80 81 82 DynamicMBean mbean = new JEMonitor(environmentDir); 83 validateGetters(mbean, 2); 84 validateOperations(mbean, 0, true, null, null); 85 86 87 env = openEnv(true); 88 validateGetters(mbean, 2 ); validateGetters(mbean, 9 ); validateOperations(mbean, 8, true, null, null); 91 92 93 env.close(); 94 validateGetters(mbean, 2); 95 validateOperations(mbean, 0, true, null, null); 96 97 101 env = openEnv(true); 102 mbean = new JEMonitor(environmentDir); 103 validateGetters(mbean, 9 ); validateOperations(mbean, 8, true, null, null); 105 106 110 try { 111 validateOperations(mbean, 8, true, "bozo", null); 112 fail("Should not have run stats on a non-existent db"); 113 } catch (MBeanException expected) { 114 } 116 117 121 DatabaseConfig dbConfig = new DatabaseConfig(); 122 dbConfig.setAllowCreate(true); 123 dbConfig.setTransactional(true); 124 Database db = env.openDatabase(null, "bozo", dbConfig); 125 126 127 DatabaseEntry entry = new DatabaseEntry(); 128 IntegerBinding.intToEntry(1, entry); 129 db.put(null, entry, entry); 130 131 validateOperations(mbean, 8, true, "bozo", new String [] {"bozo"}); 132 db.close(); 133 134 env.close(); 135 validateGetters(mbean, 2); 136 validateOperations(mbean, 0, true, null, null); 137 138 checkForNoOpenHandles(environmentDir); 139 } catch (Throwable t) { 140 t.printStackTrace(); 141 if (env != null) { 142 env.close(); 143 } 144 throw t; 145 } 146 } 147 148 151 public void testOpenableBean() 152 throws Throwable { 153 154 Environment env = null; 155 try { 156 157 env = openEnv(false); 158 env.close(); 159 160 DynamicMBean mbean = new JEApplicationMBean(environmentDir); 161 validateGetters(mbean, 5); 162 validateOperations(mbean, 1, false, null, null); 164 165 mbean.invoke(JEApplicationMBean.OP_OPEN, null, null); 166 167 validateGetters(mbean, 7 ); 168 validateOperations(mbean, 8, true, null, null); 169 170 174 validateGetters(mbean, 5); 175 validateOperations(mbean, 1, false, null, null); 176 177 178 checkForNoOpenHandles(environmentDir); 179 } catch (Throwable t) { 180 t.printStackTrace(); 181 182 if (env != null) { 183 env.close(); 184 } 185 throw t; 186 } 187 } 188 189 192 public void testMBeanSetters() 193 throws Throwable { 194 195 Environment env = null; 196 try { 197 198 env = openEnv(false); 199 200 201 DynamicMBean mbean = new JEMonitor(environmentDir); 202 203 207 EnvironmentConfig config = env.getConfig(); 208 Class configClass = config.getClass(); 209 210 Method getCacheSize = configClass.getMethod("getCacheSize", (Class []) null); 211 checkAttribute(env, 212 mbean, 213 getCacheSize, 214 JEMBeanHelper.ATT_CACHE_SIZE, 215 new Long (100000)); 217 Method getCachePercent = 218 configClass.getMethod("getCachePercent", (Class []) null); 219 checkAttribute(env, 220 mbean, 221 getCachePercent, 222 JEMBeanHelper.ATT_CACHE_PERCENT, 223 new Integer (10)); 224 env.close(); 225 226 checkForNoOpenHandles(environmentDir); 227 } catch (Throwable t) { 228 t.printStackTrace(); 229 230 if (env != null) { 231 env.close(); 232 } 233 234 throw t; 235 } 236 } 237 238 private void checkAttribute(Environment env, 239 DynamicMBean mbean, 240 Method configMethod, 241 String attributeName, 242 Object newValue) 243 throws Exception { 244 245 EnvironmentConfig config = env.getConfig(); 246 Object result = configMethod.invoke(config, (Object []) null); 247 assertTrue(!result.toString().equals(newValue.toString())); 248 249 250 mbean.setAttribute(new Attribute (attributeName, newValue)); 251 252 253 config = env.getConfig(); 254 assertEquals(newValue.toString(), 255 configMethod.invoke(config, (Object []) null).toString()); 256 257 258 Object mbeanNewValue = mbean.getAttribute(attributeName); 259 assertEquals(newValue.toString(), mbeanNewValue.toString()); 260 } 261 262 264 private void validateGetters(DynamicMBean mbean, 265 int numExpectedAttributes) 266 throws Throwable { 267 268 MBeanInfo info = mbean.getMBeanInfo(); 269 270 MBeanAttributeInfo [] attrs = info.getAttributes(); 271 272 273 int attributesWithValues = 0; 274 for (int i = 0; i < attrs.length; i++) { 275 String name = attrs[i].getName(); 276 Object result = mbean.getAttribute(name); 277 if (DEBUG) { 278 System.out.println("Attribute " + i + 279 " name=" + name + 280 " result=" + result); 281 } 282 if (result != null) { 283 attributesWithValues++; 284 checkObjectType 285 ("Attribute", name, attrs[i].getType(), result); 286 } 287 } 288 289 assertEquals(numExpectedAttributes, attributesWithValues); 290 } 291 292 299 private void validateOperations(DynamicMBean mbean, 300 int numExpectedOperations, 301 boolean tryInvoke, 302 String databaseName, 303 String [] expectedDatabases) 304 throws Throwable { 305 306 MBeanInfo info = mbean.getMBeanInfo(); 307 308 MBeanOperationInfo [] ops = info.getOperations(); 309 if (DEBUG) { 310 for (int i = 0; i < ops.length; i++) { 311 System.out.println("op: " + ops[i].getName()); 312 } 313 } 314 assertEquals(numExpectedOperations, ops.length); 315 316 if (tryInvoke) { 317 for (int i = 0; i < ops.length; i++) { 318 String opName = ops[i].getName(); 319 320 321 if ((databaseName != null) && 322 opName.equals(JEMBeanHelper.OP_DB_STAT)) { 323 324 Object result = mbean.invoke 325 (opName, 326 new Object [] {null, null, databaseName}, 327 null); 328 assertTrue(result instanceof BtreeStats); 329 checkObjectType 330 ("Operation", opName, ops[i].getReturnType(), result); 331 } 332 333 if ((expectedDatabases != null) && 334 opName.equals(JEMBeanHelper.OP_DB_NAMES)) { 335 Object result = mbean.invoke(opName, null, null); 336 List names = (List ) result; 337 assertTrue(Arrays.equals(expectedDatabases, 338 names.toArray())); 339 checkObjectType 340 ("Operation", opName, ops[i].getReturnType(), result); 341 } 342 343 347 Object result = mbean.invoke(opName, null, null); 348 if (result != null) { 349 checkObjectType 350 ("Operation", opName, ops[i].getReturnType(), result); 351 } 352 } 353 } 354 } 355 356 360 public void testSerializable() 361 throws JMException , DatabaseException { 362 363 364 Environment env = openEnv(false); 365 env.close(); 366 367 368 DynamicMBean mbean = new JEApplicationMBean(environmentDir); 369 doTestSerializable(mbean); 370 371 372 mbean.invoke(JEApplicationMBean.OP_OPEN, null, null); 373 doTestSerializable(mbean); 374 375 376 mbean.invoke(JEApplicationMBean.OP_CLOSE, null, null); 377 } 378 379 382 private void doTestSerializable(DynamicMBean mbean) { 383 384 MBeanInfo info = mbean.getMBeanInfo(); 385 386 MBeanAttributeInfo [] attrs = info.getAttributes(); 387 for (int i = 0; i < attrs.length; i++) { 388 checkSerializable 389 ("Attribute", attrs[i].getName(), attrs[i].getType()); 390 } 391 392 MBeanOperationInfo [] ops = info.getOperations(); 393 for (int i = 0; i < ops.length; i += 1) { 394 checkSerializable 395 ("Operation", 396 ops[i].getName() + " return type", 397 ops[i].getReturnType()); 398 MBeanParameterInfo [] params = ops[i].getSignature(); 399 for (int j = 0; j < params.length; j += 1) { 400 checkSerializable 401 ("Operation", 402 ops[i].getName() + " parameter " + j, 403 params[j].getType()); 404 } 405 } 406 407 MBeanConstructorInfo [] ctors = info.getConstructors(); 408 for (int i = 0; i < ctors.length; i++) { 409 MBeanParameterInfo [] params = ctors[i].getSignature(); 410 for (int j = 0; j < params.length; j += 1) { 411 checkSerializable 412 ("Constructor", 413 ctors[i].getName() + " parameter " + j, 414 params[j].getType()); 415 } 416 } 417 418 MBeanNotificationInfo [] notifs = info.getNotifications(); 419 for (int i = 0; i < notifs.length; i++) { 420 String [] types = notifs[i].getNotifTypes(); 421 for (int j = 0; j < types.length; j += 1) { 422 checkSerializable 423 ("Notification", notifs[i].getName(), types[j]); 424 } 425 } 426 } 427 428 431 private void checkSerializable(String identifier, 432 String name, 433 String type) { 434 435 if ("void".equals(type)) { 436 return; 437 } 438 String msg = identifier + ' ' + name + " is type " + type; 439 try { 440 Class cls = Class.forName(type); 441 if (!Serializable .class.isAssignableFrom(cls)) { 442 fail(msg + " -- not Serializable"); 443 } 444 } catch (Exception e) { 445 fail(msg + " -- " + e); 446 } 447 } 448 449 453 private void checkObjectType(String identifier, 454 String name, 455 String type, 456 Object object) { 457 458 String msg = identifier + ' ' + name + " is type " + type; 459 if ("void".equals(type)) { 460 assertNull(msg + "-- should be null", object); 461 return; 462 } 463 try { 464 Class cls = Class.forName(type); 465 assertTrue 466 (msg + " -- object class is " + object.getClass().getName(), 467 cls.isAssignableFrom(object.getClass())); 468 } catch (Exception e) { 469 fail(msg + " -- " + e); 470 } 471 472 476 try { 477 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 478 ObjectOutputStream oos = new ObjectOutputStream (baos); 479 oos.writeObject(object); 480 } catch (Exception e) { 481 fail(msg + " -- " + e); 482 } 483 } 484 485 private void checkForNoOpenHandles(String environmentDir) { 486 File envFile = new File (environmentDir); 487 Environment testEnv = DbInternal.getEnvironmentShell(envFile); 488 assertTrue(testEnv == null); 489 } 490 491 494 private Environment openEnv(boolean openTransactionally) 495 throws DatabaseException { 496 497 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 498 envConfig.setAllowCreate(true); 499 envConfig.setTransactional(openTransactionally); 500 return new Environment(envHome, envConfig); 501 } 502 503 } 504 | Popular Tags |