You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by dj...@apache.org on 2016/06/11 01:14:36 UTC

svn commit: r1747831 [2/2] - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/manager/ test/java/org/apache/felix/scr/integration/

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/PersistentComponentFactoryTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/PersistentComponentFactoryTest.java?rev=1747831&r1=1747830&r2=1747831&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/PersistentComponentFactoryTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/PersistentComponentFactoryTest.java Sat Jun 11 01:14:36 2016
@@ -18,12 +18,11 @@
  */
 package org.apache.felix.scr.integration;
 
+import static org.junit.Assert.assertNotNull;
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.Hashtable;
 
-import junit.framework.TestCase;
-
 import org.apache.felix.scr.integration.components.SimpleComponent;
 import org.apache.felix.scr.integration.components.SimpleService;
 import org.apache.felix.scr.integration.components.SimpleServiceImpl;
@@ -31,15 +30,12 @@ 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.cm.ConfigurationAdmin;
-import org.osgi.service.component.ComponentConstants;
-import org.osgi.service.component.ComponentException;
 import org.osgi.service.component.ComponentFactory;
 import org.osgi.service.component.ComponentInstance;
 import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
 import org.osgi.service.log.LogService;
 
+import junit.framework.TestCase;
 
 @RunWith(JUnit4TestRunner.class)
 public class PersistentComponentFactoryTest extends ComponentTestBase
@@ -49,7 +45,7 @@ public class PersistentComponentFactoryT
     {
         descriptorFile = "/integration_test_persistent_factory_components.xml";
         // uncomment to enable debugging of this test class
-//        paxRunnerVmOption = DEBUG_VM_OPTION;
+        //        paxRunnerVmOption = DEBUG_VM_OPTION;
     }
 
     @Test
@@ -58,23 +54,22 @@ public class PersistentComponentFactoryT
         final String componentname = "factory.component";
         final String componentfactory = "factory.component.factory";
 
-        getConfigurationsDisabledThenEnable(componentname, 0, -1);
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
 
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
-        final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
+        final ComponentInstance instance = createFactoryComponentInstance( componentfactory );
 
         // check registered components
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
 
         instance.dispose();
         TestCase.assertNull( SimpleComponent.INSTANCE );
         TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
-        
-        checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
 
-    }
+        checkConfigurationCount( componentname, 0, ComponentConfigurationDTO.ACTIVE );
 
+    }
 
     @Test
     public void test_component_factory_disable_factory() throws Exception
@@ -84,16 +79,16 @@ public class PersistentComponentFactoryT
         final String componentname = "factory.component";
         final String componentfactory = "factory.component.factory";
 
-        getConfigurationsDisabledThenEnable(componentname, 0, -1);
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
 
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
-        final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
+        final ComponentInstance instance = createFactoryComponentInstance( componentfactory );
 
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
 
         // disable the factory
-        disableAndCheck(componentname);
+        disableAndCheck( componentname );
         delay();
 
         // factory is disabled but the instance is still alive
@@ -105,26 +100,24 @@ public class PersistentComponentFactoryT
 
     }
 
-
     @Test
     public void test_component_factory_newInstance_failure() throws Exception
     {
         final String componentname = "factory.component";
         final String componentfactory = "factory.component.factory";
 
-        getConfigurationsDisabledThenEnable(componentname, 0, -1);
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
         Hashtable<String, String> props = new Hashtable<String, String>();
         props.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
         props.put( SimpleComponent.PROP_ACTIVATE_FAILURE, "Requested Failure" );
-        final ComponentFactory factory = getComponentFactory(componentfactory);
+        final ComponentFactory factory = getComponentFactory( componentfactory );
 
         final ComponentInstance instance = factory.newInstance( props );
         TestCase.assertNotNull( instance );
 
-
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.SATISFIED);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.SATISFIED );
     }
 
     @Test
@@ -133,8 +126,8 @@ public class PersistentComponentFactoryT
         final String componentname = "factory.component.configuration";
         final String componentfactory = "factory.component.factory.configuration";
 
-        testConfiguredFactory(componentname, componentfactory, false, false);
-        
+        testConfiguredFactory( componentname, componentfactory, false, false );
+
     }
 
     @Test
@@ -143,8 +136,8 @@ public class PersistentComponentFactoryT
         final String componentname = "factory.component.configuration.optional";
         final String componentfactory = "factory.component.factory.configuration.optional";
 
-        testConfiguredFactory(componentname, componentfactory, true, true);
-        
+        testConfiguredFactory( componentname, componentfactory, true, true );
+
     }
 
     @Test
@@ -153,7 +146,7 @@ public class PersistentComponentFactoryT
         final String componentname = "factory.component.configuration.optional.13";
         final String componentfactory = "factory.component.factory.configuration.optional.13";
 
-        testConfiguredFactory(componentname, componentfactory, true, true);
+        testConfiguredFactory( componentname, componentfactory, true, true );
     }
 
     @Test
@@ -162,44 +155,43 @@ public class PersistentComponentFactoryT
         final String componentname = "factory.component.configuration.optional.nomodify";
         final String componentfactory = "factory.component.factory.configuration.optional.nomodify";
 
-        testConfiguredFactory(componentname, componentfactory, true, true);
-        
-    }
+        testConfiguredFactory( componentname, componentfactory, true, true );
 
+    }
 
-    private ComponentInstance testConfiguredFactory(final String componentname,
-        final String componentfactory, boolean optional, boolean expectComponent) throws InvocationTargetException,
-        InterruptedException, InvalidSyntaxException
+    private ComponentInstance testConfiguredFactory(final String componentname, final String componentfactory,
+        boolean optional, boolean expectComponent)
+        throws InvocationTargetException, InterruptedException, InvalidSyntaxException
     {
         // ensure there is no configuration for the component
         deleteConfig( componentname );
         delay();
 
-        getConfigurationsDisabledThenEnable(componentname, 0, -1);
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
         // At this point, since we don't have created the configuration, then the ComponentFactory
         // should not be available.
-        
-        checkFactory(componentfactory, optional);
-        
+
+        checkFactory( componentfactory, optional );
+
         // supply configuration now and ensure active
         configure( componentname );
-        delay();        
+        delay();
 
-        checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 0, ComponentConfigurationDTO.ACTIVE );
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
         // get the component factory service
-        final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
+        final ComponentInstance instance = createFactoryComponentInstance( componentfactory );
 
         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 ) );                        
+        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
 
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
 
         // delete config, ensure factory is not active anymore and component instance gone 
         //(configuration required >> dispose of instance.  Also for pre-1.3 components, removing config unconditionally
@@ -207,15 +199,15 @@ public class PersistentComponentFactoryT
         deleteConfig( componentname );
         delay();
 
-        checkFactory(componentfactory, optional);
+        checkFactory( componentfactory, optional );
 
-        if (expectComponent) 
+        if ( expectComponent )
         {
             TestCase.assertNotNull( instance.getInstance() );
             TestCase.assertNotNull( SimpleComponent.INSTANCE );
 
             // with removal of the factory, the created instance should also be removed
-            checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);            
+            checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
         }
         else
         {
@@ -223,13 +215,11 @@ public class PersistentComponentFactoryT
             TestCase.assertNull( SimpleComponent.INSTANCE );
 
             // with removal of the factory, the created instance should also be removed
-            checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);            
+            checkConfigurationCount( componentname, 0, ComponentConfigurationDTO.ACTIVE );
         }
         return instance;
     }
 
-
-
     @Test
     public void test_component_factory_reference() throws Exception
     {
@@ -238,7 +228,7 @@ public class PersistentComponentFactoryT
 
         SimpleServiceImpl.create( bundleContext, "ignored" ).setFilterProperty( "ignored" );
 
-        getConfigurationsDisabledThenEnable(componentname, 0, -1);
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
         // register a service : filterprop=match
@@ -247,32 +237,30 @@ public class PersistentComponentFactoryT
 
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
-        final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
+        final ComponentInstance instance = createFactoryComponentInstance( componentfactory );
         TestCase.assertEquals( 1, SimpleComponent.INSTANCE.m_multiRef.size() );
         TestCase.assertTrue( SimpleComponent.INSTANCE.m_multiRef.contains( match ) );
 
         // check registered components
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
 
         instance.dispose();
         TestCase.assertNull( SimpleComponent.INSTANCE );
         TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
-        checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
-
+        checkConfigurationCount( componentname, 0, ComponentConfigurationDTO.ACTIVE );
 
         // overwritten filterprop
         Hashtable<String, String> propsNonMatch = new Hashtable<String, String>();
         propsNonMatch.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
         propsNonMatch.put( "ref.target", "(filterprop=nomatch)" );
-        ComponentFactory factory = getComponentFactory(componentfactory);
+        ComponentFactory factory = getComponentFactory( componentfactory );
         final ComponentInstance instanceNonMatch = factory.newInstance( propsNonMatch );//works even without required reference
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE );
 
         final SimpleServiceImpl noMatch = SimpleServiceImpl.create( bundleContext, "nomatch" ).setFilterProperty(
             "nomatch" );
         delay();
 
-
         TestCase.assertNotNull( instanceNonMatch.getInstance() );
         TestCase.assertEquals( SimpleComponent.INSTANCE, instanceNonMatch.getInstance() );
         TestCase.assertEquals( PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty( PROP_NAME_FACTORY ) );
@@ -281,20 +269,20 @@ public class PersistentComponentFactoryT
         TestCase.assertTrue( SimpleComponent.INSTANCE.m_multiRef.contains( noMatch ) );
 
         // check registered components
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
 
         match.getRegistration().unregister();
         delay();
 
         // check registered components (ComponentFactory is still present)
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
 
         //it has already been deactivated.... this should cause an exception?
         noMatch.getRegistration().unregister();
         delay();
 
         // check registered components (ComponentFactory is still present)
-        checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE );
 
         // deactivated due to unsatisfied reference
         TestCase.assertNull( instanceNonMatch.getInstance() );
@@ -311,17 +299,18 @@ public class PersistentComponentFactoryT
     {
         //set up the component that refers to the service the factory will create.
         final String referringComponentName = "ComponentReferringToFactoryObject";
-        getConfigurationsDisabledThenEnable(referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
+        getConfigurationsDisabledThenEnable( referringComponentName, 1,
+            ComponentConfigurationDTO.UNSATISFIED_REFERENCE );
 
         final String componentname = "factory.component.referred";
         final String componentfactory = "factory.component.factory.referred";
 
-        getConfigurationsDisabledThenEnable(componentname, 0, -1);
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
         TestCase.assertNull( SimpleComponent.INSTANCE );
 
         Hashtable<String, String> props = new Hashtable<String, String>();
         props.put( "service.pid", "myFactoryInstance" );
-        final ComponentFactory factory = getComponentFactory(componentfactory);
+        final ComponentFactory factory = getComponentFactory( componentfactory );
 
         final ComponentInstance instance = factory.newInstance( props );
         TestCase.assertNotNull( instance );
@@ -329,72 +318,75 @@ public class PersistentComponentFactoryT
         TestCase.assertNotNull( instance.getInstance() );
         TestCase.assertTrue( instance.getInstance() instanceof SimpleService );
         //The referring service should now be active
-        checkConfigurationCount(referringComponentName, 1, ComponentConfigurationDTO.ACTIVE);
+        checkConfigurationCount( referringComponentName, 1, ComponentConfigurationDTO.ACTIVE );
 
         instance.dispose();
         TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
 
         //make sure it's unsatisfied (service is no longer available)
-        checkConfigurationCount(referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
+        checkConfigurationCount( referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE );
     }
 
     @Test
     public void test_component_factory_with_target_filters() throws Exception
     {
         final String componentfactory = "factory.component.reference.targetfilter";
-        getConfigurationsDisabledThenEnable(componentfactory, 0, -1);
+        getConfigurationsDisabledThenEnable( componentfactory, 0, -1 );
 
-        SimpleServiceImpl s1 = SimpleServiceImpl.create(bundleContext, "service1");
-        SimpleServiceImpl s2 = SimpleServiceImpl.create(bundleContext, "service2");
+        SimpleServiceImpl s1 = SimpleServiceImpl.create( bundleContext, "service1" );
+        SimpleServiceImpl s2 = SimpleServiceImpl.create( bundleContext, "service2" );
 
         // supply configuration now and ensure active
         configure( componentfactory );
-        delay();        
+        delay();
 
         TestCase.assertNull( SimpleComponent.INSTANCE );
-        
+
         Hashtable<String, String> props = new Hashtable<String, String>();
         props.put( PROP_NAME_FACTORY, PROP_NAME_FACTORY );
-        props.put("ref.target", "(value=service2)");
-        final ComponentInstance instance = createFactoryComponentInstance(componentfactory, props);
+        props.put( "ref.target", "(value=service2)" );
+        final ComponentInstance instance = createFactoryComponentInstance( componentfactory, props );
 
-        log.log(LogService.LOG_WARNING, "Bound Services: " +  SimpleComponent.INSTANCE.m_multiRef);
+        log.log( LogService.LOG_WARNING, "Bound Services: " + SimpleComponent.INSTANCE.m_multiRef );
         TestCase.assertFalse( SimpleComponent.INSTANCE.m_multiRef.contains( s1 ) );
         TestCase.assertTrue( SimpleComponent.INSTANCE.m_multiRef.contains( s2 ) );
 
         instance.dispose();
         TestCase.assertNull( SimpleComponent.INSTANCE );
         TestCase.assertNull( instance.getInstance() ); // SCR 112.12.6.2
-        
+
         s2.drop();
         s1.drop();
     }
-    
+
     @Test
-    public void test_component_factory_set_bundle_location_null() throws Exception
+    public void test_component_factory_set_bundle_location() throws Exception
     {
-        final String componentfactory = "factory.component.reference.targetfilter";
-        getConfigurationsDisabledThenEnable(componentfactory, 0, -1);
-        SimpleServiceImpl s1 = SimpleServiceImpl.create(bundleContext, "service1");
+        final String componentfactoryPid = "factory.component.configuration";
+        final String componentFactoryName = "factory.component.factory.configuration";
+        getConfigurationsDisabledThenEnable( componentfactoryPid, 0, -1 );
+        checkFactory( componentFactoryName, false );
 
-        ConfigurationAdmin ca = getConfigurationAdmin();
-        org.osgi.service.cm.Configuration config = ca.getConfiguration( componentfactory, null );
-        config.setBundleLocation( null );
+        org.osgi.service.cm.Configuration config = configure( componentfactoryPid );
         delay();
-        if ( isAtLeastR5() )
-        {
-            //check that ConfigurationSupport got a Location changed event and set the bundle location
-            TestCase.assertNotNull( config.getBundleLocation() );
-        } 
-        // supply configuration now and ensure active
-        configure( componentfactory );
-        delay();        
+        assertNotNull( getComponentFactory( componentFactoryName ) );
+
+        config.setBundleLocation( "foo" );
+        delay();
+        checkFactory( componentFactoryName, false );
+
+        config.setBundleLocation( bundle.getLocation() );
+        delay();
+        assertNotNull( getComponentFactory( componentFactoryName ) );
+
+        config.setBundleLocation( "foo" );
+        delay();
+        checkFactory( componentFactoryName, false );
+
+        config.setBundleLocation( "?" );
+        delay();
+        assertNotNull( getComponentFactory( componentFactoryName ) );
 
-        TestCase.assertNull( SimpleComponent.INSTANCE );
-        
-        final ComponentFactory factory = getComponentFactory(componentfactory);
-        
-        s1.drop();
     }
-    
+
 }