package org.jboss.test.jbossmx.compliance.monitor;
import java.util.ArrayList;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.monitor.CounterMonitor;
import javax.management.monitor.GaugeMonitor;
import javax.management.monitor.MonitorNotification;
import javax.management.monitor.StringMonitor;
import org.jboss.test.jbossmx.compliance.TestCase;
import org.jboss.test.jbossmx.compliance.monitor.support.CounterSupport;
import org.jboss.test.jbossmx.compliance.monitor.support.StringSupport;
public class BasicTestCase
extends TestCase
implements NotificationListener
{
ObjectName monitorName;
MBeanServer server;
Object monitored;
ObjectName observedObject;
String observedAttribute;
ArrayList receivedNotifications = new ArrayList();
public BasicTestCase(String s)
{
super(s);
}
public void testCounterSimpleNotification()
throws Exception
{
try
{
monitored = new CounterSupport();
observedObject = new ObjectName("Monitor:type=CounterSupport");
observedAttribute = "Value";
startCounterService(false, 0, 0, 10);
ObjectName[] observed = (ObjectName[]) server.getAttribute(this.monitorName,
"ObservedObjects");
assertTrue("ObservedObjects.length == 1", observed.length == 1);
assertTrue("ObservedObjects[0] == Monitor:type=CounterSupport",
observed[0].equals(observedObject));
setAttribute(null, 0);
setAttribute(new Integer(10), 1);
setAttribute(new Integer(9), 1);
setAttribute(new Integer(10), 2);
}
finally
{
stopMonitorService();
}
}
public void testCounterDifferenceNotification()
throws Exception
{
try
{
monitored = new CounterSupport();
observedObject = new ObjectName("Monitor:type=CounterSupport");
observedAttribute = "Value";
startCounterService(true, 0, 0, 10);
setAttribute(null, 0);
setAttribute(new Integer(10), 1);
setAttribute(new Integer(9), 1);
setAttribute(new Integer(10), 1);
setAttribute(new Integer(20), 2);
}
finally
{
stopMonitorService();
}
}
public void testGaugeSimpleBothNotification()
throws Exception
{
try
{
monitored = new CounterSupport();
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(true, true, false, 10, 0);
setAttribute(null, 1);
setAttribute(new Integer(10), 2);
setAttribute(new Integer(9), 2);
setAttribute(new Integer(10), 2);
setAttribute(new Integer(0), 3);
setAttribute(new Integer(1), 3);
setAttribute(new Integer(0), 3);
}
finally
{
stopMonitorService();
}
}
public void testGaugeSimpleHighNotification()
throws Exception
{
try
{
monitored = new CounterSupport();
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(true, false, false, 10, 0);
setAttribute(null, 0);
setAttribute(new Integer(10),1 );
setAttribute(new Integer(9), 1);
setAttribute(new Integer(10), 1);
setAttribute(new Integer(0), 1);
setAttribute(new Integer(10), 2);
}
finally
{
stopMonitorService();
}
}
public void testGaugeSimpleLowNotification()
throws Exception
{
try
{
monitored = new CounterSupport();
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(false, true, false, 10, 0);
setAttribute(null, 1);
setAttribute(new Integer(10), 1);
setAttribute(new Integer(9), 1);
setAttribute(new Integer(0), 2);
setAttribute(new Integer(1), 2);
setAttribute(new Integer(0), 2);
}
finally
{
stopMonitorService();
}
}
public void testStringBothNotification()
throws Exception
{
try
{
monitored = new StringSupport();
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(true, true, "test");
setAttribute(null, 0);
setAttribute("test", 1);
setAttribute("not-test", 2);
}
finally
{
stopMonitorService();
}
}
public void testStringMatchNotification()
throws Exception
{
try
{
monitored = new StringSupport();
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(true, false, "test");
setAttribute(null, 0);
setAttribute("test", 1);
setAttribute("not-test", 1);
}
finally
{
stopMonitorService();
}
}
public void testStringDifferNotification()
throws Exception
{
try
{
monitored = new StringSupport();
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(false, true, "test");
setAttribute(null, 0);
setAttribute("test", 0);
setAttribute("not-test", 1);
}
finally
{
stopMonitorService();
}
}
private void startCounterService(boolean mode, int modulus,
int offset, int threshold)
throws Exception
{
installMonitorService(new CounterMonitor());
AttributeList attributes = new AttributeList();
attributes.add(new Attribute("DifferenceMode", new Boolean(mode)));
attributes.add(new Attribute("Modulus", new Integer(modulus)));
attributes.add(new Attribute("Offset", new Integer(offset)));
attributes.add(new Attribute("Notify", new Boolean(true)));
attributes.add(new Attribute("Threshold", new Integer(threshold)));
attributes.add(new Attribute("GranularityPeriod", new Long(PERIOD)));
attributes.add(new Attribute("ObservedObject", observedObject));
attributes.add(new Attribute("ObservedAttribute", observedAttribute));
int before = attributes.size();
attributes = server.setAttributes(monitorName, attributes);
assertEquals(before, attributes.size());
server.invoke(monitorName, "start", new Object[0], new String[0]);
}
private void startGaugeService(boolean high, boolean low, boolean differ,
int highValue, int lowValue)
throws Exception
{
installMonitorService(new GaugeMonitor());
AttributeList attributes = new AttributeList();
attributes.add(new Attribute("NotifyHigh", new Boolean(high)));
attributes.add(new Attribute("NotifyLow", new Boolean(low)));
attributes.add(new Attribute("DifferenceMode", new Boolean(differ)));
attributes.add(new Attribute("GranularityPeriod", new Long(PERIOD)));
attributes.add(new Attribute("ObservedObject", observedObject));
attributes.add(new Attribute("ObservedAttribute", observedAttribute));
int before = attributes.size();
attributes = server.setAttributes(monitorName, attributes);
assertEquals(before, attributes.size());
server.invoke(monitorName, "setThresholds",
new Object[] { new Integer(highValue), new Integer(lowValue) },
new String[] { "java.lang.Number", "java.lang.Number" });
server.invoke(monitorName, "start", new Object[0], new String[0]);
}
private void startStringService(boolean match, boolean differ,
String value)
throws Exception
{
installMonitorService(new StringMonitor());
AttributeList attributes = new AttributeList();
attributes.add(new Attribute("NotifyDiffer", new Boolean(differ)));
attributes.add(new Attribute("NotifyMatch", new Boolean(match)));
attributes.add(new Attribute("StringToCompare", value));
attributes.add(new Attribute("GranularityPeriod", new Long(PERIOD)));
attributes.add(new Attribute("ObservedObject", observedObject));
attributes.add(new Attribute("ObservedAttribute", observedAttribute));
int before = attributes.size();
attributes = server.setAttributes(monitorName, attributes);
assertEquals(before, attributes.size());
server.invoke(monitorName, "start", new Object[0], new String[0]);
}
private void installMonitorService(Object monitor)
throws Exception
{
server = MBeanServerFactory.createMBeanServer("Monitor");
monitorName = new ObjectName("Monitor:type=MonitorService");
server.registerMBean(monitor, monitorName);
receivedNotifications.clear();
NotificationFilter filter = new NotificationFilter() {
public boolean isNotificationEnabled(Notification notification)
{
return !MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR.equals(notification.getType());
}
};
server.addNotificationListener(monitorName, this, filter, null);
server.registerMBean(monitored, observedObject);
}
private void stopMonitorService()
{
try
{
server.invoke(monitorName, "stop", new Object[0], new String[0]);
server.removeNotificationListener(monitorName, this);
server.unregisterMBean(observedObject);
server.unregisterMBean(monitorName);
MBeanServerFactory.releaseMBeanServer(server);
}
catch (Exception ignored) {}
}
private void setAttribute(Object value, int expected)
throws Exception
{
if (value != null)
{
Attribute attribute = new Attribute(observedAttribute, value);
server.setAttribute(observedObject, attribute);
}
synchronized (receivedNotifications)
{
if (receivedNotifications.size() > expected )
fail("too many notifications");
if (receivedNotifications.size() <= expected )
receivedNotifications.wait(WAIT);
assertEquals(expected, receivedNotifications.size());
}
}
public void handleNotification(Notification notification, Object handback)
{
synchronized (receivedNotifications)
{
receivedNotifications.add(notification);
receivedNotifications.notifyAll();
}
}
}