"""
$Id: __init__.py,v 1.2 2005/02/26 17:56:10 sidnei Exp $
"""

import os, sys

if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

import unittest
from Testing import ZopeTestCase

from Products.Fate.tests.base import BaseTest
from zope.interface.interfaces import IInterface
from zope.component.servicenames import Utilities
from zope.component import getService
from zope.schema import getFieldNamesInOrder
from Products.Archetypes.examples.DDocument import DDocument

try:
    from Products.Five import localsite
    HAS_LOCALSITE = True
except ImportError:
    HAS_LOCALSITE = False

class ServiceTest(ZopeTestCase.Functional, BaseTest):

    def afterSetUp(self):
        from Products.Five.localsite import enableLocalSiteHook
        enableLocalSiteHook(self.portal)

    def test_getUtility(self):
        self.publish('/'.join(self.portal.getPhysicalPath()))
        us = getService(Utilities)
        schema = us.getUtility(IInterface,
                               name='Products.Archetypes.examples.DDocument')
        expected = DDocument.schema.keys()
        got = getFieldNamesInOrder(schema)
        self.assertEquals(got, expected)

    def test_getUtilitiesFor(self):
        us = getService(Utilities)
        self.failUnless(list(us.getUtilitiesFor(IInterface)))

    def test_queryUtility(self):
        us = getService(Utilities)
        qu = us.queryUtility
        self.assertEquals(qu(IInterface, name='X.Y.Z', default=None), None)

def test_suite():
    suite = unittest.TestSuite()
    if HAS_LOCALSITE:
        suite.addTest(unittest.makeSuite(ServiceTest))
    return suite

if __name__ == '__main__':
    framework()
