Search in sources :

Example 1 with ComponentMethodsImpl

use of org.apache.felix.scr.impl.inject.ComponentMethodsImpl in project felix by apache.

the class ActivateMethodTest method checkMethod.

/**
 * Checks whether a method with the given name can be found for the
 * activate/deactivate method parameter list and whether the method returns
 * the expected description when called.
 *
 * @param obj
 * @param methodName
 * @param methodDesc
 * @param version DSVersion tested
 */
private void checkMethod(BaseObject obj, String methodName, String methodDesc, DSVersion version) {
    ComponentContainer<?> container = newContainer();
    SingleComponentManager<?> icm = new SingleComponentManager(container, new ComponentMethodsImpl());
    ActivateMethod am = new ActivateMethod(methodName, methodName != null, obj.getClass(), version, false, false);
    am.invoke(obj, new ActivatorParameter(new ComponentContextImpl(icm, m_bundle, null), -1), null);
    Method m = am.getMethod();
    assertNotNull(m);
    assertEquals(methodName, m.getName());
    assertEquals(methodDesc, obj.getCalledMethod());
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) ActivatorParameter(org.apache.felix.scr.impl.inject.ActivatorParameter) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) AcceptMethod(org.apache.felix.scr.impl.metadata.instances.AcceptMethod) Method(java.lang.reflect.Method)

Example 2 with ComponentMethodsImpl

use of org.apache.felix.scr.impl.inject.ComponentMethodsImpl in project felix by apache.

the class ActivateMethodTest method ensureMethodNotFoundMethod.

/**
 * Ensures no method with the given name accepting any of the
 * activate/deactive method parameters can be found.
 *
 * @param obj
 * @param methodName
 * @param version DS version tested
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 */
private void ensureMethodNotFoundMethod(BaseObject obj, String methodName, DSVersion version) {
    ComponentContainer container = newContainer();
    SingleComponentManager icm = new SingleComponentManager(container, new ComponentMethodsImpl());
    ActivateMethod am = new ActivateMethod(methodName, methodName != null, obj.getClass(), version, false, false);
    am.invoke(obj, new ActivatorParameter(new ComponentContextImpl(icm, m_bundle, null), -1), null);
    Method m = am.getMethod();
    assertNull(m);
    assertNull(obj.getCalledMethod());
}
Also used : SingleComponentManager(org.apache.felix.scr.impl.manager.SingleComponentManager) ComponentContextImpl(org.apache.felix.scr.impl.manager.ComponentContextImpl) ActivatorParameter(org.apache.felix.scr.impl.inject.ActivatorParameter) ComponentContainer(org.apache.felix.scr.impl.manager.ComponentContainer) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) AcceptMethod(org.apache.felix.scr.impl.metadata.instances.AcceptMethod) Method(java.lang.reflect.Method)

Example 3 with ComponentMethodsImpl

use of org.apache.felix.scr.impl.inject.ComponentMethodsImpl 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)

Example 4 with ComponentMethodsImpl

use of org.apache.felix.scr.impl.inject.ComponentMethodsImpl in project felix by apache.

the class SingleComponentManagerTest method testGetServiceWithNullComponentContext.

@Test
public void testGetServiceWithNullComponentContext() 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<?> 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);
    scm.setState(scm.getState(), State.unsatisfiedReference);
    assertNull("m_componentContext is null, this should not cause an NPE", scm.getService(b, serviceRegistration));
    Field u = SingleComponentManager.class.getDeclaredField("m_useCount");
    u.setAccessible(true);
    AtomicInteger use = (AtomicInteger) u.get(scm);
    assertEquals(0, use.get());
}
Also used : Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(org.osgi.framework.Bundle) ComponentMethodsImpl(org.apache.felix.scr.impl.inject.ComponentMethodsImpl) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata) ServiceRegistration(org.osgi.framework.ServiceRegistration) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

ComponentMethodsImpl (org.apache.felix.scr.impl.inject.ComponentMethodsImpl)4 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ActivatorParameter (org.apache.felix.scr.impl.inject.ActivatorParameter)2 ComponentContextImpl (org.apache.felix.scr.impl.manager.ComponentContextImpl)2 SingleComponentManager (org.apache.felix.scr.impl.manager.SingleComponentManager)2 ComponentMetadata (org.apache.felix.scr.impl.metadata.ComponentMetadata)2 AcceptMethod (org.apache.felix.scr.impl.metadata.instances.AcceptMethod)2 Test (org.junit.Test)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ComponentContainer (org.apache.felix.scr.impl.manager.ComponentContainer)1