Search in sources :

Example 1 with DefaultRunnable

use of org.apache.aries.blueprint.sample.DefaultRunnable in project aries by apache.

the class TestReferences method testDefaultReference.

@SuppressWarnings("rawtypes")
@Test
public void testDefaultReference() throws Exception {
    BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
    assertNotNull(blueprintContainer);
    Runnable refRunnable = (Runnable) blueprintContainer.getComponentInstance("refWithDefault");
    DefaultRunnable defaultRunnable = (DefaultRunnable) blueprintContainer.getComponentInstance("defaultRunnable");
    refRunnable.run();
    waitForAsynchronousHandling();
    Thread.sleep(2000);
    assertEquals("The default runnable was not called", 1, defaultRunnable.getCount());
    final AtomicBoolean called = new AtomicBoolean(false);
    Runnable mockService = new Runnable() {

        public void run() {
            called.set(true);
        }
    };
    ServiceRegistration reg = bundleContext.registerService(Runnable.class.getName(), mockService, null);
    waitForAsynchronousHandling();
    Thread.sleep(2000);
    refRunnable.run();
    assertEquals("The default runnable was called when a service was bound", 1, defaultRunnable.getCount());
    Assert.assertTrue("Service should have been called", called.get());
    reg.unregister();
    waitForAsynchronousHandling();
    Thread.sleep(2000);
    refRunnable.run();
    assertEquals("The default runnable was not called", 2, defaultRunnable.getCount());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultRunnable(org.apache.aries.blueprint.sample.DefaultRunnable) DefaultRunnable(org.apache.aries.blueprint.sample.DefaultRunnable) ServiceRegistration(org.osgi.framework.ServiceRegistration) DestroyTest(org.apache.aries.blueprint.sample.DestroyTest) Test(org.junit.Test)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DefaultRunnable (org.apache.aries.blueprint.sample.DefaultRunnable)1 DestroyTest (org.apache.aries.blueprint.sample.DestroyTest)1 Test (org.junit.Test)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 BlueprintContainer (org.osgi.service.blueprint.container.BlueprintContainer)1