You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fm...@apache.org on 2009/12/14 11:00:13 UTC

svn commit: r890254 - in /felix/trunk/scr/src/test: java/org/apache/felix/scr/integration/ComponentFactoryTest.java resources/integration_test_simple_components.xml

Author: fmeschbe
Date: Mon Dec 14 10:00:12 2009
New Revision: 890254

URL: http://svn.apache.org/viewvc?rev=890254&view=rev
Log:
FELIX-1922 test ComponentFactory does not require configuration
FELIX-1942 test ComponentFactory instances are never reactivated

Modified:
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java
    felix/trunk/scr/src/test/resources/integration_test_simple_components.xml

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java?rev=890254&r1=890253&r2=890254&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java Mon Dec 14 10:00:12 2009
@@ -41,6 +41,8 @@
 public class ComponentFactoryTest extends ComponentTestBase
 {
 
+    private static final String PROP_NAME_FACTORY = ComponentTestBase.PROP_NAME + ".factory";
+
     static
     {
         // uncomment to enable debugging of this test class
@@ -76,13 +78,13 @@
         TestCase.assertNotNull( factory );
 
         Hashtable<String, String> props = new Hashtable<String, String>();
-        props.put( PROP_NAME, PROP_NAME );
+        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() );
-        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+        TestCase.assertEquals( PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty( PROP_NAME_FACTORY ) );
 
         final Map<?, ?> instanceMap = ( Map<?, ?> ) getFieldValue( component, "m_componentInstances" );
         TestCase.assertNotNull( instanceMap );
@@ -91,8 +93,30 @@
         final Object instanceManager = getFieldValue( instance, "m_componentManager" );
         TestCase.assertTrue( instanceMap.containsValue( instanceManager ) );
 
+        // check registered components
+        final Component[] allFactoryComponents = findComponentsByName( componentname );
+        TestCase.assertNotNull( allFactoryComponents );
+        TestCase.assertEquals( 2, allFactoryComponents.length );
+        for ( int i = 0; i < allFactoryComponents.length; i++ )
+        {
+            final Component c = allFactoryComponents[i];
+            if ( c.getId() == component.getId() )
+            {
+                TestCase.assertEquals( Component.STATE_FACTORY, c.getState() );
+            }
+            else if ( c.getId() == SimpleComponent.INSTANCE.m_id )
+            {
+                TestCase.assertEquals( Component.STATE_ACTIVE, c.getState() );
+            }
+            else
+            {
+                TestCase.fail( "Unexpected Component " + c );
+            }
+        }
+
         instance.dispose();
         TestCase.assertNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
 
         TestCase.assertEquals( 0, instanceMap.size() );
         TestCase.assertFalse( instanceMap.containsValue( instanceManager ) );
@@ -129,13 +153,13 @@
         TestCase.assertNotNull( factory );
 
         Hashtable<String, String> props = new Hashtable<String, String>();
-        props.put( PROP_NAME, PROP_NAME );
+        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() );
-        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+        TestCase.assertEquals( PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty( PROP_NAME_FACTORY ) );
 
         final Map<?, ?> instanceMap = ( Map<?, ?> ) getFieldValue( component, "m_componentInstances" );
         TestCase.assertNotNull( instanceMap );
@@ -156,6 +180,7 @@
 
         instance.dispose();
         TestCase.assertNull( SimpleComponent.INSTANCE );
+        TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
 
         TestCase.assertEquals( 0, instanceMap.size() );
         TestCase.assertFalse( instanceMap.containsValue( instanceManager ) );
@@ -190,13 +215,16 @@
         TestCase.assertNotNull( factory );
 
         Hashtable<String, String> props = new Hashtable<String, String>();
-        props.put( PROP_NAME, PROP_NAME );
+        props.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
         props.put( SimpleComponent.PROP_ACTIVATE_FAILURE, "Requested Failure" );
-        try {
+        try
+        {
             final ComponentInstance instance = factory.newInstance( props );
             TestCase.assertNotNull( instance );
-            TestCase.fail("Expected newInstance method to fail with ComponentException");
-        } catch (ComponentException ce) {
+            TestCase.fail( "Expected newInstance method to fail with ComponentException" );
+        }
+        catch ( ComponentException ce )
+        {
             // this is expected !
         }
 
@@ -205,4 +233,95 @@
         TestCase.assertTrue( instanceMap.isEmpty() );
         TestCase.assertNull( SimpleComponent.INSTANCE );
     }
+
+
+    @Test
+    public void test_component_factory_require_configuration() throws InvalidSyntaxException
+    {
+        final String componentname = "factory.component.configuration";
+        final String componentfactory = "factory.component.factory.configuration";
+
+        // ensure there is no configuration for the component
+        deleteConfig( componentname );
+        delay();
+
+        final Component component = findComponentByName( componentname );
+
+        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_FACTORY, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        // supply configuration now and ensure active
+        configure( componentname );
+        delay();
+        TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        // ensure component factory still active if config is deleted
+        deleteConfig( componentname );
+        delay();
+        TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        // supply configuration now and ensure active
+        configure( componentname );
+        delay();
+        TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        // get the component factory service
+        final ServiceReference[] refs = bundleContext.getServiceReferences( ComponentFactory.class.getName(), "("
+            + ComponentConstants.COMPONENT_FACTORY + "=" + componentfactory + ")" );
+        TestCase.assertNotNull( refs );
+        TestCase.assertEquals( 1, refs.length );
+        final ComponentFactory factory = ( ComponentFactory ) bundleContext.getService( refs[0] );
+        TestCase.assertNotNull( factory );
+
+        // create an instance
+        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 );
+
+        final Object instanceObject = instance.getInstance();
+        TestCase.assertNotNull( instanceObject );
+        TestCase.assertEquals( SimpleComponent.INSTANCE, instanceObject );
+        TestCase.assertEquals( PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty( PROP_NAME_FACTORY ) );
+        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+
+        final Map<?, ?> instanceMap = ( Map<?, ?> ) getFieldValue( component, "m_componentInstances" );
+        TestCase.assertNotNull( instanceMap );
+        TestCase.assertEquals( 1, instanceMap.size() );
+
+        final Object instanceManager = getFieldValue( instance, "m_componentManager" );
+        TestCase.assertTrue( instanceMap.containsValue( instanceManager ) );
+
+        // delete config, ensure factory still active and component instance, not changed
+        deleteConfig( componentname );
+        delay();
+        TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
+
+        TestCase.assertNotNull( instance.getInstance() );
+        TestCase.assertEquals( SimpleComponent.INSTANCE, instance.getInstance() );
+        TestCase.assertEquals( instanceObject, instance.getInstance() );
+        TestCase.assertEquals( PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty( PROP_NAME_FACTORY ) );
+        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
+
+        instance.dispose();
+        TestCase.assertNull( SimpleComponent.INSTANCE ); // component is deactivated
+        TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
+
+        // with removal of the factory, the created instance should also be removed
+        TestCase.assertEquals( 0, instanceMap.size() );
+        TestCase.assertFalse( instanceMap.containsValue( instanceManager ) );
+    }
+
 }

Modified: felix/trunk/scr/src/test/resources/integration_test_simple_components.xml
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/resources/integration_test_simple_components.xml?rev=890254&r1=890253&r2=890254&view=diff
==============================================================================
--- felix/trunk/scr/src/test/resources/integration_test_simple_components.xml (original)
+++ felix/trunk/scr/src/test/resources/integration_test_simple_components.xml Mon Dec 14 10:00:12 2009
@@ -88,5 +88,13 @@
         factory="factory.component.factory" >
         <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
     </scr:component>
+    
+    <!-- Component Factory Instances, requiring configuration -->
+    <scr:component name="factory.component.configuration"
+        enabled="false"
+        configuration-policy="require"
+        factory="factory.component.factory.configuration" >
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+    </scr:component>
 
 </components>