Search in sources :

Example 11 with ComponentMetadata

use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.

the class ActivateMethodTest method newMetadata.

private ComponentMetadata newMetadata() {
    ComponentMetadata metadata = new ComponentMetadata(DSVersion.DS11);
    metadata.setName("foo");
    metadata.setImplementationClassName(Object.class.getName());
    metadata.validate(null);
    return metadata;
}
Also used : Level1Object(org.apache.felix.scr.impl.metadata.instances.Level1Object) BaseObject(org.apache.felix.scr.impl.metadata.instances.BaseObject) Level3Object(org.apache.felix.scr.impl.metadata.instances.Level3Object) Level2Object(org.apache.felix.scr.impl.metadata.instances2.Level2Object) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Example 12 with ComponentMetadata

use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.

the class ActivateMethodTest method newContainer.

private ComponentContainer newContainer() {
    final ComponentMetadata metadata = newMetadata();
    ComponentContainer container = new ComponentContainer() {

        public BundleComponentActivator getActivator() {
            return null;
        }

        public ComponentMetadata getComponentMetadata() {
            return metadata;
        }

        public void disposed(SingleComponentManager component) {
        }

        public boolean isEnabled() {
            return false;
        }
    };
    return container;
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContainer(org.apache.felix.scr.impl.manager.ComponentContainer) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Example 13 with ComponentMetadata

use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.

the class BindMethodTest method newContainer.

private ComponentContainer newContainer() {
    final ComponentMetadata metadata = newMetadata();
    ComponentContainer container = new ComponentContainer() {

        public BundleComponentActivator getActivator() {
            return null;
        }

        public ComponentMetadata getComponentMetadata() {
            return metadata;
        }

        public void disposed(SingleComponentManager component) {
        }

        public boolean isEnabled() {
            return false;
        }
    };
    return container;
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContainer(org.apache.felix.scr.impl.manager.ComponentContainer) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Example 14 with ComponentMetadata

use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.

the class ConfiguredComponentHolderTest method test_factory.

public void test_factory() {
    // setup a holder
    final String name = "test.factory";
    final ComponentMetadata cm = createComponentMetadata(name);
    final TestingConfiguredComponentHolder holder = new TestingConfiguredComponentHolder(cm);
    holder.enableComponents(false);
    // assert single component and no map
    final SingleComponentManager cmgr = getSingleManager(holder);
    assertNotNull("Expect single component manager", cmgr);
    assertEquals("Expect no other component manager list", 1, getComponentManagers(holder).size());
    // configure with configuration
    final String pid1 = "test.factory.0001";
    final Dictionary config1 = new Hashtable();
    config1.put("value", pid1);
    TargetedPID targetedFactoryPid = new TargetedPID(name);
    TargetedPID targetedPid1 = new TargetedPID(pid1);
    holder.configurationUpdated(targetedPid1, targetedFactoryPid, config1, 0);
    // assert single component and single-entry map
    final SingleComponentManager cmgrAfterConfig = getSingleManager(holder);
    final List<SingleComponentManager> cmgrsAfterConfig = getComponentManagers(holder);
    assertNotNull("Expect single component manager", cmgrAfterConfig);
    assertNotNull("Expect component manager list", cmgrsAfterConfig);
    assertEquals("Expect one component manager in list", 1, cmgrsAfterConfig.size());
    // add another configuration
    final String pid2 = "test.factory.0002";
    final Dictionary config2 = new Hashtable();
    config1.put("value", pid2);
    TargetedPID targetedPid2 = new TargetedPID(pid2);
    holder.configurationUpdated(targetedPid2, targetedFactoryPid, config2, 1);
    final List<SingleComponentManager> cmgrsAfterConfig2 = getComponentManagers(holder);
    assertNotNull("Expect component manager list", cmgrsAfterConfig2);
    assertEquals("Expect two component manager in list", 2, cmgrsAfterConfig2.size());
    // remove second configuration
    holder.configurationDeleted(targetedPid2, targetedFactoryPid);
    final List<SingleComponentManager> cmgrsAfterUnConfig2 = getComponentManagers(holder);
    assertNotNull("Expect component manager list", cmgrsAfterUnConfig2);
    // TODO Multipids fix correct assertion        assertEquals( "Expect one component manager in list", 1, cmgrsAfterUnConfig2.size() );
    // add second config again and remove first config -> replace singleton component
    holder.configurationUpdated(targetedPid2, targetedFactoryPid, config2, 2);
    holder.configurationDeleted(targetedPid1, targetedFactoryPid);
    // assert single component and single-entry map
    final List<SingleComponentManager> cmgrsAfterConfigUnconfig = getComponentManagers(holder);
    assertNotNull("Expect component manager list", cmgrsAfterConfigUnconfig);
    // TODO Multipids fix correct assertion        assertEquals( "Expect one component manager in list", 1, cmgrsAfterConfigUnconfig.size() );
    // remove second configuration (leaving no configurations)
    holder.configurationDeleted(targetedPid2, targetedFactoryPid);
    // assert single component and single-entry map
    final List<SingleComponentManager> cmgrsAfterAllUnconfig = getComponentManagers(holder);
    assertNotNull("Expect single component manager", cmgrsAfterAllUnconfig);
// TODO Multipids fix correct assertion        assertEquals( "Expect no component manager list", 1, cmgrsAfterAllUnconfig.size() );
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) TargetedPID(org.apache.felix.scr.impl.metadata.TargetedPID)

Example 15 with ComponentMetadata

use of org.apache.felix.scr.impl.metadata.ComponentMetadata in project felix by apache.

the class SingleComponentManagerTest method testGetService.

@Test
public void testGetService() throws Exception {
    ComponentMetadata cm = new ComponentMetadata(DSVersion.DS13);
    cm.setImplementationClassName("foo.bar.SomeClass");
    cm.validate(null);
    @SuppressWarnings("unchecked") ComponentContainer<Object> cc = Mockito.mock(ComponentContainer.class);
    Mockito.when(cc.getComponentMetadata()).thenReturn(cm);
    Mockito.when(cc.getActivator()).thenReturn(componentActivator);
    SingleComponentManager<Object> scm = new SingleComponentManager<Object>(cc, new ComponentMethodsImpl()) {

        @Override
        boolean getServiceInternal(ServiceRegistration<Object> serviceRegistration) {
            return true;
        }
    };
    BundleContext bc = Mockito.mock(BundleContext.class);
    Bundle b = Mockito.mock(Bundle.class);
    Mockito.when(b.getBundleContext()).thenReturn(bc);
    ComponentContextImpl<Object> cci = new ComponentContextImpl<Object>(scm, b, null);
    Object implObj = new Object();
    cci.setImplementationObject(implObj);
    cci.setImplementationAccessible(true);
    Field f = SingleComponentManager.class.getDeclaredField("m_componentContext");
    f.setAccessible(true);
    f.set(scm, cci);
    scm.setState(scm.getState(), State.unsatisfiedReference);
    assertSame(implObj, scm.getService(b, serviceRegistration));
    Field u = SingleComponentManager.class.getDeclaredField("m_useCount");
    u.setAccessible(true);
    AtomicInteger use = (AtomicInteger) u.get(scm);
    assertEquals(1, use.get());
}
Also used : Bundle(org.osgi.framework.Bundle) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) ServiceRegistration(org.osgi.framework.ServiceRegistration) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)17 ComponentContainer (org.apache.felix.scr.impl.manager.ComponentContainer)3 SingleComponentManager (org.apache.felix.scr.impl.manager.SingleComponentManager)3 Bundle (org.osgi.framework.Bundle)3 BundleContext (org.osgi.framework.BundleContext)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComponentMethodsImpl (org.apache.felix.scr.impl.inject.ComponentMethodsImpl)2 TargetedPID (org.apache.felix.scr.impl.metadata.TargetedPID)2 BaseObject (org.apache.felix.scr.impl.metadata.instances.BaseObject)2 Level1Object (org.apache.felix.scr.impl.metadata.instances.Level1Object)2 Level3Object (org.apache.felix.scr.impl.metadata.instances.Level3Object)2 Level2Object (org.apache.felix.scr.impl.metadata.instances2.Level2Object)2 Test (org.junit.Test)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ComponentException (org.osgi.service.component.ComponentException)2 BufferedReader (java.io.BufferedReader)1