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/05/08 10:26:13 UTC

svn commit: r942342 - in /felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test: ./ java/ java/org/ java/org/apache/ java/org/apache/felix/ java/org/apache/felix/ipojo/ java/org/apache/felix/ipojo/metatype/ java/org/apache/felix/ipojo/metaty...

Author: clement
Date: Sat May  8 08:26:13 2010
New Revision: 942342

URL: http://svn.apache.org/viewvc?rev=942342&view=rev
Log:
Add tests

Added:
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/bridge/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapperTest.java

Added: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapperTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapperTest.java?rev=942342&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapperTest.java (added)
+++ felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/test/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapperTest.java Sat May  8 08:26:13 2010
@@ -0,0 +1,231 @@
+package org.apache.felix.ipojo.metatype.bridge;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Properties;
+import java.util.Vector;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.HandlerManager;
+import org.apache.felix.ipojo.IPojoContext;
+import org.apache.felix.ipojo.IPojoFactory;
+import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
+import org.apache.felix.ipojo.architecture.PropertyDescription;
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+import org.mockito.Mockito;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.metatype.AttributeDefinition;
+import org.osgi.service.metatype.ObjectClassDefinition;
+
+public class MetatypeFactoryWrapperTest extends TestCase {
+
+    private Element meta;
+    private Bundle bundle;
+    private BundleContext bc;
+
+    public void setUp() {
+        meta = new Element("component", "");
+        meta.addAttribute(new Attribute("public", "true"));
+
+        bc = (BundleContext) Mockito.mock(BundleContext.class);
+        bundle = (Bundle) Mockito.mock(Bundle.class);
+        Mockito.when(bundle.getHeaders()).thenReturn(new Properties());
+        Mockito.when(bc.getBundle()).thenReturn(bundle);
+
+
+    }
+
+    public void testMandatoryProperties() throws ConfigurationException {
+        List list = new ArrayList();
+        PropertyDescription prop = new PropertyDescription("p1", String.class.getName(), null);
+        prop.setMandatory();
+        list.add(prop);
+        PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+         
+        IPojoFactory factory = new MockFactory(props);
+
+
+        MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+        ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+        AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.REQUIRED);
+        Assert.assertNotNull(atts);
+        Assert.assertEquals(1, atts.length);
+        AttributeDefinition p = atts[0];
+        Assert.assertEquals("p1", p.getName());
+        Assert.assertEquals(0, p.getCardinality()); // according to the spec it is 0...
+        Assert.assertEquals(AttributeDefinition.STRING, p.getType());
+        Assert.assertNull(p.getDefaultValue());
+    }
+
+    public void testOptionalProperties() throws ConfigurationException {
+        List list = new ArrayList();
+        PropertyDescription prop = new PropertyDescription("p1", String.class.getName(), null);
+        list.add(prop);
+        PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+         
+        IPojoFactory factory = new MockFactory(props);
+
+
+        MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+        ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+        AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.OPTIONAL);
+        Assert.assertNotNull(atts);
+        Assert.assertEquals(1, atts.length);
+        AttributeDefinition p = atts[0];
+        Assert.assertEquals("p1", p.getName());
+        Assert.assertEquals(0, p.getCardinality()); // according to the spec it is 0...
+        Assert.assertEquals(AttributeDefinition.STRING, p.getType());
+        Assert.assertNull(p.getDefaultValue());
+    }
+
+    public void testOptionalPropertiesWithDefaultValue() throws ConfigurationException {
+       List list = new ArrayList();
+       PropertyDescription prop = new PropertyDescription("p1", String.class.getName(), "v");
+       list.add(prop);
+       PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+        
+       IPojoFactory factory = new MockFactory(props);
+
+        MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+        ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+        AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.OPTIONAL);
+        Assert.assertNotNull(atts);
+        Assert.assertEquals(1, atts.length);
+        AttributeDefinition p = atts[0];
+        Assert.assertEquals("p1", p.getName());
+        Assert.assertEquals(0, p.getCardinality()); // according to the spec it is 0...
+        Assert.assertEquals(AttributeDefinition.STRING, p.getType());
+        Assert.assertEquals("v", p.getDefaultValue()[0]);
+    }
+    
+    public void testImmutable() throws ConfigurationException {
+        List list = new ArrayList();
+        PropertyDescription prop = new PropertyDescription("p1", String.class.getName(), "v", true);
+        list.add(prop);
+        PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+         
+        IPojoFactory factory = new MockFactory(props);
+
+         MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+         ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+         AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.OPTIONAL);
+         Assert.assertNull(atts);
+     }
+    
+    public void testArrays() throws ConfigurationException {
+        List list = new ArrayList();
+        PropertyDescription prop = new PropertyDescription("p1", "java.lang.String[]", "{1,2}");
+        list.add(prop);
+        PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+         
+        IPojoFactory factory = new MockFactory(props);
+
+         MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+         ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+         AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.OPTIONAL);
+         Assert.assertNotNull(atts);
+         Assert.assertEquals(1, atts.length);
+         AttributeDefinition p = atts[0];
+         Assert.assertEquals("p1", p.getName());
+         Assert.assertEquals(Integer.MAX_VALUE, p.getCardinality());
+         Assert.assertEquals(AttributeDefinition.STRING, p.getType());
+         Assert.assertEquals("1", p.getDefaultValue()[0]);
+         Assert.assertEquals("2", p.getDefaultValue()[1]);
+     }
+    
+    public void testMandatoryArrays() throws ConfigurationException {
+        List list = new ArrayList();
+        PropertyDescription prop = new PropertyDescription("p1", "java.lang.String[]", "{1,2}");
+        list.add(prop);
+        prop.setMandatory();
+        PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+         
+        IPojoFactory factory = new MockFactory(props);
+
+         MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+         ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+         AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.REQUIRED);
+         Assert.assertNotNull(atts);
+         Assert.assertEquals(1, atts.length);
+         AttributeDefinition p = atts[0];
+         Assert.assertEquals("p1", p.getName());
+         Assert.assertEquals(Integer.MAX_VALUE, p.getCardinality());
+         Assert.assertEquals(AttributeDefinition.STRING, p.getType());
+         Assert.assertEquals("1", p.getDefaultValue()[0]);
+         Assert.assertEquals("2", p.getDefaultValue()[1]);
+     }
+    
+    public void testVectors() throws ConfigurationException {
+        List list = new ArrayList();
+        PropertyDescription prop = new PropertyDescription("p1", Vector.class.getName(), "{1,2}");
+        list.add(prop);
+        PropertyDescription[] props =  (PropertyDescription[]) list.toArray(new PropertyDescription[0]);
+         
+        IPojoFactory factory = new MockFactory(props);
+
+         MetatypeFactoryWrapper wrapper = new MetatypeFactoryWrapper(factory);
+         ObjectClassDefinition def = wrapper.getObjectClassDefinition("name", null);
+         AttributeDefinition[] atts = def.getAttributeDefinitions(ObjectClassDefinition.OPTIONAL);
+         Assert.assertNotNull(atts);
+         Assert.assertEquals(1, atts.length);
+         AttributeDefinition p = atts[0];
+         Assert.assertEquals("p1", p.getName());
+         Assert.assertEquals(Integer.MIN_VALUE, p.getCardinality());
+         Assert.assertEquals(AttributeDefinition.STRING, p.getType());
+         Assert.assertEquals("1", p.getDefaultValue()[0]);
+         Assert.assertEquals("2", p.getDefaultValue()[1]);
+     }
+
+
+    private class MockFactory extends IPojoFactory {
+        private PropertyDescription[] m_props;
+        
+        public MockFactory(PropertyDescription[] props)
+                throws ConfigurationException {
+            super(bc, meta);
+            m_props = props;
+        }
+
+        public ComponentInstance createInstance(Dictionary config,
+                IPojoContext context, HandlerManager[] handlers)
+                throws ConfigurationException {
+            return null;
+        }
+
+        public String getClassName() {
+            return null;
+        }
+
+        public String getFactoryName() {
+            return "name";
+        }
+
+        public List getRequiredHandlerList() { return null; }
+
+        public void starting() {   }
+
+        public void stopping() {   }
+
+        public String getVersion() {     return null;    }
+
+        public synchronized ComponentTypeDescription getComponentDescription() {
+
+            return new ComponentTypeDescription(this) {
+
+                public PropertyDescription[] getProperties() {
+                    return m_props;
+                }
+
+            };
+        }
+
+    }
+
+}