[jboss-cvs] JBossAS SVN: r94352 - in projects/jboss-osgi/projects/runtime/microcontainer/trunk/src: test/java/org/jboss/test/osgi and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Oct 5 06:57:04 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-10-05 06:57:04 -0400 (Mon, 05 Oct 2009)
New Revision: 94352

Modified:
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestCase.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/bundle/test/BundleContextUnitTestCase.java
Log:
[JBOSGI-159] Fire events asynchronously

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java	2009-10-05 10:49:22 UTC (rev 94351)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/framework/plugins/internal/FrameworkEventsPluginImpl.java	2009-10-05 10:57:04 UTC (rev 94352)
@@ -25,11 +25,14 @@
 
 import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.framework.bundle.AbstractBundleState;
@@ -46,6 +49,7 @@
 import org.osgi.framework.FrameworkListener;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.SynchronousBundleListener;
 
 /**
@@ -59,6 +63,9 @@
    // Provide logging
    final Logger log = Logger.getLogger(FrameworkEventsPluginImpl.class);
 
+   /** The executor service */
+   private ExecutorService executorService;
+
    /** The bundle listeners */
    private final Map<Bundle, List<BundleListener>> bundleListeners = new ConcurrentHashMap<Bundle, List<BundleListener>>();
 
@@ -71,15 +78,17 @@
    public FrameworkEventsPluginImpl(OSGiBundleManager bundleManager)
    {
       super(bundleManager);
+
+      this.executorService = Executors.newCachedThreadPool();
    }
 
    public void addBundleListener(Bundle bundle, BundleListener listener)
    {
       if (listener == null)
          throw new IllegalArgumentException("Null listener");
-      
+
       bundle = assertBundle(bundle);
-      
+
       synchronized (bundleListeners)
       {
          List<BundleListener> listeners = bundleListeners.get(bundle);
@@ -97,7 +106,7 @@
    {
       if (listener == null)
          throw new IllegalArgumentException("Null listener");
-      
+
       bundle = assertBundle(bundle);
 
       synchronized (bundleListeners)
@@ -126,7 +135,7 @@
    {
       if (listener == null)
          throw new IllegalArgumentException("Null listener");
-      
+
       bundle = assertBundle(bundle);
 
       synchronized (frameworkListeners)
@@ -146,7 +155,7 @@
    {
       if (listener == null)
          throw new IllegalArgumentException("Null listener");
-      
+
       bundle = assertBundle(bundle);
 
       synchronized (frameworkListeners)
@@ -175,7 +184,7 @@
    {
       if (listener == null)
          throw new IllegalArgumentException("Null listener");
-      
+
       bundle = assertBundle(bundle);
 
       synchronized (serviceListeners)
@@ -186,7 +195,7 @@
             listeners = new CopyOnWriteArrayList<ServiceListenerRegistration>();
             serviceListeners.put(bundle, listeners);
          }
-         
+
          ServiceListenerRegistration registration = new ServiceListenerRegistration(listener, filter);
          if (listeners.contains(registration) == false)
             listeners.add(registration);
@@ -197,9 +206,9 @@
    {
       if (listener == null)
          throw new IllegalArgumentException("Null listener");
-      
+
       bundle = assertBundle(bundle);
-      
+
       synchronized (serviceListeners)
       {
          List<ServiceListenerRegistration> listeners = serviceListeners.get(bundle);
@@ -222,29 +231,41 @@
       }
    }
 
-   public void fireBundleEvent(Bundle bundle, int type)
+   public void fireBundleEvent(final Bundle bundle, final int type)
    {
+      // Get a snapshot of the current listeners
+      final List<BundleListener> listeners = new ArrayList<BundleListener>();
       synchronized (bundleListeners)
       {
-         // Expose the wrapper not the state itself
-         bundle = assertBundle(bundle);
-         BundleEvent event = new BundleEvent(type, bundle);
-         String typeName = ConstantsHelper.bundleEvent(event.getType());
+         for (Entry<Bundle, List<BundleListener>> entry : bundleListeners.entrySet())
+         {
+            for (BundleListener listener : entry.getValue())
+            {
+               listeners.add(listener);
+            }
+         }
+      }
 
-         log.info("Bundle " + typeName + ": " + bundle);
-         
-         // Nobody is interested
-         if (bundleListeners.isEmpty())
-            return;
+      // Expose the bundl wrapper not the state itself
+      final BundleEvent event = new OSGiBundleEvent(type, assertBundle(bundle));
+      final String typeName = ConstantsHelper.bundleEvent(event.getType());
 
-         // Are we active?
-         if (getBundleManager().isActive() == false)
-            return;
+      log.info("Bundle " + typeName + ": " + bundle);
 
-         // Synchronous listeners first
-         for (Entry<Bundle, List<BundleListener>> entry : bundleListeners.entrySet())
+      // Nobody is interested
+      if (listeners.isEmpty())
+         return;
+
+      // Are we active?
+      if (getBundleManager().isActive() == false)
+         return;
+
+      Runnable runnable = new Runnable()
+      {
+         public void run()
          {
-            for (BundleListener listener : entry.getValue())
+            // Synchronous listeners first
+            for (BundleListener listener : listeners)
             {
                try
                {
@@ -256,14 +277,11 @@
                   log.warn("Error while firing " + typeName + " for bundle " + bundle, t);
                }
             }
-         }
 
-         // Normal listeners after, if required
-         if (type != BundleEvent.STARTING && type != BundleEvent.STOPPING && type != BundleEvent.LAZY_ACTIVATION)
-         {
-            for (Entry<Bundle, List<BundleListener>> entry : bundleListeners.entrySet())
+            // Normal listeners after, if required
+            if (type != BundleEvent.STARTING && type != BundleEvent.STOPPING && type != BundleEvent.LAZY_ACTIVATION)
             {
-               for (BundleListener listener : entry.getValue())
+               for (BundleListener listener : listeners)
                {
                   try
                   {
@@ -277,32 +295,55 @@
                }
             }
          }
-      }
+      };
+
+      // Fire the event in a runnable
+      executorService.execute(runnable);
    }
 
-   public void fireFrameworkEvent(Bundle bundle, int type, Throwable throwable)
+   public void fireFrameworkEvent(final Bundle bundle, final int type, final Throwable throwable)
    {
+      // Get a snapshot of the current listeners
+      final ArrayList<FrameworkListener> listeners = new ArrayList<FrameworkListener>();
       synchronized (frameworkListeners)
       {
-         // Expose the wrapper not the state itself
-         bundle = assertBundle(bundle);
-         FrameworkEvent event = new FrameworkEvent(type, bundle, throwable);
-         String typeName = ConstantsHelper.frameworkEvent(event.getType());
+         for (Entry<Bundle, List<FrameworkListener>> entry : frameworkListeners.entrySet())
+         {
+            for (FrameworkListener listener : entry.getValue())
+            {
+               listeners.add(listener);
+            }
+         }
+      }
 
-         log.info("Framwork " + typeName);
-         
-         // Nobody is interested
-         if (frameworkListeners.isEmpty())
-            return;
+      // Nobody is interested
+      if (listeners.isEmpty())
+         return;
 
-         // Are we active?
-         if (getBundleManager().isActive() == false)
-            return;
+      // Are we active?
+      if (getBundleManager().isActive() == false)
+         return;
 
-         // Call the listeners
-         for (Entry<Bundle, List<FrameworkListener>> entry : frameworkListeners.entrySet())
+      Runnable runnable = new Runnable()
+      {
+         public void run()
          {
-            for (FrameworkListener listener : entry.getValue())
+            // Expose the wrapper not the state itself
+            FrameworkEvent event = new OSGiFrameworkEvent(type, assertBundle(bundle), throwable);
+            String typeName = ConstantsHelper.frameworkEvent(event.getType());
+
+            log.info("Framwork " + typeName);
+
+            // Nobody is interested
+            if (frameworkListeners.isEmpty())
+               return;
+
+            // Are we active?
+            if (getBundleManager().isActive() == false)
+               return;
+
+            // Call the listeners
+            for (FrameworkListener listener : listeners)
             {
                try
                {
@@ -314,32 +355,47 @@
                }
             }
          }
-      }
+      };
+
+      // Fire the event in a runnable
+      executorService.execute(runnable);
    }
 
-   public void fireServiceEvent(Bundle bundle, int type, OSGiServiceState service)
+   public void fireServiceEvent(Bundle bundle, int type, final OSGiServiceState service)
    {
+      // Get a snapshot of the current listeners
+      final ArrayList<ServiceListenerRegistration> listeners = new ArrayList<ServiceListenerRegistration>();
       synchronized (serviceListeners)
       {
-         // Expose the wrapper not the state itself
-         bundle = assertBundle(bundle);
-         ServiceEvent event = new ServiceEvent(type, service.getReferenceInternal());
-         String typeName = ConstantsHelper.serviceEvent(event.getType());
+         for (Entry<Bundle, List<ServiceListenerRegistration>> entry : serviceListeners.entrySet())
+         {
+            for (ServiceListenerRegistration listener : entry.getValue())
+            {
+               listeners.add(listener);
+            }
+         }
+      }
 
-         log.info("Service " + typeName + ": " + service);
-         
-         // Nobody is interested
-         if (serviceListeners.isEmpty())
-            return;
+      // Expose the wrapper not the state itself
+      final ServiceEvent event = new OSGiServiceEvent(type, service.getReferenceInternal());
+      final String typeName = ConstantsHelper.serviceEvent(event.getType());
 
-         // Are we active?
-         if (getBundleManager().isActive() == false)
-            return;
+      log.info("Service " + typeName + ": " + service);
 
-         // Call the listeners
-         for (Entry<Bundle, List<ServiceListenerRegistration>> entry : serviceListeners.entrySet())
+      // Nobody is interested
+      if (listeners.isEmpty())
+         return;
+
+      // Are we active?
+      if (getBundleManager().isActive() == false)
+         return;
+
+      Runnable runnable = new Runnable()
+      {
+         public void run()
          {
-            for (ServiceListenerRegistration registration : entry.getValue())
+            // Call the listeners
+            for (ServiceListenerRegistration registration : listeners)
             {
                try
                {
@@ -356,7 +412,10 @@
                }
             }
          }
-      }
+      };
+
+      // Fire the event in a runnable
+      executorService.execute(runnable);
    }
 
    private Bundle assertBundle(Bundle bundle)
@@ -374,7 +433,7 @@
    /**
     * Filter and AccessControl for service events
     */
-   private static class ServiceListenerRegistration
+   static class ServiceListenerRegistration
    {
       // Any filter
       Filter filter;
@@ -393,13 +452,13 @@
       {
          if (listener == null)
             throw new IllegalArgumentException("Null listener");
-         
+
          if (filter == null)
             filter = NoFilter.INSTANCE;
 
          this.listener = listener;
          this.filter = filter;
-         
+
          if (System.getSecurityManager() != null)
             accessControlContext = AccessController.getContext();
       }
@@ -409,15 +468,63 @@
       {
          return listener.hashCode();
       }
-      
+
       @Override
       public boolean equals(Object obj)
       {
          if (obj instanceof ServiceListenerRegistration == false)
             return false;
-         
+
          ServiceListenerRegistration other = (ServiceListenerRegistration)obj;
          return other.listener.equals(listener) && other.filter.equals(filter);
       }
    }
+
+   static class OSGiFrameworkEvent extends FrameworkEvent
+   {
+      private static final long serialVersionUID = 6505331543651318189L;
+
+      public OSGiFrameworkEvent(int type, Bundle bundle, Throwable throwable)
+      {
+         super(type, bundle, throwable);
+      }
+
+      @Override
+      public String toString()
+      {
+         return "FrameworkEvent[type=" + ConstantsHelper.frameworkEvent(getType()) + ",source=" + getSource() + "]";
+      }
+   }
+
+   static class OSGiBundleEvent extends BundleEvent
+   {
+      private static final long serialVersionUID = -2705304702665185935L;
+
+      public OSGiBundleEvent(int type, Bundle bundle)
+      {
+         super(type, bundle);
+      }
+
+      @Override
+      public String toString()
+      {
+         return "BundleEvent[type=" + ConstantsHelper.bundleEvent(getType()) + ",source=" + getSource() + "]";
+      }
+   }
+
+   static class OSGiServiceEvent extends ServiceEvent
+   {
+      private static final long serialVersionUID = 62018288275708239L;
+
+      public OSGiServiceEvent(int type, ServiceReference reference)
+      {
+         super(type, reference);
+      }
+
+      @Override
+      public String toString()
+      {
+         return "ServiceEvent[type=" + ConstantsHelper.serviceEvent(getType()) + ",source=" + getSource() + "]";
+      }
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestCase.java	2009-10-05 10:49:22 UTC (rev 94351)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestCase.java	2009-10-05 10:57:04 UTC (rev 94352)
@@ -35,6 +35,7 @@
 import org.jboss.deployers.structure.spi.DeploymentUnit;
 import org.jboss.deployers.structure.spi.main.MainDeployerStructure;
 import org.jboss.osgi.framework.bundle.OSGiBundleManager;
+import org.jboss.osgi.spi.util.ConstantsHelper;
 import org.jboss.test.AbstractTestDelegate;
 import org.jboss.test.kernel.junit.MicrocontainerTest;
 import org.jboss.virtual.AssembledDirectory;
@@ -62,7 +63,7 @@
    private List<FrameworkEvent> frameworkEvents = new CopyOnWriteArrayList<FrameworkEvent>();
    private List<BundleEvent> bundleEvents = new CopyOnWriteArrayList<BundleEvent>();
    private List<ServiceEvent> serviceEvents = new CopyOnWriteArrayList<ServiceEvent>();
-   
+
    /**
     * Create a new OSGiTestCase.
     * 
@@ -92,7 +93,7 @@
     */
    protected OSGiTestDelegate getDelegate()
    {
-      return (OSGiTestDelegate) super.getDelegate();
+      return (OSGiTestDelegate)super.getDelegate();
    }
 
    /**
@@ -134,7 +135,7 @@
    {
       expected.add(getSystemBundle());
    }
-   
+
    /**
     * Create a bundle 
     * 
@@ -170,12 +171,12 @@
    {
       getDelegate().uninstall(bundle);
    }
-   
+
    protected DeploymentUnit getDeploymentUnit(Bundle bundle)
    {
       return getDelegate().getDeploymentUnit(bundle);
    }
-   
+
    /**
     * Get MainDeployerStructure from Delegate
     * 
@@ -198,7 +199,7 @@
 
    protected AssembledDirectory createAssembledDirectory(String name) throws Exception
    {
-      return getDelegate().createAssembledDirectory(name, "");     
+      return getDelegate().createAssembledDirectory(name, "");
    }
 
    protected AssembledDirectory createAssembledDirectory(String name, String rootName) throws Exception
@@ -215,17 +216,17 @@
    {
       getDelegate().addPath(dir, path, name);
    }
-   
+
    protected void assertClassEquality(Class<?> expected, Class<?> actual)
    {
-      assertTrue("Should be the same " + ClassLoaderUtils.classToString(expected) +" and " + ClassLoaderUtils.classToString(actual), expected == actual);
+      assertTrue("Should be the same " + ClassLoaderUtils.classToString(expected) + " and " + ClassLoaderUtils.classToString(actual), expected == actual);
    }
-   
+
    protected void assertNoClassEquality(Class<?> expected, Class<?> actual)
    {
-      assertTrue("Should NOT be the same " + ClassLoaderUtils.classToString(expected) +" and " + ClassLoaderUtils.classToString(actual), expected != actual);
+      assertTrue("Should NOT be the same " + ClassLoaderUtils.classToString(expected) + " and " + ClassLoaderUtils.classToString(actual), expected != actual);
    }
-   
+
    protected void assertClassLoader(Class<?> clazz, Bundle expected)
    {
       if (expected == null)
@@ -235,22 +236,22 @@
       boolean result = bundleClassLoader.equals(cl);
       assertTrue(ClassLoaderUtils.classToString(clazz) + " should have expected classloader=" + expected, result);
    }
-   
+
    protected ClassLoader getBundleClassLoader(Bundle expected)
    {
       return getDeploymentUnit(expected).getClassLoader();
    }
-   
+
    protected Class<?> assertLoadClass(Bundle start, Class<?> reference) throws Exception
    {
       return assertLoadClass(start, reference, start);
    }
-   
+
    protected Class<?> assertLoadClass(Bundle start, Class<?> reference, Bundle expected) throws Exception
    {
       return assertLoadClass(start, reference, expected, false);
    }
-   
+
    protected Class<?> assertLoadClass(Bundle start, Class<?> reference, Bundle expected, boolean isReference) throws Exception
    {
       Class<?> result = assertLoadClass(reference.getName(), start, expected);
@@ -260,7 +261,7 @@
          assertNoClassEquality(reference, result);
       return result;
    }
-   
+
    protected Class<?> assertLoadClass(String name, Bundle bundle, Bundle expected)
    {
       Class<?> result = null;
@@ -276,12 +277,12 @@
       assertClassLoader(result, expected);
       return result;
    }
-   
+
    protected void assertLoadClassFail(Bundle start, Class<?> reference)
    {
       assertLoadClassFail(start, reference.getName());
    }
-      
+
    protected void assertLoadClassFail(Bundle start, String name)
    {
       try
@@ -294,12 +295,12 @@
          checkThrowable(ClassNotFoundException.class, expected);
       }
    }
-   
+
    protected URL getBundleResource(Bundle bundle, String path)
    {
       return getDelegate().getBundleResource(bundle, path);
    }
-   
+
    protected Enumeration<URL> getBundleResources(Bundle bundle, String path) throws Exception
    {
       return getDelegate().getBundleResources(bundle, path);
@@ -319,7 +320,7 @@
          getLog().debug(bundleContext + " got nothing for clazz=" + clazz + " filter=" + filter);
       assertNull("Expected no references for clazz=" + clazz + " filter=" + filter, actual);
    }
-   
+
    protected void assertAllReferences(BundleContext bundleContext, String clazz, ServiceReference... expected) throws Exception
    {
       assertAllReferences(bundleContext, clazz, null, expected);
@@ -349,7 +350,7 @@
          getLog().debug(bundleContext + " got nothing for clazz=" + clazz + " filter=" + filter);
       assertNull("Expected no references for clazz=" + clazz + " filter=" + filter, actual);
    }
-   
+
    protected void assertReferences(BundleContext bundleContext, String clazz, ServiceReference... expected) throws Exception
    {
       assertReferences(bundleContext, clazz, null, expected);
@@ -390,23 +391,23 @@
       Set<Bundle> expected = new HashSet<Bundle>();
       for (Bundle bundle : bundles)
          expected.add(bundle);
-      
+
       Set<Bundle> actual = new HashSet<Bundle>();
       Bundle[] users = reference.getUsingBundles();
       if (users != null)
-      for (Bundle bundle : users)
-         actual.add(bundle);
-      
+         for (Bundle bundle : users)
+            actual.add(bundle);
+
       getLog().debug(reference + " users=" + actual);
-      
+
       assertEquals(expected, actual);
    }
-   
+
    protected void assertObjectClass(String expected, ServiceReference reference)
    {
       assertObjectClass(new String[] { expected }, reference);
    }
-   
+
    protected void assertObjectClass(String[] expected, ServiceReference reference)
    {
       Object actual = reference.getProperty(Constants.OBJECTCLASS);
@@ -414,38 +415,44 @@
          fail("no object class???");
       if (actual instanceof String[] == false)
          fail(actual + " is not a string array??? " + actual.getClass().getName());
-      assertEquals(expected, (String[]) actual);
+      assertEquals(expected, (String[])actual);
    }
 
    public void frameworkEvent(FrameworkEvent event)
    {
-      getLog().debug("FrameworkEvent type=" + event.getType() + " for " + event);
-      frameworkEvents.add(event);
+      synchronized (frameworkEvents)
+      {
+         getLog().debug("FrameworkEvent type=" + ConstantsHelper.frameworkEvent(event.getType()) + " for " + event);
+         frameworkEvents.add(event);
+         frameworkEvents.notifyAll();
+      }
    }
-   
+
    protected void assertNoFrameworkEvent() throws Exception
    {
+      waitForEvents(frameworkEvents);
       getLog().debug("frameworkEvents=" + frameworkEvents);
       assertEquals(0, frameworkEvents.size());
    }
-   
+
    protected void assertFrameworkEvent(int type) throws Exception
    {
       assertFrameworkEvent(type, getSystemBundle(), null);
    }
-   
+
    protected void assertFrameworkEvent(int type, Class<? extends Throwable> expectedThrowable) throws Exception
    {
       assertFrameworkEvent(type, getSystemBundle(), expectedThrowable);
    }
-   
+
    protected void assertFrameworkEvent(int type, Bundle bundle, Class<? extends Throwable> expectedThrowable) throws Exception
    {
+      waitForEvents(frameworkEvents);
       getLog().debug("frameworkEvents=" + frameworkEvents);
       int size = frameworkEvents.size();
       assertTrue("" + size, size > 0);
       FrameworkEvent event = frameworkEvents.remove(0);
-      assertEquals(type, event.getType());
+      assertEquals(ConstantsHelper.frameworkEvent(type), ConstantsHelper.frameworkEvent(event.getType()));
       Throwable t = event.getThrowable();
       if (expectedThrowable == null)
       {
@@ -466,47 +473,75 @@
 
    public void bundleChanged(BundleEvent event)
    {
-      getLog().debug("BundleChanged type=" + event.getType() + " for " + event);
-      bundleEvents.add(event);
+      synchronized (bundleEvents)
+      {
+         getLog().debug("BundleChanged type=" + ConstantsHelper.bundleEvent(event.getType()) + " for " + event);
+         bundleEvents.add(event);
+         bundleEvents.notifyAll();
+      }
    }
-   
+
    protected void assertNoBundleEvent() throws Exception
    {
+      waitForEvents(bundleEvents);
       getLog().debug("bundleEvents=" + bundleEvents);
       assertEquals(0, bundleEvents.size());
    }
-   
+
    protected void assertBundleEvent(int type, Bundle bundle) throws Exception
    {
+      waitForEvents(bundleEvents);
       getLog().debug("bundleEvents=" + bundleEvents);
       int size = bundleEvents.size();
       assertTrue("" + size, size > 0);
       BundleEvent event = bundleEvents.remove(0);
-      assertEquals(type, event.getType());
+      assertEquals(ConstantsHelper.bundleEvent(type), ConstantsHelper.bundleEvent(event.getType()));
       assertEquals(bundle, event.getSource());
       assertEquals(bundle, event.getBundle());
    }
 
    public void serviceChanged(ServiceEvent event)
    {
-      getLog().debug("ServiceChanged type=" + event.getType() + " for " + event);
-      serviceEvents.add(event);
+      synchronized (serviceEvents)
+      {
+         getLog().debug("ServiceChanged type=" + ConstantsHelper.serviceEvent(event.getType()) + " for " + event);
+         serviceEvents.add(event);
+         serviceEvents.notifyAll();
+      }
    }
-   
+
    protected void assertNoServiceEvent() throws Exception
    {
+      waitForEvents(serviceEvents);
       getLog().debug("serviceEvents=" + serviceEvents);
       assertEquals(0, serviceEvents.size());
    }
-   
+
    protected void assertServiceEvent(int type, ServiceReference reference) throws Exception
    {
+      waitForEvents(serviceEvents);
       getLog().debug("serviceEvents=" + serviceEvents);
       int size = serviceEvents.size();
       assertTrue("" + size, size > 0);
       ServiceEvent event = serviceEvents.remove(0);
-      assertEquals(type, event.getType());
+      assertEquals(ConstantsHelper.serviceEvent(type), ConstantsHelper.serviceEvent(event.getType()));
       assertEquals(reference, event.getSource());
       assertEquals(reference, event.getServiceReference());
    }
+
+   @SuppressWarnings("unchecked")
+   private void waitForEvents(List events) throws InterruptedException
+   {
+      int timeout = 50;
+      while (events.size() == 0 && 0 < timeout)
+      {
+         synchronized (events)
+         {
+            events.wait(100);
+            if (events.size() > 0)
+               break;
+         }
+         timeout--;
+      }
+   }
 }

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/bundle/test/BundleContextUnitTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/bundle/test/BundleContextUnitTestCase.java	2009-10-05 10:49:22 UTC (rev 94351)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/bundle/test/BundleContextUnitTestCase.java	2009-10-05 10:57:04 UTC (rev 94352)
@@ -43,6 +43,7 @@
  *
  * TODO test security
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author Thomas.Diesler at jboss.com
  * @version $Revision: 1.1 $
  */
 public class BundleContextUnitTestCase extends OSGiTestCase
@@ -424,7 +425,9 @@
          assertBundleEvent(BundleEvent.STOPPED, bundle);
       }
       else
+      {
          assertNoBundleEvent();
+      }
       
       bundle.start();
       if (events)
@@ -433,7 +436,9 @@
          assertBundleEvent(BundleEvent.STARTED, bundle);
       }
       else
+      {
          assertNoBundleEvent();
+      }
       
       return bundleContext;
    }




More information about the jboss-cvs-commits mailing list