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.Monitor;
016: import javax.management.monitor.MonitorNotification;
017:
018: import test.MX4JTestCase;
019: import test.MutableInteger;
020: import test.MutableObject;
021:
022: /**
023: * @version : 1.2 $
024: */
025: public abstract class MonitorTestCase extends MX4JTestCase {
026: public MonitorTestCase(String name) {
027: super (name);
028: }
029:
030: protected abstract Monitor createMonitor();
031:
032: public void testStartStopIsActive() throws Exception {
033: Monitor monitor = createMonitor();
034: monitor.setGranularityPeriod(1000);
035: assertFalse(monitor.isActive());
036: monitor.start();
037: sleep(5000);
038: assertTrue(monitor.isActive());
039: monitor.stop();
040: assertFalse(monitor.isActive());
041: monitor.start();
042: assertTrue(monitor.isActive());
043: monitor.stop();
044: assertFalse(monitor.isActive());
045: }
046:
047: public void testSetObservedObject() throws Exception {
048: Monitor monitor = createMonitor();
049: ObjectName name1 = ObjectName.getInstance(":name=one");
050: monitor.addObservedObject(name1);
051: ObjectName name2 = ObjectName.getInstance(":name=two");
052: monitor.addObservedObject(name2);
053: assertEquals(monitor.getObservedObjects().length, 2);
054: assertTrue(monitor.containsObservedObject(name1));
055: assertTrue(monitor.containsObservedObject(name2));
056: monitor.setObservedObject(name1);
057: assertEquals(monitor.getObservedObjects().length, 1);
058: assertTrue(monitor.containsObservedObject(name1));
059: }
060:
061: public void testMonitorNotificationForMBeanNotRegistered()
062: throws Exception {
063: MBeanServer server = newMBeanServer();
064: Monitor monitor = createMonitor();
065: server.registerMBean(monitor, ObjectName
066: .getInstance(":service=monitor"));
067:
068: ObjectName name1 = ObjectName.getInstance(":name=one");
069: monitor.addObservedObject(name1);
070: monitor.setGranularityPeriod(1000);
071: monitor.setObservedAttribute("dummy");
072:
073: final MutableInteger counter = new MutableInteger(0);
074: final MutableObject holder = new MutableObject(null);
075: monitor.addNotificationListener(new NotificationListener() {
076: public void handleNotification(Notification notification,
077: Object handback) {
078: counter.set(counter.get() + 1);
079: holder.set(notification);
080: }
081: }, null, null);
082: monitor.start();
083:
084: try {
085: // Wait for notification to arrive
086: while (holder.get() == null)
087: sleep(10);
088:
089: // Be sure only one arrived
090: sleep(5000);
091: assertEquals(counter.get(), 1);
092:
093: MonitorNotification notification = (MonitorNotification) holder
094: .get();
095: assertEquals(notification.getType(),
096: MonitorNotification.OBSERVED_OBJECT_ERROR);
097: } finally {
098: monitor.stop();
099: }
100: }
101:
102: public void testMonitorNotificationForUnknownAttribute()
103: throws Exception {
104: MBeanServer server = newMBeanServer();
105: Monitor monitor = createMonitor();
106: server.registerMBean(monitor, ObjectName
107: .getInstance(":service=monitor"));
108:
109: ObjectName name1 = ObjectName
110: .getInstance("JMImplementation:type=MBeanServerDelegate");
111: monitor.addObservedObject(name1);
112: monitor.setGranularityPeriod(1000);
113: monitor.setObservedAttribute("dummy");
114:
115: final MutableInteger counter = new MutableInteger(0);
116: final MutableObject holder = new MutableObject(null);
117: monitor.addNotificationListener(new NotificationListener() {
118: public void handleNotification(Notification notification,
119: Object handback) {
120: counter.set(counter.get() + 1);
121: holder.set(notification);
122: }
123: }, null, null);
124: monitor.start();
125:
126: try {
127: // Wait for notification to arrive
128: while (holder.get() == null)
129: sleep(10);
130:
131: // Be sure only one arrived
132: sleep(5000);
133: assertEquals(counter.get(), 1);
134:
135: MonitorNotification notification = (MonitorNotification) holder
136: .get();
137: assertEquals(notification.getType(),
138: MonitorNotification.OBSERVED_ATTRIBUTE_ERROR);
139: } finally {
140: monitor.stop();
141: }
142: }
143: }
|