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.CounterMonitor;
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.2 $
024: */
025: public class CounterMonitorTest extends MonitorTestCase {
026: public CounterMonitorTest(String name) {
027: super (name);
028: }
029:
030: protected Monitor createMonitor() {
031: return new CounterMonitor();
032: }
033:
034: public void testCorrectInitialization() throws Exception {
035: CounterMonitor monitor = (CounterMonitor) createMonitor();
036: assertEquals(new Integer(0), monitor.getInitThreshold());
037: assertEquals(new Integer(0), monitor.getModulus());
038: assertEquals(new Integer(0), monitor.getOffset());
039: assertFalse(monitor.getDifferenceMode());
040: assertFalse(monitor.getNotify());
041: }
042:
043: public void testSetThreshold() throws Exception {
044: CounterMonitor monitor = (CounterMonitor) createMonitor();
045: try {
046: monitor.setThreshold(new Integer(-1));
047: fail();
048: } catch (IllegalArgumentException x) {
049: }
050: try {
051: monitor.setInitThreshold(new Integer(-1));
052: fail();
053: } catch (IllegalArgumentException x) {
054: }
055:
056: Integer threshold = new Integer(1);
057: monitor.setThreshold(threshold);
058: assertEquals(monitor.getInitThreshold(), threshold);
059:
060: threshold = new Integer(2);
061: monitor.setInitThreshold(threshold);
062: assertEquals(monitor.getInitThreshold(), threshold);
063: }
064:
065: public void testSetModulus() throws Exception {
066: CounterMonitor monitor = (CounterMonitor) createMonitor();
067: try {
068: monitor.setModulus(new Integer(-1));
069: fail();
070: } catch (IllegalArgumentException x) {
071: }
072:
073: Integer modulus = new Integer(1);
074: monitor.setModulus(modulus);
075: assertEquals(monitor.getModulus(), modulus);
076: }
077:
078: public void testSetOffset() throws Exception {
079: CounterMonitor monitor = (CounterMonitor) createMonitor();
080: try {
081: monitor.setOffset(new Integer(-1));
082: fail();
083: } catch (IllegalArgumentException x) {
084: }
085:
086: Integer offset = new Integer(1);
087: monitor.setOffset(offset);
088: assertEquals(monitor.getOffset(), offset);
089: }
090:
091: public void testMonitorNotificationForBadCounter() throws Exception {
092: MBeanServer server = newMBeanServer();
093: Monitor monitor = createMonitor();
094: server.registerMBean(monitor, ObjectName
095: .getInstance(":service=monitor"));
096:
097: Counter counter = new Counter();
098: ObjectName counterName = ObjectName
099: .getInstance(":mbean=counter");
100: server.registerMBean(counter, counterName);
101:
102: monitor.addObservedObject(counterName);
103: monitor.setGranularityPeriod(1000);
104: monitor.setObservedAttribute("ObjectCounter");
105:
106: final MutableInteger times = new MutableInteger(0);
107: final MutableObject holder = new MutableObject(null);
108: monitor.addNotificationListener(new NotificationListener() {
109: public void handleNotification(Notification notification,
110: Object handback) {
111: times.set(times.get() + 1);
112: holder.set(notification);
113: }
114: }, null, null);
115: monitor.start();
116:
117: try {
118: // Wait for notification to arrive
119: while (holder.get() == null)
120: sleep(10);
121:
122: // Be sure only one arrived
123: sleep(5000);
124: assertEquals(times.get(), 1);
125:
126: MonitorNotification notification = (MonitorNotification) holder
127: .get();
128: assertEquals(notification.getType(),
129: MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR);
130: } finally {
131: monitor.stop();
132: }
133: }
134:
135: public void testIntegerCounter() throws Exception {
136: MBeanServer server = newMBeanServer();
137: CounterMonitor monitor = (CounterMonitor) createMonitor();
138: server.registerMBean(monitor, ObjectName
139: .getInstance(":service=monitor"));
140:
141: Counter counter = new Counter();
142: ObjectName counterName = ObjectName
143: .getInstance(":mbean=counter");
144: server.registerMBean(counter, counterName);
145:
146: long period = 1000;
147: monitor.addObservedObject(counterName);
148: monitor.setGranularityPeriod(period);
149: monitor.setObservedAttribute("IntegerCounter");
150: Integer initThreshold = new Integer(3);
151: monitor.setInitThreshold(initThreshold);
152: monitor.setNotify(true);
153: // No modulus, no offset
154:
155: counter.setIntegerCounter(initThreshold.intValue() - 1);
156:
157: final MutableInteger times = new MutableInteger(0);
158: final MutableObject holder = new MutableObject(null);
159: monitor.addNotificationListener(new NotificationListener() {
160: public void handleNotification(Notification notification,
161: Object handback) {
162: times.set(times.get() + 1);
163: holder.set(notification);
164: }
165: }, null, null);
166: monitor.start();
167:
168: try {
169: // Below threshold, no notifications should be sent
170: sleep(period * 3);
171: assertEquals(times.get(), 0);
172: assertNull(holder.get());
173:
174: // Above threshold, just one notification should be sent
175: counter.setIntegerCounter(initThreshold.intValue() + 1);
176: sleep(period * 3);
177: assertEquals(times.get(), 1);
178: MonitorNotification notification = (MonitorNotification) holder
179: .get();
180: assertEquals(notification.getType(),
181: MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
182:
183: times.set(0);
184: holder.set(null);
185: sleep(period * 3);
186: assertEquals(times.get(), 0);
187: } finally {
188: monitor.stop();
189: }
190: }
191:
192: public void testIntegerCounterWithOffset() throws Exception {
193: MBeanServer server = newMBeanServer();
194: CounterMonitor monitor = (CounterMonitor) createMonitor();
195: server.registerMBean(monitor, ObjectName
196: .getInstance(":service=monitor"));
197:
198: Counter counter = new Counter();
199: ObjectName counterName = ObjectName
200: .getInstance(":mbean=counter");
201: server.registerMBean(counter, counterName);
202:
203: long period = 1000;
204: monitor.addObservedObject(counterName);
205: monitor.setGranularityPeriod(period);
206: monitor.setObservedAttribute("IntegerCounter");
207: Integer initThreshold = new Integer(3);
208: monitor.setInitThreshold(initThreshold);
209: monitor.setNotify(true);
210: Integer offset = new Integer(5);
211: monitor.setOffset(offset);
212: // No modulus
213:
214: counter.setIntegerCounter(initThreshold.intValue() - 1);
215:
216: final MutableInteger times = new MutableInteger(0);
217: final MutableObject holder = new MutableObject(null);
218: monitor.addNotificationListener(new NotificationListener() {
219: public void handleNotification(Notification notification,
220: Object handback) {
221: times.set(times.get() + 1);
222: holder.set(notification);
223: }
224: }, null, null);
225: monitor.start();
226:
227: try {
228: // Below threshold, no notifications should be sent
229: sleep(period * 3);
230: assertEquals(times.get(), 0);
231: assertNull(holder.get());
232:
233: // Above threshold, just one notification should be sent
234: counter.setIntegerCounter(initThreshold.intValue() + 1);
235: sleep(period * 3);
236: assertEquals(times.get(), 1);
237: MonitorNotification notification = (MonitorNotification) holder
238: .get();
239: assertEquals(notification.getType(),
240: MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
241: // The threshold should have offset
242: Number threshold = monitor.getThreshold(counterName);
243: assertEquals(threshold.intValue(), monitor
244: .getInitThreshold().intValue()
245: + offset.intValue());
246:
247: times.set(0);
248: holder.set(null);
249: sleep(period * 3);
250: assertEquals(times.get(), 0);
251:
252: // Above threshold by more than 1 offset
253: counter.setIntegerCounter(initThreshold.intValue()
254: + offset.intValue() * 2 + 1);
255: sleep(period * 3);
256: assertEquals(times.get(), 1);
257: notification = (MonitorNotification) holder.get();
258: assertEquals(notification.getType(),
259: MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
260: // The threshold should have offset correctly
261: threshold = monitor.getThreshold(counterName);
262: assertEquals(threshold.intValue(), monitor
263: .getInitThreshold().intValue()
264: + offset.intValue() * 3);
265:
266: times.set(0);
267: holder.set(null);
268: sleep(period * 3);
269: assertEquals(times.get(), 0);
270: } finally {
271: monitor.stop();
272: }
273: }
274:
275: public interface CounterMBean {
276: public Object getObjectCounter();
277:
278: public Integer getNegativeCounter();
279:
280: public int getIntegerCounter();
281: }
282:
283: public static class Counter implements CounterMBean {
284: private int integerCounter;
285:
286: public Object getObjectCounter() {
287: return new Object();
288: }
289:
290: public Integer getNegativeCounter() {
291: return new Integer(-1);
292: }
293:
294: public int getIntegerCounter() {
295: return integerCounter;
296: }
297:
298: public void setIntegerCounter(int integerCounter) {
299: this.integerCounter = integerCounter;
300: }
301: }
302: }
|