FELIX-3971 tests for configuration change effects

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1457986 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ConfigurationChangeTest.java b/scr/src/test/java/org/apache/felix/scr/integration/ConfigurationChangeTest.java
new file mode 100644
index 0000000..407aeef
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ConfigurationChangeTest.java
@@ -0,0 +1,208 @@
+/*
+ * 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.
+ */
+package org.apache.felix.scr.integration;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.scr.Component;
+import org.apache.felix.scr.integration.components.SimpleComponent;
+import org.apache.felix.scr.integration.components.SimpleServiceImpl;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+
+@RunWith(JUnit4TestRunner.class)
+public class ConfigurationChangeTest extends ComponentTestBase
+{
+    static
+    {
+        // uncomment to enable debugging of this test class
+//         paxRunnerVmOption = DEBUG_VM_OPTION;
+
+        descriptorFile = "/integration_test_simple_components_configuration_change.xml";
+    }
+/*
+    @Test
+    public void test_optional_single_dynamic()
+    {
+        String pid = "test_optional_single_dynamic";
+        singleTest( pid, true );
+    }
+
+    @Test
+    public void test_required_single_dynamic()
+    {
+        String pid = "test_required_single_dynamic";
+        singleTest( pid, true );
+    }
+
+    @Test
+    public void test_optional_single_static()
+    {
+        String pid = "test_optional_single_static";
+        singleTest( pid, false );
+    }
+
+    @Test
+    public void test_required_single_static()
+    {
+        String pid = "test_required_single_static";
+        singleTest( pid, false );
+    }
+
+    @Test
+    public void test_optional_single_dynamic_greedy()
+    {
+        String pid = "test_optional_single_dynamic_greedy";
+        singleTest( pid, true );
+    }
+
+    @Test
+    public void test_required_single_dynamic_greedy()
+    {
+        String pid = "test_required_single_dynamic_greedy";
+        singleTest( pid, true );
+    }
+
+    @Test
+    public void test_optional_single_static_greedy()
+    {
+        String pid = "test_optional_single_static_greedy";
+        singleTest( pid, false );
+    }
+
+    @Test
+    public void test_required_single_static_greedy()
+    {
+        String pid = "test_required_single_static_greedy";
+        singleTest( pid, false );
+    }
+*/
+    private void singleTest(String pid, boolean dynamic)
+    {
+        final Component component = findComponentByName( pid );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+
+        theConfig.put("ref.target", "(value=srv1)");
+        configure( pid );
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertEquals( srv1, comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 0, comp10.m_singleRefUnbind);
+
+        // update configuration to target srv2
+        theConfig.put("ref.target", "(value=srv2)");
+        configure( pid );
+
+        delay();
+        // should bind to srv2
+        SimpleComponent comp20;
+        if ( dynamic )
+        {
+            TestCase.assertEquals( 1, comp10.m_modified );
+            comp20 = comp10;
+            TestCase.assertEquals( 2, comp20.m_singleRefBind );
+            TestCase.assertEquals( 1, comp20.m_singleRefUnbind);
+        } 
+        else
+        {
+            TestCase.assertEquals( 0, comp10.m_modified );
+            comp20 = SimpleComponent.INSTANCE;
+            TestCase.assertNotSame( comp10, comp20 );
+            TestCase.assertEquals( 0, comp20.m_modified );
+            TestCase.assertEquals( 1, comp20.m_singleRefBind );
+            TestCase.assertEquals( 0, comp20.m_singleRefUnbind);
+            TestCase.assertEquals( 1, comp10.m_singleRefUnbind);
+        }
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertEquals( srv2, comp20.m_singleRef );
+        TestCase.assertTrue( comp20.m_multiRef.isEmpty() );
+    }
+
+    @Test
+    public void test_optional_multiple_dynamic()
+    {
+        String pid = "test_optional_multiple_dynamic";
+        multipleTest( pid, true );
+    }
+
+    private void multipleTest(String pid, boolean dynamic)
+    {
+        final Component component = findComponentByName( pid );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+
+        theConfig.put("ref.target", "(value=srv1)");
+        configure( pid );
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertEquals( 1, comp10.m_multiRef.size() );
+        TestCase.assertEquals( srv1, comp10.m_multiRef.iterator().next() );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // update configuration to target srv2
+        theConfig.put("ref.target", "(value=srv2)");
+        configure( pid );
+
+        delay();
+        // should bind to srv2
+        SimpleComponent comp20;
+        if ( dynamic )
+        {
+            TestCase.assertEquals( 1, comp10.m_modified );
+            comp20 = comp10;
+            TestCase.assertEquals( 2, comp20.m_multiRefBind );
+            TestCase.assertEquals( 1, comp20.m_multiRefUnbind);
+        } 
+        else
+        {
+            TestCase.assertEquals( 0, comp10.m_modified );
+            comp20 = SimpleComponent.INSTANCE;
+            TestCase.assertNotSame( comp10, comp20 );
+            TestCase.assertEquals( 0, comp20.m_modified );
+            TestCase.assertEquals( 1, comp20.m_multiRefBind );
+            TestCase.assertEquals( 0, comp20.m_multiRefUnbind);
+            TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+        }
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertEquals( 1, comp20.m_multiRef.size() );
+        TestCase.assertEquals( srv2, comp20.m_multiRef.iterator().next() );
+    }
+    
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java
index 3bb0ba3..886d9c4 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java
@@ -60,6 +60,8 @@
     public int m_multiRefBind = 0;
 
     public int m_multiRefUnbind = 0;
+    
+    public int m_modified = 0;
 
 
     @SuppressWarnings("unused")
@@ -99,7 +101,13 @@
         setConfig( context.getProperties() );
     }
 
-
+    @SuppressWarnings("unused")
+    private void modified( ComponentContext context )
+    {
+        setConfig( context.getProperties() );
+        m_modified++;
+    }
+    
     @SuppressWarnings("unused")
     private void deactivate()
     {
diff --git a/scr/src/test/resources/integration_test_simple_components_configuration_change.xml b/scr/src/test/resources/integration_test_simple_components_configuration_change.xml
new file mode 100644
index 0000000..0af7172
--- /dev/null
+++ b/scr/src/test/resources/integration_test_simple_components_configuration_change.xml
@@ -0,0 +1,225 @@
+<?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.
+-->
+<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0">
+<!--  single -->
+<!--  reluctant -->
+    <scr:component name="test_optional_single_dynamic"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..1"
+            policy="dynamic"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_single_dynamic"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..1"
+            policy="dynamic"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_optional_single_static"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..1"
+            policy="static"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_single_static"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..1"
+            policy="static"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+<!--  greedy -->
+    <scr:component name="test_optional_single_dynamic_greedy"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..1"
+            policy-option="greedy"
+            policy="dynamic"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_single_dynamic_greedy"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..1"
+            policy-option="greedy"
+            policy="dynamic"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_optional_single_static_greedy"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..1"
+            policy-option="greedy"
+            policy="static"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_single_static_greedy"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..1"
+            policy-option="greedy"
+            policy="static"
+            bind="setSimpleService"
+            unbind="unsetSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_optional_multiple_dynamic"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..n"
+            policy="dynamic"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_multiple_dynamic"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..n"
+            policy="dynamic"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+    
+    <scr:component name="test_required_multiple_dynamic_factory"
+        enabled="false"
+        configuration-policy="ignore"
+        factory="factory_test_required_multiple_dynamic_factory">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..n"
+            policy="dynamic"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_optional_multiple_static"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..n"
+            policy="static"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_multiple_static"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified">
+
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="1..n"
+            policy="static"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+</components>