KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > monitor > BasicTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmx.compliance.monitor;
23
24 import java.util.ArrayList JavaDoc;
25
26 import javax.management.Attribute JavaDoc;
27 import javax.management.AttributeList JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MBeanServerFactory JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.NotificationFilter JavaDoc;
32 import javax.management.NotificationListener JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.monitor.CounterMonitor JavaDoc;
35 import javax.management.monitor.GaugeMonitor JavaDoc;
36 import javax.management.monitor.MonitorNotification JavaDoc;
37 import javax.management.monitor.StringMonitor JavaDoc;
38
39 import org.jboss.test.jbossmx.compliance.TestCase;
40 import org.jboss.test.jbossmx.compliance.monitor.support.CounterSupport;
41 import org.jboss.test.jbossmx.compliance.monitor.support.StringSupport;
42
43 /**
44  * Basic monitor test.<p>
45  *
46  * The aim of these tests is to check the most common uses of the monitor
47  * services.
48  *
49  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
50  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
51  */

52 public class BasicTestCase
53   extends TestCase
54   implements NotificationListener JavaDoc
55 {
56   // Attributes ----------------------------------------------------------------
57

58   /**
59    * The object name of the monitor service
60    */

61   ObjectName JavaDoc monitorName;
62
63   /**
64    * The MBean server
65    */

66   MBeanServer JavaDoc server;
67
68   /**
69    * The observed object
70    */

71   Object JavaDoc monitored;
72
73   /**
74    * The observed object name
75    */

76   ObjectName JavaDoc observedObject;
77
78   /**
79    * The observed attribute
80    */

81   String JavaDoc observedAttribute;
82
83   /**
84    * The received notifications
85    */

86   ArrayList JavaDoc receivedNotifications = new ArrayList JavaDoc();
87
88   // Constructor ---------------------------------------------------------------
89

90   public BasicTestCase(String JavaDoc s)
91   {
92     super(s);
93   }
94
95   // Tests ---------------------------------------------------------------------
96

97   /**
98    * Test simple counter notification.
99    */

100   public void testCounterSimpleNotification()
101     throws Exception JavaDoc
102   {
103     try
104     {
105       monitored = new CounterSupport();
106       observedObject = new ObjectName JavaDoc("Monitor:type=CounterSupport");
107       observedAttribute = "Value";
108       startCounterService(false, 0, 0, 10);
109        ObjectName JavaDoc[] observed = (ObjectName JavaDoc[]) server.getAttribute(this.monitorName,
110           "ObservedObjects");
111        assertTrue("ObservedObjects.length == 1", observed.length == 1);
112        assertTrue("ObservedObjects[0] == Monitor:type=CounterSupport",
113           observed[0].equals(observedObject));
114
115       setAttribute(null, 0);
116       setAttribute(new Integer JavaDoc(10), 1);
117       setAttribute(new Integer JavaDoc(9), 1);
118       setAttribute(new Integer JavaDoc(10), 2);
119     }
120     finally
121     {
122       stopMonitorService();
123     }
124   }
125
126   /**
127    * Test a counter in difference mode.
128    */

129   public void testCounterDifferenceNotification()
130     throws Exception JavaDoc
131   {
132     try
133     {
134       monitored = new CounterSupport();
135       observedObject = new ObjectName JavaDoc("Monitor:type=CounterSupport");
136       observedAttribute = "Value";
137       startCounterService(true, 0, 0, 10);
138
139       setAttribute(null, 0);
140       setAttribute(new Integer JavaDoc(10), 1);
141       setAttribute(new Integer JavaDoc(9), 1);
142       setAttribute(new Integer JavaDoc(10), 1);
143       setAttribute(new Integer JavaDoc(20), 2);
144     }
145     finally
146     {
147       stopMonitorService();
148     }
149   }
150
151   /**
152    * Test simple gauge notification high and low.
153    */

154   public void testGaugeSimpleBothNotification()
155     throws Exception JavaDoc
156   {
157     try
158     {
159       monitored = new CounterSupport();
160       observedObject = new ObjectName JavaDoc("Monitor:type=GaugeSupport");
161       observedAttribute = "Value";
162       startGaugeService(true, true, false, 10, 0);
163
164       setAttribute(null, 1);
165       setAttribute(new Integer JavaDoc(10), 2);
166       setAttribute(new Integer JavaDoc(9), 2);
167       setAttribute(new Integer JavaDoc(10), 2);
168       setAttribute(new Integer JavaDoc(0), 3);
169       setAttribute(new Integer JavaDoc(1), 3);
170       setAttribute(new Integer JavaDoc(0), 3);
171     }
172     finally
173     {
174       stopMonitorService();
175     }
176   }
177
178   /**
179    * Test simple gauge notification high.
180    */

181   public void testGaugeSimpleHighNotification()
182     throws Exception JavaDoc
183   {
184     try
185     {
186       monitored = new CounterSupport();
187       observedObject = new ObjectName JavaDoc("Monitor:type=GaugeSupport");
188       observedAttribute = "Value";
189       startGaugeService(true, false, false, 10, 0);
190
191       setAttribute(null, 0);
192       setAttribute(new Integer JavaDoc(10),1 );
193       setAttribute(new Integer JavaDoc(9), 1);
194       setAttribute(new Integer JavaDoc(10), 1);
195       setAttribute(new Integer JavaDoc(0), 1);
196       setAttribute(new Integer JavaDoc(10), 2);
197     }
198     finally
199     {
200       stopMonitorService();
201     }
202   }
203
204   /**
205    * Test simple gauge notification low.
206    */

207   public void testGaugeSimpleLowNotification()
208     throws Exception JavaDoc
209   {
210     try
211     {
212       monitored = new CounterSupport();
213       observedObject = new ObjectName JavaDoc("Monitor:type=GaugeSupport");
214       observedAttribute = "Value";
215       startGaugeService(false, true, false, 10, 0);
216
217       setAttribute(null, 1);
218       setAttribute(new Integer JavaDoc(10), 1);
219       setAttribute(new Integer JavaDoc(9), 1);
220       setAttribute(new Integer JavaDoc(0), 2);
221       setAttribute(new Integer JavaDoc(1), 2);
222       setAttribute(new Integer JavaDoc(0), 2);
223     }
224     finally
225     {
226       stopMonitorService();
227     }
228   }
229
230   /**
231    * Test a String notification (both match and differ).
232    */

233   public void testStringBothNotification()
234     throws Exception JavaDoc
235   {
236     try
237     {
238       monitored = new StringSupport();
239       observedObject = new ObjectName JavaDoc("Monitor:type=StringSupport");
240       observedAttribute = "Value";
241       startStringService(true, true, "test");
242
243       // until sun-bug #6200031 gets resolved, reduce the counters by one
244
// not expecting a MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
245
setAttribute(null, 0);
246       setAttribute("test", 1);
247       setAttribute("not-test", 2);
248     }
249     finally
250     {
251       stopMonitorService();
252     }
253   }
254
255   /**
256    * Test a String notification (just match).
257    */

258   public void testStringMatchNotification()
259     throws Exception JavaDoc
260   {
261     try
262     {
263       monitored = new StringSupport();
264       observedObject = new ObjectName JavaDoc("Monitor:type=StringSupport");
265       observedAttribute = "Value";
266       startStringService(true, false, "test");
267
268       // until sun-bug #6200031 gets resolved, reduce the counters by one
269
// not expecting a MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
270
setAttribute(null, 0);
271       setAttribute("test", 1);
272       setAttribute("not-test", 1);
273     }
274     finally
275     {
276       stopMonitorService();
277     }
278   }
279
280   /**
281    * Test a String notification (just differ).
282    */

283   public void testStringDifferNotification()
284     throws Exception JavaDoc
285   {
286     try
287     {
288       monitored = new StringSupport();
289       observedObject = new ObjectName JavaDoc("Monitor:type=StringSupport");
290       observedAttribute = "Value";
291       startStringService(false, true, "test");
292
293       // until sun-bug #6200031 gets resolved, reduce the counters by one
294
// not expecting a MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
295
setAttribute(null, 0);
296       setAttribute("test", 0);
297       setAttribute("not-test", 1);
298     }
299     finally
300     {
301       stopMonitorService();
302     }
303   }
304
305   // Support functions ---------------------------------------------------------
306

307   /**
308    * Start a counter service
309    * @param mode the difference mode
310    * @param modulus for counters that wrap
311    * @param offset the offset value
312    * @param threshold the threshold value
313    */

314   private void startCounterService(boolean mode, int modulus,
315                                    int offset, int threshold)
316     throws Exception JavaDoc
317   {
318     installMonitorService(new CounterMonitor JavaDoc());
319     AttributeList JavaDoc attributes = new AttributeList JavaDoc();
320     attributes.add(new Attribute JavaDoc("DifferenceMode", new Boolean JavaDoc(mode)));
321     attributes.add(new Attribute JavaDoc("Modulus", new Integer JavaDoc(modulus)));
322     attributes.add(new Attribute JavaDoc("Offset", new Integer JavaDoc(offset)));
323     attributes.add(new Attribute JavaDoc("Notify", new Boolean JavaDoc(true)));
324     attributes.add(new Attribute JavaDoc("Threshold", new Integer JavaDoc(threshold)));
325     attributes.add(new Attribute JavaDoc("GranularityPeriod", new Long JavaDoc(PERIOD)));
326     attributes.add(new Attribute JavaDoc("ObservedObject", observedObject));
327     attributes.add(new Attribute JavaDoc("ObservedAttribute", observedAttribute));
328     int before = attributes.size();
329     attributes = server.setAttributes(monitorName, attributes);
330     assertEquals(before, attributes.size());
331
332     server.invoke(monitorName, "start", new Object JavaDoc[0], new String JavaDoc[0]);
333   }
334
335   /**
336    * Start a gauge service
337    * @param high notify on high
338    * @param low notifiy on low
339    * @param high notify on high
340    * @param differ difference mode
341    * @param highValue high threshold
342    * @param lowValue low threshold
343    */

344   private void startGaugeService(boolean high, boolean low, boolean differ,
345                                  int highValue, int lowValue)
346     throws Exception JavaDoc
347   {
348     installMonitorService(new GaugeMonitor JavaDoc());
349     AttributeList JavaDoc attributes = new AttributeList JavaDoc();
350     attributes.add(new Attribute JavaDoc("NotifyHigh", new Boolean JavaDoc(high)));
351     attributes.add(new Attribute JavaDoc("NotifyLow", new Boolean JavaDoc(low)));
352     attributes.add(new Attribute JavaDoc("DifferenceMode", new Boolean JavaDoc(differ)));
353     attributes.add(new Attribute JavaDoc("GranularityPeriod", new Long JavaDoc(PERIOD)));
354     attributes.add(new Attribute JavaDoc("ObservedObject", observedObject));
355     attributes.add(new Attribute JavaDoc("ObservedAttribute", observedAttribute));
356     int before = attributes.size();
357     attributes = server.setAttributes(monitorName, attributes);
358     assertEquals(before, attributes.size());
359
360     server.invoke(monitorName, "setThresholds",
361       new Object JavaDoc[] { new Integer JavaDoc(highValue), new Integer JavaDoc(lowValue) },
362       new String JavaDoc[] { "java.lang.Number", "java.lang.Number" });
363
364     server.invoke(monitorName, "start", new Object JavaDoc[0], new String JavaDoc[0]);
365   }
366
367   /**
368    * Start a string service
369    * @param match notify on match
370    * @param differ notifiy on differ
371    * @param value the value to check
372    */

373   private void startStringService(boolean match, boolean differ,
374                                    String JavaDoc value)
375     throws Exception JavaDoc
376   {
377     installMonitorService(new StringMonitor JavaDoc());
378     AttributeList JavaDoc attributes = new AttributeList JavaDoc();
379     attributes.add(new Attribute JavaDoc("NotifyDiffer", new Boolean JavaDoc(differ)));
380     attributes.add(new Attribute JavaDoc("NotifyMatch", new Boolean JavaDoc(match)));
381     attributes.add(new Attribute JavaDoc("StringToCompare", value));
382     attributes.add(new Attribute JavaDoc("GranularityPeriod", new Long JavaDoc(PERIOD)));
383     attributes.add(new Attribute JavaDoc("ObservedObject", observedObject));
384     attributes.add(new Attribute JavaDoc("ObservedAttribute", observedAttribute));
385     int before = attributes.size();
386     attributes = server.setAttributes(monitorName, attributes);
387     assertEquals(before, attributes.size());
388
389     server.invoke(monitorName, "start", new Object JavaDoc[0], new String JavaDoc[0]);
390   }
391
392   /**
393    * Get an MBeanServer, install the monitor service and a notification
394    * listener.
395    * @param monitor the object doing the monitoring
396    */

397   private void installMonitorService(Object JavaDoc monitor)
398     throws Exception JavaDoc
399   {
400     server = MBeanServerFactory.createMBeanServer("Monitor");
401
402     monitorName = new ObjectName JavaDoc("Monitor:type=MonitorService");
403     server.registerMBean(monitor, monitorName);
404
405     receivedNotifications.clear();
406     
407     // until sun-bug #6200031 gets resolved
408
// filter out OBSERVED_ATTRIBUTE_TYPE_ERROR
409
NotificationFilter JavaDoc filter = new NotificationFilter JavaDoc() {
410        public boolean isNotificationEnabled(Notification JavaDoc notification)
411        {
412           return !MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR.equals(notification.getType());
413        }
414     };
415     
416     server.addNotificationListener(monitorName, this, filter, null);
417
418     server.registerMBean(monitored, observedObject);
419   }
420
421   /**
422    * Remove everything used by this test. Cannot report failures because
423    * the test might have failed earlier.
424    */

425   private void stopMonitorService()
426   {
427     try
428     {
429       server.invoke(monitorName, "stop", new Object JavaDoc[0], new String JavaDoc[0]);
430       server.removeNotificationListener(monitorName, this);
431       server.unregisterMBean(observedObject);
432       server.unregisterMBean(monitorName);
433       MBeanServerFactory.releaseMBeanServer(server);
434     }
435     catch (Exception JavaDoc ignored) {}
436   }
437
438   /**
439    * Set an attribute and check the correct notifications are received
440    * @param value the value to set, null is past at the start - only
441    * the check is perform
442    * @param expected the expected number of notifications after setting
443    * the value
444    */

445   private void setAttribute(Object JavaDoc value, int expected)
446     throws Exception JavaDoc
447   {
448     // Set the attribute unless the test has just started
449
if (value != null)
450     {
451       Attribute JavaDoc attribute = new Attribute JavaDoc(observedAttribute, value);
452       server.setAttribute(observedObject, attribute);
453     }
454
455     // Wait for the notification
456
synchronized (receivedNotifications)
457     {
458       if (receivedNotifications.size() > expected )
459         fail("too many notifications");
460       if (receivedNotifications.size() <= expected )
461         receivedNotifications.wait(WAIT);
462       assertEquals(expected, receivedNotifications.size());
463     }
464   }
465
466   /**
467    * Handle a notification, just add it to the list
468    *
469    * @param notification the notification received
470    * @param handback not used
471    */

472   public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
473   {
474     synchronized (receivedNotifications)
475     {
476       receivedNotifications.add(notification);
477       receivedNotifications.notifyAll();
478     }
479   }
480 }
481
Popular Tags