001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.monitor;
010:
011: import javax.management.MBeanServer;
012: import javax.management.Notification;
013: import javax.management.NotificationListener;
014: import javax.management.ObjectName;
015: import javax.management.monitor.Monitor;
016: import javax.management.monitor.MonitorNotification;
017: import javax.management.monitor.StringMonitor;
018:
019: import test.MutableInteger;
020: import test.MutableObject;
021:
022: /**
023: * @version $Revision: 1.8 $
024: */
025: public class StringMonitorTest extends MonitorTestCase {
026: public StringMonitorTest(String name) {
027: super (name);
028: }
029:
030: protected Monitor createMonitor() {
031: return new StringMonitor();
032: }
033:
034: public void testCorrectInitialization() throws Exception {
035: StringMonitor monitor = (StringMonitor) createMonitor();
036: assertEquals("", monitor.getStringToCompare());
037: assertFalse(monitor.getNotifyDiffer());
038: assertFalse(monitor.getNotifyMatch());
039: }
040:
041: public void testSetStringToCompare() throws Exception {
042: StringMonitor monitor = (StringMonitor) createMonitor();
043: try {
044: monitor.setStringToCompare(null);
045: fail();
046: } catch (IllegalArgumentException x) {
047: }
048: }
049:
050: /**
051: * The case outlined in the JMX specification
052: */
053: public void testSpecificationCase() throws Exception {
054: ObjectName name = new ObjectName(":mbean=target");
055: ObjectName monitorName = new ObjectName(":monitor=gauge");
056:
057: MBeanServer server = newMBeanServer();
058: StringMonitor monitor = (StringMonitor) createMonitor();
059: String reference = "XYZ";
060: monitor.setStringToCompare(reference);
061: monitor.setNotifyMatch(true);
062: monitor.setNotifyDiffer(true);
063: monitor.addObservedObject(name);
064: monitor.setObservedAttribute("String");
065: int period = 1000;
066: monitor.setGranularityPeriod(period);
067: server.registerMBean(monitor, monitorName);
068:
069: MonitorTarget target = new MonitorTarget();
070: target.setString(reference);
071: server.registerMBean(target, name);
072:
073: final MutableInteger times = new MutableInteger(0);
074: final MutableObject holder = new MutableObject(null);
075: NotificationListener listener = new NotificationListener() {
076: public void handleNotification(Notification notification,
077: Object handback) {
078: times.set(times.get() + 1);
079: holder.set(notification);
080: }
081: };
082: server.addNotificationListener(monitorName, listener, null,
083: null);
084:
085: monitor.start();
086:
087: try {
088: sleep(period * 3);
089: assertEquals(times.get(), 1);
090: MonitorNotification notification = (MonitorNotification) holder
091: .get();
092: assertEquals(notification.getType(),
093: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
094:
095: times.set(0);
096: holder.set(null);
097: target.setString("xx");
098:
099: sleep(period * 3);
100: assertEquals(times.get(), 1);
101: notification = (MonitorNotification) holder.get();
102: assertEquals(
103: notification.getType(),
104: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
105:
106: times.set(0);
107: holder.set(null);
108: target.setString(reference);
109:
110: sleep(period * 3);
111: assertEquals(times.get(), 1);
112: notification = (MonitorNotification) holder.get();
113: assertEquals(notification.getType(),
114: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
115:
116: times.set(0);
117: holder.set(null);
118: target.setString("yyyy");
119:
120: sleep(period * 3);
121: assertEquals(times.get(), 1);
122: notification = (MonitorNotification) holder.get();
123: assertEquals(
124: notification.getType(),
125: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
126:
127: times.set(0);
128: holder.set(null);
129: target.setString("zzzzz");
130:
131: sleep(period * 3);
132: assertEquals(times.get(), 0);
133: assertNull(holder.get());
134: } finally {
135: monitor.stop();
136: }
137: }
138:
139: public interface MonitorTargetMBean {
140: public String getString();
141: }
142:
143: public static class MonitorTarget implements MonitorTargetMBean {
144: private String value;
145:
146: public String getString() {
147: return value;
148: }
149:
150: public void setString(String value) {
151: this.value = value;
152: }
153: }
154: }
|