From 3d6abc8e45f618cf3308a3d44582a63f089f0d0d Mon Sep 17 00:00:00 2001 From: Thomas Calmant Date: Wed, 23 Apr 2014 12:29:10 +0200 Subject: [PATCH] Added a test for component handler inheritance Tests if a component can ignore the configuration of the Provides handler of its parent, and if it can extend it. --- tests/ipopo/test_contexts.py | 123 +++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/tests/ipopo/test_contexts.py b/tests/ipopo/test_contexts.py index a577ccd4..fa3f8d96 100644 --- a/tests/ipopo/test_contexts.py +++ b/tests/ipopo/test_contexts.py @@ -16,6 +16,9 @@ # iPOPO import pelix.ipopo.constants as constants import pelix.ipopo.contexts as contexts +from pelix.ipopo.decorators import ComponentFactory, Provides, Property +from pelix.ipopo.constants import use_ipopo +from pelix.utilities import use_service # Standard library try: @@ -29,6 +32,57 @@ # ------------------------------------------------------------------------------ +FACTORY_PARENT = 'parent' +FACTORY_ALL = "child.all" +FACTORY_NO_PROVIDE = "factory.child.no_provide" +FACTORY_EXTEND_PROVIDE = FACTORY_REPLACE_PROVIDE = "child.extend_provide" +FACTORY_REPLACE_PROVIDE = "child.replace_provide" + +SPEC_PARENT = 'spec.parent' +SPEC_CHILD = 'spec.child' + +@ComponentFactory(FACTORY_PARENT) +@Provides(SPEC_PARENT) +@Property('parent_prop', "prop.parent", "parent.value") +class ParentFactory(object): + """ + Parent factory, providing a service with a property + """ + pass + +@ComponentFactory(FACTORY_ALL) +class ChildAll(ParentFactory): + """ + Child factory, inheriting everything from its parent + """ + pass + +@ComponentFactory(FACTORY_NO_PROVIDE, excluded=Provides.HANDLER_ID) +class ChildNoProvides(ParentFactory): + """ + Child factory, removing the provided service + """ + pass + + +@ComponentFactory(FACTORY_EXTEND_PROVIDE) +@Provides(SPEC_CHILD) +class ChildExtendProvides(ParentFactory): + """ + Child factory, replacing the provided service + """ + pass + +@ComponentFactory(FACTORY_REPLACE_PROVIDE, excluded=Provides.HANDLER_ID) +@Provides(SPEC_CHILD) +class ChildReplaceProvides(ParentFactory): + """ + Child factory, replacing the provided service + """ + pass + +# ------------------------------------------------------------------------------ + class ContextsTests(unittest.TestCase): """ Tests the behavior of iPOPO contexts classes @@ -49,6 +103,41 @@ def tearDown(self): FrameworkFactory.delete_framework(self.framework) + def assertProvides(self, specification, provider): + """ + Asserts that the given service is provided and is the given object + """ + context = self.framework.get_bundle_context() + svc_refs = context.get_all_service_references(specification) + if not svc_refs: + self.fail("Service {0} not registered".format(specification)) + + for svc_ref in svc_refs: + with use_service(context, svc_ref) as svc: + if svc is provider: + # Found it + break + + else: + self.fail("Service {0} is not provided by {1}"\ + .format(specification, provider)) + + + def assertNotProvides(self, specification, provider): + """ + Asserts that the given service is not provided by the given provider + """ + context = self.framework.get_bundle_context() + svc_refs = context.get_all_service_references(specification) + if svc_refs: + for svc_ref in svc_refs: + with use_service(context, svc_ref) as svc: + if svc is provider: + # Found it + self.fail("Service {0} is provided by {1}"\ + .format(specification, provider)) + + def testRequirement(self): """ Tests the Requirement class type checking @@ -164,6 +253,40 @@ def testCopyFactoryContext(self): self.assertEqual(context, context_2, "Copy equality error") self.assertIsNot(req_1, context_2, "Requirements must be copied") + + def testHandlerInheritance(self): + """ + Tests the inheritance of handlers + """ + # Register factories + context = self.framework.get_bundle_context() + with use_ipopo(context) as ipopo: + for factory in (ChildAll, ChildNoProvides, ChildExtendProvides, + ChildReplaceProvides): + ipopo.register_factory(context, factory) + + # Check behavior of "child all" + component = ipopo.instantiate(FACTORY_ALL, 'all', {}) + self.assertProvides(SPEC_PARENT, component) + self.assertNotProvides(SPEC_CHILD, component) + + # No service provided + component = ipopo.instantiate(FACTORY_NO_PROVIDE, 'no_service', {}) + self.assertNotProvides(SPEC_PARENT, component) + self.assertNotProvides(SPEC_CHILD, component) + + # Service replaced + component = ipopo.instantiate(FACTORY_REPLACE_PROVIDE, + 'replacement', {}) + self.assertNotProvides(SPEC_PARENT, component) + self.assertProvides(SPEC_CHILD, component) + + # Service added + component = ipopo.instantiate(FACTORY_EXTEND_PROVIDE, + 'addition', {}) + self.assertProvides(SPEC_PARENT, component) + self.assertProvides(SPEC_CHILD, component) + # ------------------------------------------------------------------------------ if __name__ == "__main__":