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.GaugeMonitor ; 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 GaugeMonitorTest extends MonitorTestCase 26 { 27 public GaugeMonitorTest(String name) 28 { 29 super(name); 30 } 31 32 protected Monitor createMonitor() 33 { 34 return new GaugeMonitor (); 35 } 36 37 public void testCorrectInitialization() throws Exception 38 { 39 GaugeMonitor monitor = (GaugeMonitor )createMonitor(); 40 assertEquals(new Integer (0), monitor.getHighThreshold()); 41 assertEquals(new Integer (0), monitor.getLowThreshold()); 42 assertFalse(monitor.getDifferenceMode()); 43 assertFalse(monitor.getNotifyHigh()); 44 assertFalse(monitor.getNotifyLow()); 45 } 46 47 public void testSetThresholds() throws Exception 48 { 49 GaugeMonitor monitor = (GaugeMonitor )createMonitor(); 50 try 51 { 52 monitor.setThresholds(null, null); 53 fail(); 54 } 55 catch (IllegalArgumentException ignored) 56 { 57 } 58 try 59 { 60 monitor.setThresholds(new Integer (0), null); 61 fail(); 62 } 63 catch (IllegalArgumentException ignored) 64 { 65 } 66 try 67 { 68 monitor.setThresholds(null, new Integer (0)); 69 fail(); 70 } 71 catch (IllegalArgumentException ignored) 72 { 73 } 74 try 75 { 76 monitor.setThresholds(new Integer (1), new Long (0)); 78 fail(); 79 } 80 catch (IllegalArgumentException ignored) 81 { 82 } 83 try 84 { 85 monitor.setThresholds(new Integer (0), new Integer (1)); 87 fail(); 88 } 89 catch (IllegalArgumentException ignored) 90 { 91 } 92 93 monitor.setThresholds(new Float (5.7), new Float (5.0)); 94 } 95 96 99 public void testHighHysteresisStartBelow() throws Exception 100 { 101 ObjectName name = new ObjectName (":mbean=target"); 102 ObjectName monitorName = new ObjectName (":monitor=gauge"); 103 104 MBeanServer server = newMBeanServer(); 105 GaugeMonitor monitor = (GaugeMonitor )createMonitor(); 106 monitor.setDifferenceMode(true); 107 monitor.addObservedObject(name); 108 monitor.setObservedAttribute("Integer"); 109 int period = 1000; 110 monitor.setGranularityPeriod(period); 111 Integer high = new Integer (10); 112 Integer low = new Integer (5); 113 monitor.setThresholds(high, low); 114 monitor.setNotifyHigh(true); 115 monitor.setNotifyLow(false); 116 server.registerMBean(monitor, monitorName); 117 118 MonitorTarget target = new MonitorTarget(); 120 int value = low.intValue() - 1; 121 target.setInteger(value); 122 server.registerMBean(target, name); 123 124 final MutableInteger times = new MutableInteger(0); 125 final MutableObject holder = new MutableObject(null); 126 NotificationListener listener = new NotificationListener () 127 { 128 public void handleNotification(Notification notification, Object handback) 129 { 130 times.set(times.get() + 1); 131 holder.set(notification); 132 } 133 }; 134 server.addNotificationListener(monitorName, listener, null, null); 135 monitor.start(); 136 137 try 138 { 139 sleep(period * 3); 140 assertEquals(times.get(), 0); 141 assertNull(holder.get()); 142 143 value = value + high.intValue() + 1; 145 target.setInteger(value); 146 sleep(period * 3); 147 assertEquals(times.get(), 1); 148 MonitorNotification notification = (MonitorNotification )holder.get(); 149 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED); 150 151 times.set(0); 152 holder.set(null); 153 sleep(period * 3); 154 assertEquals(times.get(), 0); 155 156 value = value + low.intValue() + 1; 158 target.setInteger(value); 159 sleep(period * 3); 160 assertEquals(times.get(), 0); 161 assertNull(holder.get()); 162 163 value = value + high.intValue() + 1; 165 target.setInteger(value); 166 sleep(period * 3); 167 assertEquals(times.get(), 1); 168 notification = (MonitorNotification )holder.get(); 169 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED); 170 } 171 finally 172 { 173 monitor.stop(); 174 } 175 } 176 177 180 public void testLowHysteresisStartInside() throws Exception 181 { 182 ObjectName name = new ObjectName (":mbean=target"); 183 ObjectName monitorName = new ObjectName (":monitor=gauge"); 184 185 MBeanServer server = newMBeanServer(); 186 GaugeMonitor monitor = (GaugeMonitor )createMonitor(); 187 monitor.setDifferenceMode(true); 188 monitor.addObservedObject(name); 189 monitor.setObservedAttribute("Integer"); 190 int period = 1000; 191 monitor.setGranularityPeriod(period); 192 Integer high = new Integer (5); 193 Integer low = new Integer (0); 194 monitor.setThresholds(high, low); 195 monitor.setNotifyHigh(true); 196 monitor.setNotifyLow(true); 197 server.registerMBean(monitor, monitorName); 198 199 MonitorTarget target = new MonitorTarget(); 201 int value = low.intValue() + 1; 202 target.setInteger(value); 203 server.registerMBean(target, name); 204 205 final MutableInteger times = new MutableInteger(0); 206 final MutableObject holder = new MutableObject(null); 207 NotificationListener listener = new NotificationListener () 208 { 209 public void handleNotification(Notification notification, Object handback) 210 { 211 times.set(times.get() + 1); 212 holder.set(notification); 213 } 214 }; 215 server.addNotificationListener(monitorName, listener, null, null); 216 monitor.start(); 217 218 try 219 { 220 sleep(period * 3); 222 assertEquals(times.get(), 1); 223 MonitorNotification notification = (MonitorNotification )holder.get(); 224 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED); 225 226 times.set(0); 227 holder.set(null); 228 sleep(period * 3); 229 assertEquals(times.get(), 0); 230 231 monitor.setNotifyLow(false); 235 value = value + high.intValue() + 1; 237 target.setInteger(value); 238 sleep(period * 3); 239 assertEquals(times.get(), 1); 240 notification = (MonitorNotification )holder.get(); 241 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED); 242 243 times.set(0); 244 holder.set(null); 245 sleep(period * 3); 246 assertEquals(times.get(), 0); 247 248 monitor.setNotifyHigh(false); 249 monitor.setNotifyLow(true); 250 value = value + high.intValue() + 1; 252 target.setInteger(value); 253 sleep(period * 3); 254 assertEquals(times.get(), 1); 255 notification = (MonitorNotification )holder.get(); 256 assertEquals(notification.getType(), MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED); 257 258 times.set(0); 259 holder.set(null); 260 sleep(period * 3); 261 assertEquals(times.get(), 0); 262 263 value = value + low.intValue() + 1; 265 target.setInteger(value); 266 sleep(period * 3); 267 assertEquals(times.get(), 0); 268 assertNull(holder.get()); 269 } 270 finally 271 { 272 monitor.stop(); 273 } 274 } 275 276 279 public void testGetDerivedGauge() throws Exception 280 { 281 ObjectName name = new ObjectName (":mbean=target"); 282 ObjectName monitorName = new ObjectName (":monitor=gauge"); 283 284 MBeanServer server = newMBeanServer(); 285 GaugeMonitor monitor = (GaugeMonitor )createMonitor(); 286 monitor.setDifferenceMode(false); 287 monitor.addObservedObject(name); 288 monitor.setObservedAttribute("Integer"); 289 int period = 1000; 290 monitor.setGranularityPeriod(period); 291 server.registerMBean(monitor, monitorName); 292 293 MonitorTarget target = new MonitorTarget(); 295 int gauge = 4; 296 target.setInteger(gauge); 297 server.registerMBean(target, name); 298 299 monitor.start(); 300 301 try 302 { 303 sleep(period * 3); 304 305 Number observed = monitor.getDerivedGauge(name); 306 assertEquals(observed.intValue(), gauge); 307 } 308 finally 309 { 310 monitor.stop(); 311 } 312 } 313 314 public interface MonitorTargetMBean 315 { 316 public int getInteger(); 317 } 318 319 public static class MonitorTarget implements MonitorTargetMBean 320 { 321 private int value; 322 323 public int getInteger() 324 { 325 return value; 326 } 327 328 public void setInteger(int value) 329 { 330 this.value = value; 331 } 332 } 333 } 334 | Popular Tags |