def testUnnamedResourceThatProvidesAnInterface(self):
        ob = Ob()
        self.assertEqual(zapi.queryAdapter(Request(IR), IV), None)

        xmlconfig(StringIO(template %
            '''
            <resource
                factory="zope.app.component.tests.views.R1"
                type="zope.app.component.tests.views.IR"
                />
            '''
            ))

        v = zapi.queryAdapter(Request(IR), IV)
        self.assertEqual(v, None)

        xmlconfig(StringIO(template %
            '''
            <resource
                factory="zope.app.component.tests.views.R1"
                type="zope.app.component.tests.views.IR"
                provides="zope.app.component.tests.views.IV"
                />
            '''
            ))

        v = zapi.queryAdapter(Request(IR), IV)
        self.assertEqual(v.__class__, R1)
    def testResource(self):
        ob = Ob()
        self.assertEqual(
            zapi.queryAdapter(Request(IV), name=u'test'), None)
        xmlconfig(StringIO(template % (
            '''
            <resource name="test"
                  factory="zope.app.component.tests.views.R1"
                  type="zope.app.component.tests.views.IV"/>
            '''
            )))

        self.assertEqual(
            zapi.queryAdapter(Request(IV), name=u'test').__class__,
            R1)
Example #3
0
    def testFile(self):
        path = os.path.join(tests_path, 'testfiles', 'test.pt')

        self.assertEqual(zapi.queryAdapter(request, name='test'), None)

        xmlconfig(StringIO(template %
            '''
            <browser:resource
                name="index.html"
                file="%s"
                />
            ''' % path
            ))

        r = zapi.getAdapter(request, name='index.html')
        self.assertEquals(r.__class__, FileResource)
        r = ProxyFactory(r)
        self.assertEqual(r.__name__, "index.html")

        # Make sure we can access available attrs and not others
        for n in ('GET', 'HEAD', 'publishTraverse', 'request', '__call__'):
            getattr(r, n)
        self.assertEqual(r.__name__, "index.html")

        self.assertRaises(Exception, getattr, r, '_testData')

        r = removeSecurityProxy(r)
        self.assert_(r.__class__ is FileResource)
        self.assertEqual(r._testData(), open(path, 'rb').read())
Example #4
0
    def testI18nResource(self):
        self.assertEqual(zapi.queryAdapter(request, name='test'), None)

        path1 = os.path.join(tests_path, 'testfiles', 'test.pt')
        path2 = os.path.join(tests_path, 'testfiles', 'test2.pt')

        xmlconfig(StringIO(template % (
            '''
            <browser:i18n-resource name="test" defaultLanguage="fr">
              <browser:translation language="en" file="%s" />
              <browser:translation language="fr" file="%s" />
            </browser:i18n-resource>
            ''' % (path1, path2)
            )))

        v = zapi.getAdapter(request, name='test')
        self.assertEqual(
            zapi.queryAdapter(request, name='test').__class__,
            I18nFileResource)
        self.assertEqual(v._testData('en'), open(path1, 'rb').read())
        self.assertEqual(v._testData('fr'), open(path2, 'rb').read())

        # translation must be provided for the default language
        config = StringIO(template % (
            '''
            <browser:i18n-resource name="test" defaultLanguage="fr">
              <browser:translation language="en" file="%s" />
              <browser:translation language="lt" file="%s" />
            </browser:i18n-resource>
            ''' % (path1, path2)
            ))
        self.assertRaises(ConfigurationError, xmlconfig, config)

        # files and images can't be mixed
        config = StringIO(template % (
            '''
            <browser:i18n-resource name="test" defaultLanguage="fr">
              <browser:translation language="en" file="%s" />
              <browser:translation language="fr" image="%s" />
            </browser:i18n-resource>
            ''' % (path1, path2)
            ))
        self.assertRaises(ConfigurationError, xmlconfig, config)
Example #5
0
    def publishTraverse(self, request, name):
        '''See interface IBrowserPublisher'''

        resource = zapi.queryAdapter(request, name=name)
        if resource is None:
            raise NotFound(self, name)

        sm = zapi.getSiteManager()
        locate(resource, sm, name)
        return resource
Example #6
0
    def testSkinResource(self):
        self.assertEqual(zapi.queryAdapter(request, name='test'), None)

        path = os.path.join(tests_path, 'testfiles', 'test.pt')

        xmlconfig(StringIO(template % (
            '''
            <browser:layer name="zmi" />
            <browser:skin name="zmi" layers="zmi" />
            <browser:resource name="test" file="%s" 
                  layer="zmi" />
            ''' % path
            )))

        self.assertEqual(zapi.queryAdapter(request, name='test'), None)

        zmi = zapi.getUtility(ISkin, 'zmi')
        r = zapi.getAdapter(TestRequest(skin=zmi), name='test')
        r = removeSecurityProxy(r)
        self.assertEqual(r._testData(), open(path, 'rb').read())
Example #7
0
    def testSkinResource(self):
        self.assertEqual(
            zapi.queryAdapter(Request(IV), name='test'), None)

        xmlconfig(StringIO(template % (
            '''
            <browser:layer name="zmi" />
            <browser:skin name="zmi" layers="zmi" />
            <browser:resource
                name="test"
                factory="zope.app.component.tests.views.RZMI"
                layer="zmi" />
            <browser:resource
                name="test"
                factory="zope.app.component.tests.views.R1" />
            '''
            )))

        self.assertEqual(
            zapi.queryAdapter(request, name='test').__class__, R1)
        zmi = zapi.getUtility(ISkin, 'zmi')
        self.assertEqual(
            zapi.queryAdapter(TestRequest(skin=zmi), name='test').__class__,
            RZMI)
Example #8
0
    def testFactory(self):
        self.assertEqual(zapi.queryAdapter(request, name='index.html'), None)

        xmlconfig(StringIO(template %
            '''
            <browser:resource
                name="index.html"
                factory="
                  zope.app.publisher.browser.tests.test_directives.MyResource"
                />
            '''
            ))

        r = zapi.getAdapter(request, name='index.html')
        self.assertEquals(r.__class__, MyResource)
        r = ProxyFactory(r)
        self.assertEqual(r.__name__, "index.html")
    def testNamedAdapter(self):
        self.testAdapter()
        self.assertEqual(IApp(Content()).__class__, Comp)
        self.assertEqual(zapi.queryAdapter(Content(), IV, 'test'), None)

        xmlconfig(StringIO(template % (
            '''
            <adapter
              factory="zope.app.component.tests.components.Comp"
              provides="zope.app.component.tests.components.IApp"
              for="zope.app.component.tests.components.IContent"
              name="test"
              />
            '''
            )))

        self.assertEqual(
            zapi.getAdapter(Content(), IApp, "test").__class__, Comp)
Example #10
0
    def _makeInstance(self, instance_id, type_name, subdir, import_context):

        context = self.context
        class _OldStyleClass:
            pass

        if '.' in type_name:

            factory = _resolveDottedName(type_name)

            if getattr(factory, '__bases__', None) is not None:

                def _factory(instance_id,
                             container=self.context,
                             klass=factory):
                    try:
                        instance = klass(instance_id)
                    except (TypeError, ValueError):
                        instance = klass()
                    instance._setId(instance_id)
                    container._setObject(instance_id, instance)

                    return instance

                factory = _factory

        else:
            factory = zapi.queryAdapter(self.context,
                                        IContentFactory,
                                        name=type_name,
                                       )
        if factory is None:
            return None

        try:
            instance = factory(instance_id)
        except ValueError: # invalid type
            return None

        if context._getOb(instance_id, None) is None:
            context._setObject(instance_id, instance) 

        return context._getOb(instance_id)