Package test.implementation.notification

Source Code of test.implementation.notification.AsynchNotificationBroadcasterSupportTestCase

/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package test.implementation.notification;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.management.ListenerNotFoundException;
import javax.management.Notification;
import javax.management.NotificationFilterSupport;

import junit.framework.TestCase;

import org.jboss.mx.notification.AsynchNotificationBroadcasterSupport;

import test.implementation.notification.support.Listener;

/**
* Asynch Notification Broadcaster Support tests.
*
* @author  <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
*/
public class AsynchNotificationBroadcasterSupportTestCase
  extends TestCase
{
   // Attributes ----------------------------------------------------------------

   /**
    * The sent notifications
    */
   private ArrayList sent = new ArrayList();

   /**
    * The next notification sequence
    */
   private long sequence = 0;

   /**
    * The default notification type
    */
   private static final String DEFAULT_TYPE = "DefaultType";

   /**
    * A different notification type
    */
   private static final String ANOTHER_TYPE = "AnotherType";

   /**
    * No notifications
    */
   private static final ArrayList EMPTY = new ArrayList();

   // Constructor ---------------------------------------------------------------

   /**
    * Construct the test
    */
   public AsynchNotificationBroadcasterSupportTestCase(String s)
   {
      super(s);
   }

   // Tests ---------------------------------------------------------------------

   public void testAsynchDelivery()
      throws Exception
   {
      AsynchNotificationBroadcasterSupport broadcaster = new AsynchNotificationBroadcasterSupport();

      Listener listener = new Listener();
      broadcaster.addNotificationListener(listener, null, null);

      clear();
      createNotification(broadcaster);

      compare(EMPTY, received(listener, null));

      listener.doNotify(true);
      listener.doWait(false);

      compare(sent, received(listener, null));
   }

   public void testAsynchDeliveryTwice()
      throws Exception
   {
      AsynchNotificationBroadcasterSupport broadcaster = new AsynchNotificationBroadcasterSupport();

      Listener listener1 = new Listener();
      broadcaster.addNotificationListener(listener1, null, null);
      Listener listener2 = new Listener();
      broadcaster.addNotificationListener(listener2, null, null);

      clear();
      createNotification(broadcaster);

      compare(EMPTY, received(listener1, null));
      compare(EMPTY, received(listener2, null));

      listener1.doNotify(true);
      listener1.doWait(false);

      compare(sent, received(listener1, null));
      compare(EMPTY, received(listener2, null));

      listener2.doNotify(true);
      listener2.doWait(false);

      compare(sent, received(listener2, null));
   }

   // Support -------------------------------------------------------------------

   private void createNotification(AsynchNotificationBroadcasterSupport broadcaster)
   {
      createNotification(broadcaster, DEFAULT_TYPE);
   }

   private void createNotification(AsynchNotificationBroadcasterSupport broadcaster, String type)
   {
      synchronized(this)
      {
         sequence++;
      }
      Notification notification = new Notification(type, broadcaster, sequence);
      sent.add(notification);
      broadcaster.sendNotification(notification);
   }

   private void clear()
   {
      sent.clear();
   }

   private ArrayList apply(ArrayList sent, NotificationFilterSupport filter)
   {
      ArrayList result = new ArrayList();
      for (Iterator iterator = sent.iterator(); iterator.hasNext();)
      {
         Notification notification = (Notification) iterator.next();
         if (filter.isNotificationEnabled(notification))
            result.add(notification);
      }
      return result;
   }

   private ArrayList received(Listener listener, Object object)
   {
      ArrayList result = (ArrayList) listener.notifications.get(object);
      if (result == null)
         result = EMPTY;
      return result;
   }

   private void compare(ArrayList passedSent, ArrayList passedReceived)
      throws Exception
   {
      ArrayList sent = new ArrayList(passedSent);
      ArrayList received = new ArrayList(passedReceived);

      for (Iterator iterator = sent.iterator(); iterator.hasNext();)
      {
          Notification notification = (Notification) iterator.next();
          boolean found = received.remove(notification);
          assertTrue("Expected notification " + notification, found);
      }

      assertTrue("Didn't expect notification(s) " + received, received.isEmpty());
   }
}
TOP

Related Classes of test.implementation.notification.AsynchNotificationBroadcasterSupportTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.