User: ejort Date: 02/03/08 10:21:59 Modified: src/main/test/compliance/timer TimerSUITE.java Added: src/main/test/compliance/timer TimerNotificationTestCase.java Removed: src/main/test/compliance/timer BasicTEST.java TimerUnitTestSUITE.java Log: Better timer tests Revision Changes Path 1.2 +16 -2 jmx/src/main/test/compliance/timer/TimerSUITE.java Index: TimerSUITE.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/test/compliance/timer/TimerSUITE.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TimerSUITE.java 26 Jan 2002 21:03:21 -0000 1.1 +++ TimerSUITE.java 8 Mar 2002 18:21:59 -0000 1.2 @@ -19,6 +19,21 @@ extends TestSuite { /** + * The maximum wait for a notification + */ + public static final long MAX_WAIT = 10000; + + /** + * The time for a repeated notification + */ + public static final long REPEAT_TIME = 500; + + /** + * The immediate registration time, allow for processing + */ + public static final long ZERO_TIME = 100; + + /** * Run the tests * * @param args the arguments for the test @@ -37,8 +52,7 @@ { TestSuite suite = new TestSuite("Timer Service Tests"); - suite.addTest(TimerUnitTestSUITE.suite()); - suite.addTest(new TestSuite(BasicTEST.class)); + suite.addTest(new TestSuite(TimerNotificationTestCase.class)); return suite; } 1.1 jmx/src/main/test/compliance/timer/TimerNotificationTestCase.java Index: TimerNotificationTestCase.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package test.compliance.timer; import test.compliance.timer.TimerSUITE; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; import javax.management.Notification; import javax.management.NotificationListener; import javax.management.timer.Timer; import javax.management.timer.TimerNotification; import junit.framework.TestCase; /** * Timer Notification Tests * * @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>. */ public class TimerNotificationTestCase extends TestCase implements NotificationListener { // Constants --------------------------------------------------------------- String TIMER_TYPE = "TimerType"; String MESSAGE = "Message"; String USER_DATA = "UserData"; // Attributes -------------------------------------------------------------- /** * The MBeanServer */ MBeanServer server; /** * The object name of the timer */ ObjectName timerName; /** * The timer */ Timer timer; /** * The timer notification id */ Integer id; /** * The notifications */ ArrayList notifications = new ArrayList(); // Constructor ------------------------------------------------------------- /** * Construct the test */ public TimerNotificationTestCase(String s) { super(s); } // Tests ------------------------------------------------------------------- /** * Test a notification gives reasonable values */ public void testReasonableValues() { initTest(); try { initTimer(); startTimer(); long startTime = timeOffset(0).getTime(); addNotification(TimerSUITE.ZERO_TIME); sync(); stopTimer(); long endTime = timeOffset(0).getTime(); assertEquals(1, notifications.size()); TimerNotification tn = (TimerNotification) notifications.get(0); assertEquals(MESSAGE, tn.getMessage()); assertEquals(1, tn.getSequenceNumber()); assertEquals(timerName, tn.getSource()); assertEquals(TIMER_TYPE, tn.getType()); assertEquals(USER_DATA, tn.getUserData()); assertEquals(id, tn.getNotificationID()); if (tn.getTimeStamp() < startTime + TimerSUITE.ZERO_TIME) fail("Timer notification before start?"); if (tn.getTimeStamp() > endTime) fail("Timer notification after end?"); } finally { MBeanServerFactory.releaseMBeanServer(server); } } // Support ----------------------------------------------------------------- /** * Start a new test */ private void initTest() { notifications.clear(); server = MBeanServerFactory.createMBeanServer(); } /** * Create a timer and register ourselves as a listener */ private void initTimer() { try { timer = new Timer(); timerName = new ObjectName("test:type=timer"); server.registerMBean(timer, timerName); server.addNotificationListener(timerName, this, null, null); } catch (Exception e) { fail(e.toString()); } } /** * Start the timer */ private void startTimer() { timer.start(); } /** * Stop the timer, does a small wait to avoid problems with the RI */ private void stopTimer() { sleep(); timer.stop(); } /** * Add a notification */ private void addNotification(long offset) { id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA, timeOffset(TimerSUITE.ZERO_TIME)); } /** * Handle the notification, just add it to the list */ public void handleNotification(Notification n, Object ignored) { notifications.add(n); synchronized(notifications) { notifications.notifyAll(); } } /** * Sync with the notification handler */ private void sync() { synchronized(notifications) { try { notifications.wait(TimerSUITE.MAX_WAIT); } catch (InterruptedException ignored) { } } } /** * Get the time using and offset */ private Date timeOffset(long offset) { return new Date(System.currentTimeMillis() + offset); } /** * Sleep for the default time */ private void sleep() { sleep(TimerSUITE.ZERO_TIME); } /** * Sleep for a bit */ private void sleep(long time) { try { Thread.currentThread().sleep(time); } catch (InterruptedException ignored) { } } }
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development