Index: main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_logger.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_logger.py	(revision 6371)
+++ main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_logger.py	(revision 6371)
@@ -0,0 +1,207 @@
+##
+## test_logger.py
+## Login : <uli@pu.smp.net>
+## Started on  Mon Jun 13 14:40:04 2011 Uli Fouquet
+## $Id$
+## 
+## Copyright (C) 2011 Uli Fouquet
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+## 
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+## 
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##
+import logging
+import os
+import re
+import shutil
+import tempfile
+from zope.app.testing.functional import FunctionalTestCase
+from zope.component.hooks import setSite, clearSite
+from zope.interface.verify import verifyClass, verifyObject
+from waeup.sirp.app import University
+from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module
+from waeup.sirp.utils.logger import (
+    ISiteLoggers, ILogger, SiteLoggers, WAeUPLogger)
+
+class FakeDatacenter(object):
+    def __init__(self):
+        self.storage = None
+
+class FakeSite(dict):
+    def __init__(self):
+        super(FakeSite, self).__init__()
+        self.__name__ = None
+        self['datacenter'] = FakeDatacenter()
+
+
+class SiteLoggersTests(FunctionalTestCase):
+
+    layer = FunctionalLayer
+
+    def setUp(self):
+        super(SiteLoggersTests, self).setUp()
+
+        # Setup a sample site for each test
+        app = University()
+        self.dc_root = tempfile.mkdtemp()
+        app['datacenter'].setStoragePath(self.dc_root)
+
+        # Prepopulate the ZODB...
+        self.getRootFolder()['app'] = app
+        self.app = self.getRootFolder()['app']
+
+        self.workdir = tempfile.mkdtemp()
+        return
+
+    def tearDown(self):
+        super(SiteLoggersTests, self).tearDown()
+        shutil.rmtree(self.workdir)
+        shutil.rmtree(self.dc_root)
+        clearSite()
+        return
+
+    def test_iface(self):
+        loggers = SiteLoggers()
+        verifyClass(ISiteLoggers, SiteLoggers)
+        verifyObject(ISiteLoggers, loggers)
+        return
+
+    def test_loggers(self):
+        loggers = SiteLoggers()
+        loggers.register('sample1')
+        loggers.register('sample2')
+        result = loggers.loggers
+        assert len(result) == 2
+
+    def test_register(self):
+        loggers = SiteLoggers()
+        loggers.register('sample')
+        assert 'sample' in loggers._loggers.keys()
+
+    def test_register_duplicate(self):
+        loggers = SiteLoggers()
+        loggers.register('sample')
+        self.assertRaises(
+            KeyError,
+            loggers.register, 'sample')
+
+    def test_unregister(self):
+        loggers = SiteLoggers()
+        loggers.register('sample')
+        loggers.unregister('sample')
+        self.assertEqual(loggers._loggers, {})
+
+class LoggerTests(FunctionalTestCase):
+
+    layer = FunctionalLayer
+
+    def setUp(self):
+        super(LoggerTests, self).setUp()
+
+        # Setup a sample site for each test
+        app = University()
+        self.dc_root = tempfile.mkdtemp()
+        app['datacenter'].setStoragePath(self.dc_root)
+
+        # Prepopulate the ZODB...
+        self.getRootFolder()['app'] = app
+        self.app = self.getRootFolder()['app']
+
+        self.workdir = tempfile.mkdtemp()
+        return
+
+    def tearDown(self):
+        super(LoggerTests, self).tearDown()
+        shutil.rmtree(self.workdir)
+        shutil.rmtree(self.dc_root)
+        clearSite()
+        return
+
+    def test_iface(self):
+        setSite(self.app)
+        logger = WAeUPLogger('sample')
+        verifyClass(ILogger, WAeUPLogger)
+        verifyObject(ILogger, logger)
+        return
+
+    def test_logger_attr(self):
+        setSite(self.app)
+        logger = WAeUPLogger('sample')
+        raw_logger = logger.logger
+        assert isinstance(raw_logger, logging.Logger)
+
+    def test_site_name(self):
+        setSite(self.app)
+        logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app)
+        self.assertEqual(logger.logger.name, 'waeup.sirp.app.sample')
+
+    def test_site_no_name(self):
+        # While a site has no name, we get __app as placeholder
+        setSite(self.app)
+        self.app.__name__ = None
+        logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app)
+        self.assertEqual(logger.logger.name, 'waeup.sirp.__app.sample')
+
+    def test_site_changing_name(self):
+        # Changing the sitename (from None to a value) will be reflected
+        setSite(self.app)
+        self.app.__name__ = None
+        logger = WAeUPLogger('waeup.sirp.%s.sample', site=self.app)
+        self.assertEqual(logger.logger.name, 'waeup.sirp.__app.sample')
+        self.app.__name__ = 'myapp'
+        self.assertEqual(logger.logger.name, 'waeup.sirp.myapp.sample')
+        assert len(logger.logger.handlers) > 0
+
+    def test_loggername_expansion(self):
+        setSite(self.app)
+        logger = WAeUPLogger('sample', site=self.app)
+        logger.logger # Trigger setup
+        self.assertEqual(logger._loggername, 'waeup.sirp.app.sample')
+
+    def test_filename(self):
+        setSite(self.app)
+        logger = WAeUPLogger('sample')
+        logger.logger # Trigger setup
+        self.assertEqual(logger.filename, 'sample.log')
+
+    def test_filename_from_dotted_name(self):
+        setSite(self.app)
+        logger = WAeUPLogger('waeup.sirp.%s.sample')
+        logger.logger # Trigger setup
+        logfile = os.path.join(self.dc_root, 'logs', 'sample.log')
+        self.assertEqual(logger.filename, 'sample.log')
+        assert os.path.isfile(logfile)
+
+    def test_option_filename(self):
+        setSite(self.app)
+        logger = WAeUPLogger('sample', filename='my.log')
+        logger.logger # Trigger setup
+        logfile = os.path.join(self.dc_root, 'logs', 'my.log')
+        self.assertEqual(logger.filename, 'my.log')
+        assert os.path.isfile(logfile)
+
+    def test_option_propagate(self):
+        logger = WAeUPLogger('sample', site=self.app)
+        assert logger.logger.propagate is False  # default
+        logger = WAeUPLogger('sample', site=self.app, propagate=True)
+        assert logger.logger.propagate is True
+        logger = WAeUPLogger('sample', site=self.app, propagate=False)
+        assert logger.logger.propagate is False
+
+    def test_logging(self):
+        logger = WAeUPLogger('sample', site=self.app)
+        logger.logger.info('Hi there!')
+        logfile = os.path.join(self.dc_root, 'logs', 'sample.log')
+        assert os.path.isfile(logfile)
+        contents = open(logfile, 'rb').read()
+        self.assertTrue(re.match(
+                '^....-..-.. ..:..:..,... - INFO - Hi there!\n$', contents))
