Author: ihabunek
Date: Sat Sep 18 11:42:17 2010
New Revision: 998444

URL: http://svn.apache.org/viewvc?rev=998444&view=rev
Log:
LOG4PHP-105: LoggerMDC needs refactoring; added tests

Added:
    logging/log4php/trunk/src/test/php/LoggerMDCTest.php
Modified:
    logging/log4php/trunk/src/changes/changes.xml
    logging/log4php/trunk/src/main/php/LoggerMDC.php

Modified: logging/log4php/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=998444&r1=998443&r2=998444&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sat Sep 18 11:42:17 2010
@@ -24,6 +24,7 @@
   </properties>
   <body>
        <release version="2.1" description="Stabilizing">
+               <action type="fix" issue="LOG4PHP-105" by="Ivan 
Habunek">LoggerMDC needs refactoring + tests</action>
                <action type="update" by="Ivan Habunek">Added __toString magic 
method to LoggerLevel.</action>
                <action type="fix" issue="LOG4PHP-117" by="Maciej Mazur, Ivan 
Habunek">LoggerConfiguratorIni::configure() and unexptected results from 
error_get_last()</action>
                <action type="fix" issue="LOG4PHP-113" by="Ivan 
Habunek">Milliseconds do not change when using LoggerLayoutPattern</action>

Modified: logging/log4php/trunk/src/main/php/LoggerMDC.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerMDC.php?rev=998444&r1=998443&r2=998444&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerMDC.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerMDC.php Sat Sep 18 11:42:17 2010
@@ -19,11 +19,6 @@
  */
 
 /**
- * This is the global repository of user mappings
- */
-$GLOBALS['log4php.LoggerMDC.ht'] = array();
-
-/**
  * The LoggerMDC class provides <i>mapped diagnostic contexts</i>.
  * 
  * <p>A <i>Mapped Diagnostic Context</i>, or
@@ -55,6 +50,12 @@ $GLOBALS['log4php.LoggerMDC.ht'] = array
  * @package log4php
  */
 class LoggerMDC {
+       
+       /**
+        * This is the repository of user mappings
+        */
+       private static $map = array();
+               
        /**
         * Put a context value as identified with the key parameter into the 
current thread's
         *      context map.
@@ -69,7 +70,7 @@ class LoggerMDC {
         * @static
         */
        public static function put($key, $value) {
-               $GLOBALS['log4php.LoggerMDC.ht'][$key] = $value;
+               self::$map[$key] = $value;
        }
   
        /**
@@ -81,20 +82,22 @@ class LoggerMDC {
         *
         * <p>This method has no side effects.</p>
         *
-        * @param string $key
-        * @return string
+        * @param string $key the key
+        * @return string the context or an empty string if no context found
+        *      for given key
         * @static
         */
        public static function get($key) {
                if(!empty($key)) {
                        if(strpos($key, 'server.') === 0) {
                                $varName = substr($key, 7);
-                               return @$_SERVER[$varName];
+                               return isset($_SERVER[$varName]) ? 
$_SERVER[$varName] : '';
                        } else if(strpos($key, 'env.') === 0) {
                                $varName = substr($key, 4);
-                               return @$_ENV[$varName];
-                       } else if 
(isset($GLOBALS['log4php.LoggerMDC.ht'][$key])) {
-                               return $GLOBALS['log4php.LoggerMDC.ht'][$key];
+                               $value = getenv($varName);
+                               return ($value !== false) ? $value : '';
+                       } else {
+                               return isset(self::$map[$key]) ? 
self::$map[$key] : '';
                        }
                }
                return '';
@@ -103,14 +106,12 @@ class LoggerMDC {
        /**
         * Remove the the context identified by the key parameter. 
         *
-        * It only affects user mappings.
+        * It only affects user mappings, not $_ENV or $_SERVER.
         *
-        * @param string $key
-        * @return string
+        * @param string $key the key to be removed
         * @static
         */
        public static function remove($key) {
-               unset($GLOBALS['log4php.LoggerMDC.ht'][$key]);
+               unset(self::$map[$key]);
        }
-
 }

Added: logging/log4php/trunk/src/test/php/LoggerMDCTest.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerMDCTest.php?rev=998444&view=auto
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerMDCTest.php (added)
+++ logging/log4php/trunk/src/test/php/LoggerMDCTest.php Sat Sep 18 11:42:17 
2010
@@ -0,0 +1,146 @@
+<?php
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ * @category   tests
+ * @package    log4php
+ * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
Version 2.0
+ * @version    SVN: $Id$
+ * @link       http://logging.apache.org/log4php
+ */
+class LoggerMDCTest extends PHPUnit_Framework_TestCase {
+       
+       /** A pattern with 1 key. */
+       private $pattern1 = "%-5p %c: %X{key1} %m";
+       
+       /** A pattern with 2 keys. */
+       private $pattern2 = "%-5p %c: %X{key1} %X{key2} %m";
+       
+       /** A pattern with 3 keys (one is numeric). */
+       private $pattern3 = "%-5p %c: %X{key1} %X{key2} %X{3} %m";
+       
+       /** A pattern with a non-existant key. */
+       private $pattern4 = "%-5p %c: %X{key_does_not_exist} %m";
+       
+       /** A pattern with an empty key. */
+       private $pattern5 = "%-5p %c: %X{} %m";
+       
+       /** A pattern for testing values from $_ENV. */
+       private $patternEnv = "%-5p %c: %X{env.TEST} %m";
+       
+       /**
+        * A pattern for testing values from $_SERVER. PHP_SELF chosen because 
it
+        * appears on both Linux and Windows systems. 
+        */
+       private $patternServer = "%-5p %c: %X{server.PHP_SELF} %m";
+       
+
+       public function testPatterns() {
+
+               // Create some data to test with
+               LoggerMDC::put('key1', 'valueofkey1');
+               LoggerMDC::put('key2', 'valueofkey2');
+               LoggerMDC::put(3, 'valueofkey3');
+               
+               $event = new LoggerLoggingEvent("LoggerLayoutPattern", new 
Logger("TEST"), LoggerLevel::getLevelInfo(), "Test message");
+
+               // Pattern with 1 key
+               $actual = $this->formatEvent($event, $this->pattern1);
+               $expected = "INFO  TEST: valueofkey1 Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Pattern with 2 keys
+               $actual = $this->formatEvent($event, $this->pattern2);
+               $expected = "INFO  TEST: valueofkey1 valueofkey2 Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Pattern with 3 keys (one numeric)
+               $actual = $this->formatEvent($event, $this->pattern3);
+               $expected = "INFO  TEST: valueofkey1 valueofkey2 valueofkey3 
Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Pattern with non-existant key
+               $actual = $this->formatEvent($event, $this->pattern4);
+               $expected = "INFO  TEST:  Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Pattern with an empty key
+       $actual = $this->formatEvent($event, $this->pattern5);
+               $expected = "INFO  TEST:  Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Test key removal
+               LoggerMDC::remove('key1');
+               $value = LoggerMDC::get('key1');
+               self::assertEquals('', $value);
+               
+               // Pattern with 1 key, now removed
+               $actual = $this->formatEvent($event, $this->pattern1);
+               $expected = "INFO  TEST:  Test message";
+               self::assertEquals($expected, $actual);
+    }
+    
+    public function testEnvKey() {
+       
+       // Set an environment variable for testing
+       if (putenv('TEST=abc') === false) {
+               self::markTestSkipped("Unable to set environment variable for 
testing.");
+       }
+       
+       // Test reading of the set variable
+       self::assertEquals('abc', LoggerMDC::get('env.TEST'));
+       
+       // Test env variable in a pattern
+       $event = new LoggerLoggingEvent("LoggerLayoutPattern", new 
Logger("TEST"), LoggerLevel::getLevelInfo(), "Test message");
+       $actual = $this->formatEvent($event, $this->patternEnv);
+               $expected = "INFO  TEST: abc Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Test reading a non-existant env variable
+               self::assertEquals('', 
LoggerMDC::get('env.hopefully_this_variable_doesnt_exist'));
+               
+               // Test reading an empty env variable
+               self::assertEquals('', LoggerMDC::get('env.'));
+    }
+    
+    public function testServerKey() {
+       
+       // Test reading a server variable
+       $value = $_SERVER['PHP_SELF'];
+       self::assertEquals($value, LoggerMDC::get('server.PHP_SELF'));
+       
+               // Test the server variable in a pattern
+       $event = new LoggerLoggingEvent("LoggerLayoutPattern", new 
Logger("TEST"), LoggerLevel::getLevelInfo(), "Test message");
+       $actual = $this->formatEvent($event, $this->patternServer);
+               $expected = "INFO  TEST: $value Test message";
+               self::assertEquals($expected, $actual);
+               
+               // Test reading a non-existant server variable
+               self::assertEquals('', 
LoggerMDC::get('server.hopefully_this_variable_doesnt_exist'));
+               
+               // Test reading an empty server variable
+               self::assertEquals('', LoggerMDC::get('server.'));
+    }
+    
+       private function formatEvent($event, $pattern) {
+               $layout = new LoggerLayoutPattern();
+               $layout->setConversionPattern($pattern);
+               return $layout->format($event);
+       }
+}
+
+?>


Reply via email to