Search in sources :

Example 81 with CheckService

use of org.apache.felix.ipojo.runtime.core.services.CheckService in project felix by apache.

the class TestPropertyModifier method testPropertyModifier.

@Test
public void testPropertyModifier() {
    ComponentInstance ci = null;
    Factory factory = ipojoHelper.getFactory("org.apache.felix.ipojo.runtime.core.components.PropertyModifier");
    Properties props = new Properties();
    props.put("cls", new String[] { FooService.class.getName() });
    try {
        ci = factory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), ci.getInstanceName());
    assertNotNull("Check ref", ref);
    // Check the service property
    // Not exposed here:
    assertNull("Classes -0", ref.getProperty("classes"));
    CheckService check = (CheckService) osgiHelper.getRawServiceObject(ref);
    assertTrue(check.check());
    // Property exposed now.
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), ci.getInstanceName());
    Class[] str = (Class[]) ref.getProperty("classes");
    assertEquals("Classes size", 1, str.length);
    assertEquals("Classes[0]", FooService.class.getName(), str[0].getName());
    Properties p = check.getProps();
    Class[] str2 = (Class[]) p.get("classes");
    assertEquals("Classes size -2", 1, str2.length);
    assertEquals("Classes[0] -2", FooService.class.getName(), str2[0].getName());
    Properties props2 = new Properties();
    props2.put("cls", new String[] { FooService.class.getName(), CheckService.class.getName() });
    try {
        ci.reconfigure(props2);
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Check the service property
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), ci.getInstanceName());
    assertNotNull("Check ref", ref);
    str = (Class[]) ref.getProperty("classes");
    assertEquals("Classes size -3", 2, str.length);
    assertEquals("Classes[0] -3", FooService.class.getName(), str[0].getName());
    assertEquals("Classes[1] -3", CheckService.class.getName(), str[1].getName());
    check = (CheckService) osgiHelper.getRawServiceObject(ref);
    p = check.getProps();
    str2 = (Class[]) p.get("classes");
    assertEquals("Classes size -4", 2, str2.length);
    assertEquals("Classes[0] -4", FooService.class.getName(), str2[0].getName());
    assertEquals("Classes[1] -4", CheckService.class.getName(), str2[1].getName());
    ci.dispose();
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 82 with CheckService

use of org.apache.felix.ipojo.runtime.core.services.CheckService in project felix by apache.

the class TestSuperMethodProperties method testConfigurationObj.

@Test
public void testConfigurationObj() {
    ServiceReference ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Test check service availability", ref);
    CheckService check = (CheckService) osgiHelper.getRawServiceObject(ref);
    Properties props = check.getProps();
    String s = (String) props.get("string");
    String[] ss = (String[]) props.get("strings");
    assertEquals("Check string", s, "foo");
    assertEquals("Check strings 0", ss[0], "foo");
    assertEquals("Check strings 1", ss[1], "bar");
    assertEquals("Check strings 2", ss[2], "baz");
    // Integer upString = (Integer) props.get("upstring");
    // Integer upStrings = (Integer) props.get("upstrings");
    // 
    // assertEquals("Check upString", upString, new Integer(1));
    // assertEquals("Check upStrings", upStrings, new Integer(1));
    reconfigure();
    ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance.getInstanceName());
    assertNotNull("Test check service availability", ref);
    check = (CheckService) osgiHelper.getRawServiceObject(ref);
    props = check.getProps();
    s = (String) props.get("string");
    ss = (String[]) props.get("strings");
    assertEquals("2) Check string", s, "bar");
    assertEquals("2) Check strings 0", ss[0], "baz");
    assertEquals("2) Check strings 1", ss[1], "bar");
    assertEquals("2) Check strings 2", ss[2], "foo");
// upString = (Integer) props.get("upstring");
// upStrings = (Integer) props.get("upstrings");
// 
// assertEquals("2) Check upString", upString, new Integer(2));
// assertEquals("2) Check upStrings", upStrings, new Integer(2));
}
Also used : CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 83 with CheckService

use of org.apache.felix.ipojo.runtime.core.services.CheckService in project felix by apache.

the class TestContextInjectionFromAnnotations method testInstanceCreatedFromConfiguration.

@Test
public void testInstanceCreatedFromConfiguration() {
    CheckService check = ipojoHelper.getServiceObjectByName(CheckService.class, "from.configuration");
    assertNotNull(check);
    BundleContext context = (BundleContext) check.map().get("context");
    assertNotNull(context);
    assertNotSame(bc, context);
}
Also used : CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 84 with CheckService

use of org.apache.felix.ipojo.runtime.core.services.CheckService in project felix by apache.

the class TestContextInjectionFromAnnotations method testConstructorInjectionOfComponentBundleContext.

@Test
public void testConstructorInjectionOfComponentBundleContext() {
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core" + ".components.annotations.ComponentBundleContextInjectionInConstructor");
    CheckService check = ipojoHelper.getServiceObjectByName(CheckService.class, instance.getInstanceName());
    assertNotNull(check);
    BundleContext context = (BundleContext) check.map().get("context");
    assertNotNull(context);
    assertEquals(getTestBundle().getSymbolicName(), context.getBundle().getSymbolicName());
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 85 with CheckService

use of org.apache.felix.ipojo.runtime.core.services.CheckService in project felix by apache.

the class TestContextInjectionFromAnnotations method testFieldInjectionOfInstanceBundleContext.

@Test
public void testFieldInjectionOfInstanceBundleContext() {
    BundleContext bc = osgiHelper.getBundle("org.apache.felix.ipojo").getBundleContext();
    Properties configuration = new Properties();
    configuration.put("instance.bundle.context", bc);
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core" + ".components.annotations.InstanceBundleContextInjectionInField", configuration);
    CheckService check = ipojoHelper.getServiceObjectByName(CheckService.class, instance.getInstanceName());
    assertNotNull(check);
    BundleContext context = (BundleContext) check.map().get("context");
    assertNotNull(context);
    assertEquals(bc, context);
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.services.CheckService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)124 Test (org.junit.Test)122 ServiceReference (org.osgi.framework.ServiceReference)90 Properties (java.util.Properties)87 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)50 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 BundleContext (org.osgi.framework.BundleContext)20 Factory (org.apache.felix.ipojo.Factory)11 ServiceContext (org.apache.felix.ipojo.ServiceContext)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)6 Hashtable (java.util.Hashtable)5 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)2 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)2 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)2 CallbackCheckService (org.apache.felix.ipojo.runtime.core.services.CallbackCheckService)2 Before (org.junit.Before)2 List (java.util.List)1