KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > monitor > CounterMonitorTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.monitor;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationListener JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.monitor.CounterMonitor JavaDoc;
16 import javax.management.monitor.Monitor JavaDoc;
17 import javax.management.monitor.MonitorNotification JavaDoc;
18
19 import test.MutableInteger;
20 import test.MutableObject;
21
22 /**
23  * @version : $Revision 1.2 $
24  */

25 public class CounterMonitorTest extends MonitorTestCase
26 {
27    public CounterMonitorTest(String JavaDoc name)
28    {
29       super(name);
30    }
31
32    protected Monitor JavaDoc createMonitor()
33    {
34       return new CounterMonitor JavaDoc();
35    }
36
37    public void testCorrectInitialization() throws Exception JavaDoc
38    {
39       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
40       assertEquals(new Integer JavaDoc(0), monitor.getInitThreshold());
41       assertEquals(new Integer JavaDoc(0), monitor.getModulus());
42       assertEquals(new Integer JavaDoc(0), monitor.getOffset());
43       assertFalse(monitor.getDifferenceMode());
44       assertFalse(monitor.getNotify());
45    }
46
47    public void testSetThreshold() throws Exception JavaDoc
48    {
49       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
50       try
51       {
52          monitor.setThreshold(new Integer JavaDoc(-1));
53          fail();
54       }
55       catch (IllegalArgumentException JavaDoc x)
56       {
57       }
58       try
59       {
60          monitor.setInitThreshold(new Integer JavaDoc(-1));
61          fail();
62       }
63       catch (IllegalArgumentException JavaDoc x)
64       {
65       }
66
67       Integer JavaDoc threshold = new Integer JavaDoc(1);
68       monitor.setThreshold(threshold);
69       assertEquals(monitor.getInitThreshold(), threshold);
70
71       threshold = new Integer JavaDoc(2);
72       monitor.setInitThreshold(threshold);
73       assertEquals(monitor.getInitThreshold(), threshold);
74    }
75
76    public void testSetModulus() throws Exception JavaDoc
77    {
78       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
79       try
80       {
81          monitor.setModulus(new Integer JavaDoc(-1));
82          fail();
83       }
84       catch (IllegalArgumentException JavaDoc x)
85       {
86       }
87
88       Integer JavaDoc modulus = new Integer JavaDoc(1);
89       monitor.setModulus(modulus);
90       assertEquals(monitor.getModulus(), modulus);
91    }
92
93    public void testSetOffset() throws Exception JavaDoc
94    {
95       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
96       try
97       {
98          monitor.setOffset(new Integer JavaDoc(-1));
99          fail();
100       }
101       catch (IllegalArgumentException JavaDoc x)
102       {
103       }
104
105       Integer JavaDoc offset = new Integer JavaDoc(1);
106       monitor.setOffset(offset);
107       assertEquals(monitor.getOffset(), offset);
108    }
109
110    public void testMonitorNotificationForBadCounter() throws Exception JavaDoc
111    {
112       MBeanServer JavaDoc server = newMBeanServer();
113       Monitor JavaDoc monitor = createMonitor();
114       server.registerMBean(monitor, ObjectName.getInstance(":service=monitor"));
115
116       Counter counter = new Counter();
117       ObjectName JavaDoc counterName = ObjectName.getInstance(":mbean=counter");
118       server.registerMBean(counter, counterName);
119
120       monitor.addObservedObject(counterName);
121       monitor.setGranularityPeriod(1000);
122       monitor.setObservedAttribute("ObjectCounter");
123
124       final MutableInteger times = new MutableInteger(0);
125       final MutableObject holder = new MutableObject(null);
126       monitor.addNotificationListener(new NotificationListener JavaDoc()
127       {
128          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
129          {
130             times.set(times.get() + 1);
131             holder.set(notification);
132          }
133       }, null, null);
134       monitor.start();
135
136       try
137       {
138          // Wait for notification to arrive
139
while (holder.get() == null) sleep(10);
140
141          // Be sure only one arrived
142
sleep(5000);
143          assertEquals(times.get(), 1);
144
145          MonitorNotification JavaDoc notification = (MonitorNotification JavaDoc)holder.get();
146          assertEquals(notification.getType(), MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR);
147       }
148       finally
149       {
150          monitor.stop();
151       }
152    }
153
154    public void testIntegerCounter() throws Exception JavaDoc
155    {
156       MBeanServer JavaDoc server = newMBeanServer();
157       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
158       server.registerMBean(monitor, ObjectName.getInstance(":service=monitor"));
159
160       Counter counter = new Counter();
161       ObjectName JavaDoc counterName = ObjectName.getInstance(":mbean=counter");
162       server.registerMBean(counter, counterName);
163
164       long period = 1000;
165       monitor.addObservedObject(counterName);
166       monitor.setGranularityPeriod(period);
167       monitor.setObservedAttribute("IntegerCounter");
168       Integer JavaDoc initThreshold = new Integer JavaDoc(3);
169       monitor.setInitThreshold(initThreshold);
170       monitor.setNotify(true);
171       // No modulus, no offset
172

173       counter.setIntegerCounter(initThreshold.intValue() - 1);
174
175       final MutableInteger times = new MutableInteger(0);
176       final MutableObject holder = new MutableObject(null);
177       monitor.addNotificationListener(new NotificationListener JavaDoc()
178       {
179          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
180          {
181             times.set(times.get() + 1);
182             holder.set(notification);
183          }
184       }, null, null);
185       monitor.start();
186
187       try
188       {
189          // Below threshold, no notifications should be sent
190
sleep(period * 3);
191          assertEquals(times.get(), 0);
192          assertNull(holder.get());
193
194          // Above threshold, just one notification should be sent
195
counter.setIntegerCounter(initThreshold.intValue() + 1);
196          sleep(period * 3);
197          assertEquals(times.get(), 1);
198          MonitorNotification JavaDoc notification = (MonitorNotification JavaDoc)holder.get();
199          assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
200
201          times.set(0);
202          holder.set(null);
203          sleep(period * 3);
204          assertEquals(times.get(), 0);
205       }
206       finally
207       {
208          monitor.stop();
209       }
210    }
211
212    public void testIntegerCounterWithOffset() throws Exception JavaDoc
213    {
214       MBeanServer JavaDoc server = newMBeanServer();
215       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
216       server.registerMBean(monitor, ObjectName.getInstance(":service=monitor"));
217
218       Counter counter = new Counter();
219       ObjectName JavaDoc counterName = ObjectName.getInstance(":mbean=counter");
220       server.registerMBean(counter, counterName);
221
222       long period = 1000;
223       monitor.addObservedObject(counterName);
224       monitor.setGranularityPeriod(period);
225       monitor.setObservedAttribute("IntegerCounter");
226       Integer JavaDoc initThreshold = new Integer JavaDoc(3);
227       monitor.setInitThreshold(initThreshold);
228       monitor.setNotify(true);
229       Integer JavaDoc offset = new Integer JavaDoc(5);
230       monitor.setOffset(offset);
231       // No modulus
232

233       counter.setIntegerCounter(initThreshold.intValue() - 1);
234
235       final MutableInteger times = new MutableInteger(0);
236       final MutableObject holder = new MutableObject(null);
237       monitor.addNotificationListener(new NotificationListener JavaDoc()
238       {
239          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
240          {
241             times.set(times.get() + 1);
242             holder.set(notification);
243          }
244       }, null, null);
245       monitor.start();
246
247       try
248       {
249          // Below threshold, no notifications should be sent
250
sleep(period * 3);
251          assertEquals(times.get(), 0);
252          assertNull(holder.get());
253
254          // Above threshold, just one notification should be sent
255
counter.setIntegerCounter(initThreshold.intValue() + 1);
256          sleep(period * 3);
257          assertEquals(times.get(), 1);
258          MonitorNotification JavaDoc notification = (MonitorNotification JavaDoc)holder.get();
259          assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
260          // The threshold should have offset
261
Number JavaDoc threshold = monitor.getThreshold(counterName);
262          assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue());
263
264          times.set(0);
265          holder.set(null);
266          sleep(period * 3);
267          assertEquals(times.get(), 0);
268
269          // Above threshold by more than 1 offset
270
counter.setIntegerCounter(initThreshold.intValue() + offset.intValue() * 2 + 1);
271          sleep(period * 3);
272          assertEquals(times.get(), 1);
273          notification = (MonitorNotification JavaDoc)holder.get();
274          assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
275          // The threshold should have offset correctly
276
threshold = monitor.getThreshold(counterName);
277          assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue() * 3);
278
279          times.set(0);
280          holder.set(null);
281          sleep(period * 3);
282          assertEquals(times.get(), 0);
283       }
284       finally
285       {
286          monitor.stop();
287       }
288    }
289
290    public void testShutDownMonitorThread() throws Exception JavaDoc
291    {
292       MBeanServer JavaDoc server = newMBeanServer();
293       CounterMonitor JavaDoc monitor = (CounterMonitor JavaDoc)createMonitor();
294       server.registerMBean(monitor, ObjectName.getInstance(":service=monitor"));
295
296       Counter counter = new Counter();
297       ObjectName JavaDoc counterName = ObjectName.getInstance(":mbean=counter");
298       server.registerMBean(counter, counterName);
299
300       long period = 1000;
301       monitor.addObservedObject(counterName);
302       monitor.setGranularityPeriod(period);
303       monitor.setObservedAttribute("IntegerCounter");
304       Integer JavaDoc initThreshold = new Integer JavaDoc(3);
305       monitor.setInitThreshold(initThreshold);
306       monitor.setNotify(true);
307       // No modulus, no offset
308

309       counter.setIntegerCounter(initThreshold.intValue() - 1);
310
311       final MutableObject monitorThread = new MutableObject(null);
312       monitor.addNotificationListener(new NotificationListener JavaDoc()
313       {
314          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
315          {
316             monitorThread.set(Thread.currentThread());
317          }
318       }, null, null);
319       monitor.start();
320
321       // Below threshold, no notifications should be sent
322
sleep(period * 3);
323
324       // Above threshold, just one notification should be sent
325
counter.setIntegerCounter(initThreshold.intValue() + 1);
326       sleep(period * 3);
327
328       Thread JavaDoc thread = (Thread JavaDoc)monitorThread.get();
329       assertNotNull(thread);
330       assertNotSame(thread, Thread.currentThread());
331
332       monitor.stop();
333
334       sleep(period * 3);
335
336       assertFalse(thread.isAlive());
337    }
338
339    public interface CounterMBean
340    {
341       public Object JavaDoc getObjectCounter();
342
343       public Integer JavaDoc getNegativeCounter();
344
345       public int getIntegerCounter();
346    }
347
348    public static class Counter implements CounterMBean
349    {
350       private int integerCounter;
351
352       public Object JavaDoc getObjectCounter()
353       {
354          return new Object JavaDoc();
355       }
356
357       public Integer JavaDoc getNegativeCounter()
358       {
359          return new Integer JavaDoc(-1);
360       }
361
362       public int getIntegerCounter()
363       {
364          return integerCounter;
365       }
366
367       public void setIntegerCounter(int integerCounter)
368       {
369          this.integerCounter = integerCounter;
370       }
371    }
372 }
373
Popular Tags