~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Daniel Watkins
  • Date: 2007-08-23 10:33:45 UTC
  • mto: (2762.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2763.
  • Revision ID: d.m.watkins@warwick.ac.uk-20070823103345-3fwlsg33a2i622b7
Cleaned up indentation in test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from cStringIO import StringIO
18
18
 
19
19
from bzrlib import (
20
 
    errors,
21
 
    fifo_cache,
22
 
    inventory,
23
 
    xml6,
 
20
    errors, 
 
21
    inventory, 
24
22
    xml7,
25
 
    xml8,
 
23
    xml_serializer,
26
24
    )
27
25
from bzrlib.tests import TestCase
28
 
from bzrlib.inventory import Inventory
 
26
from bzrlib.inventory import Inventory, InventoryEntry
 
27
from bzrlib.xml4 import serializer_v4
29
28
import bzrlib.xml5
30
29
 
 
30
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
 
31
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
 
32
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
 
33
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
 
34
</inventory>"""
 
35
 
 
36
 
 
37
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
38
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
39
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
40
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
41
    timestamp="1125907235.212"
 
42
    timezone="36000">
 
43
<message>- start splitting code for xml (de)serialization away from objects
 
44
  preparatory to supporting multiple formats by a single library
 
45
</message>
 
46
<parents>
 
47
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
 
48
</parents>
 
49
</revision>
 
50
"""
 
51
 
31
52
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
32
53
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
33
54
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
58
79
"""
59
80
 
60
81
_committed_inv_v5 = """<inventory>
61
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
62
 
      name="bar" parent_id="TREE_ROOT"
63
 
      revision="mbp@foo-123123"
64
 
      text_sha1="A" text_size="1"/>
 
82
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
83
      name="bar" parent_id="TREE_ROOT" 
 
84
      revision="mbp@foo-123123"/>
65
85
<directory name="subdir"
66
86
           file_id="foo-20050801201819-4139aa4a272f4250"
67
 
           parent_id="TREE_ROOT"
 
87
           parent_id="TREE_ROOT" 
68
88
           revision="mbp@foo-00"/>
69
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
70
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
71
 
      revision="mbp@foo-00"
72
 
      text_sha1="B" text_size="0"/>
 
89
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
 
90
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
 
91
      revision="mbp@foo-00"/>
73
92
</inventory>
74
93
"""
75
94
 
76
95
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
77
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
78
 
      name="bar" parent_id="TREE_ROOT"
 
96
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
97
      name="bar" parent_id="TREE_ROOT" 
79
98
      revision="mbp@foo-123123"/>
80
99
<directory name="subdir"
81
100
           file_id="foo-20050801201819-4139aa4a272f4250"
82
 
           parent_id="TREE_ROOT"
 
101
           parent_id="TREE_ROOT" 
83
102
           revision="mbp@foo-00"/>
84
 
<file file_id="bar-20050824000535-6bc48cfad47ed134"
85
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
103
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
 
104
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
86
105
      revision="mbp@foo-00"/>
87
106
</inventory>
88
107
"""
102
121
 
103
122
# DO NOT REFLOW THIS. Its the exact inventory we want.
104
123
_expected_inv_v5 = """<inventory format="5">
105
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
 
124
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
106
125
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
107
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" text_sha1="B" text_size="0" />
 
126
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
108
127
</inventory>
109
128
"""
110
129
 
111
130
 
112
131
_expected_inv_v5_root = """<inventory file_id="f&lt;" format="5" revision_id="mother!">
113
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" text_sha1="A" text_size="1" />
 
132
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" />
114
133
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
115
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" text_sha1="B" text_size="0" />
116
 
<symlink file_id="link-1" name="link" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" symlink_target="a" />
117
 
</inventory>
118
 
"""
119
 
 
120
 
_expected_inv_v6 = """<inventory format="6" revision_id="rev_outer">
121
 
<directory file_id="tree-root-321" name="" revision="rev_outer" />
122
 
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
123
 
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
124
 
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
 
134
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
125
135
</inventory>
126
136
"""
127
137
 
128
138
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
129
139
<directory file_id="tree-root-321" name="" revision="rev_outer" />
130
140
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
131
 
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
132
 
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
 
141
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" />
 
142
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" />
133
143
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
134
144
</inventory>
135
145
"""
136
146
 
137
 
_expected_rev_v8 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" format="8" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
138
 
<message>- start splitting code for xml (de)serialization away from objects
139
 
  preparatory to supporting multiple formats by a single library
140
 
</message>
141
 
<parents>
142
 
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
143
 
</parents>
144
 
</revision>
145
 
"""
146
 
 
147
 
_expected_inv_v8 = """<inventory format="8" revision_id="rev_outer">
148
 
<directory file_id="tree-root-321" name="" revision="rev_outer" />
149
 
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
150
 
<file file_id="file-id" name="file" parent_id="tree-root-321" revision="rev_outer" text_sha1="A" text_size="1" />
151
 
<symlink file_id="link-id" name="link" parent_id="tree-root-321" revision="rev_outer" symlink_target="a" />
152
 
</inventory>
153
 
"""
154
 
 
155
147
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
156
148
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
157
149
    revision_id="erik@b&#229;gfors-02"
180
172
</inventory>
181
173
"""
182
174
 
183
 
# Before revision_id was always stored as an attribute
184
 
_inventory_v5a = """<inventory format="5">
185
 
</inventory>
186
 
"""
187
 
 
188
 
# Before revision_id was always stored as an attribute
189
 
_inventory_v5b = """<inventory format="5" revision_id="a-rev-id">
190
 
</inventory>
191
 
"""
192
 
 
193
175
 
194
176
class TestSerializer(TestCase):
195
177
    """Test XML serialization"""
 
178
    def test_canned_inventory(self):
 
179
        """Test unpacked a canned inventory v4 file."""
 
180
        inp = StringIO(_working_inventory_v4)
 
181
        inv = serializer_v4.read_inventory(inp)
 
182
        self.assertEqual(len(inv), 4)
 
183
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
 
184
 
 
185
    def test_unpack_revision(self):
 
186
        """Test unpacking a canned revision v4"""
 
187
        inp = StringIO(_revision_v4)
 
188
        rev = serializer_v4.read_revision(inp)
 
189
        eq = self.assertEqual
 
190
        eq(rev.committer,
 
191
           "Martin Pool <mbp@sourcefrog.net>")
 
192
        eq(rev.inventory_id,
 
193
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
 
194
        eq(len(rev.parent_ids), 1)
 
195
        eq(rev.parent_ids[0],
 
196
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
196
197
 
197
198
    def test_unpack_revision_5(self):
198
199
        """Test unpacking a canned revision v5"""
242
243
        eq(ie.name, 'bar')
243
244
        eq(inv[ie.parent_id].kind, 'directory')
244
245
 
245
 
    def test_unpack_inventory_5a(self):
246
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
247
 
                _inventory_v5a, revision_id='test-rev-id')
248
 
        self.assertEqual('test-rev-id', inv.root.revision)
249
 
 
250
 
    def test_unpack_inventory_5a_cache_and_copy(self):
251
 
        # Passing an entry_cache should get populated with the objects
252
 
        # But the returned objects should be copies if return_from_cache is
253
 
        # False
254
 
        entry_cache = fifo_cache.FIFOCache()
255
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
256
 
            _inventory_v5a, revision_id='test-rev-id',
257
 
            entry_cache=entry_cache, return_from_cache=False)
258
 
        for entry in inv.iter_just_entries():
259
 
            key = (entry.file_id, entry.revision)
260
 
            if entry.file_id is inv.root.file_id:
261
 
                # The root id is inferred for xml v5
262
 
                self.assertFalse(key in entry_cache)
263
 
            else:
264
 
                self.assertIsNot(entry, entry_cache[key])
265
 
 
266
 
    def test_unpack_inventory_5a_cache_no_copy(self):
267
 
        # Passing an entry_cache should get populated with the objects
268
 
        # The returned objects should be exact if return_from_cache is
269
 
        # True
270
 
        entry_cache = fifo_cache.FIFOCache()
271
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
272
 
            _inventory_v5a, revision_id='test-rev-id',
273
 
            entry_cache=entry_cache, return_from_cache=True)
274
 
        for entry in inv.iter_just_entries():
275
 
            key = (entry.file_id, entry.revision)
276
 
            if entry.file_id is inv.root.file_id:
277
 
                # The root id is inferred for xml v5
278
 
                self.assertFalse(key in entry_cache)
279
 
            else:
280
 
                self.assertIs(entry, entry_cache[key])
281
 
 
282
 
    def test_unpack_inventory_5b(self):
283
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
284
 
                _inventory_v5b, revision_id='test-rev-id')
285
 
        self.assertEqual('a-rev-id', inv.root.revision)
286
 
 
287
246
    def test_repack_inventory_5(self):
288
247
        inp = StringIO(_committed_inv_v5)
289
248
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
292
251
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
293
252
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
294
253
        self.assertEqual(inv, inv2)
295
 
 
 
254
    
296
255
    def assertRoundTrips(self, xml_string):
297
256
        inp = StringIO(xml_string)
298
257
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
299
258
        outp = StringIO()
300
259
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
301
260
        self.assertEqualDiff(xml_string, outp.getvalue())
302
 
        lines = bzrlib.xml5.serializer_v5.write_inventory_to_lines(inv)
303
 
        outp.seek(0)
304
 
        self.assertEqual(outp.readlines(), lines)
305
261
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
306
262
        self.assertEqual(inv, inv2)
307
263
 
347
303
        new_rev = s_v5.read_revision_from_string(txt)
348
304
        self.assertEqual(props, new_rev.properties)
349
305
 
350
 
    def get_sample_inventory(self):
 
306
    def test_roundtrip_inventory_v7(self):
351
307
        inv = Inventory('tree-root-321', revision_id='rev_outer')
 
308
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
 
309
                                        'rev_outer', 'rev_inner'))
352
310
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
353
 
        inv.add(inventory.InventoryDirectory('dir-id', 'dir',
 
311
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
354
312
                                             'tree-root-321'))
355
313
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
356
314
        inv['tree-root-321'].revision = 'rev_outer'
357
315
        inv['dir-id'].revision = 'rev_outer'
358
316
        inv['file-id'].revision = 'rev_outer'
359
 
        inv['file-id'].text_sha1 = 'A'
360
 
        inv['file-id'].text_size = 1
361
317
        inv['link-id'].revision = 'rev_outer'
362
 
        inv['link-id'].symlink_target = 'a'
363
 
        return inv
364
 
 
365
 
    def test_roundtrip_inventory_v7(self):
366
 
        inv = self.get_sample_inventory()
367
 
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
368
 
                                        'rev_outer', 'rev_inner'))
369
318
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
370
 
        lines = xml7.serializer_v7.write_inventory_to_lines(inv)
371
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
372
319
        self.assertEqualDiff(_expected_inv_v7, txt)
373
320
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
374
321
        self.assertEqual(5, len(inv2))
375
322
        for path, ie in inv.iter_entries():
376
323
            self.assertEqual(ie, inv2[ie.file_id])
377
324
 
378
 
    def test_roundtrip_inventory_v6(self):
379
 
        inv = self.get_sample_inventory()
380
 
        txt = xml6.serializer_v6.write_inventory_to_string(inv)
381
 
        lines = xml6.serializer_v6.write_inventory_to_lines(inv)
382
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
383
 
        self.assertEqualDiff(_expected_inv_v6, txt)
384
 
        inv2 = xml6.serializer_v6.read_inventory_from_string(txt)
385
 
        self.assertEqual(4, len(inv2))
386
 
        for path, ie in inv.iter_entries():
387
 
            self.assertEqual(ie, inv2[ie.file_id])
388
 
 
389
325
    def test_wrong_format_v7(self):
390
326
        """Can't accidentally open a file with wrong serializer"""
391
327
        s_v6 = bzrlib.xml6.serializer_v6
392
328
        s_v7 = xml7.serializer_v7
393
 
        self.assertRaises(errors.UnexpectedInventoryFormat,
 
329
        self.assertRaises(errors.UnexpectedInventoryFormat, 
394
330
                          s_v7.read_inventory_from_string, _expected_inv_v5)
395
 
        self.assertRaises(errors.UnexpectedInventoryFormat,
 
331
        self.assertRaises(errors.UnexpectedInventoryFormat, 
396
332
                          s_v6.read_inventory_from_string, _expected_inv_v7)
397
333
 
398
334
    def test_tree_reference(self):
399
335
        s_v5 = bzrlib.xml5.serializer_v5
400
336
        s_v6 = bzrlib.xml6.serializer_v6
401
337
        s_v7 = xml7.serializer_v7
402
 
        inv = Inventory('tree-root-321', revision_id='rev-outer')
403
 
        inv.root.revision = 'root-rev'
 
338
        inv = Inventory('tree-root-321')
404
339
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
405
340
                                        'rev-outer', 'rev-inner'))
406
 
        self.assertRaises(errors.UnsupportedInventoryKind,
 
341
        self.assertRaises(errors.UnsupportedInventoryKind, 
407
342
                          s_v5.write_inventory_to_string, inv)
408
 
        self.assertRaises(errors.UnsupportedInventoryKind,
 
343
        self.assertRaises(errors.UnsupportedInventoryKind, 
409
344
                          s_v6.write_inventory_to_string, inv)
410
345
        txt = s_v7.write_inventory_to_string(inv)
411
 
        lines = s_v7.write_inventory_to_lines(inv)
412
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
413
346
        inv2 = s_v7.read_inventory_from_string(txt)
414
347
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
415
348
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
416
349
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
417
 
        self.assertRaises(errors.UnsupportedInventoryKind,
 
350
        self.assertRaises(errors.UnsupportedInventoryKind, 
418
351
                          s_v6.read_inventory_from_string,
419
352
                          txt.replace('format="7"', 'format="6"'))
420
 
        self.assertRaises(errors.UnsupportedInventoryKind,
 
353
        self.assertRaises(errors.UnsupportedInventoryKind, 
421
354
                          s_v5.read_inventory_from_string,
422
355
                          txt.replace('format="7"', 'format="5"'))
423
356
 
424
 
    def test_roundtrip_inventory_v8(self):
425
 
        inv = self.get_sample_inventory()
426
 
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
427
 
        inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
428
 
        self.assertEqual(4, len(inv2))
429
 
        for path, ie in inv.iter_entries():
430
 
            self.assertEqual(ie, inv2[ie.file_id])
431
 
 
432
 
    def test_inventory_text_v8(self):
433
 
        inv = self.get_sample_inventory()
434
 
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
435
 
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
436
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
437
 
        self.assertEqualDiff(_expected_inv_v8, txt)
438
 
 
439
 
    def test_revision_text_v6(self):
440
 
        """Pack revision to XML v6"""
441
 
        rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
442
 
            _expected_rev_v5)
443
 
        serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
444
 
        self.assertEqualDiff(serialized, _expected_rev_v5)
445
 
 
446
 
    def test_revision_text_v7(self):
447
 
        """Pack revision to XML v7"""
448
 
        rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
449
 
            _expected_rev_v5)
450
 
        serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
451
 
        self.assertEqualDiff(serialized, _expected_rev_v5)
452
 
 
453
 
    def test_revision_text_v8(self):
454
 
        """Pack revision to XML v8"""
455
 
        rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
456
 
            _expected_rev_v8)
457
 
        serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
458
 
        self.assertEqualDiff(serialized, _expected_rev_v8)
459
 
 
460
357
    def test_revision_ids_are_utf8(self):
461
358
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
462
359
        s_v5 = bzrlib.xml5.serializer_v5
477
374
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
478
375
        fid_sub = u's\xb5bdir-01'.encode('utf8')
479
376
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
480
 
        expected = [(u'', fid_root, None, rev_id_2),
 
377
        expected = [(u'', fid_root, None, None),
481
378
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
482
379
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
483
380
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
501
398
 
502
399
        self.assertEqual(len(expected), len(actual))
503
400
 
 
401
    def test_registry(self):
 
402
        self.assertIs(serializer_v4,
 
403
                      xml_serializer.format_registry.get('4'))
 
404
        self.assertIs(bzrlib.xml5.serializer_v5,
 
405
                      xml_serializer.format_registry.get('5'))
 
406
        self.assertIs(bzrlib.xml6.serializer_v6,
 
407
                      xml_serializer.format_registry.get('6'))
 
408
        self.assertIs(bzrlib.xml7.serializer_v7,
 
409
                      xml_serializer.format_registry.get('7'))
 
410
 
504
411
 
505
412
class TestEncodeAndEscape(TestCase):
506
413
    """Whitebox testing of the _encode_and_escape function."""
507
414
 
508
415
    def setUp(self):
509
 
        TestCase.setUp(self)
510
416
        # Keep the cache clear before and after the test
511
 
        bzrlib.xml8._ensure_utf8_re()
512
 
        bzrlib.xml8._clear_cache()
513
 
        self.addCleanup(bzrlib.xml8._clear_cache)
 
417
        bzrlib.xml5._ensure_utf8_re()
 
418
        bzrlib.xml5._clear_cache()
 
419
        self.addCleanup(bzrlib.xml5._clear_cache)
514
420
 
515
421
    def test_simple_ascii(self):
516
422
        # _encode_and_escape always appends a final ", because these parameters
517
423
        # are being used in xml attributes, and by returning it now, we have to
518
424
        # do fewer string operations later.
519
 
        val = bzrlib.xml8._encode_and_escape('foo bar')
 
425
        val = bzrlib.xml5._encode_and_escape('foo bar')
520
426
        self.assertEqual('foo bar"', val)
521
427
        # The second time should be cached
522
 
        val2 = bzrlib.xml8._encode_and_escape('foo bar')
 
428
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
523
429
        self.assertIs(val2, val)
524
430
 
525
431
    def test_ascii_with_xml(self):
526
432
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
527
 
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
 
433
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
528
434
 
529
435
    def test_utf8_with_xml(self):
530
436
        # u'\xb5\xe5&\u062c'
531
437
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
532
438
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
533
 
                         bzrlib.xml8._encode_and_escape(utf8_str))
 
439
                         bzrlib.xml5._encode_and_escape(utf8_str))
534
440
 
535
441
    def test_unicode(self):
536
442
        uni_str = u'\xb5\xe5&\u062c'
537
443
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
538
 
                         bzrlib.xml8._encode_and_escape(uni_str))
539
 
 
540
 
 
541
 
class TestMisc(TestCase):
542
 
 
543
 
    def test_unescape_xml(self):
544
 
        """We get some kind of error when malformed entities are passed"""
545
 
        self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')
 
444
                         bzrlib.xml5._encode_and_escape(uni_str))