# Path: wokkel/component.py
# NS_COMPONENT_ACCEPT = 'jabber:component:accept'
# class Component(StreamManager, service.Service):
# class InternalComponent(xmlstream.XMPPHandlerCollection, service.Service):
# class ListenComponentAuthenticator(xmlstream.ListenAuthenticator):
# class Router(object):
# class XMPPComponentServerFactory(xmlstream.XmlStreamServerFactory):
#     def __init__(self, host, port, jid, password):
#     def _authd(self, xs):
#         def send(obj):
#     def initializationFailed(self, reason):
#     def startService(self):
#     def stopService(self):
#     def _getConnection(self):
#     def __init__(self, router, domain=None):
#     def startService(self):
#     def stopService(self):
#     def addHandler(self, handler):
#     def send(self, obj):
#     def __init__(self, secret):
#     def associateWithStream(self, xs):
#     def streamStarted(self, rootElement):
#     def onElement(self, element):
#     def onHandshake(self, handshake):
#     def __init__(self):
#     def addRoute(self, destination, xs):
#     def removeRoute(self, destination, xs):
#     def route(self, stanza):
#     def __init__(self, router, secret='secret'):
#         def authenticatorFactory():
#     def makeConnection(self, xs):
#         def logDataIn(buf):
#         def logDataOut(buf):
#     def connectionInitialized(self, xs):
#     def onError(self, reason):
#     def connectionLost(self, destination, xs, reason):
# 
# Path: wokkel/generic.py
# class XmlPipe(object):
#     """
#     XML stream pipe.
# 
#     Connects two objects that communicate stanzas through an XML stream like
#     interface. Each of the ends of the pipe (sink and source) can be used to
#     send XML stanzas to the other side, or add observers to process XML stanzas
#     that were sent from the other side.
# 
#     XML pipes are usually used in place of regular XML streams that are
#     transported over TCP. This is the reason for the use of the names source
#     and sink for both ends of the pipe. The source side corresponds with the
#     entity that initiated the TCP connection, whereas the sink corresponds with
#     the entity that accepts that connection. In this object, though, the source
#     and sink are treated equally.
# 
#     Unlike Jabber
#     L{XmlStream<twisted.words.protocols.jabber.xmlstream.XmlStream>}s, the sink
#     and source objects are assumed to represent an eternal connected and
#     initialized XML stream. As such, events corresponding to connection,
#     disconnection, initialization and stream errors are not dispatched or
#     processed.
# 
#     @ivar source: Source XML stream.
#     @ivar sink: Sink XML stream.
#     """
# 
#     def __init__(self):
#         self.source = utility.EventDispatcher()
#         self.sink = utility.EventDispatcher()
#         self.source.send = lambda obj: self.sink.dispatch(obj)
#         self.sink.send = lambda obj: self.source.dispatch(obj)


from zope.interface.verify import verifyObject
from twisted.internet.base import BaseConnector
from twisted.internet.error import ConnectionRefusedError
from twisted.internet.task import Clock
from twisted.python import failure
from twisted.trial import unittest
from twisted.words.protocols.jabber import xmlstream
from twisted.words.protocols.jabber.ijabber import IXMPPHandlerCollection
from twisted.words.protocols.jabber.jid import JID
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
from twisted.words.xish import domish
from wokkel import component
from wokkel.generic import XmlPipe



class RouterTest(unittest.TestCase):
    """
    Tests for L{component.Router}.
    """

    def test_addRoute(self):
        """
        Test route registration and routing on incoming stanzas.
        """
        router = component.Router()
        routed = []
        router.route = lambda element: routed.append(element)

        pipe = XmlPipe()
        router.addRoute('example.org', pipe.sink)
        self.assertEquals(1, len(router.routes))
        self.assertEquals(pipe.sink, router.routes['example.org'])

        element = domish.Element(('testns', 'test'))
        pipe.source.send(element)
        self.assertEquals([element], routed)


    def test_route(self):
        """
        Test routing of a message.
        """

# Path: wokkel/component.py
# NS_COMPONENT_ACCEPT = 'jabber:component:accept'
# class Component(StreamManager, service.Service):
# class InternalComponent(xmlstream.XMPPHandlerCollection, service.Service):
# class ListenComponentAuthenticator(xmlstream.ListenAuthenticator):
# class Router(object):
# class XMPPComponentServerFactory(xmlstream.XmlStreamServerFactory):
#     def __init__(self, host, port, jid, password):
#     def _authd(self, xs):
#         def send(obj):
#     def initializationFailed(self, reason):
#     def startService(self):
#     def stopService(self):
#     def _getConnection(self):
#     def __init__(self, router, domain=None):
#     def startService(self):
#     def stopService(self):
#     def addHandler(self, handler):
#     def send(self, obj):
#     def __init__(self, secret):
#     def associateWithStream(self, xs):
#     def streamStarted(self, rootElement):
#     def onElement(self, element):
#     def onHandshake(self, handshake):
#     def __init__(self):
#     def addRoute(self, destination, xs):
#     def removeRoute(self, destination, xs):
#     def route(self, stanza):
#     def __init__(self, router, secret='secret'):
#         def authenticatorFactory():
#     def makeConnection(self, xs):
#         def logDataIn(buf):
#         def logDataOut(buf):
#     def connectionInitialized(self, xs):
#     def onError(self, reason):
#     def connectionLost(self, destination, xs, reason):
# 
# Path: wokkel/generic.py
# class XmlPipe(object):
#     """
#     XML stream pipe.
# 
#     Connects two objects that communicate stanzas through an XML stream like
#     interface. Each of the ends of the pipe (sink and source) can be used to
#     send XML stanzas to the other side, or add observers to process XML stanzas
#     that were sent from the other side.
# 
#     XML pipes are usually used in place of regular XML streams that are
#     transported over TCP. This is the reason for the use of the names source
#     and sink for both ends of the pipe. The source side corresponds with the
#     entity that initiated the TCP connection, whereas the sink corresponds with
#     the entity that accepts that connection. In this object, though, the source
#     and sink are treated equally.
# 
#     Unlike Jabber
#     L{XmlStream<twisted.words.protocols.jabber.xmlstream.XmlStream>}s, the sink
#     and source objects are assumed to represent an eternal connected and
#     initialized XML stream. As such, events corresponding to connection,
#     disconnection, initialization and stream errors are not dispatched or
#     processed.
# 
#     @ivar source: Source XML stream.
#     @ivar sink: Sink XML stream.
#     """
# 
#     def __init__(self):
#         self.source = utility.EventDispatcher()
#         self.sink = utility.EventDispatcher()
#         self.source.send = lambda obj: self.sink.dispatch(obj)
#         self.sink.send = lambda obj: self.source.dispatch(obj)


# Path: wokkel/test/test_component.py
from zope.interface.verify import verifyObject
from twisted.internet.base import BaseConnector
from twisted.internet.error import ConnectionRefusedError
from twisted.internet.task import Clock
from twisted.python import failure
from twisted.trial import unittest
from twisted.words.protocols.jabber import xmlstream
from twisted.words.protocols.jabber.ijabber import IXMPPHandlerCollection
from twisted.words.protocols.jabber.jid import JID
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
from twisted.words.xish import domish
from wokkel import component
from wokkel.generic import XmlPipe




class RouterTest(unittest.TestCase):
    """
    Tests for L{component.Router}.
    """

    def test_addRoute(self):
        """
        Test route registration and routing on incoming stanzas.
        """
        router = component.Router()
        routed = []
        router.route = lambda element: routed.append(element)

        pipe = XmlPipe()
        router.addRoute('example.org', pipe.sink)
        self.assertEquals(1, len(router.routes))
        self.assertEquals(pipe.sink, router.routes['example.org'])

        element = domish.Element(('testns', 'test'))
        pipe.source.send(element)
        self.assertEquals([element], routed)


    def test_route(self):
        """
        Test routing of a message.
        """

        component1 = XmlPipe()