[Zope-Checkins] CVS: Zope3/lib/python/Zope/ComponentArchitecture/tests - testServiceManagerContainer.py:1.1.2.1
Paul Everitt
paul@zope.com
Thu, 28 Feb 2002 14:56:37 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/ComponentArchitecture/tests
In directory cvs.zope.org:/tmp/cvs-serv4153/tests
Added Files:
Tag: Zope-3x-branch
testServiceManagerContainer.py
Log Message:
Added unit tests for IServiceManagerContainer and added a query method
for IServiceManagerContainer. Also clarified what the get method does
when there isn't a service manager.
=== Added File Zope3/lib/python/Zope/ComponentArchitecture/tests/testServiceManagerContainer.py ===
##############################################################################
#
# 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: testServiceManagerContainer.py,v 1.1.2.1 2002/02/28 19:56:37 paul Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from Zope.ComponentArchitecture.IServiceService import IServiceService
from Zope.ComponentArchitecture.IServiceManagerContainer import IServiceManagerContainer
from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
_marker = object()
class ServiceManager:
__implements__ = IServiceService
############################################################
# Implementation methods for interface
# Zope.ComponentArchitecture.IServiceService.
def getService(self, object, name):
'''See interface IServiceService'''
raise ComponentLookupError(name)
def getServiceDefinitions(self):
'''See interface IServiceService'''
return ()
#
############################################################
class ServiceManagerContainer:
__implements__ = IServiceManagerContainer
############################################################
# Implementation methods for interface
# Zope.ComponentArchitecture.IServiceManagerContainer.
def hasServiceManager(self):
'''See interface IServiceManagerContainer'''
return hasattr(self, 'sm')
def getServiceManager(self, default=_marker):
'''See interface IServiceManagerContainer'''
try:
return self.sm
except AttributeError:
if default is _marker:
raise ComponentLookupError
else:
return default
def setServiceManager(self, sm):
'''See interface IServiceManagerContainer'''
if IServiceService.isImplementedBy(sm):
self.sm = sm
else:
raise ValueError('setServiceManager requires an IServiceService')
#
############################################################
class Test(CleanUp, TestCase):
"""This test is for objects that don't have service managers by
default and that always give back the service manager they were
given."""
def _createInstance(self):
return ServiceManagerContainer()
def testHas(self):
smc=self._createInstance()
self.failIf(smc.hasServiceManager())
smc.setServiceManager(ServiceManager())
self.failUnless(smc.hasServiceManager())
def testGet(self):
smc=self._createInstance()
self.failUnless(smc.getServiceManager(self) is self)
self.assertRaises(ComponentLookupError, smc.getServiceManager)
sm=ServiceManager()
smc.setServiceManager(sm)
self.failUnless(smc.getServiceManager() is sm)
self.failUnless(smc.getServiceManager(self) is sm)
def testSet(self):
smc=self._createInstance()
self.assertRaises(Exception, smc.setServiceManager, self)
def test_suite():
return TestSuite((
makeSuite(Test),
))
if __name__=='__main__':
main(defaultTest='test_suite')