diff options
author | gjoranv <gv@oath.com> | 2018-09-29 00:25:25 +0200 |
---|---|---|
committer | gjoranv <gv@oath.com> | 2019-01-21 15:09:23 +0100 |
commit | 7f754708f8441ff0095a841356756351c6b0e0ac (patch) | |
tree | ddb36631a6e77b24d74f6a523f8550a563a492f6 /jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application | |
parent | 6b57575ed493e1017eec32caff3d260c00fda8b4 (diff) |
Retrieve the ServiceReference via the service's own bundle.
- Going via the framework/bundleContext does not work, because
it's class is loaded by the AppClassLoader, which will then be
used to lookup the service class. Since this is a unit test,
the class will be found on the class path but, since it's a
different class than the service class in the bundle, a class
cast check will fail, and the returned service ref will be null.
- Note that this worked on Felix <6.0 because an exception was
thrown (because the framework bundle does not export our service
class), which led to the class cast check being skipped
altogether.
See felix/framework:ExtensionManager.getClassByDelegation
(commit 3c147cb58a5)
Diffstat (limited to 'jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application')
-rw-r--r-- | jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleActivatorIntegrationTest.java | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleActivatorIntegrationTest.java b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleActivatorIntegrationTest.java index 03e56d7c26d..9450d2f26c1 100644 --- a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleActivatorIntegrationTest.java +++ b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/application/BundleActivatorIntegrationTest.java @@ -7,6 +7,7 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -25,8 +26,12 @@ public class BundleActivatorIntegrationTest { Class<?> serviceClass = bundle.loadClass("com.yahoo.jdisc.bundle.my_act.MyService"); assertNotNull(serviceClass); BundleContext ctx = osgi.bundleContext(); - ServiceReference<?> serviceRef = ctx.getServiceReference(serviceClass.getName()); + + ServiceReference<?>[] serviceRefs = bundle.getRegisteredServices(); + assertEquals(1, serviceRefs.length); + ServiceReference<?> serviceRef = serviceRefs[0]; assertNotNull(serviceRef); + Object service = ctx.getService(serviceRef); assertNotNull(service); assertTrue(serviceClass.isInstance(service)); @@ -37,11 +42,16 @@ public class BundleActivatorIntegrationTest { public void requireThatApplicationBundleActivatorHasAccessToCurrentContainer() throws Exception { TestDriver driver = TestDriver.newApplicationBundleInstance("app-g-act.jar", false); OsgiFramework osgi = driver.osgiFramework(); - Class<?> serviceClass = osgi.bundles().get(1).loadClass("com.yahoo.jdisc.bundle.g_act.MyService"); + Bundle bundle = osgi.bundles().get(1); + Class<?> serviceClass = bundle.loadClass("com.yahoo.jdisc.bundle.g_act.MyService"); assertNotNull(serviceClass); BundleContext ctx = osgi.bundleContext(); - ServiceReference<?> serviceRef = ctx.getServiceReference(serviceClass.getName()); + + ServiceReference<?>[] serviceRefs = bundle.getRegisteredServices(); + assertEquals(1, serviceRefs.length); + ServiceReference<?> serviceRef = serviceRefs[0]; assertNotNull(serviceRef); + Object service = ctx.getService(serviceRef); assertNotNull(service); assertTrue(serviceClass.isInstance(service)); |