# Path: ejtp/util/compat.py

# 
# Path: ejtp/tests/tools.py
# class TestCaseWithLog(unittest.TestCase):
# 
#     def setUp(self):
#         from ejtp.util.compat import StringIO
#         self._stream = StringIO()
# 
#     def listen(self, logger):
#         import logging
#         handler = logging.StreamHandler(self._stream)
#         formatter = logging.Formatter('%(levelname)s:%(name)s: %(message)s')
#         handler.setFormatter(formatter)
#         logger.setLevel(logging.INFO)
#         logger.addHandler(handler)
# 
#     def assertInLog(self, expected):
#         value = self._stream.getvalue()
#         try:
#             self.assertIn(expected, value)
#         except AssertionError:
#             self.assertIn(expected, value.replace("u'", "'"))
# 
# Path: ejtp/router.py
# STOPPED = 0
# THREADED = 1
# class Router(object):
#     def __init__(self, jacks=[], clients=[]):
#     def recv(self, msg):
#     def jack(self, addr):
#     def client(self, addr):
#     def kill_client(self, addr):
#     def thread_all(self):
#     def stop_all(self):
#     def run(self, level=THREADED):
#     def _loadjacks(self, jacks):
#     def _loadclients(self, clients):
#     def _loadjack(self, jack):
#     def _loadclient(self, client):
# def rtuple(obj):


from ejtp.util.compat import unittest
from ejtp.tests.tools import TestCaseWithLog
from ejtp import router
        from ejtp.jacks import Jack
        from ejtp.client import Client

    def test_jack_already_loaded(self):
        class DummyJack(Jack):
            def run(self, *args):
                pass

        jack = DummyJack(self.router, (1, 2, 3))
        self.assertRaisesRegexp(ValueError,
            'jack already loaded', self.router._loadjack, jack)

    def test_client_already_loaded(self):
        client = Client(None, (4, 5, 6), make_jack = False)
        self.router._loadclient(client)
        self.assertRaisesRegexp(ValueError,
            'client already loaded', self.router._loadclient, client)


class TestRouterStream(TestCaseWithLog):

    def setUp(self):
        TestCaseWithLog.setUp(self)
        self.router = router.Router()
        self.listen(router.logger)

    def _test_message(self, expected, message='Jam and cookies', destination='["local",null,"example"]', format=None):
        data = ''.join([format, destination, '\x00', message])
        self.router.recv(data)
        self.assertInLog(expected)

    def test_recv_invalid_message(self):

# Path: ejtp/util/compat.py

# 
# Path: ejtp/tests/tools.py
# class TestCaseWithLog(unittest.TestCase):
# 
#     def setUp(self):
#         from ejtp.util.compat import StringIO
#         self._stream = StringIO()
# 
#     def listen(self, logger):
#         import logging
#         handler = logging.StreamHandler(self._stream)
#         formatter = logging.Formatter('%(levelname)s:%(name)s: %(message)s')
#         handler.setFormatter(formatter)
#         logger.setLevel(logging.INFO)
#         logger.addHandler(handler)
# 
#     def assertInLog(self, expected):
#         value = self._stream.getvalue()
#         try:
#             self.assertIn(expected, value)
#         except AssertionError:
#             self.assertIn(expected, value.replace("u'", "'"))
# 
# Path: ejtp/router.py
# STOPPED = 0
# THREADED = 1
# class Router(object):
#     def __init__(self, jacks=[], clients=[]):
#     def recv(self, msg):
#     def jack(self, addr):
#     def client(self, addr):
#     def kill_client(self, addr):
#     def thread_all(self):
#     def stop_all(self):
#     def run(self, level=THREADED):
#     def _loadjacks(self, jacks):
#     def _loadclients(self, clients):
#     def _loadjack(self, jack):
#     def _loadclient(self, client):
# def rtuple(obj):


# Path: ejtp/tests/test_router.py
from ejtp.util.compat import unittest
from ejtp.tests.tools import TestCaseWithLog
from ejtp import router
        from ejtp.jacks import Jack
        from ejtp.client import Client


    def test_jack_already_loaded(self):
        class DummyJack(Jack):
            def run(self, *args):
                pass

        jack = DummyJack(self.router, (1, 2, 3))
        self.assertRaisesRegexp(ValueError,
            'jack already loaded', self.router._loadjack, jack)

    def test_client_already_loaded(self):
        client = Client(None, (4, 5, 6), make_jack = False)
        self.router._loadclient(client)
        self.assertRaisesRegexp(ValueError,
            'client already loaded', self.router._loadclient, client)


class TestRouterStream(TestCaseWithLog):

    def setUp(self):
        TestCaseWithLog.setUp(self)
        self.router = router.Router()
        self.listen(router.logger)

    def _test_message(self, expected, message='Jam and cookies', destination='["local",null,"example"]', format=None):
        data = ''.join([format, destination, '\x00', message])
        self.router.recv(data)
        self.assertInLog(expected)

    def test_recv_invalid_message(self):

        self.router.recv('qwerty')