[Zope3-checkins] CVS: Zope3/src/zope/component/tests - __init__.py:1.1.2.1 placelesssetup.py:1.1.2.1 request.py:1.1.2.1 test_api.py:1.1.2.1 test_components.py:1.1.2.1 test_factory.py:1.1.2.1 test_providefactory.py:1.1.2.1 test_resources.py:1.1.2.1 test_service.py:1.1.2.1 test_skins.py:1.1.2.1 test_utilityservice.py:1.1.2.1 test_views.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:32:43 -0500


Update of /cvs-repository/Zope3/src/zope/component/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/component/tests

Added Files:
      Tag: NameGeddon-branch
	__init__.py placelesssetup.py request.py test_api.py 
	test_components.py test_factory.py test_providefactory.py 
	test_resources.py test_service.py test_skins.py 
	test_utilityservice.py test_views.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/component/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/component/tests/placelesssetup.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: placelesssetup.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""

# A mix-in class inheriting from CleanUp that also connects the CA services

from zope.testing.cleanup import CleanUp
from zope.component import getServiceManager

class PlacelessSetup(CleanUp):
    def setUp(self):
        CleanUp.setUp(self)
        sm=getServiceManager(None)
        defineService=sm.defineService
        provideService=sm.provideService
        # factory service
        from zope.component.interfaces import IFactoryService
        defineService('Factories',IFactoryService)
        from zope.component.GlobalFactoryService import\
             factoryService
        provideService('Factories', factoryService)
        # utility service
        from zope.component.interfaces import IUtilityService
        defineService('Utilities',IUtilityService)
        from zope.component.GlobalUtilityService import\
             utilityService
        provideService('Utilities', utilityService)
        # adapter service
        from zope.component.interfaces import IAdapterService
        defineService('Adapters',IAdapterService)
        from zope.component.GlobalAdapterService import\
             adapterService
        provideService('Adapters', adapterService)
        # resource service
        from zope.component.interfaces import IResourceService
        defineService('Resources',IResourceService)
        from zope.component.GlobalResourceService import\
             resourceService
        provideService('Resources', resourceService)
        # skin service
        from zope.component.interfaces import ISkinService
        defineService('Skins',ISkinService)
        from zope.component.GlobalSkinService import\
             skinService
        provideService('Skins', skinService)
        # view service
        from zope.component.interfaces import IViewService
        defineService('Views',IViewService)
        from zope.component.GlobalViewService import\
             viewService
        provideService('Views', viewService)
    def tearDown(self):
        CleanUp.tearDown(self)


=== Added File Zope3/src/zope/component/tests/request.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.
# 
##############################################################################
"""

$Id: request.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""

class Request:

    def __init__(self, iface, skin=''):
        self._iface     = iface
        self._skin      = skin

    # Implementation methods for interface
    # Zope.ComponentArchitecture.IPresentationService.IPresentationRequest

    def getPresentationSkin(self):
        '''See interface IPresentationRequest'''

        return self._skin

    def getPresentationType(self):
        '''See interface IPresentationRequest'''
       
        return self._iface 



=== Added File Zope3/src/zope/component/tests/test_api.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.
# 
##############################################################################
import unittest, sys
from zope.interface import Interface
from zope.component.tests.request import Request


class I1(Interface): pass
class I2(Interface): pass
class I3(Interface): pass
class Comp:
    __implements__ = I2
    def __init__(self, context, request=None): self.context = context
class Comp2:
    __implements__ = I3
    def __init__(self, context, request=None): self.context = context

comp = Comp(1)

class Ob:
    __implements__ = I1

ob = Ob()


class R1(Interface): pass
class R12(Interface): pass
class R2(R1): pass
class R3(R2): pass
class R4(R3): pass

class P1(Interface): pass
class P2(P1): pass
class P3(P2): pass
class P4(P3): pass

class default_P3: pass
class any_P3: pass
class R2_P3: pass

from zope.component.tests.placelesssetup import PlacelessSetup

class Test(PlacelessSetup, unittest.TestCase):

    def testAdapter(self):
        from zope.component \
             import getAdapter, getService, queryAdapter
        from zope.component.exceptions import ComponentLookupError

        # if an object implements the interface you want to adapt to,
        # getAdapter should simply return the object
        self.assertEquals(getAdapter(ob, I1), ob)
        
        # if an adapter isn't registered for the given object and interface,
        # and you provide no default, raise ComponentLookupError...
        self.assertRaises(ComponentLookupError, getAdapter, ob, I2)
        
        # ...otherwise, you get the default
        self.assertEquals(queryAdapter(ob, I2, Test), Test)
        
        getService(None, 'Adapters').provideAdapter(I1, I2, Comp)
        c = getAdapter(ob, I2)
        self.assertEquals(c.__class__, Comp)
        self.assertEquals(c.context, ob)

    def testNamedAdapter(self):

        self.testAdapter()
        
        from zope.component \
             import getAdapter, getService, queryAdapter
        from zope.component.exceptions import ComponentLookupError

        # if an object implements the interface you want to adapt to,
        # getAdapter should simply return the object UNLESS we are sking for a
        # names adapter.
        self.assertRaises(ComponentLookupError, getAdapter, ob, I1, 'test')
        
        # if an adapter isn't registered for the given object and interface,
        # and you provide no default, raise ComponentLookupError...
        self.assertRaises(ComponentLookupError, getAdapter, ob, I2, 'test')
        
        # ...otherwise, you get the default
        self.assertEquals(queryAdapter(ob, I2, Test, name='test'), Test)

        class Comp2(Comp): pass
        
        getService(None, 'Adapters').provideAdapter(I1, I2, Comp2, name='test')
        c = getAdapter(ob, I2, name='test')
        self.assertEquals(c.__class__, Comp2)
        self.assertEquals(c.context, ob)

    def testMultipleAdapterFactories(self):
        from zope.component import getAdapter, getService

        # Basically, this represents a 2-stage adaptation. You can get
        # from I1 to I2 by way of adapter Comp adapting Comp2
        getService(None, 'Adapters').provideAdapter(I1, I2, [Comp2, Comp])
        c = getAdapter(ob, I2)
        self.assertEquals(c.__class__, Comp)
        self.assertEquals(c.context.context, ob)

    def testAdapterForInterfaceNone(self):
        from zope.component import getAdapter, getService
        
        # providing an adapter for None says that your adapter can
        # adapt anything to I2.
        getService(None, 'Adapters').provideAdapter(None, I2, Comp)
        c = getAdapter(ob, I2)
        self.assertEquals(c.__class__, Comp)
        self.assertEquals(c.context, ob)

    def testUtility(self):
        from zope.component import getUtility, queryUtility
        from zope.component import getService
        from zope.component.exceptions import ComponentLookupError

        self.assertRaises(ComponentLookupError, getUtility, ob, I1)
        self.assertRaises(ComponentLookupError, getUtility, ob, I2)
        self.assertEquals(queryUtility(ob, I2, Test), Test)
        
        getService(None, 'Utilities').provideUtility(I2, comp)
        self.assertEquals(id(getUtility(ob, I2)), id(comp))

    def testNamedUtility(self):
        from zope.component import getUtility, queryUtility
        from zope.component import getService
        from zope.component.exceptions import ComponentLookupError

        self.testUtility()

        self.assertRaises(ComponentLookupError, getUtility, ob, I1, 'test')
        self.assertRaises(ComponentLookupError, getUtility, ob, I2, 'test')
        self.assertEquals(queryUtility(ob, I2, Test, 'test'), Test)
        
        getService(None, 'Utilities').provideUtility(I2, comp, 'test')
        self.assertEquals(id(getUtility(ob, I2, 'test')), id(comp))

    def testView(self):
        from zope.component import getView, queryView, getService
        from zope.component.exceptions import ComponentLookupError

        self.assertRaises(ComponentLookupError,
                          getView, ob, 'foo', Request(I1))
        self.assertRaises(ComponentLookupError,
                          getView, ob, 'foo', Request(I2))
        self.assertEquals(queryView(ob, 'foo', Request(I2), Test), Test)
        
        getService(None, 'Views').provideView(I1, 'foo', I2, [Comp])
        c = getView(ob, 'foo', Request(I2))
        self.assertEquals(c.__class__, Comp)
        self.assertEquals(c.context, ob)

        self.assertRaises(ComponentLookupError,
                          getView, ob, 'foo2', Request(I1))
        self.assertRaises(ComponentLookupError,
                          getView, ob, 'foo2', Request(I2))
        self.assertEquals(queryView(ob, 'foo2', Request(I2), Test), Test)

        self.assertEquals(queryView( ob, 'foo2', Request(I1), None), None)    

    def testDefaultViewName(self):
        from zope.component import getService
        from zope.exceptions import NotFoundError
        viewService = getService(None, 'Views')
        self.assertRaises(NotFoundError,
                          viewService.getDefaultViewName,
                          ob, Request(I1))
        viewService.setDefaultViewName(I1, I2, 'sample_name')
        self.assertEquals(viewService.getDefaultViewName(ob, Request(I2)),
                          'sample_name')
        self.assertRaises(NotFoundError,
                          viewService.getDefaultViewName,
                          ob, Request(I1))




    # The following tests are copied from
    # Interface.Registry.tests.IAdapterRegistry

    
    def __registery(self):
        from zope.component.adapter \
             import GlobalAdapterService
        
        registry = GlobalAdapterService()
        

        registry.provideAdapter(None, P3, [default_P3])
        registry.provideAdapter(Interface, P3, [any_P3])
        registry.provideAdapter(R2, P3, [R2_P3])

        return registry


    def test_getRegisteredMatching_all(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching())
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_R1(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            for_interfaces = (R1, )
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_multiple(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            for_interfaces = (R12, R2)
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_provided_P1(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            provided_interfaces = (P1, )
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_provided_P2(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            provided_interfaces = (P3, )
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_and_provided_1(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            for_interfaces = (R4, R12),
            provided_interfaces = (P1, ),
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)

    def test_getRegisteredMatching_for_and_provided_2(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            for_interfaces = (R4, R12),
            provided_interfaces = (P3, ),
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)
        

    def test_getRegisteredMatching_for_and_provided_exact(self):
        registry = self.__registery()
        
        got = list(registry.getRegisteredMatching(
            for_interfaces = (R2, ),
            provided_interfaces = (P3, ),
            ))
        got.sort()
        expect = [
            ('', None, P3, [default_P3]),
            ('', Interface, P3, [any_P3]),
            ('', R2, P3, [R2_P3]),
            ]
        expect.sort()
        self.assertEqual(got, expect)



        

def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/component/tests/test_components.py ===
##############################################################################
#
# Copyright (c) 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.
# 
##############################################################################
"""

$Id: test_components.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""
__metaclass__ = type # All classes are new style when run with Python 2.2+

from zope.interface import Interface
from zope.interface.element import Attribute

class IApp(Interface):
    a = Attribute('test attribute')
    def f(): "test func"

class IContent(Interface): pass

class Content: __implements__ = IContent

class Comp:
    __used_for__ = IContent
    __implements__ = IApp

    a=1
    def f(): pass

comp = Comp()



=== Added File Zope3/src/zope/component/tests/test_factory.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.
# 
##############################################################################
"""

$Id: test_factory.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""
from zope.component.interfaces import IFactory
from zope.interface import Interface

class IX(Interface):
    """the dummy interface which class X supposedly implements,
    according to the factory"""

class X:
    __implements__=IX
    def __init__(self, *args, **kwargs):
        self.args=args
        self.kwargs=kwargs


class ClassFactoryWrapper:
    __implements__ = IFactory
    def __init__(self, klass):
        self.__klass=klass
    def __call__(self, *args, **kwargs):
        return self.__klass(*args, **kwargs)
    def getInterfaces(self):
        return getattr(self.__klass,'__implements__', None)

f=ClassFactoryWrapper(X)

=== Added File Zope3/src/zope/component/tests/test_providefactory.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.
# 
##############################################################################
"""Test the provideFactory method.

$Id: test_providefactory.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""


from unittest import TestCase, TestSuite, main, makeSuite
from zope.component.tests.placelesssetup import PlacelessSetup


class ProvideFactoryTestCase(PlacelessSetup, TestCase):

    def test_provide_factory(self):
        from zope.component import getService, createObject
        from zope.component.tests.test_factory import f, X, IX
        factories=getService(None, 'Factories')
        factories.provideFactory("Some.Object", f)
        thing = createObject(None,"Some.Object")
        self.assert_(isinstance(thing, X))


def test_suite():
    return makeSuite(ProvideFactoryTestCase)

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/src/zope/component/tests/test_resources.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.
# 
##############################################################################
"""

$Id: test_resources.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""

import unittest, sys

from zope.component import getService
from zope.component.tests.placelesssetup import PlacelessSetup
from zope.component import getResource, queryResource
from zope.component.exceptions import ComponentLookupError 
from zope.interface import Interface
from zope.component.tests.request import Request

class Test(PlacelessSetup, unittest.TestCase):

    def testSkin(self):
        class I2(Interface): pass
        class C1:
            def __init__(self, request): pass
            __implements__ = I2
        class C2(C1): pass

        getService(None,'Resources').provideResource('test', I2, C1)
        self.assertEqual(getResource(None, 'test', Request(I2)).__class__, C1) 
        getService(None,'Skins').defineSkin('foo', I2, ('foo', 'default'))
        self.assertEqual(
            getResource(None, 'test', Request(I2, 'foo')).__class__,
            C1) 
        getService(None,'Resources').provideResource('test', I2, C2,
                                                     layer='foo')
        self.assertEqual(
            getResource(None, 'test', Request(I2, 'foo')).__class__,
            C2) 

    def testGetRequestResourceMethod(self):
        class I2(Interface): pass
        class C1:
            def __init__(self, request): pass

            __implements__ = I2
        class C2(C1): pass
        

        getService(None,'Resources').provideResource('test', I2, C1)
        self.assertEqual(
            getResource(None, 'test', Request(I2, 'default') ).__class__,
            C1) 
        getService(None,'Skins').defineSkin('foo', I2, ('foo', 'default'))
        self.assertEqual(
            getResource(None, 'test', Request(I2, 'foo')).__class__,
            C1)
        getService(None,'Resources').provideResource('test', I2, C2,
                                                     layer='foo')
        self.assertEqual(
            getResource(None, 'test', Request(I2, 'foo')).__class__,
            C2)

        self.assertRaises(
            ComponentLookupError,  
            getResource, None, 'test2', Request(I2, 'foo'))

        self.assertEqual(
            queryResource(None, 'test2', Request(I2, 'foo'), None),
            None)

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/component/tests/test_service.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.
#
##############################################################################
"""

$Id: test_service.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""

import unittest
from zope.interface import Interface

from zope.exceptions import DuplicationError
from zope.testing.cleanup import CleanUp

from zope.component \
     import getServiceDefinitions, getService, getServiceManager
from zope.component.service \
     import UndefinedService, InvalidService
from zope.component.exceptions import ComponentLookupError

from zope.component import queryService

class IOne(Interface):
    pass

class ITwo(Interface):
    pass

class ServiceOne:
    __implements__ = IOne

class ServiceTwo:
    __implements__ = ITwo

class Test(CleanUp, unittest.TestCase):

    def testNormal(self):
        getServiceManager(None).defineService('one', IOne)
        c = ServiceOne()
        getServiceManager(None).provideService('one', c)
        self.assertEqual(id(getService(None, 'one')), id(c))

    def testFailedLookup(self):
        self.assertRaises(ComponentLookupError, getService, None, 'two')
        self.assertEqual(queryService(None, 'two'), None)

    def testDup(self):
        getServiceManager(None).defineService('one', IOne)
        self.assertRaises(DuplicationError,
                          getServiceManager(None).defineService,
                          'one', ITwo)

        c = ServiceOne()
        getServiceManager(None).provideService('one', c)

        c2 = ServiceOne()
        self.assertRaises(DuplicationError,
                          getServiceManager(None).provideService,
                          'one', c2)

        self.assertEqual(id(getService(None, 'one')), id(c))


    def testUndefined(self):
        c = ServiceOne()
        self.assertRaises(UndefinedService,
                          getServiceManager(None).provideService,
                          'one', c)

    def testInvalid(self):
        getServiceManager(None).defineService('one', IOne)
        getServiceManager(None).defineService('two', ITwo)
        c = ServiceOne()
        self.assertRaises(InvalidService,
                          getServiceManager(None).provideService,
                          'two', c)

    def testGetService(self):
        # Testing looking up a service from a service manager container that
        # doesn't have a service manager.
        getServiceManager(None).defineService('one', IOne)
        c = ServiceOne()
        getServiceManager(None).provideService('one', c)
        class C: pass
        foo = C()
        self.assertEqual(id(getService(foo, 'one')), id(c))

    def testGetServiceDefinitions(self):
        # test that the service definitions are the ones we added
        getServiceManager(None).defineService('one', IOne)
        c = ServiceOne()
        getServiceManager(None).provideService('one', c)

        getServiceManager(None).defineService('two', ITwo)
        d = ServiceTwo()
        getServiceManager(None).provideService('two', d)
        defs = getServiceDefinitions(None)
        defs.sort()
        self.assertEqual(defs,
            [('one', IOne), ('two', ITwo)])


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/component/tests/test_skins.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.
# 
##############################################################################
import unittest, sys

from zope.component.tests.placelesssetup import PlacelessSetup
from zope.component import getView, getService, queryView
from zope.component.exceptions import ComponentLookupError 
from zope.interface import Interface
from zope.component.tests.request import Request


class Test(PlacelessSetup, unittest.TestCase):

    def testSkin(self):
        class I1(Interface): pass
        class I2(Interface): pass

        class C1:
            __implements__ = I2
            __used_for__ = I1
            def __init__(self, o, request): self._context=o
        class C2(C1): pass
        class C3(C1): pass

        class O: __implements__ = I1

        getService(None, 'Views').provideView(I1, 'test', I2, [C1])
        self.assertEqual(getView(O(), 'test', Request(I2)).__class__, C1) 
        getService(None, 'Skins').defineSkin('foo', I2, ('foo', 'default'))
        self.assertEqual(getView(O(), 'test', Request(I2, 'foo')).__class__,
                         C1) 
        getService(None, 'Views').provideView(None, 'test', I2, [C2])
        self.assertEqual(getView(O(), 'test', Request(I2, 'foo')).__class__,
                         C1) 
        getService(None, 'Views').provideView(
            None, 'test', I2, [C2], layer='foo')
        self.assertEqual(getView(O(), 'test', Request(I2, 'foo')).__class__,
                         C2) 
        getService(None, 'Views').provideView(
            I1, 'test', I2, [C3], layer='foo')
        self.assertEqual(getView(O(), 'test', Request(I2, 'foo')).__class__,
                         C3) 



    def testGetRequestViewMethod(self):

        class I1(Interface): pass
        class I2(Interface): pass

        class C1:
            __implements__ = I2
            __used_for__ = I1
            def __init__(self, o, request): self._context=o
        class C2(C1): pass
        class C3(C1): pass

        class O: __implements__ = I1


        getService(None, 'Views').provideView(I1, 'test', I2, [C1])
        self.assertEqual(getView(O(), 'test', 
            Request(I2,'') ).__class__, C1) 
        getService(None, 'Skins').defineSkin('foo', I2, ('foo', 'default'))

        self.assertEqual(getView(O(), 'test', 
            Request(I2, 'foo')).__class__, C1) 
        getService(None, 'Views').provideView(None, 'test', I2, [C2])

        self.assertEqual(getView(O(), 'test', 
            Request(I2, 'foo')).__class__, C1) 
        getService(None, 'Views').provideView(
            None, 'test', I2, [C2], layer='foo')

        self.assertEqual(getView(O(), 'test', 
            Request(I2, 'foo')).__class__, C2) 
        getService(None, 'Views').provideView(
            I1, 'test', I2, [C3], layer='foo')

        self.assertEqual(getView(O(), 'test', 
            Request(I2, 'foo')).__class__, C3) 

        self.assertRaises(ComponentLookupError,  
            getView, O(), 'test2', Request(I2, 'foo'))

        self.assertEqual(queryView(O(), 'test2', 
                                   Request(I2, 'foo'), None), None) 

        

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/component/tests/test_utilityservice.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.
#
##############################################################################
"""test utility service

XXX longer description goes here.

$Id: test_utilityservice.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.component import \
     getUtility, getService, queryUtility, getServiceManager
from zope.component.exceptions import ComponentLookupError 
from zope.interface import Interface

from zope.testing.cleanup import CleanUp # Base class w registry cleanup

class IDummyService(Interface):
    pass

class DummyService:
    __implements__ = IDummyService

dummyService = DummyService()

class Test(TestCase, CleanUp):
    def setUp(self):
        CleanUp.setUp(self)
        sm=getServiceManager(None)
        defineService=sm.defineService
        provideService=sm.provideService
        from zope.component.interfaces import IUtilityService
        defineService('Utilities',IUtilityService)
        from zope.component.GlobalUtilityService import\
             utilityService
        provideService('Utilities', utilityService)
    
    def testGetUtility(self):
        us = getService(None, "Utilities")
        self.assertRaises(
            ComponentLookupError, getUtility, None, IDummyService)
        us.provideUtility(IDummyService, dummyService)
        self.assertEqual(getUtility(None, IDummyService), dummyService)
    
    def testQueryUtility(self):
        us = getService(None, "Utilities")
        self.assertEqual(queryUtility(None, IDummyService), None)
        self.assertEqual(queryUtility(None, IDummyService, self), self)
        us.provideUtility(IDummyService, dummyService)
        self.assertEqual(queryUtility(None, IDummyService), dummyService)
        

def test_suite():
    return makeSuite(Test)

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/src/zope/component/tests/test_views.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: test_views.py,v 1.1.2.1 2002/12/23 19:32:41 jim Exp $
"""

from zope.interface import Interface


class IV(Interface):
    def index(): pass
    
class IC(Interface): pass

class V1:
    __implements__ = IV

    def __init__(self,context, request): 
        self.context = context
        self.request = request

    def index(self): return 'V1 here'

    def action(self): return 'done'

class VZMI(V1):
    def index(self): return 'ZMI here'

class R1:

    def index(self): return 'R1 here'

    def action(self): return 'R done'

    def __init__(self, request): 
        pass 

    __implements__ = IV

class RZMI(R1):
    pass