Search in sources :

Example 1 with Pojo

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

Example 2 with Pojo

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

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

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

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

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