User: ejort   
  Date: 02/02/01 19:16:24

  Added:       src/main/org/jboss/test/jbossmx/compliance/timer
                        BasicTestCase.java
  Log:
  JBossMX tests, temporary modifications to classpaths see comment in build.xml
  
  Revision  Changes    Path
  1.1                  
jbosstest/src/main/org/jboss/test/jbossmx/compliance/timer/BasicTestCase.java
  
  Index: BasicTestCase.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.test.jbossmx.compliance.timer;
  
  import org.jboss.test.jbossmx.compliance.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 BasicTestCase
    extends TestCase
    implements NotificationListener
  {
    // Attributes ----------------------------------------------------------------
  
    /**
     * The object name of the timer service
     */
    ObjectName timerName;
  
    /**
     * The MBean server
     */
    MBeanServer server;
  
    /**
     * The received notifications
     */
    ArrayList receivedNotifications = new ArrayList();
  
    // Constructor ---------------------------------------------------------------
  
    public BasicTestCase(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();
        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) {}
    }
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to