added renderer configuration test
authorChristian Grobmeier <grobmeier@apache.org>
Mon, 31 Aug 2009 06:38:24 +0000 (06:38 +0000)
committerChristian Grobmeier <grobmeier@apache.org>
Mon, 31 Aug 2009 06:38:24 +0000 (06:38 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/log4php/trunk@809468 13f79535-47bb-0310-9956-ffa450edef68

src/main/php/LoggerHierarchy.php
src/main/php/configurators/LoggerConfiguratorIni.php
src/main/php/configurators/LoggerConfiguratorXml.php
src/main/php/renderers/LoggerRendererMap.php
src/test/php/configurators/LoggerConfiguratorIniTest.php
src/test/php/configurators/LoggerConfiguratorXmlTest.php
src/test/php/configurators/test1.xml
src/test/php/configurators/test4.properties
src/test/php/renderers/LoggerRendererMapTest.php

index 488fdb9..ea8e5a2 100644 (file)
@@ -74,22 +74,11 @@ class LoggerHierarchy {
         */
        public function __construct(LoggerRoot $root) {
                $this->root = $root;
-               // TODO: isn't necessary, access via singleton?'
-//             $this->root->setHierarchy($this);
                $this->setThreshold(LoggerLevel::getLevelAll());
                $this->rendererMap = new LoggerRendererMap();
        }
         
        /**
-        * Add an object renderer for a specific class.
-        * @param string $classToRender
-        * @param LoggerObjectRenderer $or
-        */
-       public function addRenderer($classToRender, $or) {
-               $this->rendererMap->put($classToRender, $or);
-       } 
-       
-       /**
         * This call will clear all logger definitions from the internal hashtable.
         */
        public function clear() {
@@ -218,16 +207,6 @@ class LoggerHierarchy {
        }
        
        /**
-        * Used by subclasses to add a renderer to the hierarchy passed as parameter.
-        * @param string $renderedClass a LoggerRenderer class name
-        * @param LoggerRenderer $renderer
-        *
-        */
-       public function setRenderer($renderedClass, $renderer) {
-               $this->rendererMap->put($renderedClass, $renderer);
-       }
-       
-       /**
         * set a new threshold level
         *
         * @param LoggerLevel $l
index d08369a..6eb21e4 100644 (file)
@@ -351,9 +351,7 @@ class LoggerConfiguratorIni implements LoggerConfigurator {
                        } else if(strpos($key, self::RENDERER_PREFIX) === 0) {
                                $renderedClass = substr($key, strlen(self::RENDERER_PREFIX));
                                $renderingClass = $value;
-                               if(method_exists($hierarchy, 'addrenderer')) { // ?
-                                       LoggerRendererMap::addRenderer($hierarchy, $renderedClass, $renderingClass);
-                               }
+                               $hierarchy->getRendererMap()->addRenderer($renderedClass, $renderingClass);
                        }
                }
        }
index 43ca3e7..ed97387 100644 (file)
@@ -361,12 +361,7 @@ class LoggerConfiguratorXml implements LoggerConfigurator {
                 $renderingClass  = $this->subst(@$attribs['RENDERINGCLASS']);
         
                 if (!empty($renderedClass) and !empty($renderingClass)) {
-                    $renderer = LoggerReflectionUtils::createObject($renderingClass);
-                    if ($renderer === null) {
-                        // LoggerDOMConfigurator::tagOpen() RENDERER cannot instantiate '$renderingClass'
-                    } else { 
-                        $this->repository->setRenderer($renderedClass, $renderer);
-                    }
+                    $this->repository->getRendererMap()->addRenderer($renderedClass, $renderingClass);
                 }
                 break;
             
index 4d0f858..0ebc1c6 100644 (file)
@@ -54,12 +54,12 @@ class LoggerRendererMap {
         * @param string $renderingClassName
         * @static
         */
-       public static function addRenderer($repository, $renderedClassName, $renderingClassName) {
+       public function addRenderer($renderedClassName, $renderingClassName) {
                $renderer = LoggerReflectionUtils::createObject($renderingClassName);
                if($renderer == null) {
                        return;
                } else {
-                       $repository->setRenderer($renderedClassName, $renderer);
+                       $this->put($renderedClassName, $renderer);
                }
        }
 
@@ -142,7 +142,7 @@ class LoggerRendererMap {
         * @param string $class
         * @param LoggerRendererObject $or
         */
-       public function put($class, $or) {
+       private function put($class, $or) {
                $this->map[strtolower($class)] = $or;
        }
        
index 8eb4114..3971fce 100644 (file)
  * @link       http://logging.apache.org/log4php
  */
 
+class Fruit {
+    public $test1 = 'test1';
+    public $test2 = 'test2';
+    public $test3 = 'test3';
+}
+
+class FruitRenderer extends LoggerRendererObject {
+    public function doRender($o) {
+               return $o->test1.','.$o->test2.','.$o->test3;
+       }
+}
+
 class LoggerConfiguratorIniTest extends PHPUnit_Framework_TestCase {
         
        protected function setUp() {
@@ -113,4 +125,13 @@ class LoggerConfiguratorIniTest extends PHPUnit_Framework_TestCase {
                $e = LoggerLevel::getLevelWarn();
                self::assertEquals($e,$threshold);
        }
+       
+       public function testRenderer() {
+               Logger::configure('configurators/test4.properties');
+               Logger::initialize();
+               $hierarchy = Logger::getHierarchy();
+               $map = $hierarchy->getRendererMap();
+               $clazz = $map->getByClassName('Fruit');
+               self::assertTrue($clazz instanceof FruitRenderer);
+       }
 }
index 2f4708d..03e3745 100644 (file)
  * @version    SVN: $Id$
  * @link       http://logging.apache.org/log4php
  */
+class Fruit2 {
+    public $test1 = 'test1';
+    public $test2 = 'test2';
+    public $test3 = 'test3';
+}
+
+class FruitRenderer2 extends LoggerRendererObject {
+    public function doRender($o) {
+               return $o->test1.','.$o->test2.','.$o->test3;
+       }
+}
 
 class LoggerConfiguratorXmlTest extends PHPUnit_Framework_TestCase {
         
@@ -78,5 +89,10 @@ class LoggerConfiguratorXmlTest extends PHPUnit_Framework_TestCase {
                self::assertTrue($threshold instanceof LoggerLevel);
                $e = LoggerLevel::getLevelWarn();
                self::assertEquals($e,$threshold);
+               
+               $hierarchy = Logger::getHierarchy();
+               $map = $hierarchy->getRendererMap();
+               $clazz = $map->getByClassName('Fruit2');
+               self::assertTrue($clazz instanceof FruitRenderer2);
        }
 }
index b0f8b85..e5a275d 100644 (file)
@@ -17,6 +17,7 @@
 
 -->
 <log4php:configuration xmlns:log4php="http://logging.apache.org/log4php/" threshold="WARN">
+       <renderer renderedClass="Fruit2" renderingClass="FruitRenderer2" />
     <appender threshold="WARN" name="default" class="LoggerAppenderEcho">
         <layout class="LoggerLayoutSimple"/>
     </appender>
index d69fe12..4dae115 100644 (file)
@@ -13,6 +13,8 @@
 ; See the License for the specific language governing permissions and
 ; limitations under the License.
 ;
+log4php.renderer.Fruit=FruitRenderer
+
 log4php.appender.default = LoggerAppenderEcho
 log4php.appender.default.layout = LoggerLayoutSimple
 log4php.appender.default.threshold = WARN
index 7e26c35..dcef13e 100644 (file)
@@ -28,7 +28,7 @@ class LoggerRendererMapTest extends PHPUnit_Framework_TestCase {
        public function testAddRenderer() {
                $hierarchy = Logger::getHierarchy();
                //print_r($hierarchy);
-               LoggerRendererMap::addRenderer($hierarchy, 'string', 'LoggerRendererDefault');
+               $hierarchy->getRendererMap()->addRenderer('string', 'LoggerRendererDefault');
                //print_r($hierarchy);
                self::markTestIncomplete();
        }