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.GaugeMonitor;
016: import javax.management.monitor.Monitor;
017: import javax.management.monitor.MonitorNotification;
018:
019: import test.MutableInteger;
020: import test.MutableObject;
021:
022: /**
023: * @version $Revision: 1.12 $
024: */
025: public class GaugeMonitorTest extends MonitorTestCase {
026: public GaugeMonitorTest(String name) {
027: super (name);
028: }
029:
030: protected Monitor createMonitor() {
031: return new GaugeMonitor();
032: }
033:
034: public void testCorrectInitialization() throws Exception {
035: GaugeMonitor monitor = (GaugeMonitor) createMonitor();
036: assertEquals(new Integer(0), monitor.getHighThreshold());
037: assertEquals(new Integer(0), monitor.getLowThreshold());
038: assertFalse(monitor.getDifferenceMode());
039: assertFalse(monitor.getNotifyHigh());
040: assertFalse(monitor.getNotifyLow());
041: }
042:
043: public void testSetThresholds() throws Exception {
044: GaugeMonitor monitor = (GaugeMonitor) createMonitor();
045: try {
046: monitor.setThresholds(null, null);
047: fail();
048: } catch (IllegalArgumentException ignored) {
049: }
050: try {
051: monitor.setThresholds(new Integer(0), null);
052: fail();
053: } catch (IllegalArgumentException ignored) {
054: }
055: try {
056: monitor.setThresholds(null, new Integer(0));
057: fail();
058: } catch (IllegalArgumentException ignored) {
059: }
060: try {
061: // Different types
062: monitor.setThresholds(new Integer(1), new Long(0));
063: fail();
064: } catch (IllegalArgumentException ignored) {
065: }
066: try {
067: // High less than low
068: monitor.setThresholds(new Integer(0), new Integer(1));
069: fail();
070: } catch (IllegalArgumentException ignored) {
071: }
072:
073: monitor.setThresholds(new Float(5.7), new Float(5.0));
074: }
075:
076: /**
077: * This also serves as a test case for bug #710028
078: */
079: public void testHighHysteresisStartBelow() throws Exception {
080: ObjectName name = new ObjectName(":mbean=target");
081: ObjectName monitorName = new ObjectName(":monitor=gauge");
082:
083: MBeanServer server = newMBeanServer();
084: GaugeMonitor monitor = (GaugeMonitor) createMonitor();
085: monitor.setDifferenceMode(true);
086: monitor.addObservedObject(name);
087: monitor.setObservedAttribute("Integer");
088: int period = 1000;
089: monitor.setGranularityPeriod(period);
090: Integer high = new Integer(10);
091: Integer low = new Integer(5);
092: monitor.setThresholds(high, low);
093: monitor.setNotifyHigh(true);
094: monitor.setNotifyLow(false);
095: server.registerMBean(monitor, monitorName);
096:
097: // Initial value < lowThreshold
098: MonitorTarget target = new MonitorTarget();
099: int value = low.intValue() - 1;
100: target.setInteger(value);
101: server.registerMBean(target, name);
102:
103: final MutableInteger times = new MutableInteger(0);
104: final MutableObject holder = new MutableObject(null);
105: NotificationListener listener = new NotificationListener() {
106: public void handleNotification(Notification notification,
107: Object handback) {
108: times.set(times.get() + 1);
109: holder.set(notification);
110: }
111: };
112: server.addNotificationListener(monitorName, listener, null,
113: null);
114: monitor.start();
115:
116: try {
117: sleep(period * 3);
118: assertEquals(times.get(), 0);
119: assertNull(holder.get());
120:
121: // Set gauge above high threshold
122: value = value + high.intValue() + 1;
123: target.setInteger(value);
124: sleep(period * 3);
125: assertEquals(times.get(), 1);
126: MonitorNotification notification = (MonitorNotification) holder
127: .get();
128: assertEquals(notification.getType(),
129: MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);
130:
131: times.set(0);
132: holder.set(null);
133: sleep(period * 3);
134: assertEquals(times.get(), 0);
135:
136: // Set gauge inside threshold
137: value = value + low.intValue() + 1;
138: target.setInteger(value);
139: sleep(period * 3);
140: assertEquals(times.get(), 0);
141: assertNull(holder.get());
142:
143: // Set gauge above threshold again
144: value = value + high.intValue() + 1;
145: target.setInteger(value);
146: sleep(period * 3);
147: assertEquals(times.get(), 1);
148: notification = (MonitorNotification) holder.get();
149: assertEquals(notification.getType(),
150: MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);
151: } finally {
152: monitor.stop();
153: }
154: }
155:
156: /**
157: * This also serves as a test case for bug #742554
158: */
159: public void testLowHysteresisStartInside() throws Exception {
160: ObjectName name = new ObjectName(":mbean=target");
161: ObjectName monitorName = new ObjectName(":monitor=gauge");
162:
163: MBeanServer server = newMBeanServer();
164: GaugeMonitor monitor = (GaugeMonitor) createMonitor();
165: monitor.setDifferenceMode(true);
166: monitor.addObservedObject(name);
167: monitor.setObservedAttribute("Integer");
168: int period = 1000;
169: monitor.setGranularityPeriod(period);
170: Integer high = new Integer(5);
171: Integer low = new Integer(0);
172: monitor.setThresholds(high, low);
173: monitor.setNotifyHigh(true);
174: monitor.setNotifyLow(true);
175: server.registerMBean(monitor, monitorName);
176:
177: // Initial gauge inside thresholds
178: MonitorTarget target = new MonitorTarget();
179: int value = low.intValue() + 1;
180: target.setInteger(value);
181: server.registerMBean(target, name);
182:
183: final MutableInteger times = new MutableInteger(0);
184: final MutableObject holder = new MutableObject(null);
185: NotificationListener listener = new NotificationListener() {
186: public void handleNotification(Notification notification,
187: Object handback) {
188: times.set(times.get() + 1);
189: holder.set(notification);
190: }
191: };
192: server.addNotificationListener(monitorName, listener, null,
193: null);
194: monitor.start();
195:
196: try {
197: // Inside the thresholds, be sure low notification
198: sleep(period * 3);
199: assertEquals(times.get(), 1);
200: MonitorNotification notification = (MonitorNotification) holder
201: .get();
202: assertEquals(notification.getType(),
203: MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);
204:
205: times.set(0);
206: holder.set(null);
207: sleep(period * 3);
208: assertEquals(times.get(), 0);
209:
210: // Monitoring takes time, so I disable low notification to be sure to get only the high one
211: // The monitor is in difference mode, so the first time will get the high notification, but
212: // the second time will get zero, since the gauge did not change, which will triggers a low notification
213: monitor.setNotifyLow(false);
214: // Set gauge above high threshold
215: value = value + high.intValue() + 1;
216: target.setInteger(value);
217: sleep(period * 3);
218: assertEquals(times.get(), 1);
219: notification = (MonitorNotification) holder.get();
220: assertEquals(notification.getType(),
221: MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);
222:
223: times.set(0);
224: holder.set(null);
225: sleep(period * 3);
226: assertEquals(times.get(), 0);
227:
228: monitor.setNotifyHigh(false);
229: monitor.setNotifyLow(true);
230: // Set gauge above high threshold, so just after goes below low threshold
231: value = value + high.intValue() + 1;
232: target.setInteger(value);
233: sleep(period * 3);
234: assertEquals(times.get(), 1);
235: notification = (MonitorNotification) holder.get();
236: assertEquals(notification.getType(),
237: MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);
238:
239: times.set(0);
240: holder.set(null);
241: sleep(period * 3);
242: assertEquals(times.get(), 0);
243:
244: // Set gauge inside threshold
245: value = value + low.intValue() + 1;
246: target.setInteger(value);
247: sleep(period * 3);
248: assertEquals(times.get(), 0);
249: assertNull(holder.get());
250: } finally {
251: monitor.stop();
252: }
253: }
254:
255: /**
256: * This also serves as a test case for bug #822849
257: */
258: public void testGetDerivedGauge() throws Exception {
259: ObjectName name = new ObjectName(":mbean=target");
260: ObjectName monitorName = new ObjectName(":monitor=gauge");
261:
262: MBeanServer server = newMBeanServer();
263: GaugeMonitor monitor = (GaugeMonitor) createMonitor();
264: monitor.setDifferenceMode(false);
265: monitor.addObservedObject(name);
266: monitor.setObservedAttribute("Integer");
267: int period = 1000;
268: monitor.setGranularityPeriod(period);
269: server.registerMBean(monitor, monitorName);
270:
271: // Set initial gauge
272: MonitorTarget target = new MonitorTarget();
273: int gauge = 4;
274: target.setInteger(gauge);
275: server.registerMBean(target, name);
276:
277: monitor.start();
278:
279: try {
280: sleep(period * 3);
281:
282: Number observed = monitor.getDerivedGauge(name);
283: assertEquals(observed.intValue(), gauge);
284: } finally {
285: monitor.stop();
286: }
287: }
288:
289: public interface MonitorTargetMBean {
290: public int getInteger();
291: }
292:
293: public static class MonitorTarget implements MonitorTargetMBean {
294: private int value;
295:
296: public int getInteger() {
297: return value;
298: }
299:
300: public void setInteger(int value) {
301: this.value = value;
302: }
303: }
304: }
|