001: /* =====================================================================
002: *
003: * Copyright (c) 2004 Jeremy Boynes. All rights reserved.
004: *
005: * =====================================================================
006: */
007: package test.javax.management.monitor;
008:
009: import junit.framework.TestCase;
010:
011: import javax.management.*;
012: import javax.management.monitor.CounterMonitor;
013: import javax.management.monitor.MonitorNotification;
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: import mx4j.log.Log;
018: import mx4j.log.Logger;
019:
020: /**
021: * @version $Revision: 1.1 $ $Date: 2005/02/15 22:31:40 $
022: */
023: public class CounterNotificationTest extends TestCase implements
024: NotificationListener {
025: private static final Number ZERO = new Integer(0);
026: private MBeanServer mbServer;
027:
028: private ObjectName observedName;
029: private ObservedObject observed;
030:
031: private ObjectName counterMonitorName;
032: private CounterMonitor counterMonitor;
033:
034: private List notifications;
035: private int granularity;
036: private Logger logger;
037:
038: public void testSimpleIncrementingCounter() throws Exception {
039: initMonitor(ZERO, ZERO, ZERO, false);
040: counterMonitor.start();
041:
042: setAttribute(new Integer(-2));
043: sleep();
044: assertEquals(0, notifications.size());
045: checkMonitor(new Integer(-2), ZERO);
046:
047: setAttribute(new Integer(-1));
048: sleep();
049: assertEquals(0, notifications.size());
050: checkMonitor(new Integer(-1), ZERO);
051:
052: setAttribute(new Integer(0));
053: sleep();
054: assertEquals(1, notifications.size());
055: checkMonitor(new Integer(0), ZERO);
056: checkNotification((Notification) notifications.get(0));
057:
058: setAttribute(new Integer(1));
059: sleep();
060: assertEquals(1, notifications.size());
061: checkMonitor(new Integer(1), ZERO);
062:
063: setAttribute(new Integer(2));
064: sleep();
065: assertEquals(1, notifications.size());
066: checkMonitor(new Integer(2), ZERO);
067: }
068:
069: public void testSimpleCounterWithOffset() throws Exception {
070: initMonitor(ZERO, new Integer(2), ZERO, false);
071: counterMonitor.start();
072:
073: setAttribute(new Integer(0));
074: sleep();
075: checkMonitor(new Integer(0), new Integer(2));
076: assertEquals(1, notifications.size());
077: checkNotification((Notification) notifications.get(0));
078:
079: setAttribute(new Integer(1));
080: sleep();
081: checkMonitor(new Integer(1), new Integer(2));
082: assertEquals(1, notifications.size());
083:
084: setAttribute(new Integer(2));
085: sleep();
086: checkMonitor(new Integer(2), new Integer(4));
087: assertEquals(2, notifications.size());
088: checkNotification((Notification) notifications.get(1));
089:
090: setAttribute(new Integer(3));
091: sleep();
092: checkMonitor(new Integer(3), new Integer(4));
093: assertEquals(2, notifications.size());
094:
095: setAttribute(new Integer(4));
096: sleep();
097: checkMonitor(new Integer(4), new Integer(6));
098: assertEquals(3, notifications.size());
099: checkNotification((Notification) notifications.get(2));
100:
101: setAttribute(new Integer(5));
102: sleep();
103: checkMonitor(new Integer(5), new Integer(6));
104: assertEquals(3, notifications.size());
105:
106: setAttribute(new Integer(6));
107: sleep();
108: checkMonitor(new Integer(6), new Integer(8));
109: assertEquals(4, notifications.size());
110: checkNotification((Notification) notifications.get(3));
111: }
112:
113: public void testSimpleCounterWithModulusAndDecreasingOffset()
114: throws Exception {
115: initMonitor(new Integer(1), new Integer(1), new Integer(5),
116: false);
117: counterMonitor.start();
118:
119: setAttribute(new Integer(1));
120: sleep();
121: checkMonitor(new Integer(1), new Integer(2));
122: assertEquals(1, notifications.size());
123:
124: counterMonitor.setOffset(new Integer(2));
125: setAttribute(new Integer(2));
126: sleep();
127: checkMonitor(new Integer(2), new Integer(4));
128: assertEquals(2, notifications.size());
129:
130: counterMonitor.setOffset(new Integer(1));
131: setAttribute(new Integer(4));
132: sleep();
133: checkMonitor(new Integer(4), new Integer(5));
134: assertEquals(3, notifications.size());
135:
136: setAttribute(new Integer(5));
137: sleep();
138: checkMonitor(new Integer(5), new Integer(6));
139: assertEquals(4, notifications.size());
140:
141: setAttribute(new Integer(5));
142: sleep();
143: checkMonitor(new Integer(5), new Integer(6));
144: assertEquals(4, notifications.size());
145:
146: setAttribute(new Integer(2));
147: sleep();
148: checkMonitor(new Integer(2), new Integer(3));
149: assertEquals(5, notifications.size());
150:
151: setAttribute(new Integer(1));
152: sleep();
153: // Thread.sleep(10000000);
154: checkMonitor(new Integer(1), new Integer(3));
155: assertEquals(5, notifications.size());
156:
157: setAttribute(new Integer(1));
158: sleep();
159: checkMonitor(new Integer(1), new Integer(3));
160: assertEquals(5, notifications.size());
161:
162: setAttribute(new Integer(3));
163: sleep();
164: checkMonitor(new Integer(3), new Integer(4));
165: assertEquals(6, notifications.size());
166: }
167:
168: public void testDeltaWithModulus() throws Exception {
169: initMonitor(new Integer(2), ZERO, new Integer(7), true);
170: counterMonitor.start();
171:
172: setAttribute(new Integer(-2));
173: sleep();
174: checkMonitor(new Integer(-2), new Integer(2));
175: assertEquals(0, notifications.size());
176:
177: setAttribute(new Integer(-1));
178: sleep();
179: checkMonitor(new Integer(-1), new Integer(2));
180: assertEquals(0, notifications.size());
181:
182: setAttribute(new Integer(0));
183: sleep();
184: checkMonitor(new Integer(0), new Integer(2));
185: assertEquals(0, notifications.size());
186:
187: setAttribute(new Integer(8));
188: sleep();
189: checkMonitor(new Integer(8), new Integer(2));
190: assertEquals(1, notifications.size());
191:
192: setAttribute(new Integer(3));
193: sleep();
194: checkMonitor(new Integer(3), new Integer(2));
195: assertEquals(2, notifications.size());
196:
197: setAttribute(new Integer(5));
198: sleep();
199: checkMonitor(new Integer(5), new Integer(2));
200: assertEquals(3, notifications.size());
201:
202: setAttribute(new Integer(0));
203: sleep();
204: checkMonitor(new Integer(0), new Integer(2));
205: assertEquals(4, notifications.size());
206:
207: setAttribute(new Integer(1));
208: sleep();
209: checkMonitor(new Integer(1), new Integer(2));
210: assertEquals(4, notifications.size());
211:
212: setAttribute(new Integer(4));
213: sleep();
214: checkMonitor(new Integer(4), new Integer(2));
215: assertEquals(5, notifications.size());
216:
217: setAttribute(new Integer(5));
218: sleep();
219: checkMonitor(new Integer(5), new Integer(2));
220: assertEquals(5, notifications.size());
221:
222: setAttribute(new Integer(0));
223: sleep();
224: checkMonitor(new Integer(0), new Integer(2));
225: assertEquals(6, notifications.size());
226: }
227:
228: public void testDeltaWithOffsetAndModulus() throws Exception {
229: initMonitor(new Integer(2), new Integer(1), new Integer(7),
230: true);
231: counterMonitor.start();
232:
233: setAttribute(new Integer(0));
234: sleep();
235: checkMonitor(new Integer(0), new Integer(2));
236: assertEquals(0, notifications.size());
237:
238: setAttribute(new Integer(2));
239: sleep();
240: checkMonitor(new Integer(2), new Integer(3));
241: assertEquals(1, notifications.size());
242:
243: setAttribute(new Integer(3));
244: sleep();
245: checkMonitor(new Integer(3), new Integer(3));
246: assertEquals(1, notifications.size());
247:
248: setAttribute(new Integer(5));
249: sleep();
250: checkMonitor(new Integer(5), new Integer(3));
251: assertEquals(1, notifications.size());
252:
253: setAttribute(new Integer(0));
254: sleep();
255: checkMonitor(new Integer(0), new Integer(3));
256: assertEquals(2, notifications.size());
257:
258: setAttribute(new Integer(1));
259: sleep();
260: checkMonitor(new Integer(1), new Integer(3));
261: assertEquals(2, notifications.size());
262:
263: setAttribute(new Integer(4));
264: sleep();
265: checkMonitor(new Integer(4), new Integer(4));
266: assertEquals(3, notifications.size());
267:
268: setAttribute(new Integer(5));
269: sleep();
270: checkMonitor(new Integer(5), new Integer(4));
271: assertEquals(3, notifications.size());
272:
273: setAttribute(new Integer(0));
274: sleep();
275: checkMonitor(new Integer(0), new Integer(3));
276: assertEquals(4, notifications.size());
277: }
278:
279: private void setAttribute(Number value) throws Exception {
280: logger.debug("Setting attribute to " + value);
281: mbServer.setAttribute(observedName, new Attribute("Counter",
282: value));
283: }
284:
285: private void sleep() {
286: try {
287: Thread.sleep(granularity * 3);
288: } catch (InterruptedException e) {
289: }
290: }
291:
292: private void initMonitor(Number initThreshold, Number offset,
293: Number modulus, boolean difference) {
294: counterMonitor.setObservedObject(observedName);
295: counterMonitor.setObservedAttribute("Counter");
296: counterMonitor.setGranularityPeriod(granularity);
297: counterMonitor.setNotify(true);
298:
299: counterMonitor.setThreshold(initThreshold);
300: counterMonitor.setOffset(offset);
301: counterMonitor.setModulus(modulus);
302: counterMonitor.setDifferenceMode(difference);
303: }
304:
305: private void checkMonitor(Number value, Number threshold)
306: throws Exception {
307: assertEquals(value, mbServer.getAttribute(observedName,
308: "Counter"));
309: assertEquals(threshold, counterMonitor.getThreshold());
310: }
311:
312: private void checkNotification(Notification notification) {
313: assertEquals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED,
314: notification.getType());
315: }
316:
317: public void handleNotification(Notification notification,
318: Object handback) {
319: notifications.add(notification);
320: }
321:
322: protected void setUp() throws Exception {
323: logger = Log.getLogger(this .getClass().getName());
324:
325: granularity = 200;
326: notifications = new ArrayList();
327: mbServer = MBeanServerFactory.newMBeanServer();
328:
329: observedName = new ObjectName("test:name=ObservedObject");
330: observed = new ObservedObject();
331: mbServer.registerMBean(observed, observedName);
332:
333: counterMonitorName = new ObjectName("test:name=CounterMonitor");
334: counterMonitor = new CounterMonitor();
335: mbServer.registerMBean(counterMonitor, counterMonitorName);
336: counterMonitor.addNotificationListener(this , null, null);
337: }
338:
339: protected void tearDown() throws Exception {
340: if (counterMonitor.isActive())
341: counterMonitor.stop();
342: mbServer.unregisterMBean(counterMonitorName);
343: mbServer.unregisterMBean(observedName);
344: }
345:
346: public static interface ObservedObjectMBean {
347: Number getCounter();
348:
349: void setCounter(Number counter);
350: }
351:
352: public static class ObservedObject implements ObservedObjectMBean {
353: private Number counter;
354:
355: public Number getCounter() {
356: return counter;
357: }
358:
359: public void setCounter(Number counter) {
360: this.counter = counter;
361: }
362: }
363: }
|