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 import javax.management.monitor.StringMonitor ; 18 19 import test.MutableInteger; 20 import test.MutableObject; 21 22 25 public class StringMonitorTest extends MonitorTestCase 26 { 27 public StringMonitorTest(String name) 28 { 29 super(name); 30 } 31 32 protected Monitor createMonitor() 33 { 34 return new StringMonitor (); 35 } 36 37 public void testCorrectInitialization() throws Exception 38 { 39 StringMonitor monitor = (StringMonitor )createMonitor(); 40 assertEquals("", monitor.getStringToCompare()); 41 assertFalse(monitor.getNotifyDiffer()); 42 assertFalse(monitor.getNotifyMatch()); 43 } 44 45 public void testSetStringToCompare() throws Exception 46 { 47 StringMonitor monitor = (StringMonitor )createMonitor(); 48 try 49 { 50 monitor.setStringToCompare(null); 51 fail(); 52 } 53 catch (IllegalArgumentException x) 54 { 55 } 56 } 57 58 61 public void testSpecificationCase() throws Exception 62 { 63 ObjectName name = new ObjectName (":mbean=target"); 64 ObjectName monitorName = new ObjectName (":monitor=gauge"); 65 66 MBeanServer server = newMBeanServer(); 67 StringMonitor monitor = (StringMonitor )createMonitor(); 68 String reference = "XYZ"; 69 monitor.setStringToCompare(reference); 70 monitor.setNotifyMatch(true); 71 monitor.setNotifyDiffer(true); 72 monitor.addObservedObject(name); 73 monitor.setObservedAttribute("String"); 74 int period = 1000; 75 monitor.setGranularityPeriod(period); 76 server.registerMBean(monitor, monitorName); 77 78 MonitorTarget target = new MonitorTarget(); 79 target.setString(reference); 80 server.registerMBean(target, name); 81 82 final MutableInteger times = new MutableInteger(0); 83 final MutableObject holder = new MutableObject(null); 84 NotificationListener listener = new NotificationListener () 85 { 86 public void handleNotification(Notification notification, Object handback) 87 { 88 times.set(times.get() + 1); 89 holder.set(notification); 90 } 91 }; 92 server.addNotificationListener(monitorName, listener, null, null); 93 94 monitor.start(); 95 96 try 97 { 98 sleep(period * 3); 99 assertEquals(times.get(), 1); 100 MonitorNotification notification = (MonitorNotification )holder.get(); 101 assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 102 103 times.set(0); 104 holder.set(null); 105 target.setString("xx"); 106 107 sleep(period * 3); 108 assertEquals(times.get(), 1); 109 notification = (MonitorNotification )holder.get(); 110 assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 111 112 times.set(0); 113 holder.set(null); 114 target.setString(reference); 115 116 sleep(period * 3); 117 assertEquals(times.get(), 1); 118 notification = (MonitorNotification )holder.get(); 119 assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED); 120 121 times.set(0); 122 holder.set(null); 123 target.setString("yyyy"); 124 125 sleep(period * 3); 126 assertEquals(times.get(), 1); 127 notification = (MonitorNotification )holder.get(); 128 assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED); 129 130 times.set(0); 131 holder.set(null); 132 target.setString("zzzzz"); 133 134 sleep(period * 3); 135 assertEquals(times.get(), 0); 136 assertNull(holder.get()); 137 } 138 finally 139 { 140 monitor.stop(); 141 } 142 } 143 144 public interface MonitorTargetMBean 145 { 146 public String getString(); 147 } 148 149 public static class MonitorTarget implements MonitorTargetMBean 150 { 151 private String value; 152 153 public String getString() 154 { 155 return value; 156 } 157 158 public void setString(String value) 159 { 160 this.value = value; 161 } 162 } 163 } 164 | Popular Tags |