File: test_xmlbuilder.py

package info (click to toggle)
python-xml 0.8.4-10.1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,972 kB
  • ctags: 10,628
  • sloc: python: 46,730; ansic: 14,354; xml: 968; makefile: 201; sh: 20
file content (349 lines) | stat: -rw-r--r-- 13,272 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Tests of the extended features of xml.dom.expatbuilder."""

import os
import pprint
import unittest

from cStringIO import StringIO

from xml.dom import XMLNS_NAMESPACE
from xml.dom import xmlbuilder


INTERNAL_SUBSET = ("<!NOTATION x SYSTEM 'http://xml.python.org/notation/x'>\n"
                   "<!ENTITY e SYSTEM 'http://xml.python.org/entity/e'>")
DOCUMENT_SOURCE = (
    '<!DOCTYPE doc [' + INTERNAL_SUBSET.replace('\n', '\r\n') + ''']>
<doc xmlns:a="http://xml.python.org/a"
     xmlns:A="http://xml.python.org/a"
     xmlns:b="http://xml.python.org/b"
     a:a="a" b:b="b"/>
''')


class Tests(unittest.TestCase):

    def setUp(self):
        self.builder = xmlbuilder.DOMBuilder()

    def makeSource(self, text):
        source = xmlbuilder.DOMInputSource()
        source.byteStream = StringIO(text)
        return source

    def check_attrs(self, atts, expected):
        self.assertEqual(atts.length, len(expected))
        info = atts.itemsNS()
        info.sort()
        if info != expected:
            self.fail("bad attribute information:\n" + pprint.pformat(info))

    def run_checks(self, attributes):
        document = self.builder.parse(self.makeSource(DOCUMENT_SOURCE))

        self.assertEqual(document.doctype.internalSubset, INTERNAL_SUBSET)
        self.assertEqual(document.doctype.entities.length, 1,
                         "entity not stored in doctype")

        node = document.doctype.entities['e']
        self.assert_(node.notationName is None)
        self.assert_(node.publicId is None)
        self.assertEqual(node.systemId, 'http://xml.python.org/entity/e')
        self.assertEqual(document.doctype.notations.length, 1)

        node = document.doctype.notations['x']
        self.assert_(node.publicId is None)
        self.assertEqual(node.systemId, 'http://xml.python.org/notation/x')

        self.check_attrs(document.documentElement.attributes, attributes)

    def test_namespace_decls_on(self):
        self.builder.setFeature("namespace_declarations", 1)
        self.run_checks(#((nsuri, localName), value),
                        [((XMLNS_NAMESPACE, "A"), "http://xml.python.org/a"),
                         ((XMLNS_NAMESPACE, "a"), "http://xml.python.org/a"),
                         ((XMLNS_NAMESPACE, "b"), "http://xml.python.org/b"),
                         (("http://xml.python.org/a", "a"), "a"),
                         (("http://xml.python.org/b", "b"), "b"),
                         ])

    def test_namespace_decls_off(self):
        self.builder.setFeature("namespace_declarations", 0)
        self.run_checks(#((nsuri, localName), value),
                        [(("http://xml.python.org/a", "a"), "a"),
                         (("http://xml.python.org/b", "b"), "b"),
                         ])

    def test_get_element_by_id(self):
        ID_PREFIX = "<!DOCTYPE doc [ <!ATTLIST e id ID #IMPLIED> ]>"
        doc = self.builder.parse(self.makeSource(
            ID_PREFIX + "<doc id='foo'><e id='foo'/></doc>"))
        self.assert_(doc.getElementById("bar") is None,
                     "received unexpected node")
        self.assertEqual(doc.getElementById("foo").nodeName, "e",
                         "did not get expected node")
        # Check an implementation detail; this is testing the
        # ID-caching behavior.
        self.assert_(doc._id_cache.has_key("foo"))

        # make sure adding an element with an ID works
        e = doc.createElement("e")
        e.setAttribute("id", "new")
        doc.documentElement.appendChild(e)
        self.assert_(e.isSameNode(doc.getElementById("new")))

        # make sure the cache doesn't cause false hits when we remove nodes
        doc.documentElement.removeChild(e)
        self.assert_(e.parentNode is None)
        self.assert_(doc.getElementById("new") is None)

        # now add the node back, make sure we can still get it by id,
        # the change the value of the id attribute and check that it's
        # returned only for the new ID
        doc.documentElement.appendChild(e)
        self.assert_(e.isSameNode(doc.getElementById("new")))
        a = e.getAttributeNode("id")
        a.value = "no-longer-new"
        self.assert_(doc.getElementById("new") is None)
        self.assert_(e.isSameNode(doc.getElementById("no-longer-new")))

        # make sure removing the attribute makes the ID lookup return None:
        e.removeAttributeNode(a)
        self.assertEqual(e.getAttribute("id"), "")
        self.assert_(doc.getElementById("no-longer-new") is None)

        # check that modifying e.attributes works as well
        attrs = e.attributes
        e.setAttributeNode(a)
        self.assert_(e.isSameNode(doc.getElementById("no-longer-new")))
        attrs.removeNamedItem("id")
        self.assert_(doc.getElementById("no-longer-new") is None)

        a2 = doc.createAttribute("id")
        a2.value = "alternate-id"
        attrs.setNamedItem(a)
        self.assert_(e.isSameNode(doc.getElementById("no-longer-new")))
        attrs.setNamedItem(a2)
        self.assert_(doc.getElementById("no-longer-new") is None)
        self.assert_(e.isSameNode(doc.getElementById("alternate-id")))

        # make sure nodes with an ID in a fragment are not located.
        f = doc.createDocumentFragment()
        e = doc.createElement("e")
        e.setAttribute("id", "in-fragment")
        f.appendChild(e)
        self.assert_(doc.getElementById("in-fragment") is None)

        doc = self.builder.parse(self.makeSource(
            ID_PREFIX + "<doc id='foo'><d id='foo'/><e id='foo'/></doc>"))
        self.assertEqual(doc.getElementById("foo").nodeName, "e",
                         "did not get expected node")

        doc = self.builder.parse(self.makeSource(
            ID_PREFIX + ("<doc id='foo'><e id='foo' name='a'/>"
                         "<e id='bar' name='b'/></doc>")))
        self.assertEqual(doc.getElementById("foo").getAttribute("name"), "a",
                         "did not get expected node")

    def test_whitespace_in_element_content(self):
        DTD_PREFIX = "<!DOCTYPE doc [ <!ELEMENT doc (e*)> ]>"
        doc = self.builder.parse(self.makeSource(
            DTD_PREFIX + ("<doc id='foo'> <e/><e>  </e></doc>")))
        docelem = doc.documentElement
        e1, e2 = docelem.getElementsByTagName("e")
        ws1 = docelem.firstChild
        ws2 = e2.firstChild

        # test WS in element content
        self.assert_(ws1.isWhitespaceInElementContent)
        ws1.appendData("not-white")
        self.assert_(not ws1.isWhitespaceInElementContent)
        ws1.replaceData(0, len(ws1.data), "    ")
        self.assert_(ws1.isWhitespaceInElementContent)
        ws1.replaceData(0, len(ws1.data), "not-white")
        self.assert_(not ws1.isWhitespaceInElementContent)
        ws1.data = "  "
        self.assert_(ws1.isWhitespaceInElementContent)

        # test WS not in element content
        self.assert_(not ws2.isWhitespaceInElementContent)
        ws2.appendData("not-white")
        self.assert_(not ws2.isWhitespaceInElementContent)
        ws2.replaceData(0, len(ws2.data), "    ")
        self.assert_(not ws2.isWhitespaceInElementContent)
        ws2.replaceData(0, len(ws2.data), "not-white")
        self.assert_(not ws2.isWhitespaceInElementContent)
        ws2.data = "  "
        self.assert_(not ws2.isWhitespaceInElementContent)

    def check_resolver(self, content_type, encoding):
        resolver = TestingResolver(content_type)
        source = resolver.resolveEntity(None, DUMMY_URL)
        self.assertEqual(source.encoding, encoding,
                         "wrong encoding; expected %s, got %s"
                         % (repr(encoding), repr(source.encoding)))

    def test_entity_resolver_encodings(self):
        self.check_resolver((None, None, []), None)
        self.check_resolver(("text", "plain", []), None)
        self.check_resolver(("text", "plain", ["charset=iso-8859-1"]),
                            "iso-8859-1")
        self.check_resolver(("text", "plain", ["charset=UTF-8"]), "utf-8")

    def test_internal_subset_isolation(self):
        document = self.builder.parse(self.makeSource(
            "<!DOCTYPE doc ["
            "<!-- comment --> <?pi foo?>"
            "]><doc/>"
            ))
        s = document.toxml()
        self.assertEqual(s,
                         '<?xml version="1.0" ?>\n'
                         '<!DOCTYPE doc ['
                         '<!-- comment --> <?pi foo?>'
                         ']>\n'
                         '<doc/>')

    def test_document_prolog_in_order(self):
        source = self.makeSource(
            "<!-- comment -->\n"
            "<!DOCTYPE doc []>\n"
            "<?pi foo?>\n"
            "<doc/>")
        document = self.builder.parse(source)
        s = document.toxml()
        self.assertEqual(s,
                         '<?xml version="1.0" ?>\n'
                         '<!-- comment -->'
                         '<!DOCTYPE doc []>\n'
                         '<?pi foo?>'
                         '<doc/>')

    def test_docelem_has_namespace(self):
        source = self.makeSource(
            "<doc xmlns='http://xml.python.org/namespace/x'>abc<e/>def</doc>")
        document = self.builder.parse(source)
        self.assertEqual(document.documentElement.namespaceURI,
                         'http://xml.python.org/namespace/x')

    def test_isId(self):
        source = self.makeSource(
            "<!DOCTYPE doc [\n"
            "  <!ATTLIST doc id ID #IMPLIED>\n"
            "]><doc id='name' notid='name'/>")
        document = self.builder.parse(source)
        elem = document.documentElement
        a1 = elem.getAttributeNode("id")
        a2 = elem.getAttributeNode("notid")
        self.failUnless(a1.isId)
        self.failIf(a2.isId)

    def test_schemaType(self):
        source = self.makeSource(
            "<!DOCTYPE doc [\n"
            "  <!ENTITY e1 SYSTEM 'http://xml.python.org/e1'>\n"
            "  <!ENTITY e2 SYSTEM 'http://xml.python.org/e2'>\n"
            "  <!ATTLIST doc id   ID       #IMPLIED \n"
            "                ref  IDREF    #IMPLIED \n"
            "                refs IDREFS   #IMPLIED \n"
            "                enum (a|b)    #IMPLIED \n"
            "                ent  ENTITY   #IMPLIED \n"
            "                ents ENTITIES #IMPLIED \n"
            "                nm   NMTOKEN  #IMPLIED \n"
            "                nms  NMTOKENS #IMPLIED \n"
            "                text CDATA    #IMPLIED \n"
            "    >\n"
            "]><doc id='name' notid='name' text='splat!' enum='b'"
            "       ref='name' refs='name name' ent='e1' ents='e1 e2'"
            "       nm='123' nms='123 abc' />")
        document = self.builder.parse(source)
        elem = document.documentElement
        t = elem.schemaType
        self.assert_(t.name is None)
        self.assert_(t.namespace is None)

        check_attr = self.check_attr_schemaType
        check_attr(elem, "id",    "id")
        check_attr(elem, "notid", None)
        check_attr(elem, "enum",  "enumeration")
        check_attr(elem, "ent",   "entity")
        check_attr(elem, "ents",  "entities")
        check_attr(elem, "ref",   "idref")
        check_attr(elem, "refs",  "idrefs")
        check_attr(elem, "text",  "cdata")

    def check_attr_schemaType(self, elem, attrname, name):
        a = elem.getAttributeNode(attrname)
        t = a.schemaType
        self.assert_(t.namespace is None)
        self.assertEqual(t.name, name)


DUMMY_URL = "http://xml.python.org/dummy.xml"

class TestingResolver(xmlbuilder.DOMEntityResolver):
    def __init__(self, content_type):
        self._content_type = content_type

    def _create_opener(self):
        return FakeOpener(self._content_type)

if os.name == "nt":
    NULLFILE = "nul"
else:
    NULLFILE = "/dev/null"

class FakeOpener:
    def __init__(self, content_type):
        self._content_type = content_type

    def open(self, url):
        if url != DUMMY_URL:
            raise ValueError, "unexpected URL: " + repr(url)
        return FakeFile(open(NULLFILE, "rb"), self._content_type)

class FakeFile:
    def __init__(self, file, content_type):
        self._file = file
        self._content_type = content_type

    def info(self):
        return FakeMessage(self._content_type)

    def __getattr__(self, name):
        return getattr(self._file, name)

class FakeMessage:
    def __init__(self, content_type):
        self._maintype, self._subtype, self._plist = content_type

    def has_key(self, name):
        name = name.lower()
        if name != "content-type":
            raise ValueError, "unexpected has_key(%s)" % repr(name)
        return self._maintype is not None

    def getplist(self):
        return self._plist

    def getmaintype(self):
        return self._maintype or "text"

    def getsubtype(self):
        return self._subtype or "plain"

    def gettype(self):
        return "%s/%s" % (self.getmaintype(), self.getsubtype())


def test_suite():
    return unittest.makeSuite(Tests)

def test_main():
    import test_support
    test_support.run_suite(test_suite())

if __name__ == "__main__":
    import test_support
    test_support.verbose = 1
    test_main()