/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/TransportUtil.py

  • Committer: Vincent Ladeuil
  • Date: 2007-06-20 14:25:06 UTC
  • mfrom: (2540 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2646.
  • Revision ID: v.ladeuil+lp@free.fr-20070620142506-txsb1v8538kpsafw
merge bzr.dev @ 2540

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 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
 
 
18
from bzrlib.hooks import Hooks
 
19
from bzrlib.tests.test_ftp_transport import TestCaseWithFTPServer
 
20
from bzrlib.transport import (
 
21
    register_transport,
 
22
    unregister_transport,
 
23
    )
 
24
from bzrlib.transport.ftp import FtpTransport
 
25
 
 
26
 
 
27
class TransportHooks(Hooks):
 
28
    """Dict-mapping hook name to a list of callables for transport hooks"""
 
29
 
 
30
    def __init__(self):
 
31
        Hooks.__init__(self)
 
32
        # Invoked when the transport has just created a new connection.
 
33
        # The api signature is (transport, connection, credentials)
 
34
        self['_set_connection'] = []
 
35
 
 
36
 
 
37
class InstrumentedTransport(FtpTransport):
 
38
    """Instrumented transport class to test commands behavior"""
 
39
 
 
40
    hooks = TransportHooks()
 
41
 
 
42
 
 
43
class ConnectionHookedTransport(InstrumentedTransport):
 
44
    """Transport instrumented to inspect connections"""
 
45
 
 
46
    def _set_connection(self, connection, credentials):
 
47
        """Called when a new connection is created """
 
48
        super(ConnectionHookedTransport, self)._set_connection(connection,
 
49
                                                               credentials)
 
50
        for hook in self.hooks['_set_connection']:
 
51
            hook(self, connection, credentials)
 
52
 
 
53
 
 
54
class TestCaseWithConnectionHookedTransport(TestCaseWithFTPServer):
 
55
 
 
56
    def setUp(self):
 
57
        super(TestCaseWithConnectionHookedTransport, self).setUp()
 
58
        self._hooks_installed = False
 
59
 
 
60
        def cleanup():
 
61
            # Be nice and reset hooks if the test writer forgot about them
 
62
            if self._hooks_installed:
 
63
                self.reset_hooks()
 
64
 
 
65
        self.addCleanup(cleanup)
 
66
        self.connections = []
 
67
 
 
68
    def install_hooks(self):
 
69
        ConnectionHookedTransport.hooks.install_hook('_set_connection',
 
70
                                                     self.set_connection_hook)
 
71
        # Make our instrumented transport the default ftp transport
 
72
        register_transport('ftp://', ConnectionHookedTransport)
 
73
        self._hooks_installed = True
 
74
 
 
75
    def reset_hooks(self):
 
76
        InstrumentedTransport.hooks = TransportHooks()
 
77
        unregister_transport('ftp://', ConnectionHookedTransport)
 
78
        self._hooks_installed = False
 
79
 
 
80
    def reset_connections(self):
 
81
        self.connections = []
 
82
 
 
83
    def set_connection_hook(self, transport, connection, credentials):
 
84
        # Note: uncomment the following line and use 'bt' under pdb, here is
 
85
        # the extra connection.
 
86
        # import pdb; pdb.set_trace()
 
87
        self.connections.append(connection)
 
88