Search in sources :

Example 66 with Factory

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

the class TestProvidedServiceArchitecture method testPropsNoValue.

@Test
public void testPropsNoValue() {
    String factName = "PS-FooProviderType-3";
    String compName = "FooProvider";
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider", fact);
    ipojoHelper.createComponentInstance(factName, compName);
    ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
    assertNotNull("Architecture not available", arch_ref);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
    InstanceDescription id = arch.getInstanceDescription();
    assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
    assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooProviderType1");
    HandlerDescription[] handlers = id.getHandlers();
    assertEquals("Number of handlers", handlers.length, 3);
    // Look for the ProvidedService Handler
    ProvidedServiceHandlerDescription pshd = null;
    pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
    // for(int i = 0; i < handlers.length; i++) {
    // if(handlers[i].getHandlerName().equals("org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler")) {
    // pshd = (ProvidedServiceHandlerDescription) handlers[i];
    // }
    // }
    assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
    ProvidedServiceDescription[] ps = pshd.getProvidedServices();
    assertEquals("Check ProvidedService number", ps.length, 1);
    assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 1);
    assertEquals("Check Provided Service Specs - 2", ps[0].getServiceSpecifications()[0], FooService.class.getName());
    assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
    Properties prop = ps[0].getProperties();
    assertNotNull("Check Props", prop);
    assertEquals("Check service properties number (#" + prop + "?=5)", prop.size(), 2);
    assertEquals("Check instance.name property", prop.getProperty("instance.name"), compName);
    assertEquals("Check factory.name property", prop.getProperty("factory.name"), factName);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) Factory(org.apache.felix.ipojo.Factory) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) Properties(java.util.Properties) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 67 with Factory

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

the class TestUnacceptableConfiguration method testUnicity2.

/**
 * Check instance name unicity.
 */
@Test
public void testUnicity2() {
    Factory f = ipojoHelper.getFactory("Factories-FooProviderType-2");
    ComponentInstance ci1, ci2, ci3;
    try {
        Properties p1 = new Properties();
        p1.put("instance.name", "name1");
        ci1 = f.createComponentInstance(p1);
        Properties p2 = new Properties();
        p2.put("instance.name", "name2");
        ci2 = f.createComponentInstance(p2);
        Properties p3 = new Properties();
        p3.put("instance.name", "name3");
        ci3 = f.createComponentInstance(p3);
        assertThat("Check name ci1, ci2", ci1.getInstanceName(), not(ci2.getInstanceName()));
        assertThat("Check name ci1, ci3", ci1.getInstanceName(), not(ci3.getInstanceName()));
        assertThat("Check name ci3, ci2", ci3.getInstanceName(), not(ci2.getInstanceName()));
        ci1.dispose();
        ci2.dispose();
        ci3.dispose();
    } catch (Exception e) {
        fail("An acceptable configuration is refused");
    }
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) Test(org.junit.Test)

Example 68 with Factory

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

the class TestUnacceptableConfiguration method testEmptyConfiguration2.

/**
 * Empty configuration (just the name).
 */
@Test
public void testEmptyConfiguration2() {
    Factory f = ipojoHelper.getFactory("Factories-FooProviderType-Dyn2");
    Properties p = new Properties();
    p.put("instance.name", "ko");
    ComponentInstance ci;
    try {
        ci = f.createComponentInstance(p);
        ci.dispose();
    } catch (Exception e) {
        return;
    }
    fail("An unacceptable configuration is accepted");
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) Test(org.junit.Test)

Example 69 with Factory

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

the class TestUnacceptableConfiguration method testNull.

/**
 * Null configuration (accept).
 */
@Test
public void testNull() {
    Factory f = ipojoHelper.getFactory("Factories-FooProviderType-2");
    ComponentInstance ci;
    try {
        ci = f.createComponentInstance(null);
        ci.dispose();
    } catch (Exception e) {
        fail("An acceptable configuration is refused");
    }
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Test(org.junit.Test)

Example 70 with Factory

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

the class TestUnacceptableConfiguration method testWithoutName.

/**
 * Configuration without the name property.
 */
@Test
public void testWithoutName() {
    Factory f = ipojoHelper.getFactory("Factories-FooProviderType-2");
    Properties p = new Properties();
    p.put("int", 3);
    p.put("long", (long) 42);
    p.put("string", "absdir");
    p.put("strAProp", new String[] { "a" });
    p.put("intAProp", new int[] { 1, 2 });
    ComponentInstance ci;
    try {
        ci = f.createComponentInstance(p);
        ci.dispose();
    } catch (Exception e) {
        fail("an acceptable configuration is refused : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) 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