Search in sources :

Example 6 with InstanceManager

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

the class ManipulatorTest method testManipulatingTheSimplePojo.

public void testManipulatingTheSimplePojo() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/SimplePojo.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.SimplePojo", clazz);
    Class cl = classloader.findClass("test.SimplePojo");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    System.out.println(manipulator.getManipulationMetadata());
    // The manipulation add stuff to the class.
    Assert.assertTrue(clazz.length > getBytesFromFile(new File("target/test-classes/test/SimplePojo.class")).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("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)

Example 7 with InstanceManager

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

the class ManipulatorTest method testManipulatingChild.

public void testManipulatingChild() 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 the POJO interface
    Assert.assertTrue(Arrays.asList(cl.getInterfaces()).contains(Pojo.class));
    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());
}
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 8 with InstanceManager

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

the class ManipulatorTest method testManipulatingWithNoValidConstructor.

public void testManipulatingWithNoValidConstructor() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/NoValidConstructor.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.NoValidConstructor", clazz);
    Class cl = classloader.findClass("test.NoValidConstructor");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    System.out.println(manipulator.getManipulationMetadata());
    // 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);
    // 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);
}
Also used : Pojo(org.apache.felix.ipojo.Pojo) Constructor(java.lang.reflect.Constructor) InstanceManager(org.apache.felix.ipojo.InstanceManager)

Example 9 with InstanceManager

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

the class ManipulatorTest method testManipulatingTheNonSunPOJO.

public void testManipulatingTheNonSunPOJO() throws Exception {
    Manipulator manipulator = new Manipulator(this.getClass().getClassLoader());
    byte[] origin = getBytesFromFile(new File("target/test-classes/test/NonSunClass.class"));
    manipulator.prepare(origin);
    byte[] clazz = manipulator.manipulate(origin);
    ManipulatedClassLoader classloader = new ManipulatedClassLoader("test.NonSunClass", clazz);
    Class cl = classloader.findClass("test.NonSunClass");
    Assert.assertNotNull(cl);
    Assert.assertNotNull(manipulator.getManipulationMetadata());
    System.out.println(manipulator.getManipulationMetadata());
    // The manipulation add stuff to the class.
    Assert.assertTrue(clazz.length > getBytesFromFile(new File("target/test-classes/test/NonSunClass.class")).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);
    // 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("getS1", 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 InstanceManager

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

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