~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockable_files.py

  • Committer: Robert Collins
  • Date: 2006-02-13 21:52:05 UTC
  • mto: (1534.1.22 integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robertc@robertcollins.net-20060213215205-cb3bacc87092a639
Update per review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by 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 StringIO import StringIO
 
18
 
 
19
from bzrlib.branch import Branch
 
20
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
 
21
from bzrlib.lockable_files import LockableFiles
 
22
from bzrlib.tests import TestCaseInTempDir
 
23
from bzrlib.transactions import PassThroughTransaction, ReadOnlyTransaction
 
24
from bzrlib.transport import get_transport
 
25
 
 
26
class TestLockableFiles(TestCaseInTempDir):
 
27
 
 
28
    def setUp(self):
 
29
        super(TestLockableFiles, self).setUp()
 
30
        transport = get_transport('.')
 
31
        transport.mkdir('.bzr')
 
32
        transport.put('.bzr/my-lock', StringIO(''))
 
33
        self.lockable = LockableFiles(transport.clone('.bzr'), 'my-lock')
 
34
 
 
35
    def test_read_write(self):
 
36
        self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
 
37
        self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
 
38
        self.lockable.lock_write()
 
39
        try:
 
40
            unicode_string = u'bar\u1234'
 
41
            self.assertEqual(4, len(unicode_string))
 
42
            byte_string = unicode_string.encode('utf-8')
 
43
            self.assertEqual(6, len(byte_string))
 
44
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo', 
 
45
                              StringIO(unicode_string))
 
46
            self.lockable.put('foo', StringIO(byte_string))
 
47
            self.assertEqual(byte_string,
 
48
                             self.lockable.get('foo').read())
 
49
            self.assertEqual(unicode_string,
 
50
                             self.lockable.get_utf8('foo').read())
 
51
            self.assertRaises(BzrBadParameterNotString,
 
52
                              self.lockable.put_utf8,
 
53
                              'bar',
 
54
                              StringIO(unicode_string)
 
55
                              )
 
56
            self.lockable.put_utf8('bar', unicode_string)
 
57
            self.assertEqual(unicode_string, 
 
58
                             self.lockable.get_utf8('bar').read())
 
59
            self.assertEqual(byte_string, 
 
60
                             self.lockable.get('bar').read())
 
61
        finally:
 
62
            self.lockable.unlock()
 
63
 
 
64
    def test_locks(self):
 
65
        self.lockable.lock_read()
 
66
        try:
 
67
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
 
68
                              StringIO('bar\u1234'))
 
69
        finally:
 
70
            self.lockable.unlock()
 
71
 
 
72
    def test_transactions(self):
 
73
        self.assertIs(self.lockable.get_transaction().__class__,
 
74
                      PassThroughTransaction)
 
75
        self.lockable.lock_read()
 
76
        try:
 
77
            self.assertIs(self.lockable.get_transaction().__class__,
 
78
                          ReadOnlyTransaction)
 
79
        finally:
 
80
            self.lockable.unlock()
 
81
        self.assertIs(self.lockable.get_transaction().__class__,
 
82
                      PassThroughTransaction)
 
83
        self.lockable.lock_write()
 
84
        self.assertIs(self.lockable.get_transaction().__class__,
 
85
                      PassThroughTransaction)
 
86
        self.lockable.unlock()
 
87
 
 
88
    def test__escape(self):
 
89
        self.assertEqual('%25', self.lockable._escape('%'))
 
90
        
 
91
    def test__escape_empty(self):
 
92
        self.assertEqual('', self.lockable._escape(''))
 
93