1 22 package org.jboss.test.jbossmx.compliance.monitor; 23 24 import java.util.ArrayList ; 25 26 import javax.management.Attribute ; 27 import javax.management.AttributeList ; 28 import javax.management.MBeanServer ; 29 import javax.management.MBeanServerFactory ; 30 import javax.management.Notification ; 31 import javax.management.NotificationFilter ; 32 import javax.management.NotificationListener ; 33 import javax.management.ObjectName ; 34 import javax.management.monitor.CounterMonitor ; 35 import javax.management.monitor.GaugeMonitor ; 36 import javax.management.monitor.MonitorNotification ; 37 import javax.management.monitor.StringMonitor ; 38 39 import org.jboss.test.jbossmx.compliance.TestCase; 40 import org.jboss.test.jbossmx.compliance.monitor.support.CounterSupport; 41 import org.jboss.test.jbossmx.compliance.monitor.support.StringSupport; 42 43 52 public class BasicTestCase 53 extends TestCase 54 implements NotificationListener 55 { 56 58 61 ObjectName monitorName; 62 63 66 MBeanServer server; 67 68 71 Object monitored; 72 73 76 ObjectName observedObject; 77 78 81 String observedAttribute; 82 83 86 ArrayList receivedNotifications = new ArrayList (); 87 88 90 public BasicTestCase(String s) 91 { 92 super(s); 93 } 94 95 97 100 public void testCounterSimpleNotification() 101 throws Exception 102 { 103 try 104 { 105 monitored = new CounterSupport(); 106 observedObject = new ObjectName ("Monitor:type=CounterSupport"); 107 observedAttribute = "Value"; 108 startCounterService(false, 0, 0, 10); 109 ObjectName [] observed = (ObjectName []) server.getAttribute(this.monitorName, 110 "ObservedObjects"); 111 assertTrue("ObservedObjects.length == 1", observed.length == 1); 112 assertTrue("ObservedObjects[0] == Monitor:type=CounterSupport", 113 observed[0].equals(observedObject)); 114 115 setAttribute(null, 0); 116 setAttribute(new Integer (10), 1); 117 setAttribute(new Integer (9), 1); 118 setAttribute(new Integer (10), 2); 119 } 120 finally 121 { 122 stopMonitorService(); 123 } 124 } 125 126 129 public void testCounterDifferenceNotification() 130 throws Exception 131 { 132 try 133 { 134 monitored = new CounterSupport(); 135 observedObject = new ObjectName ("Monitor:type=CounterSupport"); 136 observedAttribute = "Value"; 137 startCounterService(true, 0, 0, 10); 138 139 setAttribute(null, 0); 140 setAttribute(new Integer (10), 1); 141 setAttribute(new Integer (9), 1); 142 setAttribute(new Integer (10), 1); 143 setAttribute(new Integer (20), 2); 144 } 145 finally 146 { 147 stopMonitorService(); 148 } 149 } 150 151 154 public void testGaugeSimpleBothNotification() 155 throws Exception 156 { 157 try 158 { 159 monitored = new CounterSupport(); 160 observedObject = new ObjectName ("Monitor:type=GaugeSupport"); 161 observedAttribute = "Value"; 162 startGaugeService(true, true, false, 10, 0); 163 164 setAttribute(null, 1); 165 setAttribute(new Integer (10), 2); 166 setAttribute(new Integer (9), 2); 167 setAttribute(new Integer (10), 2); 168 setAttribute(new Integer (0), 3); 169 setAttribute(new Integer (1), 3); 170 setAttribute(new Integer (0), 3); 171 } 172 finally 173 { 174 stopMonitorService(); 175 } 176 } 177 178 181 public void testGaugeSimpleHighNotification() 182 throws Exception 183 { 184 try 185 { 186 monitored = new CounterSupport(); 187 observedObject = new ObjectName ("Monitor:type=GaugeSupport"); 188 observedAttribute = "Value"; 189 startGaugeService(true, false, false, 10, 0); 190 191 setAttribute(null, 0); 192 setAttribute(new Integer (10),1 ); 193 setAttribute(new Integer (9), 1); 194 setAttribute(new Integer (10), 1); 195 setAttribute(new Integer (0), 1); 196 setAttribute(new Integer (10), 2); 197 } 198 finally 199 { 200 stopMonitorService(); 201 } 202 } 203 204 207 public void testGaugeSimpleLowNotification() 208 throws Exception 209 { 210 try 211 { 212 monitored = new CounterSupport(); 213 observedObject = new ObjectName ("Monitor:type=GaugeSupport"); 214 observedAttribute = "Value"; 215 startGaugeService(false, true, false, 10, 0); 216 217 setAttribute(null, 1); 218 setAttribute(new Integer (10), 1); 219 setAttribute(new Integer (9), 1); 220 setAttribute(new Integer (0), 2); 221 setAttribute(new Integer (1), 2); 222 setAttribute(new Integer (0), 2); 223 } 224 finally 225 { 226 stopMonitorService(); 227 } 228 } 229 230 233 public void testStringBothNotification() 234 throws Exception 235 { 236 try 237 { 238 monitored = new StringSupport(); 239 observedObject = new ObjectName ("Monitor:type=StringSupport"); 240 observedAttribute = "Value"; 241 startStringService(true, true, "test"); 242 243 setAttribute(null, 0); 246 setAttribute("test", 1); 247 setAttribute("not-test", 2); 248 } 249 finally 250 { 251 stopMonitorService(); 252 } 253 } 254 255 258 public void testStringMatchNotification() 259 throws Exception 260 { 261 try 262 { 263 monitored = new StringSupport(); 264 observedObject = new ObjectName ("Monitor:type=StringSupport"); 265 observedAttribute = "Value"; 266 startStringService(true, false, "test"); 267 268 setAttribute(null, 0); 271 setAttribute("test", 1); 272 setAttribute("not-test", 1); 273 } 274 finally 275 { 276 stopMonitorService(); 277 } 278 } 279 280 283 public void testStringDifferNotification() 284 throws Exception 285 { 286 try 287 { 288 monitored = new StringSupport(); 289 observedObject = new ObjectName ("Monitor:type=StringSupport"); 290 observedAttribute = "Value"; 291 startStringService(false, true, "test"); 292 293 setAttribute(null, 0); 296 setAttribute("test", 0); 297 setAttribute("not-test", 1); 298 } 299 finally 300 { 301 stopMonitorService(); 302 } 303 } 304 305 307 314 private void startCounterService(boolean mode, int modulus, 315 int offset, int threshold) 316 throws Exception 317 { 318 installMonitorService(new CounterMonitor ()); 319 AttributeList attributes = new AttributeList (); 320 attributes.add(new Attribute ("DifferenceMode", new Boolean (mode))); 321 attributes.add(new Attribute ("Modulus", new Integer (modulus))); 322 attributes.add(new Attribute ("Offset", new Integer (offset))); 323 attributes.add(new Attribute ("Notify", new Boolean (true))); 324 attributes.add(new Attribute ("Threshold", new Integer (threshold))); 325 attributes.add(new Attribute ("GranularityPeriod", new Long (PERIOD))); 326 attributes.add(new Attribute ("ObservedObject", observedObject)); 327 attributes.add(new Attribute ("ObservedAttribute", observedAttribute)); 328 int before = attributes.size(); 329 attributes = server.setAttributes(monitorName, attributes); 330 assertEquals(before, attributes.size()); 331 332 server.invoke(monitorName, "start", new Object [0], new String [0]); 333 } 334 335 344 private void startGaugeService(boolean high, boolean low, boolean differ, 345 int highValue, int lowValue) 346 throws Exception 347 { 348 installMonitorService(new GaugeMonitor ()); 349 AttributeList attributes = new AttributeList (); 350 attributes.add(new Attribute ("NotifyHigh", new Boolean (high))); 351 attributes.add(new Attribute ("NotifyLow", new Boolean (low))); 352 attributes.add(new Attribute ("DifferenceMode", new Boolean (differ))); 353 attributes.add(new Attribute ("GranularityPeriod", new Long (PERIOD))); 354 attributes.add(new Attribute ("ObservedObject", observedObject)); 355 attributes.add(new Attribute ("ObservedAttribute", observedAttribute)); 356 int before = attributes.size(); 357 attributes = server.setAttributes(monitorName, attributes); 358 assertEquals(before, attributes.size()); 359 360 server.invoke(monitorName, "setThresholds", 361 new Object [] { new Integer (highValue), new Integer (lowValue) }, 362 new String [] { "java.lang.Number", "java.lang.Number" }); 363 364 server.invoke(monitorName, "start", new Object [0], new String [0]); 365 } 366 367 373 private void startStringService(boolean match, boolean differ, 374 String value) 375 throws Exception 376 { 377 installMonitorService(new StringMonitor ()); 378 AttributeList attributes = new AttributeList (); 379 attributes.add(new Attribute ("NotifyDiffer", new Boolean (differ))); 380 attributes.add(new Attribute ("NotifyMatch", new Boolean (match))); 381 attributes.add(new Attribute ("StringToCompare", value)); 382 attributes.add(new Attribute ("GranularityPeriod", new Long (PERIOD))); 383 attributes.add(new Attribute ("ObservedObject", observedObject)); 384 attributes.add(new Attribute ("ObservedAttribute", observedAttribute)); 385 int before = attributes.size(); 386 attributes = server.setAttributes(monitorName, attributes); 387 assertEquals(before, attributes.size()); 388 389 server.invoke(monitorName, "start", new Object [0], new String [0]); 390 } 391 392 397 private void installMonitorService(Object monitor) 398 throws Exception 399 { 400 server = MBeanServerFactory.createMBeanServer("Monitor"); 401 402 monitorName = new ObjectName ("Monitor:type=MonitorService"); 403 server.registerMBean(monitor, monitorName); 404 405 receivedNotifications.clear(); 406 407 NotificationFilter filter = new NotificationFilter () { 410 public boolean isNotificationEnabled(Notification notification) 411 { 412 return !MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR.equals(notification.getType()); 413 } 414 }; 415 416 server.addNotificationListener(monitorName, this, filter, null); 417 418 server.registerMBean(monitored, observedObject); 419 } 420 421 425 private void stopMonitorService() 426 { 427 try 428 { 429 server.invoke(monitorName, "stop", new Object [0], new String [0]); 430 server.removeNotificationListener(monitorName, this); 431 server.unregisterMBean(observedObject); 432 server.unregisterMBean(monitorName); 433 MBeanServerFactory.releaseMBeanServer(server); 434 } 435 catch (Exception ignored) {} 436 } 437 438 445 private void setAttribute(Object value, int expected) 446 throws Exception 447 { 448 if (value != null) 450 { 451 Attribute attribute = new Attribute (observedAttribute, value); 452 server.setAttribute(observedObject, attribute); 453 } 454 455 synchronized (receivedNotifications) 457 { 458 if (receivedNotifications.size() > expected ) 459 fail("too many notifications"); 460 if (receivedNotifications.size() <= expected ) 461 receivedNotifications.wait(WAIT); 462 assertEquals(expected, receivedNotifications.size()); 463 } 464 } 465 466 472 public void handleNotification(Notification notification, Object handback) 473 { 474 synchronized (receivedNotifications) 475 { 476 receivedNotifications.add(notification); 477 receivedNotifications.notifyAll(); 478 } 479 } 480 } 481 | Popular Tags |