Search in sources :

Example 1 with InstanceManager

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

the class NullableTest method testOnGet_whenNullableEnabled_returnsAProxyWithNullableObjectAsInvocationHandler.

public void testOnGet_whenNullableEnabled_returnsAProxyWithNullableObjectAsInvocationHandler() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = Mockito.mock(BundleContext.class);
    InstanceManager im = Mockito.mock(InstanceManager.class);
    Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class);
    DependencyHandler handler = Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    Object service = dependency.onGet(null, null, null);
    Assert.assertTrue(service instanceof Proxy);
    Assert.assertTrue(Proxy.getInvocationHandler(service) instanceof NullableObject);
}
Also used : Proxy(java.lang.reflect.Proxy) MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) MockBundle(org.apache.felix.ipojo.test.MockBundle) BundleContext(org.osgi.framework.BundleContext)

Example 2 with InstanceManager

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

the class NullableTest method testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification.

public void testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = Mockito.mock(BundleContext.class);
    InstanceManager im = Mockito.mock(InstanceManager.class);
    Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class);
    DependencyHandler handler = Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    Object service = dependency.onGet(null, null, null);
    Assert.assertTrue(service instanceof Nullable);
    Assert.assertTrue(service instanceof TestSpecification);
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) MockBundle(org.apache.felix.ipojo.test.MockBundle) Nullable(org.apache.felix.ipojo.Nullable) BundleContext(org.osgi.framework.BundleContext)

Example 3 with InstanceManager

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

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

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

the class InnerClassAdapterTest method testInnerClasses.

@Test
public void testInnerClasses() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    String className = "test.inner.ComponentWithInnerClasses";
    ManipulatedClassLoader classloader = manipulate(className, manipulator);
    Class clazz = classloader.findClass(className);
    Assert.assertNotNull(clazz);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    Assert.assertFalse(manipulator.getInnerClasses().isEmpty());
    // We should have found only 2 inner classes.
    assertThat(manipulator.getInnerClasses().size()).isEqualTo(3);
    // Check that all inner classes are manipulated.
    InstanceManager im = Mockito.mock(InstanceManager.class);
    Constructor constructor = clazz.getDeclaredConstructor(InstanceManager.class);
    constructor.setAccessible(true);
    Object pojo = constructor.newInstance(im);
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = clazz.getMethod("doSomething", new Class[0]);
    String result = (String) method.invoke(pojo);
    assertEquals(result, "foofoofoofoo");
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) InstanceManager(org.apache.felix.ipojo.InstanceManager) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

InstanceManager (org.apache.felix.ipojo.InstanceManager)13 Constructor (java.lang.reflect.Constructor)9 Pojo (org.apache.felix.ipojo.Pojo)9 Method (java.lang.reflect.Method)8 MockBundle (org.apache.felix.ipojo.test.MockBundle)4 Bundle (org.osgi.framework.Bundle)4 BundleContext (org.osgi.framework.BundleContext)4 Test (org.junit.Test)3 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)2 Logger (org.apache.felix.ipojo.util.Logger)2 File (java.io.File)1 Proxy (java.lang.reflect.Proxy)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Nullable (org.apache.felix.ipojo.Nullable)1