Author: ihabunek
Date: Sat Sep 18 12:29:24 2010
New Revision: 998456

URL: http://svn.apache.org/viewvc?rev=998456&view=rev
Log:
LOG4PHP-104: Refactored LoggerNDC and added tests.

Added:
    logging/log4php/trunk/src/test/php/LoggerNDCTest.php
Modified:
    logging/log4php/trunk/src/changes/changes.xml
    logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php
    logging/log4php/trunk/src/main/php/LoggerNDC.php

Modified: logging/log4php/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=998456&r1=998455&r2=998456&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sat Sep 18 12:29:24 2010
@@ -24,6 +24,7 @@
   </properties>
   <body>
        <release version="2.1" description="Stabilizing">
+               <action type="fix" issue="LOG4PHP-104" by="Ivan 
Habunek">Refactored LoggerNDC and added tests</action>
                <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>

Modified: logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php?rev=998456&r1=998455&r2=998456&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerLoggingEvent.php Sat Sep 18 
12:29:24 2010
@@ -259,7 +259,7 @@ class LoggerLoggingEvent {
        public function getNDC() {
                if($this->ndcLookupRequired) {
                        $this->ndcLookupRequired = false;
-                       $this->ndc = implode(' ', LoggerNDC::get());
+                       $this->ndc = LoggerNDC::get();
                }
                return $this->ndc;
        }

Modified: logging/log4php/trunk/src/main/php/LoggerNDC.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/LoggerNDC.php?rev=998456&r1=998455&r2=998456&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/LoggerNDC.php (original)
+++ logging/log4php/trunk/src/main/php/LoggerNDC.php Sat Sep 18 12:29:24 2010
@@ -18,12 +18,6 @@
  * @package log4php
  */
 
-
-/**
- * This is the global repository of NDC stack
- */
-$GLOBALS['log4php.LoggerNDC.ht'] = array();
-
 /**
  * The NDC class implements <i>nested diagnostic contexts</i>.
  * 
@@ -100,7 +94,10 @@ $GLOBALS['log4php.LoggerNDC.ht'] = array
  * @since 0.3
  */
 class LoggerNDC {
-       const HT_SIZE = 7;
+       
+       /** This is the repository of NDC stack */
+       private static $stack = array();
+       
        /**
         * Clear any nested diagnostic information if any. This method is
         * useful in cases where the same thread can be potentially used
@@ -109,10 +106,10 @@ class LoggerNDC {
         * <p>This method is equivalent to calling the {...@link setMaxDepth()}
         * method with a zero <var>maxDepth</var> argument.
         *
-        * @static      
+        * @static
         */
        public static function clear() {
-               $GLOBALS['log4php.LoggerNDC.ht'] = array();
+               self::$stack = array();
        }
 
        /**
@@ -121,10 +118,7 @@ class LoggerNDC {
         * @return array
         */
        public static function get() {
-               if(!array_key_exists('log4php.LoggerNDC.ht', $GLOBALS)) {
-                       LoggerNDC::clear();
-               }
-               return $GLOBALS['log4php.LoggerNDC.ht'];
+               return implode(' ', self::$stack);
        }
   
        /**
@@ -135,7 +129,7 @@ class LoggerNDC {
         * @static
         */
        public static function getDepth() {
-               return count($GLOBALS['log4php.LoggerNDC.ht']);   
+               return count(self::$stack);
        }
 
        /**
@@ -149,8 +143,8 @@ class LoggerNDC {
         * @static
         */
        public static function pop() {
-               if(count($GLOBALS['log4php.LoggerNDC.ht']) > 0) {
-                       return array_pop($GLOBALS['log4php.LoggerNDC.ht']);
+               if(count(self::$stack) > 0) {
+                       return array_pop(self::$stack);
                } else {
                        return '';
                }
@@ -166,8 +160,8 @@ class LoggerNDC {
         * @static
         */
        public static function peek(){
-               if(count($GLOBALS['log4php.LoggerNDC.ht']) > 0) {
-                       return end($GLOBALS['log4php.LoggerNDC.ht']);
+               if(count(self::$stack) > 0) {
+                       return end(self::$stack);
                } else {
                        return '';
                }
@@ -183,7 +177,7 @@ class LoggerNDC {
         * @static      
         */
        public static function push($message) {
-               array_push($GLOBALS['log4php.LoggerNDC.ht'], (string)$message);
+               array_push(self::$stack, (string)$message);
        }
 
        /**
@@ -211,11 +205,8 @@ class LoggerNDC {
         */
        public static function setMaxDepth($maxDepth) {
                $maxDepth = (int)$maxDepth;
-               if($maxDepth <= self::HT_SIZE) {
-                       if(LoggerNDC::getDepth() > $maxDepth) {
-                               $GLOBALS['log4php.LoggerNDC.ht'] = 
array_slice($GLOBALS['log4php.LoggerNDC.ht'], $maxDepth);
-                       }
+               if(LoggerNDC::getDepth() > $maxDepth) {
+                       self::$stack = array_slice(self::$stack, 0, $maxDepth);
                }
        }
-
 }

Added: logging/log4php/trunk/src/test/php/LoggerNDCTest.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/LoggerNDCTest.php?rev=998456&view=auto
==============================================================================
--- logging/log4php/trunk/src/test/php/LoggerNDCTest.php (added)
+++ logging/log4php/trunk/src/test/php/LoggerNDCTest.php Sat Sep 18 12:29:24 
2010
@@ -0,0 +1,89 @@
+<?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 LoggerNDCTest extends PHPUnit_Framework_TestCase {
+       
+       
+       public function testItemHandling()
+       {
+               // Test the empty stack
+               self::assertSame('', LoggerNDC::get());
+               self::assertSame('', LoggerNDC::peek());
+               self::assertSame(0, LoggerNDC::getDepth());
+               self::assertSame('', LoggerNDC::pop());
+               
+               // Add some data to the stack
+               LoggerNDC::push('1');
+               LoggerNDC::push('2');
+               LoggerNDC::push('3');
+               
+               self::assertSame('1 2 3', LoggerNDC::get());
+               self::assertSame('3', LoggerNDC::peek());
+               self::assertSame(3, LoggerNDC::getDepth());
+
+               // Remove last item
+               self::assertSame('3', LoggerNDC::pop());
+               self::assertSame('1 2', LoggerNDC::get());
+               self::assertSame('2', LoggerNDC::peek());
+               self::assertSame(2, LoggerNDC::getDepth());
+
+               // Remove all items
+               LoggerNDC::remove();
+
+               // Test the empty stack
+               self::assertSame('', LoggerNDC::get());
+               self::assertSame('', LoggerNDC::peek());
+               self::assertSame(0, LoggerNDC::getDepth());
+               self::assertSame('', LoggerNDC::pop());
+       }
+       
+       public function testMaxDepth()
+       {
+               // Clear stack; add some testing data
+               LoggerNDC::clear();
+               LoggerNDC::push('1');
+               LoggerNDC::push('2');
+               LoggerNDC::push('3');
+               LoggerNDC::push('4');
+               LoggerNDC::push('5');
+               LoggerNDC::push('6');
+               
+               self::assertSame('1 2 3 4 5 6', LoggerNDC::get());
+               
+               // Edge case, should not change stack
+               LoggerNDC::setMaxDepth(6);
+               self::assertSame('1 2 3 4 5 6', LoggerNDC::get());
+               self::assertSame(6, LoggerNDC::getDepth());
+               
+               LoggerNDC::setMaxDepth(3);
+               self::assertSame('1 2 3', LoggerNDC::get());
+               self::assertSame(3, LoggerNDC::getDepth());
+               
+               LoggerNDC::setMaxDepth(0);
+               self::assertSame('', LoggerNDC::get());
+               self::assertSame(0, LoggerNDC::getDepth());
+       }
+}
+
+?>


Reply via email to