[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/ServiceManager/tests - PlacefulSetup.py:1.2 __init__.py:1.2 testServiceManager.py:1.2

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 19:28:44 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/ServiceManager/tests
In directory cvs.zope.org:/tmp/cvs-serv17445/lib/python/Zope/App/OFS/Services/ServiceManager/tests

Added Files:
	PlacefulSetup.py __init__.py testServiceManager.py 
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.


=== Zope3/lib/python/Zope/App/OFS/Services/ServiceManager/tests/PlacefulSetup.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
+
+class PlacefulSetup(PlacelessSetup):
+    
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+        # set up placeful hooks, saving originals for tearDown
+        from Zope import ComponentArchitecture as CA
+        self.__old_getServiceManager_hook=CA.getServiceManager_hook
+        self.__old_getNextServiceManager_hook=CA.getNextServiceManager_hook
+        from Zope.App.OFS.Services.ServiceManager import hooks
+        CA.getServiceManager_hook=hooks.getServiceManager_hook
+        CA.getNextServiceManager_hook=hooks.getNextServiceManager_hook
+
+    def buildFolders(self):
+        # set up a reasonably complex folder structure
+        #
+        #     ____________ rootFolder ____________
+        #    /                                    \
+        # folder1 __________________            folder2
+        #   |                       \             |
+        # folder1_1 ____           folder1_2    folder2_1
+        #   |           \            |            |
+        # folder1_1_1 folder1_1_2  folder1_2_1  folder2_1_1
+        from Zope.App.OFS.Content.Folder.Folder import Folder
+        from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+        from Zope.Proxy.ContextWrapper import ContextWrapper
+        # top
+        self.rootFolder=RootFolder()
+        # level 1
+        self.folder1=Folder()
+        self.rootFolder.setObject("folder1",self.folder1)
+        self.folder1=ContextWrapper(self.folder1, self.rootFolder,
+             name="folder1")
+        self.folder2=Folder()
+        self.rootFolder.setObject("folder2",self.folder2)
+        self.folder2=ContextWrapper(self.folder2, self.rootFolder,
+             name="folder2")
+        # level 2
+        self.folder1_1=Folder()
+        self.folder1.setObject("folder1_1",self.folder1_1)
+        self.folder1_1=ContextWrapper(self.folder1_1, self.folder1,
+             name="folder1_1")
+        self.folder1_2=Folder()
+        self.folder1.setObject("folder1_2",self.folder1_2)
+        self.folder1_2=ContextWrapper(self.folder1_2, self.folder1,
+             name="folder1_2")
+        self.folder2_1=Folder()
+        self.folder2.setObject("folder2_1",self.folder2_1)
+        self.folder2_1=ContextWrapper(self.folder2_1, self.folder2,
+             name="folder2_1")
+        # level 3
+        self.folder1_1_1=Folder()
+        self.folder1_1.setObject("folder1_1_1",self.folder1_1_1)
+        self.folder1_1_1=ContextWrapper(self.folder1_1_1, self.folder1_1,
+             name="folder1_1_1")
+        self.folder1_1_2=Folder()
+        self.folder1_1.setObject("folder1_1_2",self.folder1_1_2)
+        self.folder1_1_2=ContextWrapper(self.folder1_1_2, self.folder1_1,
+             name="folder1_1_2")
+        self.folder1_2_1=Folder()
+        self.folder1_2.setObject("folder1_2_1",self.folder1_2_1)
+        self.folder1_2_1=ContextWrapper(self.folder1_2_1, self.folder1_2,
+             name="folder1_2_1")
+        self.folder2_1_1=Folder()
+        self.folder2_1.setObject("folder2_1_1",self.folder2_1_1)
+        self.folder2_1_1=ContextWrapper(self.folder2_1_1, self.folder2_1,
+             name="folder2_1_1")
+
+    def createServiceManager(self, folder=None):
+        if folder is None: folder=self.rootFolder
+        from Zope.App.OFS.Services.ServiceManager.ServiceManager import ServiceManager
+        folder.setServiceManager(ServiceManager())
+
+    def tearDown(self):
+        # clean up folders and placeful service managers and services too?
+        from Zope import ComponentArchitecture as CA
+        CA.getServiceManager_hook=self.__old_getServiceManager_hook
+        CA.getNextServiceManager_hook=self.__old_getNextServiceManager_hook
+        PlacelessSetup.tearDown(self)
+


=== Zope3/lib/python/Zope/App/OFS/Services/ServiceManager/tests/__init__.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+


=== Zope3/lib/python/Zope/App/OFS/Services/ServiceManager/tests/testServiceManager.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+from unittest import TestCase, TestLoader, TextTestRunner
+from Zope.App.OFS.Container.tests.testIContainer import BaseTestIContainer
+
+from Interface import Interface
+from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+from Zope.App.OFS.Content.Folder.Folder import Folder
+from Zope.Proxy.ContextWrapper import getWrapperContext
+from Zope.App.OFS.Services.ServiceManager.ServiceManager import ServiceManager
+from Zope.ComponentArchitecture import getService, getServiceManager
+from Zope.Exceptions import ZopeError
+from PlacefulSetup import PlacefulSetup
+
+class ITestService(Interface): pass
+
+class TestService:
+
+    __implements__ = ITestService
+
+class ServiceManagerTests(PlacefulSetup, BaseTestIContainer, TestCase):
+
+    def setUp(self):
+        PlacefulSetup.setUp(self)
+        self.buildFolders()
+
+    def _Test__new(self):
+        return ServiceManager()
+
+    def testAddService(self):
+        sm = ServiceManager()
+        self.rootFolder.setServiceManager(sm)
+        sm=getServiceManager(self.rootFolder)
+        ts = TestService()
+        sm.setObject('test_service1', ts)
+        self.assertEqual(sm['test_service1'], ts)
+
+    def testGetService(self):
+        sm = ServiceManager()
+        self.rootFolder.setServiceManager(sm)
+        sm=getServiceManager(self.rootFolder)
+        ts = TestService()
+        sm.setObject('test_service1', ts)
+        sm.bindService('test_service', 'test_service1')
+        testOb=getService(self.rootFolder, 'test_service')
+        self.assertEqual(getWrapperContext
+             (getWrapperContext(testOb)),self.rootFolder)
+        self.assertEqual(testOb, ts)
+
+    def testUnbindService(self):
+        sm = ServiceManager()
+        self.rootFolder.setServiceManager(sm)
+        sm=getServiceManager(self.rootFolder)
+        ts = TestService()
+        root_ts = TestService()
+        gsm=getServiceManager(None)
+        gsm.defineService('test_service', ITestService)
+        gsm.provideService('test_service', root_ts)
+
+        sm.setObject('test_service1', ts)
+        sm.bindService('test_service', 'test_service1')
+        self.assertEqual(getService(self.rootFolder, 'test_service'), ts)
+        sm.unbindService('test_service')
+        self.assertEqual(getService(self.rootFolder, 'test_service'), root_ts)
+
+    def testDeleteService(self):
+        self.rootFolder.setServiceManager(ServiceManager())
+        sm=getServiceManager(self.rootFolder)
+        ts = TestService()
+        
+        sm.setObject('test_service1', ts)
+        sm.bindService('test_service', 'test_service1')
+        self.assertEqual(getService(self.rootFolder, 'test_service'), ts)
+        self.assertRaises(ZopeError, sm.__delitem__, 'test_service1')
+    
+    def testContextServiceLookup(self):
+        self.rootFolder.setServiceManager(ServiceManager())
+        sm=getServiceManager(self.rootFolder)
+        ts = TestService()
+        sm.setObject('test_service1', ts)
+        sm.bindService('test_service', 'test_service1')
+        self.assertEqual(getService(self.folder1, 'test_service'), ts)
+        self.assertEqual(getService(self.folder1_1, 'test_service'), ts)
+
+    def testContextServiceLookupWithMultipleServiceManagers(self):
+        self.rootFolder.setServiceManager(ServiceManager())
+        sm=getServiceManager(self.rootFolder)
+        ts = TestService()
+        sm.setObject('test_service1', ts)
+        sm.bindService('test_service', 'test_service1')
+
+        self.folder1.setServiceManager(ServiceManager())
+        sm2=getServiceManager(self.folder1)
+        
+        self.assertEqual(getService(self.folder1, 'test_service'), ts)
+
+    def testComponentArchitectureServiceLookup(self):
+        self.rootFolder.setServiceManager(ServiceManager())
+        sm=getServiceManager(self.rootFolder)
+        self.folder1.setServiceManager(ServiceManager())
+        sm2=getServiceManager(self.folder1)
+        
+        ts = TestService()
+
+        globsm=getServiceManager(None)
+        globsm.defineService('test_service', ITestService)
+        globsm.provideService('test_service', ts)
+
+        service = getService(self.folder1, 'test_service')
+        self.assertEqual(service, ts)
+
+
+        
+def test_suite():
+    loader=TestLoader()
+    return loader.loadTestsFromTestCase(ServiceManagerTests)
+
+if __name__=='__main__':
+    TextTestRunner().run(test_suite())