Search in sources :

Example 6 with Pojo

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

the class TestGetComponentInstance method testGetComponentInstance.

/**
 * Check if a pojo can correctly be cast in POJO.
 * Check the getComponentInstance method.
 */
@Test
public void testGetComponentInstance() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
    String factName = "Manipulation-FooProviderType-1";
    String compName = "FooProvider-1";
    ServiceReference ref = null;
    // Get the factory to create a component instance
    Factory fact = ipojoHelper.getFactory(factName);
    assertNotNull("Cannot find the factory FooProvider-1", fact);
    Properties props = new Properties();
    props.put("instance.name", compName);
    ComponentInstance ci = null;
    try {
        ci = fact.createComponentInstance(props);
    } catch (Exception e1) {
        fail(e1.getMessage());
    }
    assertEquals("Check instance name", compName, ci.getInstanceName());
    // Get a FooService provider
    ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), compName);
    assertNotNull("FS not available", ref);
    // Get foo object
    FooService fs = (FooService) osgiHelper.getServiceObject(ref);
    // Cast to POJO
    Pojo pojo = (Pojo) fs;
    Field im = fs.getClass().getDeclaredField("__IM");
    assertNotNull(im);
    im.setAccessible(true);
    assertNotNull(im.get(fs));
    Method method = fs.getClass().getMethod("getComponentInstance");
    assertNotNull(method);
    // GetComponentInstance
    ComponentInstance instance = pojo.getComponentInstance();
    assertNotNull(instance);
    assertEquals("Check component instance name", instance.getInstanceName(), compName);
    assertEquals("Check component factory name", instance.getFactory().getName(), factName);
    assertNotNull("Instance description not null", instance.getInstanceDescription());
    PrimitiveInstanceDescription id = (PrimitiveInstanceDescription) instance.getInstanceDescription();
    assertTrue("Check instance state", id.getState() == ComponentInstance.VALID);
    assertEquals("Check created pojo count", id.getCreatedObjects().length, 1);
    assertEquals("Check instance description name", id.getName(), compName);
    ci.dispose();
    // Check that there is no more FooService
    ref = osgiHelper.getServiceReference(FooService.class.getName());
    assertNull("FS available, but component instance stopped", ref);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) Field(java.lang.reflect.Field) Pojo(org.apache.felix.ipojo.Pojo) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Factory(org.apache.felix.ipojo.Factory) Method(java.lang.reflect.Method) Properties(java.util.Properties) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) BaseTest(org.ow2.chameleon.testing.helpers.BaseTest)

Example 7 with Pojo

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

the class InnerClassAdapterTest method testDoubleManipulation.

@Test
public void testDoubleManipulation() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    String className = "test.inner.ComponentWithInnerClasses";
    ManipulatedClassLoader classloader = manipulate(className, manipulator);
    manipulator = new Manipulator(this.getClass().getClassLoader());
    classloader = manipulate(className, manipulator, classloader);
    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)

Example 8 with Pojo

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

the class InnerClassAdapterTest method testManipulatingTheInner.

@Test
public void testManipulatingTheInner() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    String className = "test.PojoWithInner";
    byte[] origin = ManipulatorTest.getBytesFromFile(new File(baseClassDirectory + className.replace(".", "/") + ".class"));
    ManipulatedClassLoader classloader = manipulate(className, manipulator);
    Class cl = classloader.findClass(className);
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    Assert.assertFalse(manipulator.getInnerClasses().isEmpty());
    System.out.println(manipulator.getManipulationMetadata());
    // The manipulation add stuff to the class.
    Assert.assertTrue(classloader.get(className).length > origin.length);
    boolean found = false;
    Constructor cst = null;
    Constructor[] csts = cl.getDeclaredConstructors();
    for (Constructor cst2 : csts) {
        System.out.println(Arrays.asList(cst2.getParameterTypes()));
        if (cst2.getParameterTypes().length == 1 && cst2.getParameterTypes()[0].equals(InstanceManager.class)) {
            found = true;
            cst = cst2;
        }
    }
    Assert.assertTrue(found);
    // We still have the empty constructor
    found = false;
    csts = cl.getDeclaredConstructors();
    for (Constructor cst1 : csts) {
        System.out.println(Arrays.asList(cst1.getParameterTypes()));
        if (cst1.getParameterTypes().length == 0) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    // Check the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    InstanceManager im = Mockito.mock(InstanceManager.class);
    cst.setAccessible(true);
    Object pojo = cst.newInstance(new Object[] { im });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = cl.getMethod("doSomething", new Class[0]);
    Assert.assertTrue(((Boolean) method.invoke(pojo, new Object[0])).booleanValue());
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager) Method(java.lang.reflect.Method) File(java.io.File) Test(org.junit.Test)

Example 9 with Pojo

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

the class ManipulatorTest method testManipulatingDoubleArray.

/**
 * https://issues.apache.org/jira/browse/FELIX-3621
 */
public void testManipulatingDoubleArray() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/DoubleArray.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.DoubleArray", clazz);
    Class cl = classloader.findClass("test.DoubleArray");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    System.out.println(manipulator.getManipulationMetadata());
    Assert.assertTrue(manipulator.getManipulationMetadata().toString().contains("arguments=\"{int[][]}\""));
    // The manipulation add stuff to the class.
    Assert.assertTrue(clazz.length > origin.length);
    boolean found = false;
    Constructor cst = null;
    Constructor[] csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 1 && csts[i].getParameterTypes()[0].equals(InstanceManager.class)) {
            found = true;
            cst = csts[i];
        }
    }
    Assert.assertTrue(found);
    // We still have the empty constructor
    found = false;
    csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 0) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    // Check the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    cst.setAccessible(true);
    Object pojo = cst.newInstance(new Object[] { new InstanceManager() });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = cl.getMethod("start", new Class[0]);
    Assert.assertTrue(((Boolean) method.invoke(pojo, new Object[0])).booleanValue());
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager) Method(java.lang.reflect.Method)

Example 10 with Pojo

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

the class ManipulatorTest method testManipulatingWithConstructorModification.

public void testManipulatingWithConstructorModification() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/Child.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.Child", clazz);
    Class cl = classloader.findClass("test.Child");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    boolean found = false;
    Constructor cst = null;
    Constructor[] csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 1 && csts[i].getParameterTypes()[0].equals(InstanceManager.class)) {
            found = true;
            cst = csts[i];
        }
    }
    Assert.assertTrue(found);
    // We still have the regular constructor
    found = false;
    csts = cl.getDeclaredConstructors();
    for (int i = 0; i < csts.length; i++) {
        System.out.println(Arrays.asList(csts[i].getParameterTypes()));
        if (csts[i].getParameterTypes().length == 2) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    // Check that we have the IM, Integer, String constructor too
    Constructor cst2 = cl.getDeclaredConstructor(new Class[] { InstanceManager.class, Integer.TYPE, String.class });
    Assert.assertNotNull(cst2);
    // Check the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    // Creation using cst
    InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
    cst.setAccessible(true);
    Object pojo = cst.newInstance(new Object[] { im });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    Method method = cl.getMethod("doSomething", new Class[0]);
    Assert.assertEquals(9, ((Integer) method.invoke(pojo, new Object[0])).intValue());
    // Try to create using cst2
    im = (InstanceManager) Mockito.mock(InstanceManager.class);
    cst2.setAccessible(true);
    pojo = cst2.newInstance(new Object[] { im, new Integer(2), "bariton" });
    Assert.assertNotNull(pojo);
    Assert.assertTrue(pojo instanceof Pojo);
    method = cl.getMethod("doSomething", new Class[0]);
    Assert.assertEquals(10, ((Integer) method.invoke(pojo, new Object[0])).intValue());
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager) Method(java.lang.reflect.Method)

Aggregations

Pojo (org.apache.felix.ipojo.Pojo)11 Constructor (java.lang.reflect.Constructor)9 Method (java.lang.reflect.Method)9 InstanceManager (org.apache.felix.ipojo.InstanceManager)9 Test (org.junit.Test)4 File (java.io.File)1 Field (java.lang.reflect.Field)1 Properties (java.util.Properties)1 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)1 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)1 Factory (org.apache.felix.ipojo.Factory)1 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)1 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)1 Attribute (org.apache.felix.ipojo.metadata.Attribute)1 Element (org.apache.felix.ipojo.metadata.Element)1 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)1 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)1 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)1 ServiceReference (org.osgi.framework.ServiceReference)1 BaseTest (org.ow2.chameleon.testing.helpers.BaseTest)1