You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2010/12/04 13:56:43 UTC

svn commit: r1042164 - /felix/trunk/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java

Author: clement
Date: Sat Dec  4 12:56:43 2010
New Revision: 1042164

URL: http://svn.apache.org/viewvc?rev=1042164&view=rev
Log:
Add a test checking that we don't create proxies for abstract and concrete classes.

Modified:
    felix/trunk/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java

Modified: felix/trunk/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java?rev=1042164&r1=1042163&r2=1042164&view=diff
==============================================================================
--- felix/trunk/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java (original)
+++ felix/trunk/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java Sat Dec  4 12:56:43 2010
@@ -1,5 +1,6 @@
 package org.apache.felix.ipojo.handlers.dependency;
 
+import java.awt.Window;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -23,114 +24,69 @@ import org.osgi.framework.ServiceReferen
 
 public class SmartProxyTest extends TestCase {
 
-	private Dependency dependency;
-
-
 	public void setUp() {
 	}
 
 
 	/**
-	 * Tests if we can proxies classes from java.* package.
-	 * Indeed, a recent JVM bug fix introduces a bug:
-	 * <code>
-	 * [ERROR] test : Cannot create the proxy object
-	 * java.lang.SecurityException: Prohibited package name: java.awt
-	 * </code>
+	 * Check that we don't create smart proxies for concrete and abstract classes.
 	 */
-	public void testProxiesOfJavaClasses() {
-		Bundle bundle = new Bundle() {
-
-			public int getState() {
-				return 0;
-			}
-
-			public void start() throws BundleException {
-			}
-
-			public void stop() throws BundleException {
-			}
-
-			public void update() throws BundleException {
-			}
-
-			public void update(InputStream in) throws BundleException {
-			}
+	public void testCannotProxyAbstractAndContreteClasses() {
+		Bundle bundle = new BundleImplementingLoading();
 
-			public void uninstall() throws BundleException {
-			}
-
-			public Dictionary getHeaders() {
-				return null;
-			}
-
-			public long getBundleId() {
-				return 0;
-			}
-
-			public String getLocation() {
-				return null;
-			}
-
-			public ServiceReference[] getRegisteredServices() {
-				return null;
-			}
-
-			public ServiceReference[] getServicesInUse() {
-				return null;
-			}
-
-			public boolean hasPermission(Object permission) {
-				return false;
-			}
-
-			public URL getResource(String name) {
-				return null;
-			}
+		BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
+		Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
+		Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
+		Mockito.when(context.getBundle()).thenReturn(bundle);
 
-			public Dictionary getHeaders(String locale) {
-				return null;
-			}
+		ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
+		Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
 
-			public String getSymbolicName() {
-				return null;
-			}
+		InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
+		Mockito.when(im.getContext()).thenReturn(context);
+		Mockito.when(im.getFactory()).thenReturn(factory);
 
-			public Class loadClass(String name) throws ClassNotFoundException {
-				return Dependency.class.getClassLoader().loadClass(name);
-			}
+		DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
+		Mockito.when(handler.getInstanceManager()).thenReturn(im);
+		Logger logger = new Logger(context, "test", Logger.INFO);
 
-			public Enumeration getResources(String name) throws IOException {
-				return null;
-			}
 
-			public Enumeration getEntryPaths(String path) {
-				return null;
-			}
+		Mockito.when(handler.getLogger()).thenReturn(logger);
 
-			public URL getEntry(String name) {
-				return null;
-			}
+		// Try with java.List
+		Dependency dependency = new Dependency(handler, "a_field", Window.class, null, false, false, false,
+				true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
+		dependency.start();
 
-			public long getLastModified() {
-				return 0;
-			}
+		// No service
+		Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
 
-			public Enumeration findEntries(String path, String filePattern,
-					boolean recurse) {
-				return null;
-			}
+		dependency.stop();
 
-		};
+		// Try with javax.swing.Action
+		dependency = new Dependency(handler, "a_field", Object.class, null, false, false, false,
+				true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
+		dependency.start();
+		// OK
+		Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
+	}
 
+	/**
+	 * Tests if we can proxies classes from java.* package.
+	 * Indeed, a recent JVM bug fix introduces a bug:
+	 * <code>
+	 * [ERROR] test : Cannot create the proxy object
+	 * java.lang.SecurityException: Prohibited package name: java.awt
+	 * </code>
+	 */
+	public void testProxiesOfJavaClasses() {
+		Bundle bundle = new BundleImplementingLoading();
 
 		BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
 		Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
 		Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
 		Mockito.when(context.getBundle()).thenReturn(bundle);
 
-		//getBundle().loadClass(name);
-
 		ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
 		Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
 
@@ -146,7 +102,7 @@ public class SmartProxyTest extends Test
 		Mockito.when(handler.getLogger()).thenReturn(logger);
 
 		// Try with java.List
-		dependency = new Dependency(handler, "a_field", List.class, null, false, false, false,
+		Dependency dependency = new Dependency(handler, "a_field", List.class, null, false, false, false,
 				true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
 		dependency.start();
 
@@ -165,4 +121,86 @@ public class SmartProxyTest extends Test
 		Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof Action);
 	}
 
+	private class BundleImplementingLoading implements Bundle {
+		public int getState() {
+			return 0;
+		}
+
+		public void start() throws BundleException {
+		}
+
+		public void stop() throws BundleException {
+		}
+
+		public void update() throws BundleException {
+		}
+
+		public void update(InputStream in) throws BundleException {
+		}
+
+		public void uninstall() throws BundleException {
+		}
+
+		public Dictionary getHeaders() {
+			return null;
+		}
+
+		public long getBundleId() {
+			return 0;
+		}
+
+		public String getLocation() {
+			return null;
+		}
+
+		public ServiceReference[] getRegisteredServices() {
+			return null;
+		}
+
+		public ServiceReference[] getServicesInUse() {
+			return null;
+		}
+
+		public boolean hasPermission(Object permission) {
+			return false;
+		}
+
+		public URL getResource(String name) {
+			return null;
+		}
+
+		public Dictionary getHeaders(String locale) {
+			return null;
+		}
+
+		public String getSymbolicName() {
+			return null;
+		}
+
+		public Class loadClass(String name) throws ClassNotFoundException {
+			return Dependency.class.getClassLoader().loadClass(name);
+		}
+
+		public Enumeration getResources(String name) throws IOException {
+			return null;
+		}
+
+		public Enumeration getEntryPaths(String path) {
+			return null;
+		}
+
+		public URL getEntry(String name) {
+			return null;
+		}
+
+		public long getLastModified() {
+			return 0;
+		}
+
+		public Enumeration findEntries(String path, String filePattern,
+				boolean recurse) {
+			return null;
+		}
+	}
+
 }