Back to index

plone3  3.1.7
test_components.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Component registry export / import support unit tests.
00014 
00015 $Id: test_components.py 79597 2007-09-12 17:47:57Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from AccessControl import ClassSecurityInfo
00022 from Acquisition import aq_base
00023 from Globals import InitializeClass
00024 from OFS.Folder import Folder
00025 from OFS.SimpleItem import SimpleItem
00026 from Products.Five.component import enableSite
00027 from Products.Five.component.interfaces import IObjectManagerSite
00028 from zope.app.component.hooks import setSite, clearSite, setHooks
00029 from zope.component import getSiteManager
00030 from zope.component import queryUtility
00031 from zope.component.globalregistry import base
00032 from zope.interface import implements
00033 from zope.interface import Interface
00034 
00035 from Products.GenericSetup.testing import BodyAdapterTestCase
00036 from Products.GenericSetup.testing import ExportImportZCMLLayer
00037 
00038 try:
00039     from five.localsitemanager.registry import PersistentComponents
00040 except ImportError:
00041     # Avoid generating a spurious dependency
00042     PersistentComponents = None
00043 
00044 def createComponentRegistry(context):
00045     enableSite(context, iface=IObjectManagerSite)
00046 
00047     components = PersistentComponents()
00048     components.__bases__ = (base,)
00049     components.__parent__ = aq_base(context)
00050     context.setSiteManager(components)
00051 
00052 class IDummyInterface(Interface):
00053     """A dummy interface."""
00054 
00055     def verify():
00056         """Returns True."""
00057 
00058 class DummyUtility(object):
00059     """A dummy utility."""
00060 
00061     implements(IDummyInterface)
00062 
00063     def verify(self):
00064         return True
00065 
00066 
00067 class DummyTool(SimpleItem):
00068     """A dummy tool."""
00069     implements(IDummyInterface)
00070 
00071     id = 'dummy_tool'
00072     meta_type = 'dummy tool'
00073     security = ClassSecurityInfo()
00074 
00075     security.declarePublic('verify')
00076     def verify(self):
00077         return True
00078 
00079 InitializeClass(DummyTool)
00080 
00081 
00082 class DummyTool2(SimpleItem):
00083     """A second dummy tool."""
00084     implements(IDummyInterface)
00085 
00086     id = 'dummy_tool2'
00087     meta_type = 'dummy tool2'
00088     security = ClassSecurityInfo()
00089 
00090     security.declarePublic('verify')
00091     def verify(self):
00092         return True
00093 
00094 InitializeClass(DummyTool2)
00095 
00096 
00097 _COMPONENTS_BODY = """\
00098 <?xml version="1.0"?>
00099 <componentregistry>
00100  <adapters/>
00101  <utilities>
00102   <utility factory="Products.GenericSetup.tests.test_components.DummyUtility"
00103      interface="Products.GenericSetup.tests.test_components.IDummyInterface"/>
00104   <utility name="dummy tool name"
00105      interface="Products.GenericSetup.tests.test_components.IDummyInterface"
00106      object="dummy_tool"/>
00107   <utility name="dummy tool name2"
00108      interface="Products.GenericSetup.tests.test_components.IDummyInterface"
00109      object="dummy_tool2"/>
00110   <utility name="foo"
00111      factory="Products.GenericSetup.tests.test_components.DummyUtility"
00112      interface="Products.GenericSetup.tests.test_components.IDummyInterface"/>
00113  </utilities>
00114 </componentregistry>
00115 """
00116 
00117 
00118 class ComponentRegistryXMLAdapterTests(BodyAdapterTestCase):
00119 
00120     layer = ExportImportZCMLLayer
00121 
00122     def _getTargetClass(self):
00123         from Products.GenericSetup.components import \
00124             ComponentRegistryXMLAdapter
00125         return ComponentRegistryXMLAdapter
00126 
00127     def _populate(self, obj):
00128         obj.registerUtility(DummyUtility(), IDummyInterface)
00129         obj.registerUtility(DummyUtility(), IDummyInterface, name=u'foo')
00130 
00131         tool = aq_base(obj.aq_parent['dummy_tool'])
00132         obj.registerUtility(tool, IDummyInterface, name=u'dummy tool name')
00133 
00134         tool2 = aq_base(obj.aq_parent['dummy_tool2'])
00135         obj.registerUtility(tool2, IDummyInterface, name=u'dummy tool name2')
00136 
00137     def _verifyImport(self, obj):
00138         util = queryUtility(IDummyInterface, name=u'foo')
00139         self.failUnless(IDummyInterface.providedBy(util))
00140         self.failUnless(util.verify())
00141 
00142         util = queryUtility(IDummyInterface)
00143         self.failUnless(IDummyInterface.providedBy(util))
00144         self.failUnless(util.verify())
00145 
00146         util = queryUtility(IDummyInterface, name='dummy tool name')
00147         self.failUnless(IDummyInterface.providedBy(util))
00148         self.failUnless(util.verify())
00149         self.assertEqual(util.meta_type, 'dummy tool')
00150 
00151         # make sure we can get the tool by normal means
00152         tool = getattr(obj.aq_parent, 'dummy_tool')
00153         self.assertEqual(tool.meta_type, 'dummy tool')
00154         self.assertEquals(repr(aq_base(util)), repr(aq_base(tool)))
00155 
00156         util = queryUtility(IDummyInterface, name='dummy tool name2')
00157         self.failUnless(IDummyInterface.providedBy(util))
00158         self.failUnless(util.verify())
00159         self.assertEqual(util.meta_type, 'dummy tool2')
00160 
00161         # make sure we can get the tool by normal means
00162         tool = getattr(obj.aq_parent, 'dummy_tool2')
00163         self.assertEqual(tool.meta_type, 'dummy tool2')
00164         self.assertEquals(repr(aq_base(util)), repr(aq_base(tool)))
00165 
00166     def setUp(self):
00167         # Create and enable a local component registry
00168         site = Folder()
00169         createComponentRegistry(site)
00170         setHooks()
00171         setSite(site)
00172         sm = getSiteManager()
00173 
00174         tool = DummyTool()
00175         site._setObject(tool.id, tool)
00176 
00177         tool2 = DummyTool2()
00178         site._setObject(tool2.id, tool2)
00179 
00180         self._obj = sm
00181         self._BODY = _COMPONENTS_BODY
00182 
00183     def tearDown(self):
00184         clearSite()
00185 
00186 if PersistentComponents is not None:
00187     def test_suite():
00188         # reimport to make sure tests are run from Products
00189         from Products.GenericSetup.tests.test_components \
00190                 import ComponentRegistryXMLAdapterTests
00191 
00192         return unittest.TestSuite((
00193             unittest.makeSuite(ComponentRegistryXMLAdapterTests),
00194             ))
00195 else:
00196     def test_suite():
00197         return unittest.TestSuite()
00198 
00199 if __name__ == '__main__':
00200     from Products.GenericSetup.testing import run
00201     run(test_suite())