~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_xml.py

Merge bzr.dev (and fix NEWS)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005 by 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
 
from bzrlib import (
20
 
    errors,
21
 
    fifo_cache,
22
 
    inventory,
23
 
    xml6,
24
 
    xml7,
25
 
    xml8,
26
 
    )
27
19
from bzrlib.tests import TestCase
28
 
from bzrlib.inventory import Inventory
 
20
from bzrlib.inventory import Inventory, InventoryEntry
 
21
from bzrlib.xml4 import serializer_v4
29
22
import bzrlib.xml5
30
23
 
 
24
_working_inventory_v4 = """<inventory file_id="TREE_ROOT">
 
25
<entry file_id="bar-20050901064931-73b4b1138abc9cd2" kind="file" name="bar" parent_id="TREE_ROOT" />
 
26
<entry file_id="foo-20050801201819-4139aa4a272f4250" kind="directory" name="foo" parent_id="TREE_ROOT" />
 
27
<entry file_id="bar-20050824000535-6bc48cfad47ed134" kind="file" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" />
 
28
</inventory>"""
 
29
 
 
30
 
 
31
_revision_v4 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
32
    inventory_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
33
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
34
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
35
    timestamp="1125907235.211783886"
 
36
    timezone="36000">
 
37
<message>- start splitting code for xml (de)serialization away from objects
 
38
  preparatory to supporting multiple formats by a single library
 
39
</message>
 
40
<parents>
 
41
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" revision_sha1="7bdf4cc8c5bdac739f8cf9b10b78cf4b68f915ff" />
 
42
</parents>
 
43
</revision>
 
44
"""
 
45
 
31
46
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
32
47
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
33
48
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
34
 
    timestamp="1125907235.212"
 
49
    timestamp="1125907235.211783886"
35
50
    timezone="36000">
36
51
<message>- start splitting code for xml (de)serialization away from objects
37
52
  preparatory to supporting multiple formats by a single library
46
61
<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
47
62
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
48
63
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
49
 
    timestamp="1125907235.212"
 
64
    timestamp="1125907235.211783886"
50
65
    timezone="0">
51
66
<message>- start splitting code for xml (de)serialization away from objects
52
67
  preparatory to supporting multiple formats by a single library
58
73
"""
59
74
 
60
75
_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"/>
 
76
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
77
      name="bar" parent_id="TREE_ROOT" 
 
78
      revision="mbp@foo-123123"/>
65
79
<directory name="subdir"
66
80
           file_id="foo-20050801201819-4139aa4a272f4250"
67
 
           parent_id="TREE_ROOT"
 
81
           parent_id="TREE_ROOT" 
68
82
           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"/>
 
83
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
 
84
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
 
85
      revision="mbp@foo-00"/>
73
86
</inventory>
74
87
"""
75
88
 
76
89
_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"
 
90
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
91
      name="bar" parent_id="TREE_ROOT" 
79
92
      revision="mbp@foo-123123"/>
80
93
<directory name="subdir"
81
94
           file_id="foo-20050801201819-4139aa4a272f4250"
82
 
           parent_id="TREE_ROOT"
 
95
           parent_id="TREE_ROOT" 
83
96
           revision="mbp@foo-00"/>
84
 
<file file_id="bar-20050824000535-6bc48cfad47ed134"
85
 
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250"
 
97
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
 
98
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
86
99
      revision="mbp@foo-00"/>
87
100
</inventory>
88
101
"""
89
102
 
90
103
 
91
104
# DO NOT REFLOW THIS. Its the exact revision we want.
92
 
_expected_rev_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.212" timezone="36000">
 
105
_expected_rev_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;" format="5" inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41" revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9" timestamp="1125907235.211783886" timezone="36000">
93
106
<message>- start splitting code for xml (de)serialization away from objects
94
107
  preparatory to supporting multiple formats by a single library
95
108
</message>
102
115
 
103
116
# DO NOT REFLOW THIS. Its the exact inventory we want.
104
117
_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" />
 
118
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
106
119
<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" />
 
120
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
108
121
</inventory>
109
122
"""
110
123
 
111
124
 
112
125
_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" />
 
126
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" />
114
127
<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" />
125
 
</inventory>
126
 
"""
127
 
 
128
 
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
129
 
<directory file_id="tree-root-321" name="" revision="rev_outer" />
130
 
<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" />
133
 
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
134
 
</inventory>
135
 
"""
136
 
 
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
 
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
156
 
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
157
 
    revision_id="erik@b&#229;gfors-02"
158
 
    timestamp="1125907235.212"
159
 
    timezone="36000">
160
 
<message>Include &#181;nicode characters
161
 
</message>
162
 
<parents>
163
 
<revision_ref revision_id="erik@b&#229;gfors-01"/>
164
 
</parents>
165
 
</revision>
166
 
"""
167
 
 
168
 
_inventory_utf8_v5 = """<inventory file_id="TRE&#233;_ROOT" format="5"
169
 
                                   revision_id="erik@b&#229;gfors-02">
170
 
<file file_id="b&#229;r-01"
171
 
      name="b&#229;r" parent_id="TRE&#233;_ROOT"
172
 
      revision="erik@b&#229;gfors-01"/>
173
 
<directory name="s&#181;bdir"
174
 
           file_id="s&#181;bdir-01"
175
 
           parent_id="TRE&#233;_ROOT"
176
 
           revision="erik@b&#229;gfors-01"/>
177
 
<file executable="yes" file_id="b&#229;r-02"
178
 
      name="b&#229;r" parent_id="s&#181;bdir-01"
179
 
      revision="erik@b&#229;gfors-02"/>
180
 
</inventory>
181
 
"""
182
 
 
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">
 
128
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
190
129
</inventory>
191
130
"""
192
131
 
193
132
 
194
133
class TestSerializer(TestCase):
195
134
    """Test XML serialization"""
 
135
    def test_canned_inventory(self):
 
136
        """Test unpacked a canned inventory v4 file."""
 
137
        inp = StringIO(_working_inventory_v4)
 
138
        inv = serializer_v4.read_inventory(inp)
 
139
        self.assertEqual(len(inv), 4)
 
140
        self.assert_('bar-20050901064931-73b4b1138abc9cd2' in inv)
 
141
 
 
142
    def test_unpack_revision(self):
 
143
        """Test unpacking a canned revision v4"""
 
144
        inp = StringIO(_revision_v4)
 
145
        rev = serializer_v4.read_revision(inp)
 
146
        eq = self.assertEqual
 
147
        eq(rev.committer,
 
148
           "Martin Pool <mbp@sourcefrog.net>")
 
149
        eq(rev.inventory_id,
 
150
           "mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9")
 
151
        eq(len(rev.parent_ids), 1)
 
152
        eq(rev.parent_ids[0],
 
153
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
196
154
 
197
155
    def test_unpack_revision_5(self):
198
156
        """Test unpacking a canned revision v5"""
242
200
        eq(ie.name, 'bar')
243
201
        eq(inv[ie.parent_id].kind, 'directory')
244
202
 
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
203
    def test_repack_inventory_5(self):
288
204
        inp = StringIO(_committed_inv_v5)
289
205
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
292
208
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
293
209
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
294
210
        self.assertEqual(inv, inv2)
295
 
 
 
211
    
296
212
    def assertRoundTrips(self, xml_string):
297
213
        inp = StringIO(xml_string)
298
214
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
299
215
        outp = StringIO()
300
216
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
301
217
        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
218
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
306
219
        self.assertEqual(inv, inv2)
307
220
 
346
259
        txt = s_v5.write_revision_to_string(rev)
347
260
        new_rev = s_v5.read_revision_from_string(txt)
348
261
        self.assertEqual(props, new_rev.properties)
349
 
 
350
 
    def get_sample_inventory(self):
351
 
        inv = Inventory('tree-root-321', revision_id='rev_outer')
352
 
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
353
 
        inv.add(inventory.InventoryDirectory('dir-id', 'dir',
354
 
                                             'tree-root-321'))
355
 
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
356
 
        inv['tree-root-321'].revision = 'rev_outer'
357
 
        inv['dir-id'].revision = 'rev_outer'
358
 
        inv['file-id'].revision = 'rev_outer'
359
 
        inv['file-id'].text_sha1 = 'A'
360
 
        inv['file-id'].text_size = 1
361
 
        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
 
        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
 
        self.assertEqualDiff(_expected_inv_v7, txt)
373
 
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
374
 
        self.assertEqual(5, len(inv2))
375
 
        for path, ie in inv.iter_entries():
376
 
            self.assertEqual(ie, inv2[ie.file_id])
377
 
 
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
 
    def test_wrong_format_v7(self):
390
 
        """Can't accidentally open a file with wrong serializer"""
391
 
        s_v6 = bzrlib.xml6.serializer_v6
392
 
        s_v7 = xml7.serializer_v7
393
 
        self.assertRaises(errors.UnexpectedInventoryFormat,
394
 
                          s_v7.read_inventory_from_string, _expected_inv_v5)
395
 
        self.assertRaises(errors.UnexpectedInventoryFormat,
396
 
                          s_v6.read_inventory_from_string, _expected_inv_v7)
397
 
 
398
 
    def test_tree_reference(self):
399
 
        s_v5 = bzrlib.xml5.serializer_v5
400
 
        s_v6 = bzrlib.xml6.serializer_v6
401
 
        s_v7 = xml7.serializer_v7
402
 
        inv = Inventory('tree-root-321', revision_id='rev-outer')
403
 
        inv.root.revision = 'root-rev'
404
 
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
405
 
                                        'rev-outer', 'rev-inner'))
406
 
        self.assertRaises(errors.UnsupportedInventoryKind,
407
 
                          s_v5.write_inventory_to_string, inv)
408
 
        self.assertRaises(errors.UnsupportedInventoryKind,
409
 
                          s_v6.write_inventory_to_string, inv)
410
 
        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
 
        inv2 = s_v7.read_inventory_from_string(txt)
414
 
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
415
 
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
416
 
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
417
 
 
418
 
    def test_roundtrip_inventory_v8(self):
419
 
        inv = self.get_sample_inventory()
420
 
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
421
 
        inv2 = xml8.serializer_v8.read_inventory_from_string(txt)
422
 
        self.assertEqual(4, len(inv2))
423
 
        for path, ie in inv.iter_entries():
424
 
            self.assertEqual(ie, inv2[ie.file_id])
425
 
 
426
 
    def test_inventory_text_v8(self):
427
 
        inv = self.get_sample_inventory()
428
 
        txt = xml8.serializer_v8.write_inventory_to_string(inv)
429
 
        lines = xml8.serializer_v8.write_inventory_to_lines(inv)
430
 
        self.assertEqual(bzrlib.osutils.split_lines(txt), lines)
431
 
        self.assertEqualDiff(_expected_inv_v8, txt)
432
 
 
433
 
    def test_revision_text_v6(self):
434
 
        """Pack revision to XML v6"""
435
 
        rev = bzrlib.xml6.serializer_v6.read_revision_from_string(
436
 
            _expected_rev_v5)
437
 
        serialized = bzrlib.xml6.serializer_v6.write_revision_to_string(rev)
438
 
        self.assertEqualDiff(serialized, _expected_rev_v5)
439
 
 
440
 
    def test_revision_text_v7(self):
441
 
        """Pack revision to XML v7"""
442
 
        rev = bzrlib.xml7.serializer_v7.read_revision_from_string(
443
 
            _expected_rev_v5)
444
 
        serialized = bzrlib.xml7.serializer_v7.write_revision_to_string(rev)
445
 
        self.assertEqualDiff(serialized, _expected_rev_v5)
446
 
 
447
 
    def test_revision_text_v8(self):
448
 
        """Pack revision to XML v8"""
449
 
        rev = bzrlib.xml8.serializer_v8.read_revision_from_string(
450
 
            _expected_rev_v8)
451
 
        serialized = bzrlib.xml8.serializer_v8.write_revision_to_string(rev)
452
 
        self.assertEqualDiff(serialized, _expected_rev_v8)
453
 
 
454
 
    def test_revision_ids_are_utf8(self):
455
 
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
456
 
        s_v5 = bzrlib.xml5.serializer_v5
457
 
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
458
 
        self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
459
 
        self.assertIsInstance(rev.revision_id, str)
460
 
        self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
461
 
        for parent_id in rev.parent_ids:
462
 
            self.assertIsInstance(parent_id, str)
463
 
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
464
 
        self.assertIsInstance(rev.message, unicode)
465
 
 
466
 
        # ie.revision should either be None or a utf-8 revision id
467
 
        inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
468
 
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
469
 
        rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
470
 
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
471
 
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
472
 
        fid_sub = u's\xb5bdir-01'.encode('utf8')
473
 
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
474
 
        expected = [(u'', fid_root, None, rev_id_2),
475
 
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
476
 
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
477
 
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
478
 
                   ]
479
 
        self.assertEqual(rev_id_2, inv.revision_id)
480
 
        self.assertIsInstance(inv.revision_id, str)
481
 
 
482
 
        actual = list(inv.iter_entries_by_dir())
483
 
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
484
 
             (act_path, act_ie)) in zip(expected, actual):
485
 
            self.assertEqual(exp_path, act_path)
486
 
            self.assertIsInstance(act_path, unicode)
487
 
            self.assertEqual(exp_file_id, act_ie.file_id)
488
 
            self.assertIsInstance(act_ie.file_id, str)
489
 
            self.assertEqual(exp_parent_id, act_ie.parent_id)
490
 
            if exp_parent_id is not None:
491
 
                self.assertIsInstance(act_ie.parent_id, str)
492
 
            self.assertEqual(exp_rev_id, act_ie.revision)
493
 
            if exp_rev_id is not None:
494
 
                self.assertIsInstance(act_ie.revision, str)
495
 
 
496
 
        self.assertEqual(len(expected), len(actual))
497
 
 
498
 
 
499
 
class TestEncodeAndEscape(TestCase):
500
 
    """Whitebox testing of the _encode_and_escape function."""
501
 
 
502
 
    def setUp(self):
503
 
        super(TestEncodeAndEscape, self).setUp()
504
 
        # Keep the cache clear before and after the test
505
 
        bzrlib.xml_serializer._clear_cache()
506
 
        self.addCleanup(bzrlib.xml_serializer._clear_cache)
507
 
 
508
 
    def test_simple_ascii(self):
509
 
        # _encode_and_escape always appends a final ", because these parameters
510
 
        # are being used in xml attributes, and by returning it now, we have to
511
 
        # do fewer string operations later.
512
 
        val = bzrlib.xml_serializer.encode_and_escape('foo bar')
513
 
        self.assertEqual('foo bar"', val)
514
 
        # The second time should be cached
515
 
        val2 = bzrlib.xml_serializer.encode_and_escape('foo bar')
516
 
        self.assertIs(val2, val)
517
 
 
518
 
    def test_ascii_with_xml(self):
519
 
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
520
 
                         bzrlib.xml_serializer.encode_and_escape('&\'"<>'))
521
 
 
522
 
    def test_utf8_with_xml(self):
523
 
        # u'\xb5\xe5&\u062c'
524
 
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
525
 
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
526
 
                         bzrlib.xml_serializer.encode_and_escape(utf8_str))
527
 
 
528
 
    def test_unicode(self):
529
 
        uni_str = u'\xb5\xe5&\u062c'
530
 
        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;')