Search in sources :

Example 56 with Factory

use of org.apache.felix.ipojo.Factory 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 57 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestSuperMethodProperties method setUp.

@Before
public void setUp() {
    Factory fact = ipojoHelper.getFactory("CONFIG-ParentMethodConfigurableCheckService");
    Properties props = new Properties();
    props.put("instance.name", "under-test");
    props.put("b", "1");
    props.put("s", "1");
    props.put("i", "1");
    props.put("l", "1");
    props.put("d", "1");
    props.put("f", "1");
    props.put("c", "a");
    props.put("bool", "true");
    props.put("bs", "{1,2,3}");
    props.put("ss", "{1,2,3}");
    props.put("is", "{1,2,3}");
    props.put("ls", "{1,2,3}");
    props.put("ds", "{1,2,3}");
    props.put("fs", "{1,2,3}");
    props.put("cs", "{a,b,c}");
    props.put("bools", "{true,true,true}");
    props.put("string", "foo");
    props.put("strings", "{foo, bar, baz}");
    try {
        instance = fact.createComponentInstance(props);
    } catch (Exception e) {
        fail("Cannot create the under-test instance : " + e.getMessage());
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) Before(org.junit.Before)

Example 58 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestFactoryProperties method testSimpleExposition.

@Test
public void testSimpleExposition() {
    ServiceReference ref1 = ipojoHelper.getServiceReferenceByName(Factory.class.getName(), "Factories-FooProviderType-1");
    assertNotNull("The factory is available", ref1);
    String[] spec = (String[]) ref1.getProperty("component.providedServiceSpecifications");
    assertEquals("Check array length", spec.length, 1);
    assertEquals("Check spec", spec[0], FooService.class.getName());
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Factory(org.apache.felix.ipojo.Factory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 59 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestInstances method testOneDuplicateInstance.

@Test
public void testOneDuplicateInstance() {
    Factory factory = ipojoHelper.getFactory("OneDuplicateInstance");
    assertNotNull(factory);
    // Create one instance
    ComponentInstance instance = createInstanceOrFail(factory, "OneDuplicateInstance-0");
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(1, factory.getInstancesNames().size());
    assertEquals(1, factory.getInstances().size());
    assertContains("", factory.getInstancesNames().toArray(new String[1]), "OneDuplicateInstance-0");
    // The instance should not be created, and we should have only the first instance
    try {
        createInstance(factory, "OneDuplicateInstance-0");
        fail("The instance OneDuplicateInstance-0 has been created with factory " + factory.getName() + ". " + "It's shouldn't have been created has it's a duplicate instance");
    } catch (UnacceptableConfiguration e) {
    // expected Exception
    } catch (Exception e) {
        fail("Cannot create instance OneDuplicateInstance-0 with factory " + factory.getName() + ". " + "The wrong exception has been raised : " + e.toString());
    }
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(1, factory.getInstancesNames().size());
    assertEquals(1, factory.getInstances().size());
    // Dispose of the instance
    instance.dispose();
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(0, factory.getInstancesNames().size());
    assertEquals(0, factory.getInstances().size());
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) UnacceptableConfiguration(org.apache.felix.ipojo.UnacceptableConfiguration) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) MissingHandlerException(org.apache.felix.ipojo.MissingHandlerException) Test(org.junit.Test)

Example 60 with Factory

use of org.apache.felix.ipojo.Factory in project felix by apache.

the class TestInstances method testFiveInstances.

@Test
public void testFiveInstances() {
    Factory factory = ipojoHelper.getFactory("FiveInstances");
    assertNotNull(factory);
    // No instances for the moment
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(0, factory.getInstancesNames().size());
    assertEquals(0, factory.getInstances().size());
    // Create 5 instances
    ComponentInstance[] instances = createNInstanceOrFail(factory, "FiveInstances", 5);
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(5, factory.getInstancesNames().size());
    assertEquals(5, factory.getInstances().size());
    assertContains("", factory.getInstancesNames().toArray(new String[5]), "FiveInstances-0");
    assertContains("", factory.getInstancesNames().toArray(new String[5]), "FiveInstances-1");
    assertContains("", factory.getInstancesNames().toArray(new String[5]), "FiveInstances-2");
    assertContains("", factory.getInstancesNames().toArray(new String[5]), "FiveInstances-3");
    assertContains("", factory.getInstancesNames().toArray(new String[5]), "FiveInstances-4");
    // Dispose of instances 0, 3 and 2
    // 2 instances left
    instances[0].dispose();
    instances[3].dispose();
    instances[2].dispose();
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(2, factory.getInstancesNames().size());
    assertEquals(2, factory.getInstances().size());
    assertContains("", factory.getInstancesNames().toArray(new String[2]), "FiveInstances-1");
    assertContains("", factory.getInstancesNames().toArray(new String[2]), "FiveInstances-4");
    // Dispose of instances 1 and 4
    // No instances left
    instances[1].dispose();
    instances[4].dispose();
    assertNotNull(factory.getInstancesNames());
    assertNotNull(factory.getInstances());
    assertEquals(0, factory.getInstancesNames().size());
    assertEquals(0, factory.getInstances().size());
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Test(org.junit.Test)

Aggregations

Factory (org.apache.felix.ipojo.Factory)111 Properties (java.util.Properties)88 Test (org.junit.Test)71 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)53 Before (org.junit.Before)33 ServiceReference (org.osgi.framework.ServiceReference)29 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)17 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)17 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)13 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)11 IOException (java.io.IOException)6 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)6 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 CompositeManager (org.apache.felix.ipojo.composite.CompositeManager)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)4 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)4