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");
}
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());
}
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());
}
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);
}
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());
}
Aggregations