~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: 2007-08-01 17:14:51 UTC
  • mfrom: (2662.1.1 bzr.easy_install)
  • Revision ID: pqm@pqm.ubuntu.com-20070801171451-en3tds1hzlru2j83
allow ``easy_install bzr`` runs without fatal errors. (#125521, bialix,
 r=mbp)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from cStringIO import StringIO
 
18
 
 
19
from bzrlib import (
 
20
    errors, 
 
21
    inventory, 
 
22
    xml7,
 
23
    xml_serializer,
 
24
    )
 
25
from bzrlib.tests import TestCase
 
26
from bzrlib.inventory import Inventory, InventoryEntry
 
27
from bzrlib.xml4 import serializer_v4
 
28
import bzrlib.xml5
 
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
 
 
52
_revision_v5 = """<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
53
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
54
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
55
    timestamp="1125907235.212"
 
56
    timezone="36000">
 
57
<message>- start splitting code for xml (de)serialization away from objects
 
58
  preparatory to supporting multiple formats by a single library
 
59
</message>
 
60
<parents>
 
61
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
 
62
</parents>
 
63
</revision>
 
64
"""
 
65
 
 
66
_revision_v5_utc = """\
 
67
<revision committer="Martin Pool &lt;mbp@sourcefrog.net&gt;"
 
68
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
69
    revision_id="mbp@sourcefrog.net-20050905080035-e0439293f8b6b9f9"
 
70
    timestamp="1125907235.212"
 
71
    timezone="0">
 
72
<message>- start splitting code for xml (de)serialization away from objects
 
73
  preparatory to supporting multiple formats by a single library
 
74
</message>
 
75
<parents>
 
76
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92"/>
 
77
</parents>
 
78
</revision>
 
79
"""
 
80
 
 
81
_committed_inv_v5 = """<inventory>
 
82
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
83
      name="bar" parent_id="TREE_ROOT" 
 
84
      revision="mbp@foo-123123"/>
 
85
<directory name="subdir"
 
86
           file_id="foo-20050801201819-4139aa4a272f4250"
 
87
           parent_id="TREE_ROOT" 
 
88
           revision="mbp@foo-00"/>
 
89
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" 
 
90
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
 
91
      revision="mbp@foo-00"/>
 
92
</inventory>
 
93
"""
 
94
 
 
95
_basis_inv_v5 = """<inventory revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92">
 
96
<file file_id="bar-20050901064931-73b4b1138abc9cd2" 
 
97
      name="bar" parent_id="TREE_ROOT" 
 
98
      revision="mbp@foo-123123"/>
 
99
<directory name="subdir"
 
100
           file_id="foo-20050801201819-4139aa4a272f4250"
 
101
           parent_id="TREE_ROOT" 
 
102
           revision="mbp@foo-00"/>
 
103
<file file_id="bar-20050824000535-6bc48cfad47ed134" 
 
104
      name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" 
 
105
      revision="mbp@foo-00"/>
 
106
</inventory>
 
107
"""
 
108
 
 
109
 
 
110
# DO NOT REFLOW THIS. Its the exact revision we want.
 
111
_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">
 
112
<message>- start splitting code for xml (de)serialization away from objects
 
113
  preparatory to supporting multiple formats by a single library
 
114
</message>
 
115
<parents>
 
116
<revision_ref revision_id="mbp@sourcefrog.net-20050905063503-43948f59fa127d92" />
 
117
</parents>
 
118
</revision>
 
119
"""
 
120
 
 
121
 
 
122
# DO NOT REFLOW THIS. Its the exact inventory we want.
 
123
_expected_inv_v5 = """<inventory format="5">
 
124
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" revision="mbp@foo-123123" />
 
125
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" revision="mbp@foo-00" />
 
126
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
 
127
</inventory>
 
128
"""
 
129
 
 
130
 
 
131
_expected_inv_v5_root = """<inventory file_id="f&lt;" format="5" revision_id="mother!">
 
132
<file file_id="bar-20050901064931-73b4b1138abc9cd2" name="bar" parent_id="f&lt;" revision="mbp@foo-123123" />
 
133
<directory file_id="foo-20050801201819-4139aa4a272f4250" name="subdir" parent_id="f&lt;" revision="mbp@foo-00" />
 
134
<file executable="yes" file_id="bar-20050824000535-6bc48cfad47ed134" name="bar" parent_id="foo-20050801201819-4139aa4a272f4250" revision="mbp@foo-00" />
 
135
</inventory>
 
136
"""
 
137
 
 
138
_expected_inv_v7 = """<inventory format="7" revision_id="rev_outer">
 
139
<directory file_id="tree-root-321" name="" revision="rev_outer" />
 
140
<directory file_id="dir-id" name="dir" parent_id="tree-root-321" revision="rev_outer" />
 
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" />
 
143
<tree-reference file_id="nested-id" name="nested" parent_id="tree-root-321" revision="rev_outer" reference_revision="rev_inner" />
 
144
</inventory>
 
145
"""
 
146
 
 
147
_revision_utf8_v5 = """<revision committer="Erik B&#229;gfors &lt;erik@foo.net&gt;"
 
148
    inventory_sha1="e79c31c1deb64c163cf660fdedd476dd579ffd41"
 
149
    revision_id="erik@b&#229;gfors-02"
 
150
    timestamp="1125907235.212"
 
151
    timezone="36000">
 
152
<message>Include &#181;nicode characters
 
153
</message>
 
154
<parents>
 
155
<revision_ref revision_id="erik@b&#229;gfors-01"/>
 
156
</parents>
 
157
</revision>
 
158
"""
 
159
 
 
160
_inventory_utf8_v5 = """<inventory file_id="TRE&#233;_ROOT" format="5"
 
161
                                   revision_id="erik@b&#229;gfors-02">
 
162
<file file_id="b&#229;r-01"
 
163
      name="b&#229;r" parent_id="TRE&#233;_ROOT"
 
164
      revision="erik@b&#229;gfors-01"/>
 
165
<directory name="s&#181;bdir"
 
166
           file_id="s&#181;bdir-01"
 
167
           parent_id="TRE&#233;_ROOT"
 
168
           revision="erik@b&#229;gfors-01"/>
 
169
<file executable="yes" file_id="b&#229;r-02"
 
170
      name="b&#229;r" parent_id="s&#181;bdir-01"
 
171
      revision="erik@b&#229;gfors-02"/>
 
172
</inventory>
 
173
"""
 
174
 
 
175
 
 
176
class TestSerializer(TestCase):
 
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")
 
197
 
 
198
    def test_unpack_revision_5(self):
 
199
        """Test unpacking a canned revision v5"""
 
200
        inp = StringIO(_revision_v5)
 
201
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
202
        eq = self.assertEqual
 
203
        eq(rev.committer,
 
204
           "Martin Pool <mbp@sourcefrog.net>")
 
205
        eq(len(rev.parent_ids), 1)
 
206
        eq(rev.timezone, 36000)
 
207
        eq(rev.parent_ids[0],
 
208
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
209
 
 
210
    def test_unpack_revision_5_utc(self):
 
211
        inp = StringIO(_revision_v5_utc)
 
212
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
213
        eq = self.assertEqual
 
214
        eq(rev.committer,
 
215
           "Martin Pool <mbp@sourcefrog.net>")
 
216
        eq(len(rev.parent_ids), 1)
 
217
        eq(rev.timezone, 0)
 
218
        eq(rev.parent_ids[0],
 
219
           "mbp@sourcefrog.net-20050905063503-43948f59fa127d92")
 
220
 
 
221
    def test_unpack_inventory_5(self):
 
222
        """Unpack canned new-style inventory"""
 
223
        inp = StringIO(_committed_inv_v5)
 
224
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
225
        eq = self.assertEqual
 
226
        eq(len(inv), 4)
 
227
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
 
228
        eq(ie.kind, 'file')
 
229
        eq(ie.revision, 'mbp@foo-00')
 
230
        eq(ie.name, 'bar')
 
231
        eq(inv[ie.parent_id].kind, 'directory')
 
232
 
 
233
    def test_unpack_basis_inventory_5(self):
 
234
        """Unpack canned new-style inventory"""
 
235
        inp = StringIO(_basis_inv_v5)
 
236
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
237
        eq = self.assertEqual
 
238
        eq(len(inv), 4)
 
239
        eq(inv.revision_id, 'mbp@sourcefrog.net-20050905063503-43948f59fa127d92')
 
240
        ie = inv['bar-20050824000535-6bc48cfad47ed134']
 
241
        eq(ie.kind, 'file')
 
242
        eq(ie.revision, 'mbp@foo-00')
 
243
        eq(ie.name, 'bar')
 
244
        eq(inv[ie.parent_id].kind, 'directory')
 
245
 
 
246
    def test_repack_inventory_5(self):
 
247
        inp = StringIO(_committed_inv_v5)
 
248
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
249
        outp = StringIO()
 
250
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
 
251
        self.assertEqualDiff(_expected_inv_v5, outp.getvalue())
 
252
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
 
253
        self.assertEqual(inv, inv2)
 
254
    
 
255
    def assertRoundTrips(self, xml_string):
 
256
        inp = StringIO(xml_string)
 
257
        inv = bzrlib.xml5.serializer_v5.read_inventory(inp)
 
258
        outp = StringIO()
 
259
        bzrlib.xml5.serializer_v5.write_inventory(inv, outp)
 
260
        self.assertEqualDiff(xml_string, outp.getvalue())
 
261
        inv2 = bzrlib.xml5.serializer_v5.read_inventory(StringIO(outp.getvalue()))
 
262
        self.assertEqual(inv, inv2)
 
263
 
 
264
    def tests_serialize_inventory_v5_with_root(self):
 
265
        self.assertRoundTrips(_expected_inv_v5_root)
 
266
 
 
267
    def check_repack_revision(self, txt):
 
268
        """Check that repacking a revision yields the same information"""
 
269
        inp = StringIO(txt)
 
270
        rev = bzrlib.xml5.serializer_v5.read_revision(inp)
 
271
        outp = StringIO()
 
272
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
 
273
        outfile_contents = outp.getvalue()
 
274
        rev2 = bzrlib.xml5.serializer_v5.read_revision(StringIO(outfile_contents))
 
275
        self.assertEqual(rev, rev2)
 
276
 
 
277
    def test_repack_revision_5(self):
 
278
        """Round-trip revision to XML v5"""
 
279
        self.check_repack_revision(_revision_v5)
 
280
 
 
281
    def test_repack_revision_5_utc(self):
 
282
        self.check_repack_revision(_revision_v5_utc)
 
283
 
 
284
    def test_pack_revision_5(self):
 
285
        """Pack revision to XML v5"""
 
286
        # fixed 20051025, revisions should have final newline
 
287
        rev = bzrlib.xml5.serializer_v5.read_revision_from_string(_revision_v5)
 
288
        outp = StringIO()
 
289
        bzrlib.xml5.serializer_v5.write_revision(rev, outp)
 
290
        outfile_contents = outp.getvalue()
 
291
        self.assertEqual(outfile_contents[-1], '\n')
 
292
        self.assertEqualDiff(outfile_contents, bzrlib.xml5.serializer_v5.write_revision_to_string(rev))
 
293
        self.assertEqualDiff(outfile_contents, _expected_rev_v5)
 
294
 
 
295
    def test_empty_property_value(self):
 
296
        """Create an empty property value check that it serializes correctly"""
 
297
        s_v5 = bzrlib.xml5.serializer_v5
 
298
        rev = s_v5.read_revision_from_string(_revision_v5)
 
299
        outp = StringIO()
 
300
        props = {'empty':'', 'one':'one'}
 
301
        rev.properties = props
 
302
        txt = s_v5.write_revision_to_string(rev)
 
303
        new_rev = s_v5.read_revision_from_string(txt)
 
304
        self.assertEqual(props, new_rev.properties)
 
305
 
 
306
    def test_roundtrip_inventory_v7(self):
 
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'))
 
310
        inv.add(inventory.InventoryFile('file-id', 'file', 'tree-root-321'))
 
311
        inv.add(inventory.InventoryDirectory('dir-id', 'dir', 
 
312
                                             'tree-root-321'))
 
313
        inv.add(inventory.InventoryLink('link-id', 'link', 'tree-root-321'))
 
314
        inv['tree-root-321'].revision = 'rev_outer'
 
315
        inv['dir-id'].revision = 'rev_outer'
 
316
        inv['file-id'].revision = 'rev_outer'
 
317
        inv['link-id'].revision = 'rev_outer'
 
318
        txt = xml7.serializer_v7.write_inventory_to_string(inv)
 
319
        self.assertEqualDiff(_expected_inv_v7, txt)
 
320
        inv2 = xml7.serializer_v7.read_inventory_from_string(txt)
 
321
        self.assertEqual(5, len(inv2))
 
322
        for path, ie in inv.iter_entries():
 
323
            self.assertEqual(ie, inv2[ie.file_id])
 
324
 
 
325
    def test_wrong_format_v7(self):
 
326
        """Can't accidentally open a file with wrong serializer"""
 
327
        s_v6 = bzrlib.xml6.serializer_v6
 
328
        s_v7 = xml7.serializer_v7
 
329
        self.assertRaises(errors.UnexpectedInventoryFormat, 
 
330
                          s_v7.read_inventory_from_string, _expected_inv_v5)
 
331
        self.assertRaises(errors.UnexpectedInventoryFormat, 
 
332
                          s_v6.read_inventory_from_string, _expected_inv_v7)
 
333
 
 
334
    def test_tree_reference(self):
 
335
        s_v5 = bzrlib.xml5.serializer_v5
 
336
        s_v6 = bzrlib.xml6.serializer_v6
 
337
        s_v7 = xml7.serializer_v7
 
338
        inv = Inventory('tree-root-321')
 
339
        inv.add(inventory.TreeReference('nested-id', 'nested', 'tree-root-321',
 
340
                                        'rev-outer', 'rev-inner'))
 
341
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
342
                          s_v5.write_inventory_to_string, inv)
 
343
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
344
                          s_v6.write_inventory_to_string, inv)
 
345
        txt = s_v7.write_inventory_to_string(inv)
 
346
        inv2 = s_v7.read_inventory_from_string(txt)
 
347
        self.assertEqual('tree-root-321', inv2['nested-id'].parent_id)
 
348
        self.assertEqual('rev-outer', inv2['nested-id'].revision)
 
349
        self.assertEqual('rev-inner', inv2['nested-id'].reference_revision)
 
350
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
351
                          s_v6.read_inventory_from_string,
 
352
                          txt.replace('format="7"', 'format="6"'))
 
353
        self.assertRaises(errors.UnsupportedInventoryKind, 
 
354
                          s_v5.read_inventory_from_string,
 
355
                          txt.replace('format="7"', 'format="5"'))
 
356
 
 
357
    def test_revision_ids_are_utf8(self):
 
358
        """Parsed revision_ids should all be utf-8 strings, not unicode."""
 
359
        s_v5 = bzrlib.xml5.serializer_v5
 
360
        rev = s_v5.read_revision_from_string(_revision_utf8_v5)
 
361
        self.assertEqual('erik@b\xc3\xa5gfors-02', rev.revision_id)
 
362
        self.assertIsInstance(rev.revision_id, str)
 
363
        self.assertEqual(['erik@b\xc3\xa5gfors-01'], rev.parent_ids)
 
364
        for parent_id in rev.parent_ids:
 
365
            self.assertIsInstance(parent_id, str)
 
366
        self.assertEqual(u'Include \xb5nicode characters\n', rev.message)
 
367
        self.assertIsInstance(rev.message, unicode)
 
368
 
 
369
        # ie.revision should either be None or a utf-8 revision id
 
370
        inv = s_v5.read_inventory_from_string(_inventory_utf8_v5)
 
371
        rev_id_1 = u'erik@b\xe5gfors-01'.encode('utf8')
 
372
        rev_id_2 = u'erik@b\xe5gfors-02'.encode('utf8')
 
373
        fid_root = u'TRE\xe9_ROOT'.encode('utf8')
 
374
        fid_bar1 = u'b\xe5r-01'.encode('utf8')
 
375
        fid_sub = u's\xb5bdir-01'.encode('utf8')
 
376
        fid_bar2 = u'b\xe5r-02'.encode('utf8')
 
377
        expected = [(u'', fid_root, None, None),
 
378
                    (u'b\xe5r', fid_bar1, fid_root, rev_id_1),
 
379
                    (u's\xb5bdir', fid_sub, fid_root, rev_id_1),
 
380
                    (u's\xb5bdir/b\xe5r', fid_bar2, fid_sub, rev_id_2),
 
381
                   ]
 
382
        self.assertEqual(rev_id_2, inv.revision_id)
 
383
        self.assertIsInstance(inv.revision_id, str)
 
384
 
 
385
        actual = list(inv.iter_entries_by_dir())
 
386
        for ((exp_path, exp_file_id, exp_parent_id, exp_rev_id),
 
387
             (act_path, act_ie)) in zip(expected, actual):
 
388
            self.assertEqual(exp_path, act_path)
 
389
            self.assertIsInstance(act_path, unicode)
 
390
            self.assertEqual(exp_file_id, act_ie.file_id)
 
391
            self.assertIsInstance(act_ie.file_id, str)
 
392
            self.assertEqual(exp_parent_id, act_ie.parent_id)
 
393
            if exp_parent_id is not None:
 
394
                self.assertIsInstance(act_ie.parent_id, str)
 
395
            self.assertEqual(exp_rev_id, act_ie.revision)
 
396
            if exp_rev_id is not None:
 
397
                self.assertIsInstance(act_ie.revision, str)
 
398
 
 
399
        self.assertEqual(len(expected), len(actual))
 
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
 
 
411
 
 
412
class TestEncodeAndEscape(TestCase):
 
413
    """Whitebox testing of the _encode_and_escape function."""
 
414
 
 
415
    def setUp(self):
 
416
        # Keep the cache clear before and after the test
 
417
        bzrlib.xml5._ensure_utf8_re()
 
418
        bzrlib.xml5._clear_cache()
 
419
        self.addCleanup(bzrlib.xml5._clear_cache)
 
420
 
 
421
    def test_simple_ascii(self):
 
422
        # _encode_and_escape always appends a final ", because these parameters
 
423
        # are being used in xml attributes, and by returning it now, we have to
 
424
        # do fewer string operations later.
 
425
        val = bzrlib.xml5._encode_and_escape('foo bar')
 
426
        self.assertEqual('foo bar"', val)
 
427
        # The second time should be cached
 
428
        val2 = bzrlib.xml5._encode_and_escape('foo bar')
 
429
        self.assertIs(val2, val)
 
430
 
 
431
    def test_ascii_with_xml(self):
 
432
        self.assertEqual('&amp;&apos;&quot;&lt;&gt;"',
 
433
                         bzrlib.xml5._encode_and_escape('&\'"<>'))
 
434
 
 
435
    def test_utf8_with_xml(self):
 
436
        # u'\xb5\xe5&\u062c'
 
437
        utf8_str = '\xc2\xb5\xc3\xa5&\xd8\xac'
 
438
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
 
439
                         bzrlib.xml5._encode_and_escape(utf8_str))
 
440
 
 
441
    def test_unicode(self):
 
442
        uni_str = u'\xb5\xe5&\u062c'
 
443
        self.assertEqual('&#181;&#229;&amp;&#1580;"',
 
444
                         bzrlib.xml5._encode_and_escape(uni_str))