FELIX-2233 Ensure new component instances (created for factory configurations) are not enabled if the component has been disabled by ComponentContext.disableComponent(String)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@926971 13f79535-47bb-0310-9956-ffa450edef68
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 8708899..be076ff 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
@@ -305,4 +305,97 @@
         TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( twoConfigs[1].getId() ) );
     }
 
+    @Test
+    public void test_SimpleComponent_factory_configuration_enabled()
+    {
+        final String factoryPid = "FactoryConfigurationComponent_enabled";
+
+        deleteFactoryConfigurations( factoryPid );
+        delay();
+
+        // one single component exists without configuration
+        final Component[] enabledNoConfigs = findComponentsByName( factoryPid );
+        TestCase.assertNotNull( enabledNoConfigs );
+        TestCase.assertEquals( 1, enabledNoConfigs.length );
+        TestCase.assertEquals( Component.STATE_UNSATISFIED, enabledNoConfigs[0].getState() );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.isEmpty() );
+
+        // create two factory configurations expecting two components
+        final String pid0 = createFactoryConfiguration( factoryPid );
+        final String pid1 = createFactoryConfiguration( factoryPid );
+        delay();
+
+        // expect two components, all active
+        final Component[] twoConfigs = findComponentsByName( factoryPid );
+        TestCase.assertNotNull( twoConfigs );
+        TestCase.assertEquals( 2, twoConfigs.length );
+        TestCase.assertEquals( Component.STATE_ACTIVE, twoConfigs[0].getState() );
+        TestCase.assertEquals( Component.STATE_ACTIVE, twoConfigs[1].getState() );
+        TestCase.assertEquals( 2, SimpleComponent.INSTANCES.size() );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( twoConfigs[0].getId() ) );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( twoConfigs[1].getId() ) );
+
+        // disable the name component
+        SimpleComponent.INSTANCES.values().iterator().next().m_activateContext.disableComponent( factoryPid );
+        delay();
+
+        // expect two disabled components
+        final Component[] twoConfigsDisabled = findComponentsByName( factoryPid );
+        TestCase.assertNotNull( twoConfigsDisabled );
+        TestCase.assertEquals( 2, twoConfigsDisabled.length );
+        TestCase.assertEquals( Component.STATE_DISABLED, twoConfigsDisabled[0].getState() );
+        TestCase.assertEquals( Component.STATE_DISABLED, twoConfigsDisabled[1].getState() );
+        TestCase.assertEquals( 0, SimpleComponent.INSTANCES.size() );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( twoConfigs[0].getId() ) );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( twoConfigs[1].getId() ) );
+
+        // create a configuration
+        final String pid3 = createFactoryConfiguration( factoryPid );
+        delay();
+
+        // expect three disabled components
+        final Component[] threeConfigsDisabled = findComponentsByName( factoryPid );
+        TestCase.assertNotNull( threeConfigsDisabled );
+        TestCase.assertEquals( 3, threeConfigsDisabled.length );
+        TestCase.assertEquals( Component.STATE_DISABLED, threeConfigsDisabled[0].getState() );
+        TestCase.assertEquals( Component.STATE_DISABLED, threeConfigsDisabled[1].getState() );
+        TestCase.assertEquals( Component.STATE_DISABLED, threeConfigsDisabled[2].getState() );
+        TestCase.assertEquals( 0, SimpleComponent.INSTANCES.size() );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( threeConfigsDisabled[0].getId() ) );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( threeConfigsDisabled[1].getId() ) );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( threeConfigsDisabled[2].getId() ) );
+
+        // enable a single component (to get ComponentContext later)
+        threeConfigsDisabled[0].enable();
+        delay();
+
+        // expect one enabled and two disabled components
+        final Component[] threeConfigs21 = findComponentsByName( factoryPid );
+        TestCase.assertNotNull( threeConfigs21 );
+        TestCase.assertEquals( 3, threeConfigs21.length );
+        TestCase.assertEquals( Component.STATE_ACTIVE, threeConfigs21[0].getState() );
+        TestCase.assertEquals( Component.STATE_DISABLED, threeConfigs21[1].getState() );
+        TestCase.assertEquals( Component.STATE_DISABLED, threeConfigs21[2].getState() );
+        TestCase.assertEquals( 1, SimpleComponent.INSTANCES.size() );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( threeConfigs21[0].getId() ) );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( threeConfigs21[1].getId() ) );
+        TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( threeConfigs21[2].getId() ) );
+
+        // enable all components now
+        SimpleComponent.INSTANCES.values().iterator().next().m_activateContext.enableComponent( factoryPid );
+        delay();
+
+        // expect all enabled
+        final Component[] threeConfigsEnabled = findComponentsByName( factoryPid );
+        TestCase.assertNotNull( threeConfigsEnabled );
+        TestCase.assertEquals( 3, threeConfigsEnabled.length );
+        TestCase.assertEquals( Component.STATE_ACTIVE, threeConfigsEnabled[0].getState() );
+        TestCase.assertEquals( Component.STATE_ACTIVE, threeConfigsEnabled[1].getState() );
+        TestCase.assertEquals( Component.STATE_ACTIVE, threeConfigsEnabled[2].getState() );
+        TestCase.assertEquals( 3, SimpleComponent.INSTANCES.size() );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( threeConfigsEnabled[0].getId() ) );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( threeConfigsEnabled[1].getId() ) );
+        TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( threeConfigsEnabled[2].getId() ) );
+    }
+
 }
diff --git a/scr/src/test/resources/integration_test_simple_components.xml b/scr/src/test/resources/integration_test_simple_components.xml
index c6e90f0..5bbfee8 100644
--- a/scr/src/test/resources/integration_test_simple_components.xml
+++ b/scr/src/test/resources/integration_test_simple_components.xml
@@ -58,6 +58,14 @@
         <property name="service.pid" value="FactoryConfigurationComponent" />
     </scr:component>
 
+    <!-- component instances created by factory configuration -->
+    <scr:component name="FactoryConfigurationComponent_enabled"
+        enabled="true"
+        configuration-policy="require" >
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+        <property name="service.pid" value="FactoryConfigurationComponent" />
+    </scr:component>
+
     <!-- component is a simple service to verify non-use of private props -->
     <scr:component name="ServiceComponent"
         enabled="false" immediate="true"