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.Monitor ; 16 import javax.management.monitor.MonitorNotification ; 17 18 import test.MX4JTestCase; 19 import test.MutableInteger; 20 import test.MutableObject; 21 22 25 public abstract class MonitorTestCase extends MX4JTestCase 26 { 27 public MonitorTestCase(String name) 28 { 29 super(name); 30 } 31 32 protected abstract Monitor createMonitor(); 33 34 public void testStartStopIsActive() throws Exception 35 { 36 Monitor monitor = createMonitor(); 37 monitor.setGranularityPeriod(1000); 38 assertFalse(monitor.isActive()); 39 monitor.start(); 40 sleep(5000); 41 assertTrue(monitor.isActive()); 42 monitor.stop(); 43 assertFalse(monitor.isActive()); 44 monitor.start(); 45 assertTrue(monitor.isActive()); 46 monitor.stop(); 47 assertFalse(monitor.isActive()); 48 } 49 50 public void testSetObservedObject() throws Exception 51 { 52 Monitor monitor = createMonitor(); 53 ObjectName name1 = ObjectName.getInstance(":name=one"); 54 monitor.addObservedObject(name1); 55 ObjectName name2 = ObjectName.getInstance(":name=two"); 56 monitor.addObservedObject(name2); 57 assertEquals(monitor.getObservedObjects().length, 2); 58 assertTrue(monitor.containsObservedObject(name1)); 59 assertTrue(monitor.containsObservedObject(name2)); 60 monitor.setObservedObject(name1); 61 assertEquals(monitor.getObservedObjects().length, 1); 62 assertTrue(monitor.containsObservedObject(name1)); 63 } 64 65 public void testMonitorNotificationForMBeanNotRegistered() throws Exception 66 { 67 MBeanServer server = newMBeanServer(); 68 Monitor monitor = createMonitor(); 69 server.registerMBean(monitor, ObjectName.getInstance(":service=monitor")); 70 71 ObjectName name1 = ObjectName.getInstance(":name=one"); 72 monitor.addObservedObject(name1); 73 monitor.setGranularityPeriod(1000); 74 monitor.setObservedAttribute("dummy"); 75 76 final MutableInteger counter = new MutableInteger(0); 77 final MutableObject holder = new MutableObject(null); 78 monitor.addNotificationListener(new NotificationListener () 79 { 80 public void handleNotification(Notification notification, Object handback) 81 { 82 counter.set(counter.get() + 1); 83 holder.set(notification); 84 } 85 }, null, null); 86 monitor.start(); 87 88 try 89 { 90 while (holder.get() == null) sleep(10); 92 93 sleep(5000); 95 assertEquals(counter.get(), 1); 96 97 MonitorNotification notification = (MonitorNotification )holder.get(); 98 assertEquals(notification.getType(), MonitorNotification.OBSERVED_OBJECT_ERROR); 99 } 100 finally 101 { 102 monitor.stop(); 103 } 104 } 105 106 public void testMonitorNotificationForUnknownAttribute() throws Exception 107 { 108 MBeanServer server = newMBeanServer(); 109 Monitor monitor = createMonitor(); 110 server.registerMBean(monitor, ObjectName.getInstance(":service=monitor")); 111 112 ObjectName name1 = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); 113 monitor.addObservedObject(name1); 114 monitor.setGranularityPeriod(1000); 115 monitor.setObservedAttribute("dummy"); 116 117 final MutableInteger counter = new MutableInteger(0); 118 final MutableObject holder = new MutableObject(null); 119 monitor.addNotificationListener(new NotificationListener () 120 { 121 public void handleNotification(Notification notification, Object handback) 122 { 123 counter.set(counter.get() + 1); 124 holder.set(notification); 125 } 126 }, null, null); 127 monitor.start(); 128 129 try 130 { 131 while (holder.get() == null) sleep(10); 133 134 sleep(5000); 136 assertEquals(counter.get(), 1); 137 138 MonitorNotification notification = (MonitorNotification )holder.get(); 139 assertEquals(notification.getType(), MonitorNotification.OBSERVED_ATTRIBUTE_ERROR); 140 } 141 finally 142 { 143 monitor.stop(); 144 } 145 } 146 } 147 | Popular Tags |