~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-05-01 11:25:12 UTC
  • mfrom: (3211.7.10 protocol-v3-doc)
  • Revision ID: pqm@pqm.ubuntu.com-20080501112512-b9lgs4w8r43evtn1
Add the smart protocol v3 specification to network-protocol.txt

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
23
    xml8,
 
24
    xml_serializer,
26
25
    )
27
26
from bzrlib.tests import TestCase
28
 
from bzrlib.inventory import Inventory
 
27
from bzrlib.inventory import Inventory, InventoryEntry
 
28
from bzrlib.xml4 import serializer_v4
29
29
import bzrlib.xml5
30
30
 
 
31
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
 
32
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
 
33
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
 
34
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
 
35
</inventory>"""
 
36
 
 
37
 
 
38
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
39
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
40
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
41
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
42
    timestamp="1125907235.212"
 
43
    timezone="36000">
 
44
<message>- start splitting code for xml (de)serialization away from objects
 
45
  preparatory to supporting multiple formats by a single library
 
46
</message>
 
47
<parents>
 
48
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
 
49
</parents>
 
50
</revision>
 
51
"""
 
52
 
31
53
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
32
54
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
33
55
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
58
80
"""
59
81
 
60
82
_committed_inv_v5 = """<inventory>
61
 
<file file_id="bar-20050901064931-73b4b1138abc9cd2"
62
 
      name="bar" parent_id="TREE_ROOT"
 
83
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
84
      name="bar" parent_id="TREE_ROOT" 
63
85
      revision="mbp@foo-123123"
64
86
      text_sha1="A" text_size="1"/>
65
87
<directory name="subdir"
66
88
           file_id="foo-20050801201819-4139aa4a272f4250"
67
 
           parent_id="TREE_ROOT"
 
89
           parent_id="TREE_ROOT" 
68
90
           revision="mbp@foo-00"/>
69
 
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134"
70
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
91
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
 
92
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
71
93
      revision="mbp@foo-00"
72
94
      text_sha1="B" text_size="0"/>
73
95
</inventory>
74
96
"""
75
97
 
76
98
_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"
 
99
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
100
      name="bar" parent_id="TREE_ROOT" 
79
101
      revision="mbp@foo-123123"/>
80
102
<directory name="subdir"
81
103
           file_id="foo-20050801201819-4139aa4a272f4250"
82
 
           parent_id="TREE_ROOT"
 
104
           parent_id="TREE_ROOT" 
83
105
           revision="mbp@foo-00"/>
84
 
<file file_id="bar-20050824000535-6bc48cfad47ed134"
85
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
106
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
 
107
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
86
108
      revision="mbp@foo-00"/>
87
109
</inventory>
88
110
"""
117
139
</inventory>
118
140
"""
119
141
 
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" />
125
 
</inventory>
126
 
"""
127
 
 
128
142
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
129
143
<directory file_id="tree-root-321" name="" revision="rev_outer" />
130
144
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
194
208
class TestSerializer(TestCase):
195
209
    """Test XML serialization"""
196
210
 
 
211
    def test_canned_inventory(self):
 
212
        """Test unpacked a canned inventory v4 file."""
 
213
        inp = StringIO(_working_inventory_v4)
 
214
        inv = serializer_v4.read_inventory(inp)
 
215
        self.assertEqual(len(inv), 4)
 
216
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
 
217
 
 
218
    def test_unpack_revision(self):
 
219
        """Test unpacking a canned revision v4"""
 
220
        inp = StringIO(_revision_v4)
 
221
        rev = serializer_v4.read_revision(inp)
 
222
        eq = self.assertEqual
 
223
        eq(rev.committer,
 
224
           "Martin Pool <mbp@sourcefrog.net>")
 
225
        eq(rev.inventory_id,
 
226
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
 
227
        eq(len(rev.parent_ids), 1)
 
228
        eq(rev.parent_ids[0],
 
229
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
230
 
197
231
    def test_unpack_revision_5(self):
198
232
        """Test unpacking a canned revision v5"""
199
233
        inp = StringIO(_revision_v5)
247
281
                _inventory_v5a, revision_id='test-rev-id')
248
282
        self.assertEqual('test-rev-id', inv.root.revision)
249
283
 
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
284
    def test_unpack_inventory_5b(self):
283
285
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(
284
286
                _inventory_v5b, revision_id='test-rev-id')
292
294
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
293
295
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
294
296
        self.assertEqual(inv, inv2)
295
 
 
 
297
    
296
298
    def assertRoundTrips(self, xml_string):
297
299
        inp = StringIO(xml_string)
298
300
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
350
352
    def get_sample_inventory(self):
351
353
        inv = Inventory('tree-root-321', revision_id='rev_outer')
352
354
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
353
 
        inv.add(inventory.InventoryDirectory('dir-id', 'dir',
 
355
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
354
356
                                             'tree-root-321'))
355
357
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
356
358
        inv['tree-root-321'].revision = 'rev_outer'
375
377
        for path, ie in inv.iter_entries():
376
378
            self.assertEqual(ie, inv2[ie.file_id])
377
379
 
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
380
    def test_wrong_format_v7(self):
390
381
        """Can't accidentally open a file with wrong serializer"""
391
382
        s_v6 = bzrlib.xml6.serializer_v6
392
383
        s_v7 = xml7.serializer_v7
393
 
        self.assertRaises(errors.UnexpectedInventoryFormat,
 
384
        self.assertRaises(errors.UnexpectedInventoryFormat, 
394
385
                          s_v7.read_inventory_from_string, _expected_inv_v5)
395
 
        self.assertRaises(errors.UnexpectedInventoryFormat,
 
386
        self.assertRaises(errors.UnexpectedInventoryFormat, 
396
387
                          s_v6.read_inventory_from_string, _expected_inv_v7)
397
388
 
398
389
    def test_tree_reference(self):
414
405
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
415
406
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
416
407
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
 
408
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
409
                          s_v6.read_inventory_from_string,
 
410
                          txt.replace('format="7"', 'format="6"'))
 
411
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
412
                          s_v5.read_inventory_from_string,
 
413
                          txt.replace('format="7"', 'format="5"'))
417
414
 
418
415
    def test_roundtrip_inventory_v8(self):
419
416
        inv = self.get_sample_inventory()
495
492
 
496
493
        self.assertEqual(len(expected), len(actual))
497
494
 
 
495
    def test_registry(self):
 
496
        self.assertIs(serializer_v4,
 
497
                      xml_serializer.format_registry.get('4'))
 
498
        self.assertIs(bzrlib.xml5.serializer_v5,
 
499
                      xml_serializer.format_registry.get('5'))
 
500
        self.assertIs(bzrlib.xml6.serializer_v6,
 
501
                      xml_serializer.format_registry.get('6'))
 
502
        self.assertIs(bzrlib.xml7.serializer_v7,
 
503
                      xml_serializer.format_registry.get('7'))
 
504
        self.assertIs(bzrlib.xml8.serializer_v8,
 
505
                      xml_serializer.format_registry.get('8'))
 
506
 
498
507
 
499
508
class TestEncodeAndEscape(TestCase):
500
509
    """Whitebox testing of the _encode_and_escape function."""
501
510
 
502
511
    def setUp(self):
503
 
        TestCase.setUp(self)
504
512
        # Keep the cache clear before and after the test
505
 
        bzrlib.xml_serializer._clear_cache()
506
 
        self.addCleanup(bzrlib.xml_serializer._clear_cache)
 
513
        bzrlib.xml8._ensure_utf8_re()
 
514
        bzrlib.xml8._clear_cache()
 
515
        self.addCleanup(bzrlib.xml8._clear_cache)
507
516
 
508
517
    def test_simple_ascii(self):
509
518
        # _encode_and_escape always appends a final ", because these parameters
510
519
        # are being used in xml attributes, and by returning it now, we have to
511
520
        # do fewer string operations later.
512
 
        val = bzrlib.xml_serializer.encode_and_escape('foo bar')
 
521
        val = bzrlib.xml8._encode_and_escape('foo bar')
513
522
        self.assertEqual('foo bar"', val)
514
523
        # The second time should be cached
515
 
        val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
 
524
        val2 = bzrlib.xml8._encode_and_escape('foo bar')
516
525
        self.assertIs(val2, val)
517
526
 
518
527
    def test_ascii_with_xml(self):
519
528
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
520
 
                         bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
 
529
                         bzrlib.xml8._encode_and_escape('&\'"<>'))
521
530
 
522
531
    def test_utf8_with_xml(self):
523
532
        # u'\xb5\xe5&\u062c'
524
533
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
525
534
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
526
 
                         bzrlib.xml_serializer.encode_and_escape(utf8_str))
 
535
                         bzrlib.xml8._encode_and_escape(utf8_str))
527
536
 
528
537
    def test_unicode(self):
529
538
        uni_str = u'\xb5\xe5&\u062c'
530
539
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
531
 
                         bzrlib.xml_serializer.encode_and_escape(uni_str))
532
 
 
533
 
 
534
 
class TestMisc(TestCase):
535
 
 
536
 
    def test_unescape_xml(self):
537
 
        """We get some kind of error when malformed entities are passed"""
538
 
        self.assertRaises(KeyError, bzrlib.xml8._unescape_xml, 'foo&bar;')
 
540
                         bzrlib.xml8._encode_and_escape(uni_str))