Search in sources :

Example 26 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class TestReferences method testListReferences.

@Test
public void testListReferences() throws Exception {
    BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
    assertNotNull(blueprintContainer);
    BindingListener listener = (BindingListener) blueprintContainer.getComponentInstance("listBindingListener");
    assertNull(listener.getA());
    assertNull(listener.getReference());
    List<?> refs = (List<?>) blueprintContainer.getComponentInstance("ref-list");
    assertNotNull(refs);
    assertTrue(refs.isEmpty());
    InterfaceA testService = new InterfaceA() {

        public String hello(String msg) {
            return "Hello " + msg + "!";
        }
    };
    bundleContext.registerService(InterfaceA.class.getName(), testService, null);
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals(1, refs.size());
    InterfaceA a = (InterfaceA) refs.get(0);
    assertNotNull(a);
    assertEquals("Hello world!", a.hello("world"));
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) InterfaceA(org.apache.aries.blueprint.sample.InterfaceA) List(java.util.List) BindingListener(org.apache.aries.blueprint.sample.BindingListener) DestroyTest(org.apache.aries.blueprint.sample.DestroyTest) Test(org.junit.Test)

Example 27 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class TestReferences method testUnaryReference.

@SuppressWarnings("rawtypes")
@Test
public void testUnaryReference() throws Exception {
    BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
    assertNotNull(blueprintContainer);
    BindingListener listener = (BindingListener) blueprintContainer.getComponentInstance("bindingListener");
    assertNull(listener.getA());
    assertNull(listener.getReference());
    InterfaceA a = (InterfaceA) blueprintContainer.getComponentInstance("ref2");
    try {
        a.hello("world");
        fail("A ServiceUnavailableException should have been thrown");
    } catch (ServiceUnavailableException e) {
    // Ignore, expected
    }
    ServiceRegistration reg1 = bundleContext.registerService(InterfaceA.class.getName(), new InterfaceA() {

        public String hello(String msg) {
            return "Hello " + msg + "!";
        }
    }, null);
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals("Hello world!", a.hello("world"));
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, Integer.valueOf(1));
    ServiceRegistration reg2 = bundleContext.registerService(InterfaceA.class.getName(), new InterfaceA() {

        public String hello(String msg) {
            return "Good morning " + msg + "!";
        }
    }, props);
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals("Hello world!", a.hello("world"));
    reg1.unregister();
    waitForAsynchronousHandling();
    assertNotNull(listener.getA());
    assertNotNull(listener.getReference());
    assertEquals("Good morning world!", a.hello("world"));
    reg2.unregister();
    waitForAsynchronousHandling();
    assertNull(listener.getA());
    assertNull(listener.getReference());
    try {
        a.hello("world");
        fail("A ServiceUnavailableException should have been thrown");
    } catch (ServiceUnavailableException e) {
    // Ignore, expected
    }
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) InterfaceA(org.apache.aries.blueprint.sample.InterfaceA) Hashtable(java.util.Hashtable) ServiceUnavailableException(org.osgi.service.blueprint.container.ServiceUnavailableException) BindingListener(org.apache.aries.blueprint.sample.BindingListener) ServiceRegistration(org.osgi.framework.ServiceRegistration) DestroyTest(org.apache.aries.blueprint.sample.DestroyTest) Test(org.junit.Test)

Example 28 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class SpringExtenderTest method testSpringBundle.

@Test
public void testSpringBundle() throws Exception {
    try {
        context().getService(BeanCItf.class, 1);
        fail("The service should not be registered");
    } catch (RuntimeException e) {
    // Expected
    }
    Bundle bundles = context().getBundleByName("org.apache.aries.blueprint.testbundlee");
    assertNotNull(bundles);
    bundles.start();
    BlueprintContainer container = startBundleBlueprint("org.apache.aries.blueprint.testbundlee");
    assertNotNull(container);
    BeanCItf beanC1 = context().getService(BeanCItf.class, "(name=BeanC-1)");
    assertEquals(1, beanC1.getInitialized());
    BeanCItf beanC2 = context().getService(BeanCItf.class, "(name=BeanC-2)");
    assertEquals(1, beanC2.getInitialized());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Bundle(org.osgi.framework.Bundle) Helper.mvnBundle(org.apache.aries.blueprint.itests.Helper.mvnBundle) BeanCItf(org.apache.aries.blueprint.testbundlee.BeanCItf) Test(org.junit.Test)

Example 29 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class AbstractBlueprintIntegrationTest method startBundleBlueprint.

protected BlueprintContainer startBundleBlueprint(String symbolicName) throws BundleException {
    Bundle b = context().getBundleByName(symbolicName);
    assertNotNull("Bundle " + symbolicName + " not found", b);
    b.start();
    BlueprintContainer beanContainer = Helper.getBlueprintContainerForBundle(context(), symbolicName);
    assertNotNull(beanContainer);
    return beanContainer;
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Helper.mvnBundle(org.apache.aries.blueprint.itests.Helper.mvnBundle) Bundle(org.osgi.framework.Bundle)

Aggregations

BlueprintContainer (org.osgi.service.blueprint.container.BlueprintContainer)29 Test (org.junit.Test)14 Bundle (org.osgi.framework.Bundle)11 ServiceReference (org.osgi.framework.ServiceReference)6 Helper.mvnBundle (org.apache.aries.blueprint.itests.Helper.mvnBundle)5 List (java.util.List)4 BundleContext (org.osgi.framework.BundleContext)4 DestroyTest (org.apache.aries.blueprint.sample.DestroyTest)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 URL (java.net.URL)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2 BindingListener (org.apache.aries.blueprint.sample.BindingListener)2 Foo (org.apache.aries.blueprint.sample.Foo)2 InterfaceA (org.apache.aries.blueprint.sample.InterfaceA)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)2