Changeset 562

Show
Ignore:
Timestamp:
04/19/07 17:19:35 (2 years ago)
Author:
mikey
Message:

refactored net.stubbles.util.log.stubLoggerXJConfFactory, added unit test
fixed bug in net.stubbles.util.log.stubLogger: instance list still contained the id of an already destroyed logger instance
make sure that every logger instance created during a test case will be destroyed at the end of a test case

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/log/stubFileLogAppender.php

    r400 r562  
    2727     */ 
    2828    protected $logDir = ''; 
    29      
     29 
    3030    /** 
    3131     * constructor 
    3232     * 
    33      * @param  string  $logDir  the directory to write the logfiles into 
     33     * @param  string  $logDir  optional  the directory to write the logfiles into 
    3434     */ 
    35     public function __construct($logDir
     35    public function __construct($logDir = null
    3636    { 
     37        if (null == $logDir) { 
     38            $logDir = stubConfig::getLogPath(); 
     39        } 
     40         
    3741        $this->setLogDir($logDir); 
    3842    } 
    39      
     43 
     44    /** 
     45     * sets the configuration data 
     46     * 
     47     * @param  array  $config 
     48     */ 
     49    public function setConfig(array $config) 
     50    { 
     51        // intentionally empty 
     52    } 
     53 
     54    /** 
     55     * returns the configuration 
     56     * 
     57     * @return  array 
     58     */ 
     59    public function getConfig() 
     60    { 
     61        return array(); 
     62    } 
     63 
    4064    /** 
    4165     * set the logpath 
     
    5175        $this->logDir = $logDir; 
    5276    } 
    53      
     77 
    5478    /** 
    5579     * returns the logpath 
     
    6185        return $this->logDir; 
    6286    } 
    63      
     87 
    6488    /** 
    6589     * builds the log directory 
     
    7195        return str_replace('{Y}', date('Y'), str_replace('{M}', date('m'), $this->logDir)); 
    7296    } 
    73      
     97 
    7498    /** 
    7599     * append the log data to the log target 
     
    89113        error_log($logData->get() . "\n", 3, $logDir . '/' . $logData->getTarget() . '-' . date('Y-m-d') . '.log'); 
    90114    } 
    91      
     115 
    92116    /** 
    93117     * finalize the log target 
  • trunk/src/main/php/net/stubbles/util/log/stubLogAppender.php

    r373 r562  
    1717 * @subpackage  util_log 
    1818 */ 
    19 interface stubLogAppender 
     19interface stubLogAppender extends stubObject 
    2020{ 
     21    /** 
     22     * sets the configuration data 
     23     * 
     24     * @param  array  $config 
     25     */ 
     26    public function setConfig(array $config); 
     27 
     28    /** 
     29     * returns the configuration 
     30     * 
     31     * @return  array 
     32     */ 
     33    public function getConfig(); 
     34 
    2135    /** 
    2236     * append the log data to the log target 
     
    2539     */ 
    2640    public function append(stubLogData $logData); 
    27      
     41 
    2842    /** 
    2943     * finalize the log target 
  • trunk/src/main/php/net/stubbles/util/log/stubLogData.php

    r146 r562  
    1515 * @subpackage  util_log 
    1616 */ 
    17 interface stubLogData 
     17interface stubLogData extends stubObject 
    1818{ 
    1919    /** 
     
    2121     */ 
    2222    const SEPERATOR = '|'; 
    23      
     23 
    2424    /** 
    2525     * adds data to the log object 
     
    3030     */ 
    3131    public function addData($data); 
    32      
     32 
    3333    /** 
    3434     * returns the whole log data as one line 
     
    3737     */ 
    3838    public function get(); 
    39      
     39 
    4040    /** 
    4141     * returns the level of the log data 
     
    4545     */ 
    4646    public function getLevel(); 
    47      
     47 
    4848    /** 
    4949     * returns the target where the log data should go to 
  • trunk/src/main/php/net/stubbles/util/log/stubLogPreInterceptor.php

    r473 r562  
    2525    public function preProcess(stubRequest $request, stubSession $session, stubResponse $response) 
    2626    { 
    27         stubLoggerXJConfFactory::init(stubConfig::getConfigPath() . '/xml/logging.xml'); 
     27        $loggerXJConFactory = new stubLoggerXJConfFactory(); 
     28        $loggerXJConFactory->init(); 
    2829    } 
    2930} 
  • trunk/src/main/php/net/stubbles/util/log/stubLogger.php

    r400 r562  
    7878     */ 
    7979    protected $logAppender      = array(); 
    80      
     80 
    8181    /** 
    8282     * constructor 
     
    9292        $this->level = $level; 
    9393    } 
    94      
     94 
    9595    /** 
    9696     * destructor 
     
    104104        } 
    105105    } 
    106      
     106 
    107107    /** 
    108108     * returns a logger instance 
     
    125125        return self::$instances[$id]; 
    126126    } 
    127      
     127 
    128128    /** 
    129129     * returns a list of existing instances 
     
    135135        return array_keys(self::$instances); 
    136136    } 
    137      
     137 
    138138    /** 
    139139     * destroys the logger with the given id 
     
    145145        if (isset(self::$instances[$id]) == true) { 
    146146            self::$instances[$id] = null; 
    147         } 
    148     } 
    149      
     147            unset(self::$instances[$id]); 
     148        } 
     149    } 
     150 
    150151    /** 
    151152     * clone is not supported 
     
    155156        throw new stubLoggerException('Cloning a logger is not allowed.'); 
    156157    } 
    157      
     158 
    158159    /** 
    159160     * returns the id of the logger 
     
    165166        return $this->id; 
    166167    } 
    167      
     168 
    168169    /** 
    169170     * returns the level where the logger is applicable for 
     
    175176        return $this->level; 
    176177    } 
    177      
     178 
    178179    /** 
    179180     * checks whether the logger is applicable for the given level 
     
    190191        return (($this->level & $level) != 0); 
    191192    } 
    192      
     193 
    193194    /** 
    194195     * adds a log appender to the logger 
     
    202203        $this->logAppender[] = $logAppender; 
    203204    } 
    204      
     205 
    205206    /** 
    206207     * returns a list of log appenders appended to the logger 
     
    212213        return $this->logAppender; 
    213214    } 
    214      
     215 
    215216    /** 
    216217     * log data to all loggers created 
     
    228229        } 
    229230    } 
    230      
     231 
    231232    /** 
    232233     * sends log data to all registered log appenders 
  • trunk/src/main/php/net/stubbles/util/log/stubLoggerXJConfFactory.php

    r443 r562  
    77 * @subpackage  util_log 
    88 */ 
    9 stubClassLoader::load('net.stubbles.util.stubFactory', 
     9stubClassLoader::load('net.stubbles.util.log.stubLogger', 
    1010                      'net.stubbles.util.xjconf.xjconf' 
    1111); 
     
    1818 * @uses        http://php.xjconf.net/ 
    1919 */ 
    20 class stubLoggerXJConfFactory 
     20class stubLoggerXJConfFactory extends stubBaseObject implements stubXJConfInitializer 
    2121{ 
    2222    /** 
    23      * initialize the stubLogger with configuration values from given configuration file 
     23     * returns the descriptor that identifies this initializer 
    2424     * 
    25      * @param   string  $configFile 
     25     * @return  string 
     26     */ 
     27    public function getDescriptor() 
     28    { 
     29        return 'logging'; 
     30    } 
     31 
     32    /** 
     33     * returns the data to cache  
     34     * 
     35     * @return  array 
     36     */ 
     37    public function getCacheData() 
     38    { 
     39        $cacheData = array(); 
     40        $loggerIds = stubLogger::getInstanceList(); 
     41        foreach ($loggerIds as $loggerId) { 
     42            $logger = stubLogger::getInstance($loggerId); 
     43            $cacheData[$loggerId] = array('level'    => $logger->getLevel(), 
     44                                          'appender' => array() 
     45                                    ); 
     46            foreach ($logger->getLogAppenders() as $logAppender) { 
     47                $cacheData[$loggerId]['appender'][$logAppender->getClassname()] = $logAppender->getConfig(); 
     48            } 
     49        } 
     50         
     51        return $cacheData; 
     52    } 
     53 
     54    /** 
     55     * sets the data from the cache 
     56     * 
     57     * @param  array  $cacheData 
     58     */ 
     59    public function setCacheData(array $cacheData) 
     60    { 
     61        foreach ($cacheData as $loggerId => $loggerConfig) { 
     62            $logger = stubLogger::getInstance($loggerId, $loggerConfig['level']); 
     63            foreach ($loggerConfig['appender'] as $logAppenderClassName => $logAppenderConfig) { 
     64                $nqClassName = stubClassLoader::getNonQualifiedClassName($logAppenderClassName); 
     65                if (class_exists($nqClassName, false) == false) { 
     66                    stubClassLoader::load($logAppenderClassName); 
     67                } 
     68                 
     69                $logAppender = new $nqClassName(); 
     70                $logAppender->setConfig($logAppenderConfig); 
     71                $logger->addLogAppender($logAppender); 
     72            } 
     73        } 
     74    } 
     75 
     76    /** 
     77     * will be called in case the stubXJConfProxy did not found the data in the 
     78     * cache and the initializer has to load values from the facade 
     79     * 
     80     * @param  stubXJConfFacade  $xjconf 
     81     */ 
     82    public function loadData(stubXJConfFacade $xjconf) 
     83    { 
     84        // intentionally empty, all stubLogger instances are inside a static member of stubLogger 
     85    } 
     86 
     87    /** 
     88     * initialize the stubLogger 
     89     * 
    2690     * @throws  stubXJConfException 
    2791     */ 
    28     public static function init($configFile
     92    public function init(
    2993    { 
    30         $xjconf = new stubXJConfFacade(array('__default' => stubXJConfLoader::getInstance())); 
    31         $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/logging.xml')); 
    32         $configExtension = new stubConfigXJConfExtension(); 
    33         $xjconf->addExtension($configExtension); 
    34         $xjconf->enableXIncludes(); 
    35         $xjconf->parse($configFile); 
     94        $xjconfProxy = new stubXJConfProxy($this); 
     95        $xjconfProxy->process(array(new stubConfigXJConfExtension())); 
    3696    } 
    3797} 
  • trunk/src/main/php/net/stubbles/util/xjconf/stubXJConfProxy.php

    r538 r562  
    6262     * initialize with configuration values from given configuration file 
    6363     * 
    64      * @return  bool  true if data was cached, else false 
     64     * @param   array<Extension>     a list of XJConf extensions 
     65     * @return  bool                 true if data was cached, else false 
    6566     * @throws  stubXJConfException 
    6667     */ 
    67     public function process(
     68    public function process(array $extensions = array()
    6869    { 
    6970        if (file_exists($this->cacheFile) == true && filemtime($this->cacheFile) >= filemtime($this->configFile)) { 
     
    7576        $xjconf->parseAndAddDefinitions(stubFactory::getResourceURIs('xjconf/' . $this->initializer->getDescriptor() . '.xml')); 
    7677        $xjconf->enableXIncludes(); 
     78        foreach ($extensions as $extension) { 
     79            $xjconf->addExtension($extension); 
     80        } 
     81         
    7782        $xjconf->parse($this->configFile); 
    7883        $this->initializer->loadData($xjconf); 
  • trunk/src/test/php/net/stubbles/integration/LoggerTestCase.php

    r400 r562  
    1717{ 
    1818    /** 
     19     * clean up test environment 
     20     */ 
     21    public function tearDown() 
     22    { 
     23        $loggerIds = stubLogger::getInstanceList(); 
     24        foreach ($loggerIds as $loggerId) { 
     25            stubLogger::destroyInstance($loggerId); 
     26        } 
     27    } 
     28 
     29    /** 
    1930     * assure that creating the logger instances works correct 
    2031     */ 
    2132    public function testLoggerXJConfFactory() 
    2233    { 
    23         stubLoggerXJConfFactory::init(stubConfig::getConfigPath() . '/xml/logging.xml'); 
     34        $loggerXJConfFactory = new stubLoggerXJConfFactory(); 
     35        $loggerXJConfFactory->init(); 
    2436        $this->assertEqual(stubLogger::getInstanceList(), array(stubLogger::DEFAULT_ID)); 
    2537        $logger = stubLogger::getInstance(); 
  • trunk/src/test/php/net/stubbles/util/UtilTestSuite.php

    r538 r562  
    3939        $this->addTestFile($dir . '/log/stubBaseLogDataTestCase.php'); 
    4040        $this->addTestFile($dir . '/log/stubLogDataFactoryTestCase.php'); 
     41        $this->addTestFile($dir . '/log/stubLoggerXJConfFactoryTestCase.php'); 
    4142 
    4243        // now all the validators 
  • trunk/src/test/php/net/stubbles/util/errorhandler/stubLogErrorHandlerTestCase.php

    r422 r562  
    99stubClassLoader::load('net.stubbles.util.errorhandler.stubLogErrorHandler'); 
    1010Mock::generate('stubSession'); 
    11 class stubTestLogAppender implements stubLogAppender 
     11class stubTestLogAppender extends stubBaseObject implements stubLogAppender 
    1212{ 
    1313    protected $logData = array(); 
    14      
     14 
     15    public function setConfig(array $config) { } 
     16 
     17    public function getConfig() 
     18    { 
     19        return array(); 
     20    } 
     21 
    1522    public function append(stubLogData $logData) 
    1623    { 
    1724        $this->logData[] = $logData; 
    1825    } 
    19      
     26 
    2027    public function getLogData() 
    2128    { 
    2229        return $this->logData; 
    2330    } 
    24      
     31 
    2532    public function finalize() {} 
    2633} 
     
    3946     */ 
    4047    protected $logErrorHandler; 
    41      
     48 
    4249    /** 
    4350     * set up test environment 
     
    4855        stubRegistry::setConfig('net.stubbles.util.log.class', 'net.stubbles.util.log.stubBaseLogData'); 
    4956    } 
    50      
     57 
     58    /** 
     59     * clean up test environment 
     60     */ 
     61    public function tearDown() 
     62    { 
     63        $loggerIds = stubLogger::getInstanceList(); 
     64        foreach ($loggerIds as $loggerId) { 
     65            stubLogger::destroyInstance($loggerId); 
     66        } 
     67    } 
     68 
    5169    /** 
    5270     * test that event listener is removed if it has a session 
     
    5977        $this->assertTrue($this->logErrorHandler->autoremove()); 
    6078    } 
    61      
     79 
    6280    /** 
    6381     * assure that isResponsible() works correct 
     
    6785        $this->assertTrue($this->logErrorHandler->isResponsible(E_NOTICE, 'foo')); 
    6886    } 
    69      
     87 
    7088    /** 
    7189     * assure that isSupressable() works correct 
     
    7593        $this->assertFalse($this->logErrorHandler->isSupressable(E_NOTICE, 'foo')); 
    7694    } 
    77      
     95 
    7896    /** 
    7997     * assure that the error is put onto a stack of the session is set 
     
    91109        ); 
    92110    } 
    93      
     111 
    94112    /** 
    95113     * assure that the error is logged if the session is set 
     
    118136        stubLogger::destroyInstance(__CLASS__); 
    119137    } 
    120      
     138 
    121139    /** 
    122140     * assure that the error is logged after the session is set 
  • trunk/src/test/php/net/stubbles/util/log/stubLoggerTestCase.php

    r146 r562  
    2424     */ 
    2525    protected $stubLogger; 
    26      
     26 
    2727    /** 
    2828     * set up the test environment 
     
    3232        $this->stubLogger = stubLogger::getInstance(); 
    3333    } 
    34      
     34 
    3535    /** 
    3636     * clean up test environment 
     
    3838    public function tearDown() 
    3939    { 
    40         stubLogger::destroyInstance(stubLogger::DEFAULT_ID); 
    41     } 
    42      
     40        $loggerIds = stubLogger::getInstanceList(); 
     41        foreach ($loggerIds as $loggerId) { 
     42            stubLogger::destroyInstance($loggerId); 
     43        } 
     44    } 
     45 
    4346    /** 
    4447     * assure that creation of loggers works as expected 
     
    5154        $this->assertReference($this->stubLogger, $stubLogger); 
    5255    } 
    53      
     56 
    5457    /** 
    5558     * assert that level handling is coorect 
     
    164167        stubLogger::destroyInstance('warnerror'); 
    165168    } 
    166      
     169 
    167170    /** 
    168171     * test the instance logging method 
     
    179182        $this->stubLogger->log($mockLogData); 
    180183    } 
    181      
     184 
    182185    /** 
    183186     * test the static logging method 
     
    207210        stubLogger::destroyInstance('warn'); 
    208211    } 
     212 
     213    /** 
     214     * assure that instances are really destroyed 
     215     */ 
     216    public function testDestroy() 
     217    { 
     218        $loggerTest = stubLogger::getInstance('test'); 
     219        stubLogger::destroyInstance('test'); 
     220        $this->assertFalse(in_array('test', stubLogger::getInstanceList())); 
     221    } 
    209222} 
    210223?>