392396: add test to show bug and fix the bug
diff --git a/extender/src/main/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapter.java b/extender/src/main/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapter.java
index 1246757..45048d9 100644
--- a/extender/src/main/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapter.java
+++ b/extender/src/main/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapter.java
@@ -1,5 +1,5 @@
/******************************************************************************
- * Copyright (c) 2006, 2010 VMware Inc.
+ * Copyright (c) 2006, 2012 VMware Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
@@ -30,14 +30,14 @@
* dynamic collections which can be updated during iteration.
*
* @author Costin Leau
- *
*/
@SuppressWarnings("unchecked")
class ListListenerAdapter implements OsgiBundleApplicationContextListener<OsgiBundleApplicationContextEvent>,
InitializingBean, DisposableBean {
private final ServiceTracker tracker;
- private final Map<Class<? extends OsgiBundleApplicationContextListener>, Class<? extends OsgiBundleApplicationContextEvent>> eventCache =
+ @SuppressWarnings("rawtypes")
+ private final Map<Class<? extends OsgiBundleApplicationContextListener>, Class<? extends OsgiBundleApplicationContextEvent>> eventCache =
new WeakHashMap<Class<? extends OsgiBundleApplicationContextListener>, Class<? extends OsgiBundleApplicationContextEvent>>();
/**
@@ -56,13 +56,14 @@
eventCache.clear();
}
- public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
+ @SuppressWarnings("rawtypes")
+ public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
Object[] listeners = tracker.getServices();
if (listeners != null) {
synchronized (eventCache) {
for (Object listnr : listeners) {
- OsgiBundleApplicationContextListener listener = (OsgiBundleApplicationContextListener) listnr;
+ OsgiBundleApplicationContextListener listener = (OsgiBundleApplicationContextListener) listnr;
Class<? extends OsgiBundleApplicationContextListener> listenerClass = listener.getClass();
Class<? extends OsgiBundleApplicationContextEvent> eventType = eventCache.get(listenerClass);
if (eventType == null) {
@@ -72,7 +73,7 @@
if (evtType == null) {
evtType = OsgiBundleApplicationContextEvent.class;
}
- if (evtType != null && evtType.isAssignableFrom(OsgiBundleApplicationContextEvent.class)) {
+ if (evtType != null && OsgiBundleApplicationContextEvent.class.isAssignableFrom(evtType)) {
eventType = (Class<? extends OsgiBundleApplicationContextEvent>) evtType;
} else {
eventType = OsgiBundleApplicationContextEvent.class;
diff --git a/extender/src/test/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapterTest.java b/extender/src/test/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapterTest.java
index c9735bb..5410ce0 100644
--- a/extender/src/test/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapterTest.java
+++ b/extender/src/test/java/org/eclipse/gemini/blueprint/extender/internal/activator/ListListenerAdapterTest.java
@@ -25,6 +25,7 @@
import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyEvent;
import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEvent;
import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
+import org.eclipse.gemini.blueprint.context.event.OsgiBundleContextClosedEvent;
import org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext;
import org.eclipse.gemini.blueprint.extender.event.BootstrappingDependenciesEvent;
import org.osgi.framework.InvalidSyntaxException;
@@ -38,78 +39,107 @@
public class ListListenerAdapterTest extends TestCase {
+ private final class TestBundleContext extends MockBundleContext {
+
+ private TestBundleContext() {
+ }
+
+ public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
+ return null;
+ }
+
+ public Object getService(ServiceReference reference) {
+ if (reference != null) {
+ Object service = reference.getProperty(SERVICE_PROPERTY);
+ if (service != null) {
+ return service;
+ }
+ }
+
+ return super.getService(reference);
+ }
+
+ @SuppressWarnings("rawtypes")
+ public ServiceRegistration registerService(String[] clazzes, final Object service, Dictionary properties) {
+ MockServiceRegistration reg = new MockServiceRegistration(properties);
+
+ MockServiceReference ref = new MockServiceReference(getBundle(), properties, reg, clazzes) {
+
+ @Override
+ public Object getProperty(String key) {
+ if (SERVICE_PROPERTY.equals(key)) {
+ return service;
+ } else {
+ return super.getProperty(key);
+ }
+ }
+
+ };
+ ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, ref);
+
+ for (Iterator iter = serviceListeners.iterator(); iter.hasNext();) {
+ ServiceListener listener = (ServiceListener) iter.next();
+ listener.serviceChanged(event);
+ }
+
+ return reg;
+ }
+ }
+
private static final String SERVICE_PROPERTY = "service";
-
+
private boolean fired;
- public void testX() throws Exception {
-
- fired = false;
+ private MockBundleContext ctx;
- final OsgiBundleApplicationContextListener<OsgiBundleApplicationContextEvent> listener = new OsgiBundleApplicationContextListener<OsgiBundleApplicationContextEvent>() {
+ private ListListenerAdapter adapter;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ fired = false;
+ ctx = new TestBundleContext();
+ adapter = new ListListenerAdapter(ctx);
+ adapter.afterPropertiesSet();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ adapter.destroy();
+ super.tearDown();
+ }
+
+ public void testNormal() throws Exception {
+
+ OsgiBundleApplicationContextListener<OsgiBundleApplicationContextEvent> listener = new OsgiBundleApplicationContextListener<OsgiBundleApplicationContextEvent>() {
public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
fired = true;
}
};
- final ServiceReference ref = null;
-
- MockBundleContext ctx = new MockBundleContext() {
-
- public ServiceReference getServiceReference(String clazz) {
- return ref;
- }
-
- public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
- return new ServiceReference[] { ref };
- }
-
- public Object getService(ServiceReference reference) {
- if (reference != null) {
- Object service = reference.getProperty(SERVICE_PROPERTY);
- if (service != null) {
- return service;
- }
- }
-
- return (reference == ref ? listener : super.getService(reference));
- }
-
- @SuppressWarnings("rawtypes")
- public ServiceRegistration registerService(String[] clazzes, final Object service, Dictionary properties) {
- MockServiceRegistration reg = new MockServiceRegistration(properties);
-
- MockServiceReference ref = new MockServiceReference(getBundle(), properties, reg, clazzes) {
-
- @Override
- public Object getProperty(String key) {
- if (SERVICE_PROPERTY.equals(key)) {
- return service;
- } else {
- return super.getProperty(key);
- }
- }
-
- };
- ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, ref);
-
- for (Iterator iter = serviceListeners.iterator(); iter.hasNext();) {
- ServiceListener listener = (ServiceListener) iter.next();
- listener.serviceChanged(event);
- }
-
- return reg;
- }
-
- };
-
- ListListenerAdapter adapter = new ListListenerAdapter(ctx);
- adapter.afterPropertiesSet();
ctx.registerService(OsgiBundleApplicationContextListener.class.getName(), listener, null);
+
adapter.onOsgiApplicationEvent(new BootstrappingDependenciesEvent(new OsgiBundleXmlApplicationContext(), new MockBundle(),
new ArrayList<OsgiServiceDependencyEvent>(), null, 0));
Assert.assertTrue(fired);
- adapter.destroy();
+ }
+
+ public void testEventFiltering() throws Exception {
+
+ OsgiBundleApplicationContextListener<OsgiBundleContextClosedEvent> listener = new OsgiBundleApplicationContextListener<OsgiBundleContextClosedEvent>() {
+
+ public void onOsgiApplicationEvent(OsgiBundleContextClosedEvent event) {
+ fired = true;
+ }
+ };
+
+ ctx.registerService(OsgiBundleApplicationContextListener.class.getName(), listener, null);
+
+ adapter.onOsgiApplicationEvent(new BootstrappingDependenciesEvent(new OsgiBundleXmlApplicationContext(), new MockBundle(),
+ new ArrayList<OsgiServiceDependencyEvent>(), null, 0));
+
+ adapter.onOsgiApplicationEvent(new OsgiBundleContextClosedEvent(new OsgiBundleXmlApplicationContext(), new MockBundle()));
+ Assert.assertTrue(fired);
}
}