001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jbossmx.compliance.monitor;
023:
024: import java.util.ArrayList;
025:
026: import javax.management.Attribute;
027: import javax.management.AttributeList;
028: import javax.management.MBeanServer;
029: import javax.management.MBeanServerFactory;
030: import javax.management.Notification;
031: import javax.management.NotificationFilter;
032: import javax.management.NotificationListener;
033: import javax.management.ObjectName;
034: import javax.management.monitor.CounterMonitor;
035: import javax.management.monitor.GaugeMonitor;
036: import javax.management.monitor.MonitorNotification;
037: import javax.management.monitor.StringMonitor;
038:
039: import org.jboss.test.jbossmx.compliance.TestCase;
040: import org.jboss.test.jbossmx.compliance.monitor.support.CounterSupport;
041: import org.jboss.test.jbossmx.compliance.monitor.support.StringSupport;
042:
043: /**
044: * Basic monitor test.<p>
045: *
046: * The aim of these tests is to check the most common uses of the monitor
047: * services.
048: *
049: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
050: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
051: */
052: public class BasicTestCase extends TestCase implements
053: NotificationListener {
054: // Attributes ----------------------------------------------------------------
055:
056: /**
057: * The object name of the monitor service
058: */
059: ObjectName monitorName;
060:
061: /**
062: * The MBean server
063: */
064: MBeanServer server;
065:
066: /**
067: * The observed object
068: */
069: Object monitored;
070:
071: /**
072: * The observed object name
073: */
074: ObjectName observedObject;
075:
076: /**
077: * The observed attribute
078: */
079: String observedAttribute;
080:
081: /**
082: * The received notifications
083: */
084: ArrayList receivedNotifications = new ArrayList();
085:
086: // Constructor ---------------------------------------------------------------
087:
088: public BasicTestCase(String s) {
089: super (s);
090: }
091:
092: // Tests ---------------------------------------------------------------------
093:
094: /**
095: * Test simple counter notification.
096: */
097: public void testCounterSimpleNotification() throws Exception {
098: try {
099: monitored = new CounterSupport();
100: observedObject = new ObjectName(
101: "Monitor:type=CounterSupport");
102: observedAttribute = "Value";
103: startCounterService(false, 0, 0, 10);
104: ObjectName[] observed = (ObjectName[]) server.getAttribute(
105: this .monitorName, "ObservedObjects");
106: assertTrue("ObservedObjects.length == 1",
107: observed.length == 1);
108: assertTrue(
109: "ObservedObjects[0] == Monitor:type=CounterSupport",
110: observed[0].equals(observedObject));
111:
112: setAttribute(null, 0);
113: setAttribute(new Integer(10), 1);
114: setAttribute(new Integer(9), 1);
115: setAttribute(new Integer(10), 2);
116: } finally {
117: stopMonitorService();
118: }
119: }
120:
121: /**
122: * Test a counter in difference mode.
123: */
124: public void testCounterDifferenceNotification() throws Exception {
125: try {
126: monitored = new CounterSupport();
127: observedObject = new ObjectName(
128: "Monitor:type=CounterSupport");
129: observedAttribute = "Value";
130: startCounterService(true, 0, 0, 10);
131:
132: setAttribute(null, 0);
133: setAttribute(new Integer(10), 1);
134: setAttribute(new Integer(9), 1);
135: setAttribute(new Integer(10), 1);
136: setAttribute(new Integer(20), 2);
137: } finally {
138: stopMonitorService();
139: }
140: }
141:
142: /**
143: * Test simple gauge notification high and low.
144: */
145: public void testGaugeSimpleBothNotification() throws Exception {
146: try {
147: monitored = new CounterSupport();
148: observedObject = new ObjectName("Monitor:type=GaugeSupport");
149: observedAttribute = "Value";
150: startGaugeService(true, true, false, 10, 0);
151:
152: setAttribute(null, 1);
153: setAttribute(new Integer(10), 2);
154: setAttribute(new Integer(9), 2);
155: setAttribute(new Integer(10), 2);
156: setAttribute(new Integer(0), 3);
157: setAttribute(new Integer(1), 3);
158: setAttribute(new Integer(0), 3);
159: } finally {
160: stopMonitorService();
161: }
162: }
163:
164: /**
165: * Test simple gauge notification high.
166: */
167: public void testGaugeSimpleHighNotification() throws Exception {
168: try {
169: monitored = new CounterSupport();
170: observedObject = new ObjectName("Monitor:type=GaugeSupport");
171: observedAttribute = "Value";
172: startGaugeService(true, false, false, 10, 0);
173:
174: setAttribute(null, 0);
175: setAttribute(new Integer(10), 1);
176: setAttribute(new Integer(9), 1);
177: setAttribute(new Integer(10), 1);
178: setAttribute(new Integer(0), 1);
179: setAttribute(new Integer(10), 2);
180: } finally {
181: stopMonitorService();
182: }
183: }
184:
185: /**
186: * Test simple gauge notification low.
187: */
188: public void testGaugeSimpleLowNotification() throws Exception {
189: try {
190: monitored = new CounterSupport();
191: observedObject = new ObjectName("Monitor:type=GaugeSupport");
192: observedAttribute = "Value";
193: startGaugeService(false, true, false, 10, 0);
194:
195: setAttribute(null, 1);
196: setAttribute(new Integer(10), 1);
197: setAttribute(new Integer(9), 1);
198: setAttribute(new Integer(0), 2);
199: setAttribute(new Integer(1), 2);
200: setAttribute(new Integer(0), 2);
201: } finally {
202: stopMonitorService();
203: }
204: }
205:
206: /**
207: * Test a String notification (both match and differ).
208: */
209: public void testStringBothNotification() throws Exception {
210: try {
211: monitored = new StringSupport();
212: observedObject = new ObjectName(
213: "Monitor:type=StringSupport");
214: observedAttribute = "Value";
215: startStringService(true, true, "test");
216:
217: // until sun-bug #6200031 gets resolved, reduce the counters by one
218: // not expecting a MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
219: setAttribute(null, 0);
220: setAttribute("test", 1);
221: setAttribute("not-test", 2);
222: } finally {
223: stopMonitorService();
224: }
225: }
226:
227: /**
228: * Test a String notification (just match).
229: */
230: public void testStringMatchNotification() throws Exception {
231: try {
232: monitored = new StringSupport();
233: observedObject = new ObjectName(
234: "Monitor:type=StringSupport");
235: observedAttribute = "Value";
236: startStringService(true, false, "test");
237:
238: // until sun-bug #6200031 gets resolved, reduce the counters by one
239: // not expecting a MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
240: setAttribute(null, 0);
241: setAttribute("test", 1);
242: setAttribute("not-test", 1);
243: } finally {
244: stopMonitorService();
245: }
246: }
247:
248: /**
249: * Test a String notification (just differ).
250: */
251: public void testStringDifferNotification() throws Exception {
252: try {
253: monitored = new StringSupport();
254: observedObject = new ObjectName(
255: "Monitor:type=StringSupport");
256: observedAttribute = "Value";
257: startStringService(false, true, "test");
258:
259: // until sun-bug #6200031 gets resolved, reduce the counters by one
260: // not expecting a MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
261: setAttribute(null, 0);
262: setAttribute("test", 0);
263: setAttribute("not-test", 1);
264: } finally {
265: stopMonitorService();
266: }
267: }
268:
269: // Support functions ---------------------------------------------------------
270:
271: /**
272: * Start a counter service
273: * @param mode the difference mode
274: * @param modulus for counters that wrap
275: * @param offset the offset value
276: * @param threshold the threshold value
277: */
278: private void startCounterService(boolean mode, int modulus,
279: int offset, int threshold) throws Exception {
280: installMonitorService(new CounterMonitor());
281: AttributeList attributes = new AttributeList();
282: attributes.add(new Attribute("DifferenceMode",
283: new Boolean(mode)));
284: attributes.add(new Attribute("Modulus", new Integer(modulus)));
285: attributes.add(new Attribute("Offset", new Integer(offset)));
286: attributes.add(new Attribute("Notify", new Boolean(true)));
287: attributes.add(new Attribute("Threshold",
288: new Integer(threshold)));
289: attributes.add(new Attribute("GranularityPeriod", new Long(
290: PERIOD)));
291: attributes.add(new Attribute("ObservedObject", observedObject));
292: attributes.add(new Attribute("ObservedAttribute",
293: observedAttribute));
294: int before = attributes.size();
295: attributes = server.setAttributes(monitorName, attributes);
296: assertEquals(before, attributes.size());
297:
298: server.invoke(monitorName, "start", new Object[0],
299: new String[0]);
300: }
301:
302: /**
303: * Start a gauge service
304: * @param high notify on high
305: * @param low notifiy on low
306: * @param high notify on high
307: * @param differ difference mode
308: * @param highValue high threshold
309: * @param lowValue low threshold
310: */
311: private void startGaugeService(boolean high, boolean low,
312: boolean differ, int highValue, int lowValue)
313: throws Exception {
314: installMonitorService(new GaugeMonitor());
315: AttributeList attributes = new AttributeList();
316: attributes.add(new Attribute("NotifyHigh", new Boolean(high)));
317: attributes.add(new Attribute("NotifyLow", new Boolean(low)));
318: attributes.add(new Attribute("DifferenceMode", new Boolean(
319: differ)));
320: attributes.add(new Attribute("GranularityPeriod", new Long(
321: PERIOD)));
322: attributes.add(new Attribute("ObservedObject", observedObject));
323: attributes.add(new Attribute("ObservedAttribute",
324: observedAttribute));
325: int before = attributes.size();
326: attributes = server.setAttributes(monitorName, attributes);
327: assertEquals(before, attributes.size());
328:
329: server
330: .invoke(monitorName, "setThresholds",
331: new Object[] { new Integer(highValue),
332: new Integer(lowValue) },
333: new String[] { "java.lang.Number",
334: "java.lang.Number" });
335:
336: server.invoke(monitorName, "start", new Object[0],
337: new String[0]);
338: }
339:
340: /**
341: * Start a string service
342: * @param match notify on match
343: * @param differ notifiy on differ
344: * @param value the value to check
345: */
346: private void startStringService(boolean match, boolean differ,
347: String value) throws Exception {
348: installMonitorService(new StringMonitor());
349: AttributeList attributes = new AttributeList();
350: attributes.add(new Attribute("NotifyDiffer",
351: new Boolean(differ)));
352: attributes
353: .add(new Attribute("NotifyMatch", new Boolean(match)));
354: attributes.add(new Attribute("StringToCompare", value));
355: attributes.add(new Attribute("GranularityPeriod", new Long(
356: PERIOD)));
357: attributes.add(new Attribute("ObservedObject", observedObject));
358: attributes.add(new Attribute("ObservedAttribute",
359: observedAttribute));
360: int before = attributes.size();
361: attributes = server.setAttributes(monitorName, attributes);
362: assertEquals(before, attributes.size());
363:
364: server.invoke(monitorName, "start", new Object[0],
365: new String[0]);
366: }
367:
368: /**
369: * Get an MBeanServer, install the monitor service and a notification
370: * listener.
371: * @param monitor the object doing the monitoring
372: */
373: private void installMonitorService(Object monitor) throws Exception {
374: server = MBeanServerFactory.createMBeanServer("Monitor");
375:
376: monitorName = new ObjectName("Monitor:type=MonitorService");
377: server.registerMBean(monitor, monitorName);
378:
379: receivedNotifications.clear();
380:
381: // until sun-bug #6200031 gets resolved
382: // filter out OBSERVED_ATTRIBUTE_TYPE_ERROR
383: NotificationFilter filter = new NotificationFilter() {
384: public boolean isNotificationEnabled(
385: Notification notification) {
386: return !MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR
387: .equals(notification.getType());
388: }
389: };
390:
391: server.addNotificationListener(monitorName, this , filter, null);
392:
393: server.registerMBean(monitored, observedObject);
394: }
395:
396: /**
397: * Remove everything used by this test. Cannot report failures because
398: * the test might have failed earlier.
399: */
400: private void stopMonitorService() {
401: try {
402: server.invoke(monitorName, "stop", new Object[0],
403: new String[0]);
404: server.removeNotificationListener(monitorName, this );
405: server.unregisterMBean(observedObject);
406: server.unregisterMBean(monitorName);
407: MBeanServerFactory.releaseMBeanServer(server);
408: } catch (Exception ignored) {
409: }
410: }
411:
412: /**
413: * Set an attribute and check the correct notifications are received
414: * @param value the value to set, null is past at the start - only
415: * the check is perform
416: * @param expected the expected number of notifications after setting
417: * the value
418: */
419: private void setAttribute(Object value, int expected)
420: throws Exception {
421: // Set the attribute unless the test has just started
422: if (value != null) {
423: Attribute attribute = new Attribute(observedAttribute,
424: value);
425: server.setAttribute(observedObject, attribute);
426: }
427:
428: // Wait for the notification
429: synchronized (receivedNotifications) {
430: if (receivedNotifications.size() > expected)
431: fail("too many notifications");
432: if (receivedNotifications.size() <= expected)
433: receivedNotifications.wait(WAIT);
434: assertEquals(expected, receivedNotifications.size());
435: }
436: }
437:
438: /**
439: * Handle a notification, just add it to the list
440: *
441: * @param notification the notification received
442: * @param handback not used
443: */
444: public void handleNotification(Notification notification,
445: Object handback) {
446: synchronized (receivedNotifications) {
447: receivedNotifications.add(notification);
448: receivedNotifications.notifyAll();
449: }
450: }
451: }
|