FELIX-1830 Support for modified property type name "Character" for
character properties

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@831214 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/metadata/PropertyMetadata.java b/scr/src/main/java/org/apache/felix/scr/impl/metadata/PropertyMetadata.java
index c1e7d7c..e2c847d 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/metadata/PropertyMetadata.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/metadata/PropertyMetadata.java
@@ -199,41 +199,66 @@
         {
             throw componentMetadata.validationFailure( "Property name attribute is mandatory" );
         }
+
+        // check character type name
+        if ( componentMetadata.isDS11() && m_type.equals( "Char" ) )
+        {
+            throw componentMetadata
+                .validationFailure( "Illegal property type 'Char' used for DS 1.1 descriptor, use 'Character' instead" );
+        }
+        else if ( !componentMetadata.isDS11() && m_type.equals( "Character" ) )
+        {
+            throw componentMetadata
+                .validationFailure( "Illegal property type 'Character' used for DS 1.0 descriptor, use 'Char' instead" );
+        }
     }
 
-    private Object toType(String value) {
+
+    private Object toType( String value )
+    {
         // 112.4.5 Parsing of the value is done by the valueOf(String) method (P. 291)
         // Should the type accept lowercase too?
-        if(m_type.equals("String")) {
-            return String.valueOf(value);
+        if ( m_type.equals( "String" ) )
+        {
+            return String.valueOf( value );
         }
-        else if(m_type.equals("Long")) {
-            return Long.valueOf(value);
+        else if ( m_type.equals( "Long" ) )
+        {
+            return Long.valueOf( value );
         }
-        else if(m_type.equals("Double")) {
-            return Double.valueOf(value);
+        else if ( m_type.equals( "Double" ) )
+        {
+            return Double.valueOf( value );
         }
-        else if(m_type.equals("Float")) {
-            return Float.valueOf(value);
+        else if ( m_type.equals( "Float" ) )
+        {
+            return Float.valueOf( value );
         }
-        else if(m_type.equals("Integer")) {
-            return Integer.valueOf(value);
+        else if ( m_type.equals( "Integer" ) )
+        {
+            return Integer.valueOf( value );
         }
-        else if(m_type.equals("Byte")) {
-            return Byte.valueOf(value);
+        else if ( m_type.equals( "Byte" ) )
+        {
+            return Byte.valueOf( value );
         }
-        else if(m_type.equals("Char")) {
+        else if ( m_type.equals( "Char" ) || m_type.equals( "Character" ) )
+        {
+            // DS 1.1 changes the "Char" type to "Character", here we support both
             char c = ( value.length() > 0 ) ? value.charAt( 0 ) : 0;
             return new Character( c );
         }
-        else if(m_type.equals("Boolean")) {
-            return Boolean.valueOf(value);
+        else if ( m_type.equals( "Boolean" ) )
+        {
+            return Boolean.valueOf( value );
         }
-        else if(m_type.equals("Short")) {
-            return Short.valueOf(value);
+        else if ( m_type.equals( "Short" ) )
+        {
+            return Short.valueOf( value );
         }
-        else {
-            throw new IllegalArgumentException("Undefined property type '"+m_type+"'");
+        else
+        {
+            throw new IllegalArgumentException( "Undefined property type '" + m_type + "'" );
         }
     }
 }
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/metadata/ComponentMetadataTest.java b/scr/src/test/java/org/apache/felix/scr/impl/metadata/ComponentMetadataTest.java
index 5a1dd86..1c51f68 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/metadata/ComponentMetadataTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/metadata/ComponentMetadataTest.java
@@ -548,6 +548,92 @@
     }
 
 
+    public void test_property_no_name_ds10()
+    {
+        final ComponentMetadata cm = createComponentMetadata( null, null );
+        cm.addProperty( createPropertyMetadata( null, null, "" ) );
+        try
+        {
+            cm.validate( logger );
+            fail( "Expect validation failure for missing property name" );
+        }
+        catch ( ComponentException ce )
+        {
+            // expected
+        }
+    }
+
+
+    public void test_property_no_name_ds11()
+    {
+        final ComponentMetadata cm = createComponentMetadata11( null, null );
+        cm.addProperty( createPropertyMetadata( null, null, "" ) );
+        try
+        {
+            cm.validate( logger );
+            fail( "Expect validation failure for missing property name" );
+        }
+        catch ( ComponentException ce )
+        {
+            // expected
+        }
+    }
+
+
+    public void test_property_char_ds10() throws ComponentException
+    {
+        final ComponentMetadata cm = createComponentMetadata( null, null );
+        PropertyMetadata prop = createPropertyMetadata( "x", "Char", "x" );
+        cm.addProperty( prop );
+        cm.validate( logger );
+        assertTrue( prop.getValue() instanceof Character );
+        assertEquals( new Character( 'x' ), prop.getValue() );
+    }
+
+
+    public void test_property_char_ds11()
+    {
+        final ComponentMetadata cm = createComponentMetadata11( null, null );
+        cm.addProperty( createPropertyMetadata( "x", "Char", "x" ) );
+        try
+        {
+            cm.validate( logger );
+            fail( "Expect validation failure for illegal property type Char" );
+        }
+        catch ( ComponentException ce )
+        {
+            // expected
+        }
+    }
+
+
+    public void test_property_character_ds10()
+    {
+        final ComponentMetadata cm = createComponentMetadata( null, null );
+        cm.addProperty( createPropertyMetadata( "x", "Character", "x" ) );
+        try
+        {
+            cm.validate( logger );
+            fail( "Expect validation failure for illegal property type Character" );
+        }
+        catch ( ComponentException ce )
+        {
+            // expected
+        }
+    }
+
+
+    public void test_property_character_ds11() throws ComponentException
+    {
+        final ComponentMetadata cm = createComponentMetadata11( null, null );
+        PropertyMetadata prop = createPropertyMetadata( "x", "Character", "x" );
+        cm.addProperty( prop );
+        cm.validate( logger );
+        assertTrue( prop.getValue() instanceof Character );
+        assertEquals( new Character( 'x' ), prop.getValue() );
+    }
+
+
     //---------- Helper methods
 
     // Creates DS 1.0 Component Metadata
@@ -605,4 +691,23 @@
         meta.setInterface( "place.holder" );
         return meta;
     }
+
+
+    private PropertyMetadata createPropertyMetadata( String propertyName, String type, String value )
+    {
+        PropertyMetadata meta = new PropertyMetadata();
+        if ( propertyName != null )
+        {
+            meta.setName( propertyName );
+        }
+        if ( type != null )
+        {
+            meta.setType( type );
+        }
+        if ( value != null )
+        {
+            meta.setValue( value );
+        }
+        return meta;
+    }
 }