1 8 9 package test.javax.management.monitor; 10 11 import javax.management.MBeanServer ; 12 import javax.management.Notification ; 13 import javax.management.NotificationListener ; 14 import javax.management.ObjectName ; 15 import javax.management.monitor.CounterMonitor ; 16 import javax.management.monitor.Monitor ; 17 import javax.management.monitor.MonitorNotification ; 18 19 import test.MutableInteger; 20 import test.MutableObject; 21 22 25 public class CounterMonitorTest extends MonitorTestCase 26 { 27 public CounterMonitorTest(String name) 28 { 29 super(name); 30 } 31 32 protected Monitor createMonitor() 33 { 34 return new CounterMonitor (); 35 } 36 37 public void testCorrectInitialization() throws Exception 38 { 39 CounterMonitor monitor = (CounterMonitor )createMonitor(); 40 assertEquals(new Integer (0), monitor.getInitThreshold()); 41 assertEquals(new Integer (0), monitor.getModulus()); 42 assertEquals(new Integer (0), monitor.getOffset()); 43 assertFalse(monitor.getDifferenceMode()); 44 assertFalse(monitor.getNotify()); 45 } 46 47 public void testSetThreshold() throws Exception 48 { 49 CounterMonitor monitor = (CounterMonitor )createMonitor(); 50 try 51 { 52 monitor.setThreshold(new Integer (-1)); 53 fail(); 54 } 55 catch (IllegalArgumentException x) 56 { 57 } 58 try 59 { 60 monitor.setInitThreshold(new Integer (-1)); 61 fail(); 62 } 63 catch (IllegalArgumentException x) 64 { 65 } 66 67 Integer threshold = new Integer (1); 68 monitor.setThreshold(threshold); 69 assertEquals(monitor.getInitThreshold(), threshold); 70 71 threshold = new Integer (2); 72 monitor.setInitThreshold(threshold); 73 assertEquals(monitor.getInitThreshold(), threshold); 74 } 75 76 public void testSetModulus() throws Exception 77 { 78 CounterMonitor monitor = (CounterMonitor )createMonitor(); 79 try 80 { 81 monitor.setModulus(new Integer (-1)); 82 fail(); 83 } 84 catch (IllegalArgumentException x) 85 { 86 } 87 88 Integer modulus = new Integer (1); 89 monitor.setModulus(modulus); 90 assertEquals(monitor.getModulus(), modulus); 91 } 92 93 public void testSetOffset() throws Exception 94 { 95 CounterMonitor monitor = (CounterMonitor )createMonitor(); 96 try 97 { 98 monitor.setOffset(new Integer (-1)); 99 fail(); 100 } 101 catch (IllegalArgumentException x) 102 { 103 } 104 105 Integer offset = new Integer (1); 106 monitor.setOffset(offset); 107 assertEquals(monitor.getOffset(), offset); 108 } 109 110 public void testMonitorNotificationForBadCounter() throws Exception 111 { 112 MBeanServer server = newMBeanServer(); 113 Monitor monitor = createMonitor(); 114 server.registerMBean(monitor, ObjectName.getInstance(":service=monitor")); 115 116 Counter counter = new Counter(); 117 ObjectName counterName = ObjectName.getInstance(":mbean=counter"); 118 server.registerMBean(counter, counterName); 119 120 monitor.addObservedObject(counterName); 121 monitor.setGranularityPeriod(1000); 122 monitor.setObservedAttribute("ObjectCounter"); 123 124 final MutableInteger times = new MutableInteger(0); 125 final MutableObject holder = new MutableObject(null); 126 monitor.addNotificationListener(new NotificationListener () 127 { 128 public void handleNotification(Notification notification, Object handback) 129 { 130 times.set(times.get() + 1); 131 holder.set(notification); 132 } 133 }, null, null); 134 monitor.start(); 135 136 try 137 { 138 while (holder.get() == null) sleep(10); 140 141 sleep(5000); 143 assertEquals(times.get(), 1); 144 145 MonitorNotification notification = (MonitorNotification )holder.get(); 146 assertEquals(notification.getType(), MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR); 147 } 148 finally 149 { 150 monitor.stop(); 151 } 152 } 153 154 public void testIntegerCounter() throws Exception 155 { 156 MBeanServer server = newMBeanServer(); 157 CounterMonitor monitor = (CounterMonitor )createMonitor(); 158 server.registerMBean(monitor, ObjectName.getInstance(":service=monitor")); 159 160 Counter counter = new Counter(); 161 ObjectName counterName = ObjectName.getInstance(":mbean=counter"); 162 server.registerMBean(counter, counterName); 163 164 long period = 1000; 165 monitor.addObservedObject(counterName); 166 monitor.setGranularityPeriod(period); 167 monitor.setObservedAttribute("IntegerCounter"); 168 Integer initThreshold = new Integer (3); 169 monitor.setInitThreshold(initThreshold); 170 monitor.setNotify(true); 171 173 counter.setIntegerCounter(initThreshold.intValue() - 1); 174 175 final MutableInteger times = new MutableInteger(0); 176 final MutableObject holder = new MutableObject(null); 177 monitor.addNotificationListener(new NotificationListener () 178 { 179 public void handleNotification(Notification notification, Object handback) 180 { 181 times.set(times.get() + 1); 182 holder.set(notification); 183 } 184 }, null, null); 185 monitor.start(); 186 187 try 188 { 189 sleep(period * 3); 191 assertEquals(times.get(), 0); 192 assertNull(holder.get()); 193 194 counter.setIntegerCounter(initThreshold.intValue() + 1); 196 sleep(period * 3); 197 assertEquals(times.get(), 1); 198 MonitorNotification notification = (MonitorNotification )holder.get(); 199 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 200 201 times.set(0); 202 holder.set(null); 203 sleep(period * 3); 204 assertEquals(times.get(), 0); 205 } 206 finally 207 { 208 monitor.stop(); 209 } 210 } 211 212 public void testIntegerCounterWithOffset() throws Exception 213 { 214 MBeanServer server = newMBeanServer(); 215 CounterMonitor monitor = (CounterMonitor )createMonitor(); 216 server.registerMBean(monitor, ObjectName.getInstance(":service=monitor")); 217 218 Counter counter = new Counter(); 219 ObjectName counterName = ObjectName.getInstance(":mbean=counter"); 220 server.registerMBean(counter, counterName); 221 222 long period = 1000; 223 monitor.addObservedObject(counterName); 224 monitor.setGranularityPeriod(period); 225 monitor.setObservedAttribute("IntegerCounter"); 226 Integer initThreshold = new Integer (3); 227 monitor.setInitThreshold(initThreshold); 228 monitor.setNotify(true); 229 Integer offset = new Integer (5); 230 monitor.setOffset(offset); 231 233 counter.setIntegerCounter(initThreshold.intValue() - 1); 234 235 final MutableInteger times = new MutableInteger(0); 236 final MutableObject holder = new MutableObject(null); 237 monitor.addNotificationListener(new NotificationListener () 238 { 239 public void handleNotification(Notification notification, Object handback) 240 { 241 times.set(times.get() + 1); 242 holder.set(notification); 243 } 244 }, null, null); 245 monitor.start(); 246 247 try 248 { 249 sleep(period * 3); 251 assertEquals(times.get(), 0); 252 assertNull(holder.get()); 253 254 counter.setIntegerCounter(initThreshold.intValue() + 1); 256 sleep(period * 3); 257 assertEquals(times.get(), 1); 258 MonitorNotification notification = (MonitorNotification )holder.get(); 259 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 260 Number threshold = monitor.getThreshold(counterName); 262 assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue()); 263 264 times.set(0); 265 holder.set(null); 266 sleep(period * 3); 267 assertEquals(times.get(), 0); 268 269 counter.setIntegerCounter(initThreshold.intValue() + offset.intValue() * 2 + 1); 271 sleep(period * 3); 272 assertEquals(times.get(), 1); 273 notification = (MonitorNotification )holder.get(); 274 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED); 275 threshold = monitor.getThreshold(counterName); 277 assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue() * 3); 278 279 times.set(0); 280 holder.set(null); 281 sleep(period * 3); 282 assertEquals(times.get(), 0); 283 } 284 finally 285 { 286 monitor.stop(); 287 } 288 } 289 290 public void testShutDownMonitorThread() throws Exception 291 { 292 MBeanServer server = newMBeanServer(); 293 CounterMonitor monitor = (CounterMonitor )createMonitor(); 294 server.registerMBean(monitor, ObjectName.getInstance(":service=monitor")); 295 296 Counter counter = new Counter(); 297 ObjectName counterName = ObjectName.getInstance(":mbean=counter"); 298 server.registerMBean(counter, counterName); 299 300 long period = 1000; 301 monitor.addObservedObject(counterName); 302 monitor.setGranularityPeriod(period); 303 monitor.setObservedAttribute("IntegerCounter"); 304 Integer initThreshold = new Integer (3); 305 monitor.setInitThreshold(initThreshold); 306 monitor.setNotify(true); 307 309 counter.setIntegerCounter(initThreshold.intValue() - 1); 310 311 final MutableObject monitorThread = new MutableObject(null); 312 monitor.addNotificationListener(new NotificationListener () 313 { 314 public void handleNotification(Notification notification, Object handback) 315 { 316 monitorThread.set(Thread.currentThread()); 317 } 318 }, null, null); 319 monitor.start(); 320 321 sleep(period * 3); 323 324 counter.setIntegerCounter(initThreshold.intValue() + 1); 326 sleep(period * 3); 327 328 Thread thread = (Thread )monitorThread.get(); 329 assertNotNull(thread); 330 assertNotSame(thread, Thread.currentThread()); 331 332 monitor.stop(); 333 334 sleep(period * 3); 335 336 assertFalse(thread.isAlive()); 337 } 338 339 public interface CounterMBean 340 { 341 public Object getObjectCounter(); 342 343 public Integer getNegativeCounter(); 344 345 public int getIntegerCounter(); 346 } 347 348 public static class Counter implements CounterMBean 349 { 350 private int integerCounter; 351 352 public Object getObjectCounter() 353 { 354 return new Object (); 355 } 356 357 public Integer getNegativeCounter() 358 { 359 return new Integer (-1); 360 } 361 362 public int getIntegerCounter() 363 { 364 return integerCounter; 365 } 366 367 public void setIntegerCounter(int integerCounter) 368 { 369 this.integerCounter = integerCounter; 370 } 371 } 372 } 373 | Popular Tags |