FELIX-1841 Apply DependencyManager part of FELIX-1841.patch.2 supplied by Pierre de Rop (thanks) with the slight modification of guarding the rebind with a configuration setting. Also added integration tests validating this fix.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@884122 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
index 9425478..c590812 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
@@ -171,24 +171,49 @@
                 m_componentManager.log( LogService.LOG_DEBUG, "Dependency Manager: Updating {0}", new Object[]
                     { serviceString }, null );
 
-                // remove the service first
-                // only continue with further event handling if the service
-                // removal did not cause the component to be deactivated
-                if ( serviceRemoved( ref ) )
+                if ( getBoundService( ref ) == null )
                 {
-                    // recalculate the number of services matching the filter
-                    // because we don't know whether this service previously matched
-                    // or not
-                    ServiceReference refs[] = getFrameworkServiceReferences();
-                    m_size = ( refs == null ) ? 0 : refs.length;
-
-                    // now try to bind the service - if it matches the target filter
-                    // without recalculating the size (already done).
+                    // service not currently bound --- what to do ?
+                    // if static
+                    //    if inactive and target match: activate
+                    // if dynamic
+                    //    if multiple and target match: bind
                     if ( targetFilterMatch( ref ) )
                     {
-                        serviceAdded( ref );
+                        // new filter match, so increase the counter
+                        m_size++;
+
+                        if ( isStatic() )
+                        {
+                            // if static reference: activate if currentl unsatisifed, otherwise no influence
+                            if ( m_componentManager.getState() == AbstractComponentManager.STATE_UNSATISFIED )
+                            {
+                                m_componentManager.log( LogService.LOG_INFO,
+                                    "Dependency Manager: Service {0} registered, activate component", new Object[]
+                                        { m_dependencyMetadata.getName() }, null );
+
+                                m_componentManager.activate();
+                            }
+                        }
+                        else if ( isMultiple() )
+                        {
+                            // if dynamic and multiple reference, bind, otherwise ignore
+                            serviceAdded( ref );
+                        }
                     }
                 }
+                else if ( !targetFilterMatch( ref ) )
+                {
+                    // service reference does not match target any more, remove
+                    m_size--;
+                    serviceRemoved( ref );
+                }
+                else if ( "true".equalsIgnoreCase( m_componentManager.getBundle().getBundleContext().getProperty(
+                    "ds.rebind.enabled" ) ) )
+                {
+                    // service is bound, bind again to update reference properties
+                    bind();
+                }
 
                 break;
 
@@ -390,6 +415,7 @@
                 ungetService( reference );
             }
         }
+
         else
         {
             m_componentManager.log( LogService.LOG_DEBUG,
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ComponentConfigurationTest.java b/scr/src/test/java/org/apache/felix/scr/integration/ComponentConfigurationTest.java
index 297a190..8708899 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ComponentConfigurationTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ComponentConfigurationTest.java
@@ -42,28 +42,188 @@
     @Test
     public void test_SimpleComponent_configuration_ignore()
     {
-        test_configuration_ignore( "SimpleComponent" );
+        final String pid = "SimpleComponent.configuration.ignore";
+        final Component component = findComponentByName( pid );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertNotNull( component );
+        TestCase.assertFalse( component.isDefaultEnabled() );
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+
+        configure( pid );
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+
+        component.disable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
     }
 
 
     @Test
     public void test_SimpleComponent_configuration_optional()
     {
-        test_configuration_optional( "SimpleComponent" );
+        final String pid = "SimpleComponent.configuration.optional";
+        final Component component = findComponentByName( pid );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertNotNull( component );
+        TestCase.assertFalse( component.isDefaultEnabled() );
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.enable();
+        delay();
+
+        final SimpleComponent firstInstance = SimpleComponent.INSTANCE;
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( firstInstance );
+        TestCase.assertNull( firstInstance.getProperty( PROP_NAME ) );
+
+        configure( pid );
+        delay();
+
+        final SimpleComponent secondInstance = SimpleComponent.INSTANCE;
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( secondInstance );
+        TestCase.assertEquals( PROP_NAME, secondInstance.getProperty( PROP_NAME ) );
+
+        deleteConfig( pid );
+        delay();
+
+        final SimpleComponent thirdInstance = SimpleComponent.INSTANCE;
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( thirdInstance );
+        TestCase.assertNull( thirdInstance.getProperty( PROP_NAME ) );
+
+        TestCase.assertNotSame( "Expect new instance object after reconfiguration", firstInstance, secondInstance );
+        TestCase.assertNotSame( "Expect new instance object after configuration deletion (1)", firstInstance,
+            thirdInstance );
+        TestCase.assertNotSame( "Expect new instance object after configuration deletion (2)", secondInstance,
+            thirdInstance );
+
+        component.disable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
     }
 
 
     @Test
     public void test_SimpleComponent_configuration_require()
     {
-        test_configuration_require( "SimpleComponent" );
+        final String pid = "SimpleComponent.configuration.require";
+        final Component component = findComponentByName( pid );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertNotNull( component );
+        TestCase.assertFalse( component.isDefaultEnabled() );
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        configure( pid );
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( SimpleComponent.INSTANCE );
+        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.disable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
     }
 
 
     @Test
     public void test_SimpleComponent_dynamic_configuration()
     {
-        test_dynamic_configuration( "DynamicConfigurationComponent" );
+        final String pid = "DynamicConfigurationComponent";
+        final Component component = findComponentByName( pid );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertNotNull( component );
+        TestCase.assertFalse( component.isDefaultEnabled() );
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertNotNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+        TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
+
+        final SimpleComponent instance = SimpleComponent.INSTANCE;
+
+        configure( pid );
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
+        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+        TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
+
+        deleteConfig( pid );
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
+        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+        TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
+
+        component.disable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
     }
 
 
@@ -145,187 +305,4 @@
         TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( twoConfigs[1].getId() ) );
     }
 
-
-    private void test_configuration_ignore( final String componentName )
-    {
-        final String pid = componentName + ".configuration.ignore";
-        final Component component = findComponentByName( pid );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertNotNull( component );
-        TestCase.assertFalse( component.isDefaultEnabled() );
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-
-        component.enable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( SimpleComponent.INSTANCE );
-        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-
-        configure( pid );
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( SimpleComponent.INSTANCE );
-        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( SimpleComponent.INSTANCE );
-        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-
-        component.disable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-    }
-
-
-    private void test_configuration_optional( final String componentName )
-    {
-        final String pid = componentName + ".configuration.optional";
-        final Component component = findComponentByName( pid );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertNotNull( component );
-        TestCase.assertFalse( component.isDefaultEnabled() );
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-
-        component.enable();
-        delay();
-
-        final SimpleComponent firstInstance = SimpleComponent.INSTANCE;
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( firstInstance );
-        TestCase.assertNull( firstInstance.getProperty( PROP_NAME ) );
-
-        configure( pid );
-        delay();
-
-        final SimpleComponent secondInstance = SimpleComponent.INSTANCE;
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( secondInstance );
-        TestCase.assertEquals( PROP_NAME, secondInstance.getProperty( PROP_NAME ) );
-
-        deleteConfig( pid );
-        delay();
-
-        final SimpleComponent thirdInstance = SimpleComponent.INSTANCE;
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( thirdInstance );
-        TestCase.assertNull( thirdInstance.getProperty( PROP_NAME ) );
-
-        TestCase.assertNotSame( "Expect new instance object after reconfiguration", firstInstance, secondInstance );
-        TestCase.assertNotSame( "Expect new instance object after configuration deletion (1)", firstInstance,
-            thirdInstance );
-        TestCase.assertNotSame( "Expect new instance object after configuration deletion (2)", secondInstance,
-            thirdInstance );
-
-        component.disable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-    }
-
-
-    private void test_configuration_require( final String componentName )
-    {
-        final String pid = componentName + ".configuration.require";
-        final Component component = findComponentByName( pid );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertNotNull( component );
-        TestCase.assertFalse( component.isDefaultEnabled() );
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-
-        component.enable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-
-        configure( pid );
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( SimpleComponent.INSTANCE );
-        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-
-        component.disable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-    }
-
-
-    private void test_dynamic_configuration( final String componentName )
-    {
-        final String pid = componentName;
-        final Component component = findComponentByName( pid );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertNotNull( component );
-        TestCase.assertFalse( component.isDefaultEnabled() );
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-
-        component.enable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertNotNull( SimpleComponent.INSTANCE );
-        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-        TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
-
-        final SimpleComponent instance = SimpleComponent.INSTANCE;
-
-        configure( pid );
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
-        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-        TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
-
-        deleteConfig( pid );
-        delay();
-
-        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
-        TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
-        TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
-        TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
-
-        component.disable();
-        delay();
-
-        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-    }
 }
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ServiceChangedTest.java b/scr/src/test/java/org/apache/felix/scr/integration/ServiceChangedTest.java
new file mode 100644
index 0000000..9151c49
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ServiceChangedTest.java
@@ -0,0 +1,633 @@
+/*
+ * 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 ServiceChangedTest extends ComponentTestBase
+{
+    static
+    {
+        // uncomment to enable debugging of this test class
+        // paxRunnerVmOption = DEBUG_VM_OPTION;
+
+        descriptorFile = "/integration_test_simple_components_service_binding.xml";
+    }
+
+
+    @Test
+    public void test_optional_single_dynamic()
+    {
+        final Component component = findComponentByName( "test_optional_single_dynamic_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // 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 a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertEquals( srv1, comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 0, comp10.m_singleRefUnbind);
+
+        // set target to not match any more
+        srv1.setFilterProperty( "don't match" );
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp11 = SimpleComponent.INSTANCE;
+        TestCase.assertSame( comp10, comp11 );
+        TestCase.assertNull( comp11.m_singleRef );
+        TestCase.assertTrue( comp11.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 1, comp10.m_singleRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertSame( comp10, comp12 );
+        TestCase.assertEquals( srv2, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 2, comp10.m_singleRefBind );
+        TestCase.assertEquals( 1, comp10.m_singleRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        TestCase.assertEquals( srv2, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 2, comp10.m_singleRefBind );
+        TestCase.assertEquals( 1, comp10.m_singleRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        TestCase.assertEquals( srv1, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 3, comp10.m_singleRefBind );
+        TestCase.assertEquals( 2, comp10.m_singleRefUnbind);
+    }
+
+
+    @Test
+    public void test_required_single_dynamic()
+    {
+        final Component component = findComponentByName( "test_required_single_dynamic_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // 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 a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertEquals( srv1, comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 0, comp10.m_singleRefUnbind);
+
+        // set target to not match any more -> deactivate this component
+        srv1.setFilterProperty( "don't match" );
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 1, comp10.m_singleRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertEquals( srv2, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp12.m_singleRefBind );
+        TestCase.assertEquals( 0, comp12.m_singleRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        TestCase.assertEquals( srv2, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp12.m_singleRefBind );
+        TestCase.assertEquals( 0, comp12.m_singleRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        TestCase.assertEquals( srv1, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 2, comp12.m_singleRefBind );
+        TestCase.assertEquals( 1, comp12.m_singleRefUnbind);
+    }
+
+
+    @Test
+    public void test_optional_multiple_dynamic()
+    {
+        final Component component = findComponentByName( "test_optional_multiple_dynamic_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // update a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // set target to not match any more
+        srv1.setFilterProperty( "don't match" );
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp11 = SimpleComponent.INSTANCE;
+        TestCase.assertSame( comp10, comp11 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertFalse( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertSame( comp10, comp12 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertFalse( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 2, comp10.m_multiRefBind );
+        TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 3, comp10.m_multiRefBind );
+        TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertFalse( comp10.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 3, comp10.m_multiRefBind );
+        TestCase.assertEquals( 2, comp10.m_multiRefUnbind);
+    }
+
+
+    @Test
+    public void test_required_multiple_dynamic()
+    {
+        final Component component = findComponentByName( "test_required_multiple_dynamic_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // update a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // set target to not match any more
+        srv1.setFilterProperty( "don't match" );
+
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
+        final SimpleComponent comp11 = SimpleComponent.INSTANCE;
+        TestCase.assertNull( comp11 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertFalse( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertTrue( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 1, comp12.m_multiRefBind );
+        TestCase.assertEquals( 0, comp12.m_multiRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertTrue( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 2, comp12.m_multiRefBind );
+        TestCase.assertEquals( 0, comp12.m_multiRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 2, comp12.m_multiRefBind );
+        TestCase.assertEquals( 1, comp12.m_multiRefUnbind);
+    }
+
+
+    @Test
+    public void test_optional_single_static()
+    {
+        final Component component = findComponentByName( "test_optional_single_static_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // 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 a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertEquals( srv1, comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 0, comp10.m_singleRefUnbind);
+
+        // set target to not match any more -> recreate !
+        srv1.setFilterProperty( "don't match" );
+        delay(); // async reactivation
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp11 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp11 );
+        TestCase.assertNull( comp11.m_singleRef );
+        TestCase.assertTrue( comp11.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 0, comp11.m_singleRefBind );
+        TestCase.assertEquals( 0, comp11.m_singleRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertSame( comp11, comp12 );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 0, comp12.m_singleRefBind );
+        TestCase.assertEquals( 0, comp12.m_singleRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        final SimpleComponent comp13 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp13 );
+        TestCase.assertSame( comp11, comp13 );
+        TestCase.assertSame( comp12, comp13 );
+        TestCase.assertNull( comp13.m_singleRef );
+        TestCase.assertTrue( comp13.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 0, comp13.m_singleRefBind );
+        TestCase.assertEquals( 0, comp13.m_singleRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        final SimpleComponent comp14 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp14 );
+        TestCase.assertSame( comp11, comp14 );
+        TestCase.assertSame( comp12, comp14 );
+        TestCase.assertSame( comp13, comp14 );
+        TestCase.assertNull( comp14.m_singleRef );
+        TestCase.assertTrue( comp14.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 0, comp14.m_singleRefBind );
+        TestCase.assertEquals( 0, comp14.m_singleRefUnbind);
+    }
+
+
+    @Test
+    public void test_required_single_static()
+    {
+        final Component component = findComponentByName( "test_required_single_static_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // 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 a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertEquals( srv1, comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 0, comp10.m_singleRefUnbind);
+
+        // set target to not match any more -> deactivate this component
+        srv1.setFilterProperty( "don't match" );
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp10.m_singleRefBind );
+        TestCase.assertEquals( 1, comp10.m_singleRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertEquals( srv2, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp12.m_singleRefBind );
+        TestCase.assertEquals( 0, comp12.m_singleRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        final SimpleComponent comp13 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertSame( comp12, comp13 );
+        TestCase.assertEquals( srv2, comp12.m_singleRef );
+        TestCase.assertTrue( comp12.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp12.m_singleRefBind );
+        TestCase.assertEquals( 0, comp12.m_singleRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        delay(); // reactivation required
+        final SimpleComponent comp14 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp14 );
+        TestCase.assertNotSame( comp12, comp14 );
+        TestCase.assertNotSame( comp13, comp14 );
+        TestCase.assertEquals( srv1, comp14.m_singleRef );
+        TestCase.assertTrue( comp14.m_multiRef.isEmpty() );
+        TestCase.assertEquals( 1, comp14.m_singleRefBind );
+        TestCase.assertEquals( 0, comp14.m_singleRefUnbind);
+    }
+
+
+    @Test
+    public void test_optional_multiple_static()
+    {
+        final Component component = findComponentByName( "test_optional_multiple_static_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // update a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // set target to not match any more
+        srv1.setFilterProperty( "don't match" );
+        delay(); // async reactivation (for unbind)
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp11 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp11 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertFalse( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+        TestCase.assertNull( comp11.m_singleRef );
+        TestCase.assertFalse( comp11.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 0, comp11.m_multiRefBind );
+        TestCase.assertEquals( 0, comp11.m_multiRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding (not expected for an optional static ref)
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertSame( comp11, comp12 );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 0, comp12.m_multiRefBind );
+        TestCase.assertEquals( 0, comp12.m_multiRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 0, comp12.m_multiRefBind );
+        TestCase.assertEquals( 0, comp12.m_multiRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        delay(); // allow reactivation delay (for unbind/bind)
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp13 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp13 );
+        TestCase.assertSame( comp11, comp13 );
+        TestCase.assertSame( comp12, comp13 );
+        TestCase.assertNull( comp13.m_singleRef );
+        TestCase.assertFalse( comp13.m_multiRef.contains( srv1 ) );
+        TestCase.assertFalse( comp13.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 0, comp13.m_multiRefBind );
+        TestCase.assertEquals( 0, comp13.m_multiRefUnbind);
+    }
+
+
+    @Test
+    public void test_required_multiple_static()
+    {
+        final Component component = findComponentByName( "test_required_multiple_static_target" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // update a service property
+        srv1.update( "srv1-modified" );
+
+        // no changes in bindings expected
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertTrue( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 0, comp10.m_multiRefUnbind);
+
+        // set target to not match any more
+        srv1.setFilterProperty( "don't match" );
+
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, component.getState() );
+        final SimpleComponent comp11 = SimpleComponent.INSTANCE;
+        TestCase.assertNull( comp11 );
+        TestCase.assertNull( comp10.m_singleRef );
+        TestCase.assertFalse( comp10.m_multiRef.contains( srv1 ) );
+        TestCase.assertEquals( 1, comp10.m_multiRefBind );
+        TestCase.assertEquals( 1, comp10.m_multiRefUnbind);
+
+        final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "srv2" );
+        delay(); // async binding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp12 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp12 );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertTrue( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 1, comp12.m_multiRefBind );
+        TestCase.assertEquals( 0, comp12.m_multiRefUnbind);
+
+        // make srv1 match again, expect not changes in bindings
+        srv1.setFilterProperty( "match" );
+        TestCase.assertNull( comp12.m_singleRef );
+        TestCase.assertFalse( comp12.m_multiRef.contains( srv1 ) );
+        TestCase.assertTrue( comp12.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 1, comp12.m_multiRefBind );
+        TestCase.assertEquals( 0, comp12.m_multiRefUnbind);
+
+        // make srv2 to not match, expect binding to srv1
+        srv2.setFilterProperty( "don't match" );
+        delay(); // allow reactivation/rebinding
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent comp13 = SimpleComponent.INSTANCE;
+        TestCase.assertNotSame( comp10, comp13 );
+        TestCase.assertNotSame( comp11, comp13 );
+        TestCase.assertNotSame( comp12, comp13 );
+        TestCase.assertNull( comp13.m_singleRef );
+        TestCase.assertTrue( comp13.m_multiRef.contains( srv1 ) );
+        TestCase.assertFalse( comp13.m_multiRef.contains( srv2 ) );
+        TestCase.assertEquals( 1, comp13.m_multiRefBind );
+        TestCase.assertEquals( 0, comp13.m_multiRefUnbind);
+   }
+}
\ No newline at end of file
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 6d54707..3bb0ba3 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
@@ -51,8 +51,16 @@
 
     public SimpleService m_singleRef;
 
+    public int m_singleRefBind = 0;
+
+    public int m_singleRefUnbind = 0;
+
     public final Set<SimpleService> m_multiRef = new HashSet<SimpleService>();
 
+    public int m_multiRefBind = 0;
+
+    public int m_multiRefUnbind = 0;
+
 
     @SuppressWarnings("unused")
     private void activate( ComponentContext activateContext, Map<?, ?> config )
@@ -132,6 +140,7 @@
     public void setSimpleService( SimpleService simpleService )
     {
         this.m_singleRef = simpleService;
+        this.m_singleRefBind++;
     }
 
 
@@ -142,6 +151,7 @@
         {
             this.m_singleRef = null;
         }
+        this.m_singleRefUnbind++;
     }
 
 
@@ -149,6 +159,7 @@
     public void bindSimpleService( SimpleService simpleService )
     {
         this.m_multiRef.add( simpleService );
+        this.m_multiRefBind++;
     }
 
 
@@ -156,5 +167,6 @@
     public void unbindSimpleService( SimpleService simpleService )
     {
         this.m_multiRef.remove( simpleService );
+        this.m_multiRefUnbind++;
     }
 }
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleServiceImpl.java b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleServiceImpl.java
index 8563e10..39a9903 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleServiceImpl.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleServiceImpl.java
@@ -29,7 +29,11 @@
 public class SimpleServiceImpl implements SimpleService
 {
 
-    private final String m_value;
+    private String m_value;
+
+    private int m_ranking;
+
+    private String m_filterProp;
 
     private ServiceRegistration m_registration;
 
@@ -42,22 +46,51 @@
 
     public static SimpleServiceImpl create( BundleContext bundleContext, String value, int ranking )
     {
-        SimpleServiceImpl instance = new SimpleServiceImpl( value );
-        Properties props = new Properties();
-        props.put( "value", value );
-        if ( ranking != 0 )
-        {
-            props.put( Constants.SERVICE_RANKING, Integer.valueOf( ranking ) );
-        }
+        SimpleServiceImpl instance = new SimpleServiceImpl( value, ranking );
+        Properties props = instance.getProperties();
         instance.setRegistration( bundleContext.registerService( SimpleService.class.getName(), instance, props ) );
         return instance;
     }
 
 
-    SimpleServiceImpl( String value )
+    SimpleServiceImpl( final String value, final int ranking )
     {
         this.m_value = value;
+        this.m_ranking = ranking;
+        this.m_filterProp = "match";
+    }
 
+
+    private Properties getProperties()
+    {
+        final Properties props = new Properties();
+        props.put( "value", m_value );
+        props.put( "filterprop", m_filterProp );
+        if ( m_ranking != 0 )
+        {
+            props.put( Constants.SERVICE_RANKING, Integer.valueOf( m_ranking ) );
+        }
+        return props;
+    }
+
+
+    public void update( String value )
+    {
+        if ( this.m_registration != null )
+        {
+            this.m_value = value;
+            this.m_registration.setProperties( getProperties() );
+        }
+    }
+
+
+    public void setFilterProperty( String filterProp )
+    {
+        if ( this.m_registration != null )
+        {
+            this.m_filterProp = filterProp;
+            this.m_registration.setProperties( getProperties() );
+        }
     }
 
 
@@ -93,6 +126,6 @@
     @Override
     public String toString()
     {
-        return getClass().getSimpleName() + ": value=" + getValue();
+        return getClass().getSimpleName() + ": value=" + getValue() + ", filterprop=" + m_filterProp;
     }
 }
diff --git a/scr/src/test/resources/integration_test_simple_components_service_binding.xml b/scr/src/test/resources/integration_test_simple_components_service_binding.xml
index 7d37b37..9c8c630 100644
--- a/scr/src/test/resources/integration_test_simple_components_service_binding.xml
+++ b/scr/src/test/resources/integration_test_simple_components_service_binding.xml
@@ -131,4 +131,127 @@
         />
     </scr:component>
 
+    
+    <!-- Same components as above but using a target spec -->
+
+    <scr:component name="test_optional_single_dynamic_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_single_dynamic_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
+    <scr:component name="test_optional_multiple_dynamic_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_multiple_dynamic_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+    
+    <scr:component name="test_optional_single_static_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_single_static_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
+    <scr:component name="test_optional_multiple_static_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_multiple_static_target"
+        enabled="false"
+        configuration-policy="ignore">
+        <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"
+            target="(filterprop=match)"
+        />
+    </scr:component>
+
 </components>