User: ejort
Date: 02/01/26 13:03:22
Added: src/main/test/compliance/timer BasicTEST.java
TimerSUITE.java TimerUnitTestSUITE.java
Log:
Timer tests
Revision Changes Path
1.1 jmx/src/main/test/compliance/timer/BasicTEST.java
Index: BasicTEST.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.compliance.timer;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Date;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
/**
* Basic timer test.<p>
*
* The aim of these tests is to check the most common uses of the timer
* service.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>.
*/
public class BasicTEST
extends TestCase
implements NotificationListener
{
// Attributes ----------------------------------------------------------------
/**
* The period for a timer 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 timer service
*/
ObjectName timerName;
/**
* The MBean server
*/
MBeanServer server;
/**
* The received notifications
*/
ArrayList receivedNotifications = new ArrayList();
// Constructor ---------------------------------------------------------------
public BasicTEST(String s)
{
super(s);
}
// Tests ---------------------------------------------------------------------
/**
* Test a single notification works.
*/
public void testSingleNotification()
{
try
{
startTimerService();
Integer id = addNotification("test", "hello", "data", calcTime(period),
0, 1);
sleep(wait);
assertEquals(1, receivedNotifications.size());
// It should NOT be be registered!
assertEquals(null, getNotificationType(id));
stopTimerService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopTimerService();
}
}
/**
* Test a repeated notification works.
*/
public void testRepeatedNotification()
{
try
{
startTimerService();
long repeats = 2;
Integer id = addNotification("test", "hello", "data", calcTime(period),
period, repeats);
sleep(wait * repeats);
assertEquals(repeats, receivedNotifications.size());
// It should NOT be be registered!
assertEquals(null, getNotificationType(id));
stopTimerService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopTimerService();
}
}
/**
* Test infinite notification works.
*/
public void testInfiniteNotification()
{
try
{
startTimerService();
Integer id = addNotification("test", "hello", "data", calcTime(period),
period, 0);
sleep(wait * repeats);
assertEquals(repeats, receivedNotifications.size());
// It should still be registered!
assertEquals("test", getNotificationType(id));
stopTimerService();
}
catch (Exception e)
{
fail(e.toString());
}
finally
{
stopTimerService();
}
}
// Support functions ---------------------------------------------------------
/**
* Get an MBeanServer, install the timer service and a notification
* listener.
*/
private void startTimerService()
throws Exception
{
server = MBeanServerFactory.createMBeanServer("Timer");
timerName = new ObjectName("Timer:type=TimerService");
server.createMBean("javax.management.timer.Timer", timerName,
new Object[0], new String[0]);
server.invoke(timerName, "start", new Object[0], new String[0]);
receivedNotifications.clear();
server.addNotificationListener(timerName, this, null, null);
}
/**
* Remove everything used by this test. Cannot report failures because
* the test might have failed earlier.
*/
private void stopTimerService()
{
try
{
server.invoke(timerName, "stop", new Object[0], new String[0]);
server.unregisterMBean(timerName);
MBeanServerFactory.releaseMBeanServer(server);
}
catch (Exception ignored) {}
}
/**
* 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);
}
/**
* Add a timer notification
*
* @param type the type of the notification
* @param message the message
* @param data the user data
* @param time the time of the notification
* @param period the period of notification
* @param occurs the number of occurances
* @return the id of the notfication
*/
private Integer addNotification(String type, String message, String data,
long time, long period, long occurs)
throws Exception
{
return (Integer) server.invoke(timerName, "addNotification",
new Object[] { type, message, data, new Date(time), new Long(period),
new Long(occurs) },
new String[] { "java.lang.String", "java.lang.String", "java.lang.Object",
"java.util.Date", "long", "long" } );
}
/**
* Get the notification type for an id
*
* @param id the id of the notification
* @return the type of the notification
*/
private String getNotificationType(Integer id)
throws Exception
{
return (String) server.invoke(timerName, "getNotificationType",
new Object[] { id },
new String[] { "java.lang.Integer" });
}
/**
* 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/timer/TimerSUITE.java
Index: TimerSUITE.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.compliance.timer;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Tests for the timer service.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>.
*/
public class TimerSUITE
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("Timer Service Tests");
suite.addTest(TimerUnitTestSUITE.suite());
suite.addTest(new TestSuite(BasicTEST.class));
return suite;
}
}
1.1 jmx/src/main/test/compliance/timer/TimerUnitTestSUITE.java
Index: TimerUnitTestSUITE.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.compliance.timer;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Timer Unit tests.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>.
*/
public class TimerUnitTestSUITE
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("Timer Service Unit Tests");
return suite;
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development