1
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Tests for L{twisted.words.xish.xmlstream}.
8
from twisted.internet import protocol
9
from twisted.trial import unittest
10
from twisted.words.xish import domish, utility, xmlstream
12
class XmlStreamTest(unittest.TestCase):
15
self.xmlstream = xmlstream.XmlStream()
16
self.xmlstream.transport = self
17
self.xmlstream.transport.write = self.outlist.append
20
def loseConnection(self):
22
Stub loseConnection because we are a transport.
24
self.xmlstream.connectionLost("no reason")
29
Sending data should result into it being written to the transport.
31
self.xmlstream.connectionMade()
32
self.xmlstream.send("<root>")
33
self.assertEquals(self.outlist[0], "<root>")
36
def test_receiveRoot(self):
38
Receiving the starttag of the root element results in stream start.
42
def streamStartEvent(rootelem):
43
streamStarted.append(None)
45
self.xmlstream.addObserver(xmlstream.STREAM_START_EVENT,
47
self.xmlstream.connectionMade()
48
self.xmlstream.dataReceived("<root>")
49
self.assertEquals(1, len(streamStarted))
52
def test_receiveBadXML(self):
54
Receiving malformed XML should result in in error.
59
def streamErrorEvent(reason):
60
streamError.append(reason)
62
def streamEndEvent(_):
63
streamEnd.append(None)
65
self.xmlstream.addObserver(xmlstream.STREAM_ERROR_EVENT,
67
self.xmlstream.addObserver(xmlstream.STREAM_END_EVENT,
69
self.xmlstream.connectionMade()
71
self.xmlstream.dataReceived("<root>")
72
self.assertEquals(0, len(streamError))
73
self.assertEquals(0, len(streamEnd))
75
self.xmlstream.dataReceived("<child><unclosed></child>")
76
self.assertEquals(1, len(streamError))
77
self.assertTrue(streamError[0].check(domish.ParserError))
78
self.assertEquals(1, len(streamEnd))
82
class DummyProtocol(protocol.Protocol, utility.EventDispatcher):
84
I am a protocol with an event dispatcher without further processing.
86
This protocol is only used for testing XmlStreamFactoryMixin to make
87
sure the bootstrap observers are added to the protocol instance.
90
def __init__(self, *args, **kwargs):
95
utility.EventDispatcher.__init__(self)
99
class BootstrapMixinTest(unittest.TestCase):
101
Tests for L{xmlstream.BootstrapMixin}.
103
@ivar factory: Instance of the factory or mixin under test.
107
self.factory = xmlstream.BootstrapMixin()
110
def test_installBootstraps(self):
112
Dispatching an event should fire registered bootstrap observers.
119
dispatcher = DummyProtocol()
120
self.factory.addBootstrap('//event/myevent', cb)
121
self.factory.installBootstraps(dispatcher)
123
dispatcher.dispatch(None, '//event/myevent')
124
self.assertEquals(1, len(called))
127
def test_addAndRemoveBootstrap(self):
129
Test addition and removal of a bootstrap event handler.
137
self.factory.addBootstrap('//event/myevent', cb)
138
self.factory.removeBootstrap('//event/myevent', cb)
140
dispatcher = DummyProtocol()
141
self.factory.installBootstraps(dispatcher)
143
dispatcher.dispatch(None, '//event/myevent')
144
self.assertFalse(called)
148
class GenericXmlStreamFactoryTestsMixin(BootstrapMixinTest):
150
Generic tests for L{XmlStream} factories.
154
self.factory = xmlstream.XmlStreamFactory()
157
def test_buildProtocolInstallsBootstraps(self):
159
The protocol factory installs bootstrap event handlers on the protocol.
166
self.factory.addBootstrap('//event/myevent', cb)
168
xs = self.factory.buildProtocol(None)
169
xs.dispatch(None, '//event/myevent')
171
self.assertEquals(1, len(called))
174
def test_buildProtocolStoresFactory(self):
176
The protocol factory is saved in the protocol.
178
xs = self.factory.buildProtocol(None)
179
self.assertIdentical(self.factory, xs.factory)
183
class XmlStreamFactoryMixinTest(GenericXmlStreamFactoryTestsMixin):
185
Tests for L{xmlstream.XmlStreamFactoryMixin}.
189
self.factory = xmlstream.XmlStreamFactoryMixin(None, test=None)
190
self.factory.protocol = DummyProtocol
193
def test_buildProtocolFactoryArguments(self):
195
Arguments passed to the factory should be passed to protocol on
198
xs = self.factory.buildProtocol(None)
200
self.assertEquals((None,), xs.args)
201
self.assertEquals({'test': None}, xs.kwargs)