User: ejort
Date: 02/01/26 13:02:21
Added: src/main/test/compliance/monitor BasicTEST.java
MonitorSUITE.java MonitorUnitTestSUITE.java
Log:
Monitor tests
Revision Changes Path
1.1 jmx/src/main/test/compliance/monitor/BasicTEST.java
Index: BasicTEST.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.compliance.monitor;
import junit.framework.TestCase;
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.NotificationListener;
import javax.management.ObjectName;
/**
* Basic monitor test.<p>
*
* The aim of these tests is to check the most common uses of the monitor
* services.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>.
*/
public class BasicTEST
extends TestCase
implements NotificationListener
{
// Attributes ----------------------------------------------------------------
/**
* The period for a monitor notification. This needs to be small so the tests
* don't take too long, but not so small that the tests fail because
* the computer is slow. The wait gives a bit of extra time for
* the notification to happen.
* WARNING: wait - period needs to be big enough, otherwise the RI will hang
* It doesn't use daemon threads and can get confused.
*/
long period = 50;
long wait = 60;
/**
* The number of repeats for occurances tests
*/
long repeats = 2;
/**
* The object name of the monitor service
*/
ObjectName monitorName;
/**
* The MBean server
*/
MBeanServer server;
/**
* The observed class name
*/
String observedClassName;
/**
* The observed object name
*/
ObjectName observedObject;
/**
* The observed attribute
*/
String observedAttribute;
/**
* The received notifications
*/
ArrayList receivedNotifications = new ArrayList();
// Constructor ---------------------------------------------------------------
public BasicTEST(String s)
{
super(s);
}
// Tests ---------------------------------------------------------------------
/**
* Test simple counter notification.
*/
public void testCounterSimpleNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.CounterSupport";
observedObject = new ObjectName("Monitor:type=CounterSupport");
observedAttribute = "Value";
startCounterService(false, 0, 0, 10);
sleep(wait);
assertEquals(0, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(9));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(2, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test a counter in difference mode.
*/
public void testCounterDifferenceNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.CounterSupport";
observedObject = new ObjectName("Monitor:type=CounterSupport");
observedAttribute = "Value";
startCounterService(true, 0, 0, 10);
sleep(wait);
assertEquals(0, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(9));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(20));
sleep(wait);
assertEquals(2, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test simple gauge notification high and low.
*/
public void testGaugeSimpleBothNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.CounterSupport";
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(true, true, false, 10, 0);
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute(new Integer(9));
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute(new Integer(0));
sleep(wait);
assertEquals(3, receivedNotifications.size());
setAttribute(new Integer(1));
sleep(wait);
assertEquals(3, receivedNotifications.size());
setAttribute(new Integer(0));
sleep(wait);
assertEquals(3, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test simple gauge notification high.
*/
public void testGaugeSimpleHighNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.CounterSupport";
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(true, false, false, 10, 0);
sleep(wait);
assertEquals(0, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(9));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(0));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(2, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test simple gauge notification low.
*/
public void testGaugeSimpleLowNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.CounterSupport";
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(false, true, false, 10, 0);
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(10));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(9));
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute(new Integer(0));
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute(new Integer(1));
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute(new Integer(0));
sleep(wait);
assertEquals(2, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test a String notification (both match and differ).
*/
public void testStringBothNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.StringSupport";
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(true, true, "test");
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute("test");
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute("not-test");
sleep(wait);
assertEquals(3, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test a String notification (just match).
*/
public void testStringMatchNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.StringSupport";
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(true, false, "test");
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute("test");
sleep(wait);
assertEquals(2, receivedNotifications.size());
setAttribute("not-test");
sleep(wait);
assertEquals(2, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
/**
* Test a String notification (just differ).
*/
public void testStringDifferNotification()
{
try
{
observedClassName = "test.compliance.monitor.support.StringSupport";
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(false, true, "test");
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute("test");
sleep(wait);
assertEquals(1, receivedNotifications.size());
setAttribute("not-test");
sleep(wait);
assertEquals(2, receivedNotifications.size());
stopMonitorService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopMonitorService();
}
}
// Support functions ---------------------------------------------------------
/**
* Start a counter service
* @param mode the difference mode
* @param modulus for counters that wrap
* @param offset the offset value
* @param threshold the threshold value
*/
private void startCounterService(boolean mode, int modulus,
int offset, int threshold)
throws Exception
{
installMonitorService("javax.management.monitor.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]);
}
/**
* Start a gauge service
* @param high notify on high
* @param low notifiy on low
* @param high notify on high
* @param differ difference mode
* @param highValue high threshold
* @param lowValue low threshold
*/
private void startGaugeService(boolean high, boolean low, boolean differ,
int highValue, int lowValue)
throws Exception
{
installMonitorService("javax.management.monitor.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]);
}
/**
* Start a string service
* @param match notify on match
* @param differ notifiy on differ
* @param value the value to check
*/
private void startStringService(boolean match, boolean differ,
String value)
throws Exception
{
installMonitorService("javax.management.monitor.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]);
}
/**
* Get an MBeanServer, install the monitor service and a notification
* listener.
* @param monitorClassName the class name of the monitor
*/
private void installMonitorService(String monitorClassName)
throws Exception
{
server = MBeanServerFactory.createMBeanServer("Monitor");
monitorName = new ObjectName("Monitor:type=MonitorService");
server.createMBean(monitorClassName, monitorName,
new Object[0], new String[0]);
receivedNotifications.clear();
server.addNotificationListener(monitorName, this, null, null);
server.createMBean(observedClassName, observedObject,
new Object[0], new String[0]);
}
/**
* Remove everything used by this test. Cannot report failures because
* the test might have failed earlier.
*/
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) {}
}
/**
* Set an attribute
* @param value the value to set
*/
private void setAttribute(Object value)
throws Exception
{
Attribute attribute = new Attribute(observedAttribute, value);
server.setAttribute(observedObject, attribute);
}
/**
* Handle a notification, just add it to the list
*
* @param notification the notification received
* @param handback not used
*/
public void handleNotification(Notification notification, Object handback)
{
receivedNotifications.add(notification);
}
/**
* Calculate the time using an offset from the current time.
* @param offset the offset from the current time
* @return the calculated time
*/
private long calcTime(long offset)
{
return System.currentTimeMillis() + offset;
}
/**
* Sleep for some time.
* @param sleep the number of millis to sleep.
*/
private void sleep(long sleep)
{
try
{
Thread.currentThread().sleep(sleep);
}
catch (InterruptedException ignored) {}
}
}
1.1 jmx/src/main/test/compliance/monitor/MonitorSUITE.java
Index: MonitorSUITE.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.compliance.monitor;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Tests for the monitor service.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>.
*/
public class MonitorSUITE
extends TestSuite
{
/**
* Run the tests
*
* @param args the arguments for the test
*/
public static void main(String[] args)
{
junit.textui.TestRunner.run(suite());
}
/**
* Get a list of tests.
*
* @return the tests
*/
public static Test suite()
{
TestSuite suite = new TestSuite("Monitor Service Tests");
suite.addTest(new TestSuite(BasicTEST.class));
suite.addTest(MonitorUnitTestSUITE.suite());
return suite;
}
}
1.1 jmx/src/main/test/compliance/monitor/MonitorUnitTestSUITE.java
Index: MonitorUnitTestSUITE.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.compliance.monitor;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Monitor Unit tests.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>.
*/
public class MonitorUnitTestSUITE
extends TestSuite
{
/**
* Run the tests
*
* @param args the arguments for the test
*/
public static void main(String[] args)
{
junit.textui.TestRunner.run(suite());
}
/**
* Get a list of tests.
*
* @return the tests
*/
public static Test suite()
{
TestSuite suite = new TestSuite("Monitor Service Unit Tests");
return suite;
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development