FELIX-2519 Specification of activate, deactivate, modified, and configuration-policy attributes is not allowed for DS 1.0 descriptors.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@982591 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/metadata/ComponentMetadata.java b/scr/src/main/java/org/apache/felix/scr/impl/metadata/ComponentMetadata.java
index 0676efd..a2ded08 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/metadata/ComponentMetadata.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/metadata/ComponentMetadata.java
@@ -657,13 +657,11 @@
         }
         else if ( m_namespaceCode < XmlHandler.DS_VERSION_1_1 )
         {
-            logger.log( LogService.LOG_WARNING, "Ignoring configuration policy, DS 1.1 or later namespace required",
-                this, null );
-            m_configurationPolicy = CONFIGURATION_POLICY_OPTIONAL;
+            throw validationFailure( "configuration-policy declaration requires DS 1.1 or later namespace " );
         }
         else if ( !CONFIGURATION_POLICY_VALID.contains( m_configurationPolicy ) )
         {
-            throw validationFailure( "Configuration policy must be one of " + CONFIGURATION_POLICY_VALID );
+            throw validationFailure( "configuration-policy must be one of " + CONFIGURATION_POLICY_VALID );
         }
 
         // 112.5.8 activate can be specified (since DS 1.1)
@@ -674,11 +672,7 @@
         }
         else if ( m_namespaceCode < XmlHandler.DS_VERSION_1_1 )
         {
-            // DS 1.0 cannot declare the activate method, assume default and undeclared
-            logger.log( LogService.LOG_WARNING,
-                "Ignoring activate method declaration, DS 1.1 or later namespace required", this, null );
-            m_activate = "activate";
-            m_activateDeclared = false;
+            throw validationFailure( "activate method declaration requires DS 1.1 or later namespace " );
         }
 
         // 112.5.12 deactivate can be specified (since DS 1.1)
@@ -689,20 +683,13 @@
         }
         else if ( m_namespaceCode < XmlHandler.DS_VERSION_1_1 )
         {
-            // DS 1.0 cannot declare the deactivate method, assume default and undeclared
-            logger.log( LogService.LOG_WARNING,
-                "Ignoring deactivate method declaration, DS 1.1 or later namespace required", this, null );
-            m_deactivate = "deactivate";
-            m_deactivateDeclared = false;
+            throw validationFailure( "deactivate method declaration requires DS 1.1 or later namespace " );
         }
 
         // 112.??.?? modified can be specified (since DS 1.1)
         if ( m_modified != null && m_namespaceCode < XmlHandler.DS_VERSION_1_1 )
         {
-            // require new namespace if modified is specified
-            logger.log( LogService.LOG_WARNING,
-                "Ignoring modified method declaration, DS 1.1 or later namespace required", this, null );
-            m_modified = null;
+            throw validationFailure( "modified method declaration requires DS 1.1 or later namespace " );
         }
 
         // Next check if the properties are valid (and extract property values)
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 ea86bd3..f708538 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
@@ -279,9 +279,7 @@
 
         final ComponentMetadata cm2 = createComponentMetadata( Boolean.TRUE, null );
         cm2.setActivate( "someMethod" );
-        cm2.validate( logger );
-        assertEquals( "Activate method name", "activate", cm2.getActivate() );
-        assertFalse( "Activate method expected to not be declared", cm2.isActivateDeclared() );
+        failDS10Validation( cm2, "activate", logger );
     }
 
 
@@ -309,9 +307,7 @@
 
         final ComponentMetadata cm2 = createComponentMetadata( Boolean.TRUE, null );
         cm2.setDeactivate( "someMethod" );
-        cm2.validate( logger );
-        assertEquals( "Deactivate method name", "deactivate", cm2.getDeactivate() );
-        assertFalse( "Deactivate method expected to not be declared", cm2.isDeactivateDeclared() );
+        failDS10Validation( cm2, "deactivate", logger );
     }
 
 
@@ -338,8 +334,7 @@
 
         final ComponentMetadata cm2 = createComponentMetadata( Boolean.TRUE, null );
         cm2.setModified( "someName" );
-        cm2.validate( logger );
-        assertNull( "Modified method name", cm2.getModified() );
+        failDS10Validation( cm2, "modified", logger );
     }
 
 
@@ -365,27 +360,19 @@
 
         final ComponentMetadata cm2 = createComponentMetadata( Boolean.TRUE, null );
         cm2.setConfigurationPolicy( ComponentMetadata.CONFIGURATION_POLICY_IGNORE );
-        cm2.validate( logger );
-        assertEquals( "Configuration policy", ComponentMetadata.CONFIGURATION_POLICY_OPTIONAL, cm2
-            .getConfigurationPolicy() );
+        failDS10Validation( cm2, "configuration-policy", logger );
 
         final ComponentMetadata cm3 = createComponentMetadata( Boolean.TRUE, null );
         cm3.setConfigurationPolicy( ComponentMetadata.CONFIGURATION_POLICY_OPTIONAL );
-        cm3.validate( logger );
-        assertEquals( "Configuration policy", ComponentMetadata.CONFIGURATION_POLICY_OPTIONAL, cm3
-            .getConfigurationPolicy() );
+        failDS10Validation( cm3, "configuration-policy", logger );
 
         final ComponentMetadata cm4 = createComponentMetadata( Boolean.TRUE, null );
         cm4.setConfigurationPolicy( ComponentMetadata.CONFIGURATION_POLICY_REQUIRE );
-        cm4.validate( logger );
-        assertEquals( "Configuration policy", ComponentMetadata.CONFIGURATION_POLICY_OPTIONAL, cm4
-            .getConfigurationPolicy() );
+        failDS10Validation( cm4, "configuration-policy", logger );
 
         final ComponentMetadata cm5 = createComponentMetadata( Boolean.TRUE, null );
         cm5.setConfigurationPolicy( "undefined" );
-        cm5.validate( logger );
-        assertEquals( "Configuration policy", ComponentMetadata.CONFIGURATION_POLICY_OPTIONAL, cm5
-            .getConfigurationPolicy() );
+        failDS10Validation( cm5, "configuration-policy", logger );
     }
 
 
@@ -681,6 +668,23 @@
 
     //---------- Helper methods
 
+    // method also used by XmlHandlerTest
+    static void failDS10Validation( final ComponentMetadata metadata, final String expectedValidationReason,
+        final MockLogger logger )
+    {
+        try
+        {
+            metadata.validate( logger );
+            fail( "Expected validation failure for Component " + metadata.getName() + " containing '"
+                + expectedValidationReason + "'" );
+        }
+        catch ( ComponentException ce )
+        {
+            assertTrue( "Expected validation reason to contain '" + expectedValidationReason + "': actual: "
+                + ce.getMessage(), ce.getMessage().indexOf( expectedValidationReason ) >= 0 );
+        }
+    }
+
     // Creates Component Metadata for the given namespace
     private ComponentMetadata createComponentMetadata( int nameSpaceCode, Boolean immediate, String factory )
     {
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/metadata/XmlHandlerTest.java b/scr/src/test/java/org/apache/felix/scr/impl/metadata/XmlHandlerTest.java
index 8a06123..1647a9e 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/metadata/XmlHandlerTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/metadata/XmlHandlerTest.java
@@ -60,19 +60,12 @@
     public void test_component_attributes_11() throws Exception
     {
         final List metadataList10 = readMetadata( "/components_activate_10.xml" );
-        assertEquals( "Component Descriptors", 1, metadataList10.size() );
-
-        final ComponentMetadata cm10 = ( ComponentMetadata ) metadataList10.get( 0 );
-        cm10.validate( logger );
-        assertEquals( "DS Version 1.0", XmlHandler.DS_VERSION_1_0, cm10.getNamespaceCode() );
-        assertFalse( "DS Version 1.0", cm10.isDS11() );
-        assertEquals( "Expected Activate Method not set", "activate", cm10.getActivate() );
-        assertFalse( "Activate method expected to not be declared", cm10.isActivateDeclared() );
-        assertEquals( "Expected Deactivate Method not set", "deactivate", cm10.getDeactivate() );
-        assertFalse( "Deactivate method expected to not be declared", cm10.isDeactivateDeclared() );
-        assertNull( "Expected Modified Method not set", cm10.getModified() );
-        assertEquals( "Expected Configuration Policy not set", ComponentMetadata.CONFIGURATION_POLICY_OPTIONAL, cm10
-            .getConfigurationPolicy() );
+        assertEquals( "Component Descriptors", 4, metadataList10.size() );
+        ComponentMetadataTest.failDS10Validation( ( ComponentMetadata ) metadataList10.get( 0 ), "activate", logger );
+        ComponentMetadataTest.failDS10Validation( ( ComponentMetadata ) metadataList10.get( 1 ), "deactivate", logger );
+        ComponentMetadataTest.failDS10Validation( ( ComponentMetadata ) metadataList10.get( 2 ), "modified", logger );
+        ComponentMetadataTest.failDS10Validation( ( ComponentMetadata ) metadataList10.get( 3 ),
+            "configuration-policy", logger );
 
         final List metadataList11 = readMetadata( "/components_activate_11.xml" );
         assertEquals( "Component Descriptors", 1, metadataList11.size() );
diff --git a/scr/src/test/resources/components_activate_10.xml b/scr/src/test/resources/components_activate_10.xml
index 3da1344..864204a 100644
--- a/scr/src/test/resources/components_activate_10.xml
+++ b/scr/src/test/resources/components_activate_10.xml
@@ -1,26 +1,39 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!--
-    Licensed to the Apache Software Foundation (ASF) under one
-    or more contributor license agreements.  See the NOTICE file
-    distributed with this work for additional information
-    regarding copyright ownership.  The ASF licenses this file
-    to you under the Apache License, Version 2.0 (the
-    "License"); you may not use this file except in compliance
-    with the License.  You may obtain a copy of the License at
-    
-        http://www.apache.org/licenses/LICENSE-2.0
-    
-    Unless required by applicable law or agreed to in writing,
-    software distributed under the License is distributed on an
-    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-    KIND, either express or implied.  See the License for the
-    specific language governing permissions and limitations
-    under the License.
--->
+    <!--
+        Licensed to the Apache Software Foundation (ASF) under one or
+        more contributor license agreements. See the NOTICE file
+        distributed with this work for additional information regarding
+        copyright ownership. The ASF licenses this file to you under the
+        Apache License, Version 2.0 (the "License"); you may not use
+        this file except in compliance with the License. You may obtain
+        a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+        applicable law or agreed to in writing, software distributed
+        under the License is distributed on an "AS IS" BASIS, WITHOUT
+        WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+        See the License for the specific language governing permissions
+        and limitations under the License.
+    -->
 <components>
-    <scr:component name="components.activate.10"
-        activate="myactivate" deactivate="mydeactivate" modified="mymodified"
-        configuration-policy="ignore" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
+    <scr:component name="components.activate.10" activate="myactivate"
+        xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"
+    >
+        <implementation class="components.activate.10" />
+    </scr:component>
+    <scr:component name="components.deactivate.10"
+        deactivate="mydeactivate" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"
+    >
+        <implementation class="components.activate.10" />
+    </scr:component>
+    <scr:component name="components.modified.10" modified="mymodified"
+        xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"
+    >
+        <implementation class="components.activate.10" />
+    </scr:component>
+    <scr:component name="components.configuration.policy.10"
+        configuration-policy="ignore" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"
+    >
         <implementation class="components.activate.10" />
     </scr:component>
 </components>