Search in sources :

Example 1 with ComponentFactory

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

the class SmartProxyTest method testProxiesOfJavaClasses.

/**
 * Tests if we can proxies classes from java.* package.
 * Indeed, a recent JVM bug fix introduces a bug:
 * <code>
 * [ERROR] test : Cannot create the proxy object
 * java.lang.SecurityException: Prohibited package name: java.awt
 * </code>
 */
public void testProxiesOfJavaClasses() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
    Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
    Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
    Mockito.when(context.getBundle()).thenReturn(bundle);
    ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
    Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
    InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
    Mockito.when(im.getContext()).thenReturn(context);
    Mockito.when(im.getFactory()).thenReturn(factory);
    DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Logger logger = new Logger(context, "test", Logger.INFO);
    Mockito.when(handler.getLogger()).thenReturn(logger);
    // Try with java.List
    Dependency dependency = new Dependency(handler, "a_field", List.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // OK
    Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
    Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof List);
    dependency.stop();
    // Try with javax.sql.CommonDataSource
    dependency = new Dependency(handler, "a_field", javax.sql.DataSource.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // OK
    Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
    Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof javax.sql.DataSource);
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Logger(org.apache.felix.ipojo.util.Logger) MockBundle(org.apache.felix.ipojo.test.MockBundle) List(java.util.List) ArrayList(java.util.ArrayList) BundleContext(org.osgi.framework.BundleContext)

Example 2 with ComponentFactory

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

the class SmartProxyTest method testCannotProxyAbstractAndConcreteClasses.

/**
 * Check that we don't create smart proxies for concrete and abstract classes.
 */
public void testCannotProxyAbstractAndConcreteClasses() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
    Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
    Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
    Mockito.when(context.getBundle()).thenReturn(bundle);
    ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
    Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
    InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
    Mockito.when(im.getContext()).thenReturn(context);
    Mockito.when(im.getFactory()).thenReturn(factory);
    DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Logger logger = new Logger(context, "test", Logger.INFO);
    Mockito.when(handler.getLogger()).thenReturn(logger);
    Dependency dependency = new Dependency(handler, "a_field", ArrayList.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // No service
    Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
    dependency.stop();
    // Try with an Object.
    dependency = new Dependency(handler, "a_field", Object.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // OK
    Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) MockBundle(org.apache.felix.ipojo.test.MockBundle) Logger(org.apache.felix.ipojo.util.Logger) BundleContext(org.osgi.framework.BundleContext)

Example 3 with ComponentFactory

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

the class TestComp2 method setUp.

@Before
public void setUp() {
    tataFactory = (ComponentFactory) ipojoHelper.getFactory("tata");
    totoFactory = (ComponentFactory) ipojoHelper.getFactory("toto");
    tataFactory2 = (ComponentFactory) ipojoHelper.getFactory("comp-6");
    tataFactory2.stop();
    tataFactory.stop();
    Properties props = new Properties();
    props.put("instance.name", "toto provider");
    try {
        totoProv = totoFactory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Properties props3 = new Properties();
    props3.put("instance.name", "toto provider 2");
    try {
        totoProv2 = totoFactory.createComponentInstance(props3);
    } catch (Exception e) {
        e.printStackTrace();
    }
    totoProv.stop();
    totoProv2.stop();
    Factory factory = ipojoHelper.getFactory("comp-2");
    Properties props2 = new Properties();
    props2.put("instance.name", "ff");
    try {
        under = factory.createComponentInstance(props2);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Properties(java.util.Properties) Before(org.junit.Before)

Example 4 with ComponentFactory

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

the class TestComp5 method setUp.

@Before
public void setUp() {
    tataFactory = (ComponentFactory) ipojoHelper.getFactory("tata");
    totoFactory = (ComponentFactory) ipojoHelper.getFactory("toto");
    tataFactory2 = (ComponentFactory) ipojoHelper.getFactory("comp-6");
    tataFactory2.stop();
    tataFactory.stop();
    Properties props = new Properties();
    props.put("instance.name", "toto provider");
    try {
        totoProv = totoFactory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Properties props3 = new Properties();
    props3.put("instance.name", "toto provider 2");
    try {
        totoProv2 = totoFactory.createComponentInstance(props3);
    } catch (Exception e) {
        e.printStackTrace();
    }
    totoProv.stop();
    totoProv2.stop();
    Factory factory = ipojoHelper.getFactory("comp-5");
    Properties props2 = new Properties();
    props2.put("instance.name", "ff");
    try {
        under = factory.createComponentInstance(props2);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Cannot create an instance from comp-5 : " + e.getMessage());
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Properties(java.util.Properties) Before(org.junit.Before)

Example 5 with ComponentFactory

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

the class TestComp6 method setUp.

@Before
public void setUp() {
    tataFactory = (ComponentFactory) ipojoHelper.getFactory("tata");
    totoFactory = (ComponentFactory) ipojoHelper.getFactory("toto");
    tataFactory.stop();
    Properties props = new Properties();
    props.put("instance.name", "toto provider");
    try {
        totoProv = totoFactory.createComponentInstance(props);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Properties props3 = new Properties();
    props3.put("instance.name", "toto provider 2");
    try {
        totoProv2 = totoFactory.createComponentInstance(props3);
    } catch (Exception e) {
        e.printStackTrace();
    }
    totoProv.stop();
    totoProv2.stop();
    Factory factory = ipojoHelper.getFactory("comp-6");
    Properties props2 = new Properties();
    props2.put("instance.name", "ff");
    try {
        under = factory.createComponentInstance(props2);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Factory(org.apache.felix.ipojo.Factory) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Properties(java.util.Properties) Before(org.junit.Before)

Aggregations

ComponentFactory (org.apache.felix.ipojo.ComponentFactory)35 Properties (java.util.Properties)27 Before (org.junit.Before)15 Test (org.junit.Test)15 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)14 Factory (org.apache.felix.ipojo.Factory)14 ServiceContext (org.apache.felix.ipojo.ServiceContext)7 Architecture (org.apache.felix.ipojo.architecture.Architecture)5 ServiceReference (org.osgi.framework.ServiceReference)5 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 CompositeInstanceDescription (org.apache.felix.ipojo.composite.CompositeInstanceDescription)4 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)4 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)2 InstanceManager (org.apache.felix.ipojo.InstanceManager)2 Element (org.apache.felix.ipojo.metadata.Element)2 ParseException (org.apache.felix.ipojo.parser.ParseException)2 MockBundle (org.apache.felix.ipojo.test.MockBundle)2 Logger (org.apache.felix.ipojo.util.Logger)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2