Search in sources :

Example 1 with Foo

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

the class Helper method testBlueprintContainer.

public static void testBlueprintContainer(RichBundleContext context, Bundle bundle) throws Exception {
    BlueprintContainer blueprintContainer = getBlueprintContainerForBundle(context, SAMPLE_SYM_NAME);
    assertNotNull(blueprintContainer);
    Bar bar = getInstance(blueprintContainer, "bar", Bar.class);
    checkBar(bar);
    Foo foo = getInstance(blueprintContainer, "foo", Foo.class);
    checkFoo(bar, foo);
    Foo fooService = context.getService(Foo.class);
    assertNotNull(fooService);
    checkFoo(bar, fooService);
    // TODO Does not work
    //assertEquals(obj, foo);
    Account account = getInstance(blueprintContainer, "accountOne", Account.class);
    assertEquals(1, account.getAccountNumber());
    Account account2 = getInstance(blueprintContainer, "accountTwo", Account.class);
    assertEquals(2, account2.getAccountNumber());
    Account account3 = getInstance(blueprintContainer, "accountThree", Account.class);
    assertEquals(3, account3.getAccountNumber());
    AccountFactory accountFactory = getInstance(blueprintContainer, "accountFactory", AccountFactory.class);
    assertEquals("account factory", accountFactory.getFactoryName());
    bundle.stop();
    Thread.sleep(1000);
    try {
        blueprintContainer = getBlueprintContainerForBundle(context, SAMPLE_SYM_NAME, 1);
        fail("BlueprintContainer should have been unregistered");
    } catch (Exception e) {
    // Expected, as the module container should have been unregistered
    }
    assertTrue(foo.isInitialized());
    assertTrue(foo.isDestroyed());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Account(org.apache.aries.blueprint.sample.Account) AccountFactory(org.apache.aries.blueprint.sample.AccountFactory) Bar(org.apache.aries.blueprint.sample.Bar) Foo(org.apache.aries.blueprint.sample.Foo) ParseException(java.text.ParseException)

Example 2 with Foo

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

the class TestRegistrationListener method testWithAutoExportEnabled.

@Test
public void testWithAutoExportEnabled() throws Exception {
    BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
    assertNotNull(blueprintContainer);
    Foo foo = context().getService(Foo.class, "(" + BlueprintConstants.COMPONENT_NAME_PROPERTY + "=foo)");
    assertEquals(5, foo.getA());
    FooRegistrationListener listener = (FooRegistrationListener) blueprintContainer.getComponentInstance("fooRegistrationListener");
    // If registration listener works fine, the registration method should
    // have already been called and properties that were passed to this
    // method should have been not null
    Map<?, ?> props = listener.getProperties();
    assertNotNull(props);
    assertTrue(props.containsKey(BlueprintConstants.COMPONENT_NAME_PROPERTY));
    assertEquals("foo", props.get(BlueprintConstants.COMPONENT_NAME_PROPERTY));
    assertTrue(props.containsKey("key"));
    assertEquals("value", props.get("key"));
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Foo(org.apache.aries.blueprint.sample.Foo) FooRegistrationListener(org.apache.aries.blueprint.sample.FooRegistrationListener) Test(org.junit.Test)

Example 3 with Foo

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

the class TestConfigAdmin method testManagedServiceFactory.

@SuppressWarnings("rawtypes")
@Test
public void testManagedServiceFactory() throws Exception {
    Configuration cf = ca.createFactoryConfiguration("blueprint-sample-managed-service-factory", null);
    cf.update(getConfig1());
    startTestBundle();
    // Make sure only one service is registered
    // Ask the service registry, not the container, since the container might have got it wrong :)
    Foo foo = context().getService(Foo.class, "(service.pid=blueprint-sample-managed-service-factory.*)");
    ServiceReference[] refs = context().getAllServiceReferences(Foo.class.getName(), "(service.pid=blueprint-sample-managed-service-factory.*)");
    assertNotNull("No services were registered for the managed service factory", refs);
    assertEquals("Multiple services were registered for the same pid.", 1, refs.length);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Foo(org.apache.aries.blueprint.sample.Foo) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with Foo

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

the class TestConfigAdmin method testStrategyComponent.

@Test
public void testStrategyComponent() throws Exception {
    // foo should receive initial configuration
    ca.getConfiguration("blueprint-sample-managed.component", null).update(getConfig1());
    startTestBundle();
    Foo foo = getComponent("component-managed");
    assertEquals(5, foo.getA());
    assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
    // Foo.update() should have been called but the bean properties should not have been updated
    ca.getConfiguration("blueprint-sample-managed.component", null).update(getConfig2());
    Thread.sleep(100);
    assertEquals(5, foo.getA());
    assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
    assertNotNull(foo.getProps());
    assertEquals("10", foo.getProps().get("a"));
    assertEquals("USD", foo.getProps().get("currency"));
}
Also used : Foo(org.apache.aries.blueprint.sample.Foo) Test(org.junit.Test)

Example 5 with Foo

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

the class TestConfigAdmin method testStrategyNone.

@Test
public void testStrategyNone() throws Exception {
    ca.getConfiguration("blueprint-sample-managed.none", null).update(getConfig1());
    startTestBundle();
    // foo should receive initial configuration
    Foo foo = getComponent("none-managed");
    assertEquals(5, foo.getA());
    assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
    // foo should not reflect changes in config
    ca.getConfiguration("blueprint-sample-managed.none", null).update(getConfig2());
    Thread.sleep(100);
    assertEquals(5, foo.getA());
    assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
}
Also used : Foo(org.apache.aries.blueprint.sample.Foo) Test(org.junit.Test)

Aggregations

Foo (org.apache.aries.blueprint.sample.Foo)8 Test (org.junit.Test)7 URL (java.net.URL)2 BlueprintContainerImpl (org.apache.aries.blueprint.container.BlueprintContainerImpl)2 BlueprintContainer (org.osgi.service.blueprint.container.BlueprintContainer)2 ParseException (java.text.ParseException)1 Account (org.apache.aries.blueprint.sample.Account)1 AccountFactory (org.apache.aries.blueprint.sample.AccountFactory)1 Bar (org.apache.aries.blueprint.sample.Bar)1 FooRegistrationListener (org.apache.aries.blueprint.sample.FooRegistrationListener)1 ServiceReference (org.osgi.framework.ServiceReference)1 Configuration (org.osgi.service.cm.Configuration)1