felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r1044497 - in /felix/sandbox/clement/ipojo-constructor-injection/manipulator: ./ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/felix/ src/test/java/org/apache/felix/ipojo/ src/test/java/org/a...
Date Fri, 10 Dec 2010 20:03:33 GMT
Author: clement
Date: Fri Dec 10 20:03:32 2010
New Revision: 1044497

URL: http://svn.apache.org/viewvc?rev=1044497&view=rev
Log:
Add a couple of unit tests to the manipulator.

Added:
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/PojoizationTest.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Parent.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/PojoWithInner.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/SimplePojo.java
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/metadata.xml
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation-no-annotations.jar
  (with props)
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation.java5.jar
  (with props)
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulator-annotations.jar
  (with props)
Modified:
    felix/sandbox/clement/ipojo-constructor-injection/manipulator/pom.xml

Modified: felix/sandbox/clement/ipojo-constructor-injection/manipulator/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/pom.xml?rev=1044497&r1=1044496&r2=1044497&view=diff
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/pom.xml (original)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/pom.xml Fri Dec 10 20:03:32
2010
@@ -51,6 +51,13 @@
       <artifactId>org.apache.felix.ipojo.metadata</artifactId>
       <version>1.4.0</version>
     </dependency>
+    <!-- iPOJO Dependencies for test only -->
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.ipojo</artifactId>
+      <version>1.6.0</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/ManipulatorTest.java
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,221 @@
+package org.apache.felix.ipojo.manipulation;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.InstanceManager;
+import org.apache.felix.ipojo.Pojo;
+import org.mockito.Mockito;
+
+public class ManipulatorTest extends TestCase {
+
+	public void testManipulatingTheSimplePojo() throws Exception {
+		Manipulator manipulator = new Manipulator();
+		byte[] clazz = manipulator.manipulate(getBytesFromFile(new File("target/test-classes/test/SimplePojo.class")));
+		TestClassLoader classloader = new TestClassLoader("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));
+
+		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.assertTrue(((Boolean) method.invoke(pojo, new Object[0])).booleanValue());
+
+	}
+
+	public void testManipulatingChild() throws Exception {
+		Manipulator manipulator = new Manipulator();
+		byte[] clazz = manipulator.manipulate(getBytesFromFile(new File("target/test-classes/test/Child.class")));
+		TestClassLoader classloader = new TestClassLoader("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());
+
+	}
+
+	public void testManipulatingTheInner() throws Exception {
+		Manipulator manipulator = new Manipulator();
+		byte[] clazz = manipulator.manipulate(getBytesFromFile(new File("target/test-classes/test/PojoWithInner.class")));
+		TestClassLoader classloader = new TestClassLoader("test.PojoWithInner", clazz);
+		Class cl = classloader.findClass("test.PojoWithInner");
+		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(clazz.length > getBytesFromFile(new File("target/test-classes/test/PojoWithInner.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));
+
+		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.assertTrue(((Boolean) method.invoke(pojo, new Object[0])).booleanValue());
+
+	}
+
+	public static byte[] getBytesFromFile(File file) throws IOException {
+	    InputStream is = new FileInputStream(file);
+	    long length = file.length();
+	    byte[] bytes = new byte[(int)length];
+
+	    // Read in the bytes
+	    int offset = 0;
+	    int numRead = 0;
+	    while (offset < bytes.length
+	           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
+	        offset += numRead;
+	    }
+
+	    // Ensure all the bytes have been read in
+	    if (offset < bytes.length) {
+	        throw new IOException("Could not completely read file "+file.getName());
+	    }
+
+	    // Close the input stream and return bytes
+	    is.close();
+	    return bytes;
+	}
+
+	class TestClassLoader extends ClassLoader {
+
+		private String name;
+		private byte[] clazz;
+
+		public TestClassLoader(String name, byte[] clazz) {
+			this.name = name;
+			this.clazz = clazz;
+		}
+
+        public Class findClass(String name) throws ClassNotFoundException {
+        	if (name.equals(this.name)) {
+	            return defineClass(name, clazz, 0, clazz.length);
+        	}
+        	return super.findClass(name);
+        }
+
+		public Class loadClass(String arg0) throws ClassNotFoundException {
+			System.out.println("Try to load " + arg0);
+			return super.loadClass(arg0);
+		}
+
+
+
+    }
+
+}

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/PojoizationTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/PojoizationTest.java?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/PojoizationTest.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/org/apache/felix/ipojo/manipulation/PojoizationTest.java
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,43 @@
+package org.apache.felix.ipojo.manipulation;
+
+import java.io.File;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.manipulator.Pojoization;
+
+public class PojoizationTest extends TestCase {
+
+	public void testJarManipulation() {
+		Pojoization pojoization = new Pojoization();
+		File in = new File("target/test-classes/tests.manipulation-no-annotations.jar");
+		File out = new File("target/test-classes/tests.manipulation-no-annotations-manipulated.jar");
+		out.delete();
+		File metadata = new File("target/test-classes/metadata.xml");
+		pojoization.pojoization(in, out, metadata);
+
+		Assert.assertTrue(out.exists());
+	}
+
+	public void testManipulationWithAnnotations() {
+		Pojoization pojoization = new Pojoization();
+		File in = new File("target/test-classes/tests.manipulator-annotations.jar");
+		File out = new File("target/test-classes/tests.manipulation-annotations-manipulated.jar");
+		out.delete();
+		pojoization.pojoization(in, out, (File) null);
+
+		Assert.assertTrue(out.exists());
+	}
+
+	public void testJarManipulationJava5() {
+		Pojoization pojoization = new Pojoization();
+		File in = new File("target/test-classes/tests.manipulation.java5.jar");
+		File out = new File("target/test-classes/tests.manipulation.java5-manipulated.jar");
+		out.delete();
+		pojoization.pojoization(in, out, (File) null);
+
+		Assert.assertTrue(out.exists());
+	}
+
+}

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Child.java
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,17 @@
+package test;
+
+public class Child extends Parent {
+
+	Child(int i, String f) {
+		super(0, f);
+	}
+
+	Child() {
+		super(5, "foo");
+	}
+
+	public int doSomething() {
+		return getIndex() + 1; // 9
+	}
+
+}

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Parent.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Parent.java?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Parent.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/Parent.java
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,15 @@
+package test;
+
+public class Parent {
+
+	private int m_index = 0;
+
+	public Parent(int i, String s) {
+		m_index = i + s.length();
+	}
+
+	public int getIndex() {
+		return m_index;
+	}
+
+}

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/PojoWithInner.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/PojoWithInner.java?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/PojoWithInner.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/PojoWithInner.java
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,21 @@
+package test;
+
+public class PojoWithInner {
+
+	private MyInner m_result = new MyInner();
+
+	// This is a simple POJO
+
+	public boolean doSomething() {
+		return m_result.getInner();
+	}
+
+	public class MyInner {
+
+		public boolean getInner() {
+			return true;
+		}
+
+	}
+
+}

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/SimplePojo.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/SimplePojo.java?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/SimplePojo.java
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/java/test/SimplePojo.java
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,13 @@
+package test;
+
+public class SimplePojo {
+
+	private boolean m_result = true;
+
+	// This is a simple POJO
+
+	public boolean doSomething() {
+		return m_result;
+	}
+
+}

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/metadata.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/metadata.xml?rev=1044497&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/metadata.xml
(added)
+++ felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/metadata.xml
Fri Dec 10 20:03:32 2010
@@ -0,0 +1,37 @@
+<ipojo
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="org.apache.felix.ipojo http://felix.apache.org/ipojo/schemas/SNAPSHOT/core.xsd"
+    xmlns="org.apache.felix.ipojo"
+>
+	<!-- Simple provider  used for manipulation analysis -->
+	<component
+		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+		name="Manipulation-FooProviderType-1" architecture="true">
+		<provides />
+	</component>
+	
+	<!-- Non lazzy service provider, to check instantiation -->
+	<component
+		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+		name="Manipulation-ImmediateFooProviderType" immediate="true"
+		architecture="true">
+		<provides />
+	</component>
+	
+	<!-- Nested & Inner classes -->
+	<component name="inners" classname="org.apache.felix.ipojo.test.scenarios.component.InnerClasses">
+		<provides>
+			<property field="privateObject"/>
+			<property field="privateInt"/>
+			
+			<property field="protectedObject"/>
+			<property field="protectedInt"/>
+			
+			<property field="packageObject"/>
+			<property field="packageInt"/>
+			
+			<property field="publicObject"/>
+			<property field="publicInt"/>
+		</provides>
+	</component>
+</ipojo>

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation-no-annotations.jar
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation-no-annotations.jar?rev=1044497&view=auto
==============================================================================
Binary file - no diff available.

Propchange: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation-no-annotations.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation.java5.jar
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation.java5.jar?rev=1044497&view=auto
==============================================================================
Binary file - no diff available.

Propchange: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulation.java5.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulator-annotations.jar
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulator-annotations.jar?rev=1044497&view=auto
==============================================================================
Binary file - no diff available.

Propchange: felix/sandbox/clement/ipojo-constructor-injection/manipulator/src/test/resources/tests.manipulator-annotations.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message