FELIX-3971 (disabled due to uncertainty about correct behavior) test for factory component

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1457987 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
index 407aeef..b802a4a 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ConfigurationChangeTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ConfigurationChangeTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.scr.integration;
 
+import java.util.Hashtable;
+
 import junit.framework.TestCase;
 
 import org.apache.felix.scr.Component;
@@ -26,10 +28,17 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentConstants;
+import org.osgi.service.component.ComponentFactory;
+import org.osgi.service.component.ComponentInstance;
 
 @RunWith(JUnit4TestRunner.class)
 public class ConfigurationChangeTest extends ComponentTestBase
 {
+    private static final String PROP_NAME_FACTORY = ComponentTestBase.PROP_NAME + ".factory";
+
     static
     {
         // uncomment to enable debugging of this test class
@@ -37,7 +46,7 @@
 
         descriptorFile = "/integration_test_simple_components_configuration_change.xml";
     }
-/*
+
     @Test
     public void test_optional_single_dynamic()
     {
@@ -93,7 +102,7 @@
         String pid = "test_required_single_static_greedy";
         singleTest( pid, false );
     }
-*/
+
     private void singleTest(String pid, boolean dynamic)
     {
         final Component component = findComponentByName( pid );
@@ -153,6 +162,56 @@
         multipleTest( pid, true );
     }
 
+
+    @Test
+    public void test_required_multiple_dynamic()
+    {
+        String pid = "test_required_multiple_dynamic";
+        multipleTest( pid, true );
+    }
+
+    @Test
+    public void test_optional_multiple_static()
+    {
+        String pid = "test_optional_multiple_static";
+        multipleTest( pid, false );
+    }
+
+    @Test
+    public void test_required_multiple_static()
+    {
+        String pid = "test_required_multiple_static";
+        multipleTest( pid, false );
+    }
+
+    @Test
+    public void test_optional_multiple_dynamic_greedy()
+    {
+        String pid = "test_optional_multiple_dynamic_greedy";
+        multipleTest( pid, true );
+    }
+
+    @Test
+    public void test_required_multiple_dynamic_greedy()
+    {
+        String pid = "test_required_multiple_dynamic_greedy";
+        multipleTest( pid, true );
+    }
+
+    @Test
+    public void test_optional_multiple_static_greedy()
+    {
+        String pid = "test_optional_multiple_static_greedy";
+        multipleTest( pid, false );
+    }
+
+    @Test
+    public void test_required_multiple_static_greedy()
+    {
+        String pid = "test_required_multiple_static_greedy";
+        multipleTest( pid, false );
+    }
+
     private void multipleTest(String pid, boolean dynamic)
     {
         final Component component = findComponentByName( pid );
@@ -204,5 +263,79 @@
         TestCase.assertEquals( 1, comp20.m_multiRef.size() );
         TestCase.assertEquals( srv2, comp20.m_multiRef.iterator().next() );
     }
-    
+ 
+    //I'm not sure what should happen in this case, asking on dev list.
+//    @Test
+    public void testSingleDynamicRequiredFactory() throws InvalidSyntaxException
+    {
+        String pid = "test_required_single_dynamic_factory";
+        final String factoryPid = "factory_" + pid;
+        boolean dynamic = true;
+        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_FACTORY, component.getState() );
+        
+        // create a component instance
+        final ServiceReference[] refs = bundleContext.getServiceReferences( ComponentFactory.class.getName(), "("
+            + ComponentConstants.COMPONENT_FACTORY + "=" + factoryPid + ")" );
+        TestCase.assertNotNull( refs );
+        TestCase.assertEquals( 1, refs.length );
+        final ComponentFactory factory = ( ComponentFactory ) bundleContext.getService( refs[0] );
+        TestCase.assertNotNull( factory );
+
+        Hashtable<String, String> props = new Hashtable<String, String>();
+        props.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
+        final ComponentInstance instance = factory.newInstance( props );
+        TestCase.assertNotNull( instance );
+        TestCase.assertNotNull( instance.getInstance() );
+        TestCase.assertEquals( SimpleComponent.INSTANCE, instance.getInstance() );
+
+        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 )
+        {
+            //fails here, config modifications are not propagated to instances from factory.
+            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() );
+    }
+
 }
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
index 0af7172..ef6b989 100644
--- a/scr/src/test/resources/integration_test_simple_components_configuration_change.xml
+++ b/scr/src/test/resources/integration_test_simple_components_configuration_change.xml
@@ -145,6 +145,8 @@
         />
     </scr:component>
 
+<!-- multiple -->
+<!-- reluctant -->
     <scr:component name="test_optional_multiple_dynamic"
         enabled="false"
         configuration-policy="require"
@@ -175,26 +177,10 @@
         />
     </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"
@@ -210,7 +196,6 @@
         enabled="false"
         configuration-policy="require"
         modified="modified">
-
         <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
         <reference
             name="ref"
@@ -222,4 +207,87 @@
         />
     </scr:component>
 
+<!-- greedy -->
+    <scr:component name="test_optional_multiple_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..n"
+            policy="dynamic"
+            policy-option="greedy"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_multiple_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..n"
+            policy="dynamic"
+            policy-option="greedy"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+    
+    <scr:component name="test_optional_multiple_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..n"
+            policy="static"
+            policy-option="greedy"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+    <scr:component name="test_required_multiple_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..n"
+            policy="static"
+            policy-option="greedy"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+    </scr:component>
+
+<!-- factory, we'll just try single dynamic -->
+    <scr:component name="test_required_single_dynamic_factory"
+        enabled="false"
+        configuration-policy="require"
+        modified="modified"
+        factory="factory_test_required_single_dynamic_factory">
+        <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>
+
+
 </components>