[Zope3-checkins] CVS: Zope3/src/zope/app/content/tests - test_xmldocument.py:1.2

Martijn Faassen m.faassen@vet.uu.nl
Thu, 10 Apr 2003 09:04:44 -0400


Update of /cvs-repository/Zope3/src/zope/app/content/tests
In directory cvs.zope.org:/tmp/cvs-serv24220/zope/app/content/tests

Modified Files:
	test_xmldocument.py 
Log Message:
Made XMLDocument instances dynamically modify their interfaces. If an
instance has XML content that refers to an XML schema (using schemaLocation)
then the instance will automatically implement the XML Schema Interface
for that. This way, Zope adapters and views can be associated with 
particular instances that conform to a particular schema.

Currently the manipulation of an instance's interfaces is done by 
a rather terrible hack. This should change after interfacegheddon.


=== Zope3/src/zope/app/content/tests/test_xmldocument.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/content/tests/test_xmldocument.py:1.1	Wed Apr  9 07:47:52 2003
+++ Zope3/src/zope/app/content/tests/test_xmldocument.py	Thu Apr 10 09:04:43 2003
@@ -12,22 +12,39 @@
 #
 ##############################################################################
 """
-Basic tests for Page Templates used in content-space.
+Basic tests for XML Document.
 
 $Id$
 """
 
 import unittest
+
 from zope.schema.interfaces import ValidationError
 from zope.app.content.xmldocument import XMLDocument
+from zope.app.interfaces.content.xmldocument import IXMLDocument
+from zope.app.tests.placelesssetup import PlacelessSetup
+
+from zope.interface import Interface
+from zope.interface.interface import InterfaceClass
+from zope.app.interfaces.xml.representable import IXMLRepresentable
+from zope.app.component.globalinterfaceservice import interfaceService
+
+class IRandomInterface(Interface):
+    pass
+
+class XMLDocument2(XMLDocument):
+    __implements__ = IXMLDocument, IRandomInterface
 
-class XMLDocumentTests(unittest.TestCase):
+    
+class XMLDocumentTests(PlacelessSetup, unittest.TestCase):
 
     def test_create(self):
         doc = XMLDocument()
+        self.assertEquals('<doc/>', doc.source)
+        
         doc = XMLDocument('<mydoc/>')
-        self.assertRaises(ValidationError, XMLDocument, 'foo')
-
+        self.assertEquals('<mydoc/>', doc.source)
+    
     def test_set(self):
         src = '<mydoc/>'
         doc = XMLDocument(src)
@@ -36,5 +53,114 @@
         doc.source = new_src
         self.assertEqual(new_src, doc.source)
 
+    def test_xmlschema_interfaces(self):
+        doc = XMLDocument()
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+
+        schema1 = 'http://schema.zope.org/hypothetical/schema1'
+        schema2 = 'http://schema.zope.org/hypothetical/schema2'
+        extends = (IXMLRepresentable,)
+        interface1 = InterfaceClass(schema1, extends, {})
+        interface2 = InterfaceClass(schema2, extends, {})
+        
+        interfaceService.provideInterface(schema1, interface1)
+
+        xml = '''
+<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://schema.zope.org/hypothetical/schema1">
+foo
+</doc>'''
+        doc.source = xml
+
+        self.assert_(interface1.isImplementedBy(doc))
+        self.assert_(not interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+        
+        doc.source = '<doc />'
+
+        self.assert_(not interface1.isImplementedBy(doc))
+        self.assert_(not interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+
+        xml = '''
+<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://schema.zope.org/hypothetical/schema1
+                         http://schema.zope.org/hypothetical/schema2">
+foo
+</doc>'''
+        doc.source = xml
+        
+        self.assert_(interface1.isImplementedBy(doc))
+        # can't find it as it isn't provided yet
+        self.assert_(not interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+
+        # finally provide and set document again
+        interfaceService.provideInterface(schema2, interface2)
+
+        doc.source = xml
+        
+        self.assert_(interface1.isImplementedBy(doc))
+        self.assert_(interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+
+    def test_xmlschema_interfaces2(self):
+        # the same tests, but XMLDocument2 has two interfaces not just one
+        
+        doc = XMLDocument2()
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+        self.assert_(IRandomInterface.isImplementedBy(doc))
+        
+        schema1 = 'http://schema.zope.org/hypothetical/schema1'
+        schema2 = 'http://schema.zope.org/hypothetical/schema2'
+        extends = (IXMLRepresentable,)
+        interface1 = InterfaceClass(schema1, extends, {})
+        interface2 = InterfaceClass(schema2, extends, {})
+        
+        interfaceService.provideInterface(schema1, interface1)
+
+        xml = '''
+<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://schema.zope.org/hypothetical/schema1">
+foo
+</doc>'''
+        doc.source = xml
+
+        self.assert_(interface1.isImplementedBy(doc))
+        self.assert_(not interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+        self.assert_(IRandomInterface.isImplementedBy(doc))
+
+        doc.source = '<doc />'
+
+        self.assert_(not interface1.isImplementedBy(doc))
+        self.assert_(not interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+        self.assert_(IRandomInterface.isImplementedBy(doc))
+        
+        xml = '''
+<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://schema.zope.org/hypothetical/schema1
+                         http://schema.zope.org/hypothetical/schema2">
+foo
+</doc>'''
+        doc.source = xml
+        
+        self.assert_(interface1.isImplementedBy(doc))
+        # can't find it as it isn't provided yet
+        self.assert_(not interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+        self.assert_(IRandomInterface.isImplementedBy(doc))
+        
+        # finally provide and set document again
+        interfaceService.provideInterface(schema2, interface2)
+
+        doc.source = xml
+        
+        self.assert_(interface1.isImplementedBy(doc))
+        self.assert_(interface2.isImplementedBy(doc))
+        self.assert_(IXMLDocument.isImplementedBy(doc))
+        self.assert_(IRandomInterface.isImplementedBy(doc))
+        
 def test_suite():
     return unittest.makeSuite(XMLDocumentTests)