Refactored tests
Added tests about FELIX-3009
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1142472 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java
index 9028b12..f48375e 100644
--- a/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java
+++ b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/SmartProxyTest.java
@@ -1,11 +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;
-import java.util.Dictionary;
-import java.util.Enumeration;
import java.util.List;
import javax.swing.Action;
@@ -15,192 +10,109 @@
import org.apache.felix.ipojo.ComponentFactory;
import org.apache.felix.ipojo.InstanceManager;
+import org.apache.felix.ipojo.test.MockBundle;
import org.apache.felix.ipojo.util.Logger;
import org.mockito.Mockito;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.ServiceReference;
public class SmartProxyTest extends TestCase {
- public void setUp() {
- }
+ public void setUp() {
+ }
- /**
- * Check that we don't create smart proxies for concrete and abstract classes.
- */
- public void testCannotProxyAbstractAndContreteClasses() {
- Bundle bundle = new BundleImplementingLoading();
+ /**
+ * Check that we don't create smart proxies for concrete and abstract classes.
+ */
+ public void testCannotProxyAbstractAndContreteClasses() {
+ Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
- 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);
+ 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);
- ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
- Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
+ ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
+ Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
- InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
- Mockito.when(im.getContext()).thenReturn(context);
- Mockito.when(im.getFactory()).thenReturn(factory);
+ InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
+ Mockito.when(im.getContext()).thenReturn(context);
+ Mockito.when(im.getFactory()).thenReturn(factory);
- DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
- Mockito.when(handler.getInstanceManager()).thenReturn(im);
- Logger logger = new Logger(context, "test", Logger.INFO);
+ DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
+ Mockito.when(handler.getInstanceManager()).thenReturn(im);
+ Logger logger = new Logger(context, "test", Logger.INFO);
- Mockito.when(handler.getLogger()).thenReturn(logger);
+ Mockito.when(handler.getLogger()).thenReturn(logger);
- // 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();
+ // 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();
- // No service
- Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
+ // No service
+ Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
- dependency.stop();
+ 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));
- }
+ // 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();
+ /**
+ * 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 MockBundle(Dependency.class.getClassLoader());
- 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);
+ 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);
- ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
- Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
+ ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
+ Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
- InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
- Mockito.when(im.getContext()).thenReturn(context);
- Mockito.when(im.getFactory()).thenReturn(factory);
+ InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
+ Mockito.when(im.getContext()).thenReturn(context);
+ Mockito.when(im.getFactory()).thenReturn(factory);
- DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
- Mockito.when(handler.getInstanceManager()).thenReturn(im);
- Logger logger = new Logger(context, "test", Logger.INFO);
+ DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
+ Mockito.when(handler.getInstanceManager()).thenReturn(im);
+ Logger logger = new Logger(context, "test", Logger.INFO);
- Mockito.when(handler.getLogger()).thenReturn(logger);
+ Mockito.when(handler.getLogger()).thenReturn(logger);
- // Try with java.List
- Dependency dependency = new Dependency(handler, "a_field", List.class, null, false, false, false,
- true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
- dependency.start();
+ // Try with java.List
+ Dependency dependency = new Dependency(handler, "a_field", List.class, null, false, false, false,
+ true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
+ dependency.start();
- // OK
- Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
- Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof List);
+ // OK
+ Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
+ Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof List);
- dependency.stop();
+ dependency.stop();
- // Try with javax.swing.Action
- dependency = new Dependency(handler, "a_field", Action.class, null, false, false, false,
- true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
- dependency.start();
- // OK
- Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
- 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;
- }
- }
+ // Try with javax.swing.Action
+ dependency = new Dependency(handler, "a_field", Action.class, null, false, false, false,
+ true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null);
+ dependency.start();
+ // OK
+ Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
+ Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof Action);
+ }
}
diff --git a/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ComponentTestWithAnotherSuperClass.java b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ComponentTestWithAnotherSuperClass.java
new file mode 100644
index 0000000..7bc4996
--- /dev/null
+++ b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ComponentTestWithAnotherSuperClass.java
@@ -0,0 +1,12 @@
+package org.apache.felix.ipojo.handlers.providedservice;
+
+import java.beans.MethodDescriptor;
+import java.lang.reflect.Method;
+
+public class ComponentTestWithAnotherSuperClass extends MethodDescriptor {
+
+ public ComponentTestWithAnotherSuperClass(Method method) {
+ super(method);
+ }
+
+}
diff --git a/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ComponentTestWithSuperClass.java b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ComponentTestWithSuperClass.java
new file mode 100644
index 0000000..ab61262
--- /dev/null
+++ b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ComponentTestWithSuperClass.java
@@ -0,0 +1,7 @@
+package org.apache.felix.ipojo.handlers.providedservice;
+
+import java.beans.SimpleBeanInfo;
+
+public class ComponentTestWithSuperClass extends SimpleBeanInfo {
+
+}
diff --git a/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandlerTest.java b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandlerTest.java
new file mode 100644
index 0000000..164f7ec
--- /dev/null
+++ b/ipojo/core/src/test/java/org/apache/felix/ipojo/handlers/providedservice/ProvidedServiceHandlerTest.java
@@ -0,0 +1,133 @@
+package org.apache.felix.ipojo.handlers.providedservice;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.InstanceManager;
+import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
+import org.apache.felix.ipojo.handlers.dependency.DependencyHandler;
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+import org.apache.felix.ipojo.parser.PojoMetadata;
+import org.apache.felix.ipojo.test.MockBundle;
+import org.apache.felix.ipojo.util.Logger;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+
+public class ProvidedServiceHandlerTest extends TestCase {
+
+ BundleContext context;
+ ComponentFactory factory;
+ InstanceManager im;
+ ComponentTypeDescription desc;
+ ProvidedServiceHandler handler;
+
+ public void setUp() {
+ 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(new MockBundle(this.getClass().getClassLoader()));
+
+ factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
+ Mockito.when(factory.getBundleClassLoader()).thenReturn(ProvidedServiceHandler.class.getClassLoader());
+ Mockito.when(factory.getLogger()).thenReturn(new Logger(context, "TEST", Logger.INFO));
+
+ im = (InstanceManager) Mockito.mock(InstanceManager.class);
+ Mockito.when(im.getContext()).thenReturn(context);
+ Mockito.when(im.getFactory()).thenReturn(factory);
+
+ desc = (ComponentTypeDescription) Mockito.mock(ComponentTypeDescription.class);
+ Mockito.when(desc.getFactory()).thenReturn(factory);
+ Mockito.when(desc.getBundleContext()).thenReturn(context);
+
+ handler = new ProvidedServiceHandler();
+ handler.setFactory(factory);
+ }
+
+ public void testServiceDetectionNoInterface() throws ConfigurationException {
+ String classname = "org.apache.felix.ipojo.handlers.providedservice.ComponentTest";
+
+ Element metadata = new Element("component", "");
+ Element manipulation = new Element("manipulation", "");
+ metadata.addAttribute(new Attribute("classname", classname));
+ metadata.addElement(new Element("provides", ""));
+ metadata.addElement(manipulation);
+ manipulation.addAttribute(new Attribute("classname", classname));
+
+ Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
+ Mockito.when(factory.getClassName()).thenReturn(classname);
+
+ handler.initializeComponentFactory(desc, metadata);
+
+ //Expected behavior: the implementation classname
+ Assert.assertEquals("{org.apache.felix.ipojo.handlers.providedservice.ComponentTest}",
+ metadata.getElements("provides")[0].getAttribute("specifications"));
+ }
+
+ public void testServiceDetectionSuperClass() throws ConfigurationException {
+ String classname = "org.apache.felix.ipojo.handlers.providedservice.ComponentTestWithSuperClass";
+
+ Element metadata = new Element("component", "");
+ Element manipulation = new Element("manipulation", "");
+ metadata.addAttribute(new Attribute("classname", classname));
+ Element provides = new Element("provides", "");
+ provides.addAttribute(new Attribute("specifications", "java.beans.SimpleBeanInfo"));
+ metadata.addElement(provides);
+ metadata.addElement(manipulation);
+ manipulation.addAttribute(new Attribute("classname", classname));
+ manipulation.addAttribute(new Attribute("super", "java.beans.SimpleBeanInfo"));
+ Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
+ Mockito.when(factory.getClassName()).thenReturn(classname);
+
+ handler.initializeComponentFactory(desc, metadata);
+
+ System.out.println(metadata);
+
+ }
+
+ public void testServiceDetectionImplementationClass() throws ConfigurationException {
+ String classname = "org.apache.felix.ipojo.handlers.providedservice.ComponentTestWithSuperClass";
+
+ Element metadata = new Element("component", "");
+ Element manipulation = new Element("manipulation", "");
+ metadata.addAttribute(new Attribute("classname", classname));
+ Element provides = new Element("provides", "");
+ provides.addAttribute(new Attribute("specifications", classname));
+ metadata.addElement(provides);
+ metadata.addElement(manipulation);
+ manipulation.addAttribute(new Attribute("classname", classname));
+ manipulation.addAttribute(new Attribute("super", "java.beans.SimpleBeanInfo"));
+ Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
+ Mockito.when(factory.getClassName()).thenReturn(classname);
+
+ handler.initializeComponentFactory(desc, metadata);
+
+ System.out.println(metadata);
+
+ }
+
+ public void testServiceDetectionSuperSuperClass() throws ConfigurationException {
+ String classname = "org.apache.felix.ipojo.handlers.providedservice.ComponentTestWithAnotherSuperClass";
+
+ Element metadata = new Element("component", "");
+ Element manipulation = new Element("manipulation", "");
+ metadata.addAttribute(new Attribute("classname", classname));
+ Element provides = new Element("provides", "");
+ provides.addAttribute(new Attribute("specifications", "java.beans.FeatureDescriptor"));
+ metadata.addElement(provides);
+ metadata.addElement(manipulation);
+ manipulation.addAttribute(new Attribute("classname", classname));
+ manipulation.addAttribute(new Attribute("super", "java.beans.MethodDescriptor"));
+
+ Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
+ Mockito.when(factory.getClassName()).thenReturn(classname);
+
+ handler.initializeComponentFactory(desc, metadata);
+
+ System.out.println(metadata);
+
+ }
+
+}
diff --git a/ipojo/core/src/test/java/org/apache/felix/ipojo/test/MockBundle.java b/ipojo/core/src/test/java/org/apache/felix/ipojo/test/MockBundle.java
new file mode 100644
index 0000000..bfc6a51
--- /dev/null
+++ b/ipojo/core/src/test/java/org/apache/felix/ipojo/test/MockBundle.java
@@ -0,0 +1,103 @@
+package org.apache.felix.ipojo.test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+
+public class MockBundle implements Bundle {
+
+ private final ClassLoader m_classloader;
+
+ /**
+ * @param classloader the Classloader to load classes and resources.
+ */
+ public MockBundle(ClassLoader cl) {
+ m_classloader = cl;
+ }
+
+ 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 1; // 0 is the system bundle
+ }
+
+ 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 m_classloader.getResource(name);
+ }
+
+ public Dictionary getHeaders(String locale) {
+ return null;
+ }
+
+ public String getSymbolicName() {
+ return null;
+ }
+
+ public Class loadClass(String name) throws ClassNotFoundException {
+ return m_classloader.loadClass(name);
+ }
+
+ public Enumeration getResources(String name) throws IOException {
+ return m_classloader.getResources(name);
+ }
+
+ 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;
+ }
+}
\ No newline at end of file