You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by tj...@apache.org on 2020/11/06 15:29:24 UTC

[felix-dev] branch master updated: Add test for failed getService for constructor injection.

This is an automated email from the ASF dual-hosted git repository.

tjwatson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new a6c986f  Add test for failed getService for constructor injection.
a6c986f is described below

commit a6c986ffc8f6f08f71fc435909a7f1061eeb961a
Author: Thomas Watson <tj...@us.ibm.com>
AuthorDate: Fri Nov 6 09:28:49 2020 -0600

    Add test for failed getService for constructor injection.
---
 .../scr/integration/ComponentConstructorTest.java  | 68 ++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ComponentConstructorTest.java b/scr/src/test/java/org/apache/felix/scr/integration/ComponentConstructorTest.java
index dbc011a..548a284 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ComponentConstructorTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ComponentConstructorTest.java
@@ -27,11 +27,17 @@ import static org.junit.Assert.assertTrue;
 import java.util.Collection;
 
 import org.apache.felix.scr.integration.components.ConstructorComponent;
+import org.apache.felix.scr.integration.components.ConstructorMultiReference;
+import org.apache.felix.scr.integration.components.ConstructorSingleReference;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceException;
+import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
 
 import junit.framework.Assert;
@@ -82,6 +88,8 @@ public class ComponentConstructorTest extends ComponentTestBase
             Assert.assertEquals( "Nothing for filter: " + filter, 1, srs.size() );
             ServiceReference<S> sr = srs.iterator().next();
             assertNull(bundleContext.getService( sr ));
+            findComponentConfigurationByName(dto.description.name,
+                ComponentConfigurationDTO.FAILED_ACTIVATION);
         }
         catch ( InvalidSyntaxException e )
         {
@@ -247,4 +255,64 @@ public class ComponentConstructorTest extends ComponentTestBase
 
         disableAndCheck(cc);
     }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void test_constructor_failGetService() throws Exception
+    {
+        ServiceFactory<?> failFactory = new ServiceFactory<Object>()
+        {
+            @Override
+            public Object getService(Bundle bundle,
+                ServiceRegistration<Object> registration)
+            {
+                throw new ServiceException("Testing failed get service.");
+            }
+
+            @Override
+            public void ungetService(Bundle bundle,
+                ServiceRegistration<Object> registration, Object service)
+            {
+                // nothing to do
+            }
+        };
+
+        bundleContext.registerService(ConstructorSingleReference.class,
+            (ServiceFactory<ConstructorSingleReference>) failFactory, null);
+        bundleContext.registerService(ConstructorMultiReference.class,
+            (ServiceFactory<ConstructorMultiReference>) failFactory, null);
+
+        // try mandatory components first
+        doTestMandatoryFailGetService("ConstructorComponent.refsingle");
+        doTestMandatoryFailGetService("ConstructorComponent.refmulti");
+
+        // try the optional components second
+        doTestOptionalFailGetService("ConstructorComponent.refsingleoptional",
+            "ref is null");
+        doTestOptionalFailGetService("ConstructorComponent.refmultioptional",
+            "ref has size: 0");
+    }
+
+    private void doTestOptionalFailGetService(String componentName, String expectedMsg)
+        throws Exception
+    {
+        ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(componentName,
+            ComponentConfigurationDTO.SATISFIED);
+        assertEquals(1, cc.description.init);
+
+        ConstructorComponent cmp1 = this.getServiceFromConfiguration(cc,
+            ConstructorComponent.class);
+
+        final String msg1 = cmp1.test();
+        assertEquals("Wrong refs size.", expectedMsg, msg1);
+    }
+
+    private void doTestMandatoryFailGetService(String componentName)
+        throws Exception
+    {
+        ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(componentName,
+            ComponentConfigurationDTO.SATISFIED);
+        assertEquals(1, cc.description.init);
+        failGetServiceFromConfiguration(cc, ConstructorComponent.class);
+    }
 }