logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject svn commit: r1183662 - in /logging/log4php/branches/experimental/config-adapters: src/test/php/ src/test/php/appenders/ src/test/php/configurators/ src/test/php/filters/ xsd/
Date Sat, 15 Oct 2011 15:38:45 GMT
Author: ihabunek
Date: Sat Oct 15 15:38:45 2011
New Revision: 1183662

URL: http://svn.apache.org/viewvc?rev=1183662&view=rev
Log:
Improved tests.

Added:
    logging/log4php/branches/experimental/config-adapters/xsd/
      - copied from r1173517, logging/log4php/branches/experimental/config-adapters/src/main/php/xml/
Modified:
    logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderConsoleTest.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderFileTest.php
    logging/log4php/branches/experimental/config-adapters/src/test/php/configurators/config1.ini
    logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
    logging/log4php/branches/experimental/config-adapters/xsd/log4php.xsd

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php?rev=1183662&r1=1183661&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerConfiguratorTest.php
Sat Oct 15 15:38:45 2011
@@ -34,7 +34,7 @@
  		$this->assertInstanceOf('LoggerAppenderEcho', $appender);
  		
  		$layout = $appender->getLayout();
- 		$this->assertInstanceOf('LoggerLayoutSimple', $layout);
+ 		$this->assertInstanceOf('LoggerLayoutTTCC', $layout);
  		
  		$root = Logger::getRootLogger();
  		$appenders = $root->getAllAppenders();
@@ -42,8 +42,8 @@
  		$this->assertEquals(count($appenders), 1);
 		
  		$actual = $root->getLevel();
- 		$expected = LoggerLevel::getLevelInfo();
- 		$this->assertSame($expected, $actual);
+ 		$expected = LoggerLevel::getLevelDebug();
+ 		$this->assertEquals($expected, $actual);
  	}
  	
  	/**

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php?rev=1183662&r1=1183661&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/LoggerTestHelper.php
Sat Oct 15 15:38:45 2011
@@ -3,30 +3,40 @@
 /** A set of helper functions for running tests. */
 class LoggerTestHelper {
 	
+	/** Returns a test logging event with level set to TRACE. */
 	public static function getTraceEvent($message = 'test') {
 		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelTrace(),
$message);
 	}
 	
+	/** Returns a test logging event with level set to DEBUG. */
 	public static function getDebugEvent($message = 'test') {
 		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelDebug(),
$message);
 	}
 	
+	/** Returns a test logging event with level set to INFO. */
 	public static function getInfoEvent($message = 'test') {
 		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelInfo(),
$message);
 	}
 	
+	/** Returns a test logging event with level set to WARN. */
 	public static function getWarnEvent($message = 'test') {
 		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelWarn(),
$message);
 	}
 	
+	/** Returns a test logging event with level set to ERROR. */
 	public static function getErrorEvent($message = 'test') {
 		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelError(),
$message);
 	}
 	
+	/** Returns a test logging event with level set to FATAL. */
 	public static function getFatalEvent($message = 'test') {
 		return new LoggerLoggingEvent(__CLASS__, new Logger("test"), LoggerLevel::getLevelFatal(),
$message);
 	}
 	
+	/** 
+	 * Returns an array of logging events, one for each level, sorted ascending
+	 * by severitiy. 
+	 */
 	public static function getAllEvents($message = 'test') {
 		return array(
 			self::getTraceEvent($message),
@@ -37,11 +47,46 @@ class LoggerTestHelper {
 			self::getFatalEvent($message),
 		);
 	}
+	
+	/** Returns an array of all existing levels, sorted ascending by severity. */
+	public static function getAllLevels() {
+		return array(
+			LoggerLevel::getLevelTrace(),
+			LoggerLevel::getLevelDebug(),
+			LoggerLevel::getLevelInfo(),
+			LoggerLevel::getLevelWarn(),
+			LoggerLevel::getLevelError(),
+			LoggerLevel::getLevelFatal(),
+		);
+	}
+	
+	/** Returns a string representation of a filter decision. */
+	public static function decisionToString($decision) {
+		switch($decision) {
+			case LoggerFilter::ACCEPT: return 'ACCEPT';
+			case LoggerFilter::NEUTRAL: return 'NEUTRAL';
+			case LoggerFilter::DENY: return 'DENY';
+		}
+	}
+	
+	/** Returns a simple configuration with one echo appender tied to root logger. */
+	public static function getEchoConfig() {
+		return array(
+	        'threshold' => 'ALL',
+	        'rootLogger' => array(
+	            'level' => 'trace',
+	            'appenders' => array('default'),
+			),
+	        'appenders' => array(
+	            'default' => array(
+	                'class' => 'LoggerAppenderEcho',
+	                'layout' => array(
+	                    'class' => 'LoggerLayoutSimple',
+					),
+				),
+			),
+		);
+	}
 }
 
-
-
-
-
-
 ?>
\ No newline at end of file

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderConsoleTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderConsoleTest.php?rev=1183662&r1=1183661&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderConsoleTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderConsoleTest.php
Sat Oct 15 15:38:45 2011
@@ -41,6 +41,7 @@ class LoggerAppenderConsoleTest extends 
 				'layout' => array(
 					'class' => 'LoggerLayoutPattern',
 					'params' => array(
+						// Intentionally blank so output doesn't clutter phpunit output
 						'conversionPattern' => '' 
 					)
 				),

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderFileTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderFileTest.php?rev=1183662&r1=1183661&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderFileTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/appenders/LoggerAppenderFileTest.php
Sat Oct 15 15:38:45 2011
@@ -47,10 +47,6 @@ class LoggerAppenderFileTest extends PHP
 	
 	public function __construct() {
 		$this->testPath = PHPUNIT_TEMP_DIR . '/TEST.txt';
-		
-		if (is_dir(PHPUNIT_TEMP_DIR)) {
-			rmdir(PHPUNIT_TEMP_DIR);
-		}
 	}
 	
 	public function setUp() {

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/configurators/config1.ini
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/configurators/config1.ini?rev=1183662&r1=1183661&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/configurators/config1.ini
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/configurators/config1.ini
Sat Oct 15 15:38:45 2011
@@ -5,7 +5,7 @@ log4php.appender.default.layout = Logger
 
 log4php.appender.file = LoggerAppenderDailyFile
 log4php.appender.file.layout = LoggerLayoutPattern
-log4php.appender.file.layout.conversionPattern =%d{ISO8601} [%p] %c: %m (at %F line %L)%n
+log4php.appender.file.layout.conversionPattern = %d{ISO8601} [%p] %c: %m (at %F line %L)%n
 log4php.appender.file.datePattern = Ymd
 log4php.appender.file.file = target/examples/daily_%s.log
 log4php.appender.file.threshold = warn

Modified: logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php?rev=1183662&r1=1183661&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
(original)
+++ logging/log4php/branches/experimental/config-adapters/src/test/php/filters/LoggerFilterLevelMatchTest.php
Sat Oct 15 15:38:45 2011
@@ -27,114 +27,98 @@
  * @group filters
  */
 class LoggerFilterLevelMatchTest extends PHPUnit_Framework_TestCase {
-		
-	public function testDecideAcceptErrorLevel() {
+
+	/** 
+	 * Tests all possible combinations of event level and filter levelToMatch 
+	 * option, with acceptOnMatch set to TRUE. 
+	 */
+	public function testDecideAccept() {
 		$filter = new LoggerFilterLevelMatch();
-		$filter->setAcceptOnMatch(true);
-		$filter->setLevelToMatch(LoggerLevel::getLevelError());
-		
-		$eventError = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$eventDebug = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelDebug(),
"testmessage");
-		$eventWarn = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelWarn(),
"testmessage");
-		
-		$result = $filter->decide($eventError);
-		self::assertEquals($result, LoggerFilter::ACCEPT);
+		$filter->setAcceptOnMatch("true");
 		
-		$result = $filter->decide($eventDebug);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
+		$levels = LoggerTestHelper::getAllLevels();
+		$events = LoggerTestHelper::getAllEvents();
 		
-		$result = $filter->decide($eventWarn);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-	}
-	
-	public function testDecideDenyErrorLevel() {
+		foreach($levels as $level) {
+			$filter->setLevelToMatch($level);
+			
+			foreach($events as $event) {
+				// Expecting given level to be accepted, neutral for others
+				$expected = ($event->getLevel() == $level) ? LoggerFilter::ACCEPT : LoggerFilter::NEUTRAL;
+				$actual = $filter->decide($event);
+					
+				// Get string represenations for logging
+				$sExpected = LoggerTestHelper::decisionToString($expected);
+				$sActual = LoggerTestHelper::decisionToString($actual);
+				
+				$this->assertSame($expected, $actual, "Failed asserting filter decision for event
level <$level>. Expected <$sExpected>, found <$sActual>.");
+			}
+		}
+	}
+	
+	/**
+	 * Tests all possible combinations of event level and filter levelToMatch
+	 * option, with acceptOnMatch set to TRUE.
+	 */
+	public function testDecideDeny() {
 		$filter = new LoggerFilterLevelMatch();
 		$filter->setAcceptOnMatch("false");
-		$filter->setLevelToMatch(LoggerLevel::getLevelError());
-		
-		$eventError = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$eventDebug = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelDebug(),
"testmessage");
-		$eventWarn = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelWarn(),
"testmessage");
-		
-		$result = $filter->decide($eventError);
-		self::assertEquals($result, LoggerFilter::DENY);
-		
-		$result = $filter->decide($eventDebug);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-		
-		$result = $filter->decide($eventWarn);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-	}
 	
-	public function testDecideAcceptWarnLevel() {
-		$filter = new LoggerFilterLevelMatch();
-		$filter->setAcceptOnMatch("true");
-		$filter->setLevelToMatch(LoggerLevel::getLevelWarn());
-		
-		$eventError = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$eventDebug = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelDebug(),
"testmessage");
-		$eventWarn = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelWarn(),
"testmessage");
-		
-		$result = $filter->decide($eventError);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-		
-		$result = $filter->decide($eventDebug);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-		
-		$result = $filter->decide($eventWarn);
-		self::assertEquals($result, LoggerFilter::ACCEPT);
-	}
+		$levels = LoggerTestHelper::getAllLevels();
+		$events = LoggerTestHelper::getAllEvents();
 	
-	public function testDecideDenyWarnLevel() {
-		$filter = new LoggerFilterLevelMatch();
-		$filter->setAcceptOnMatch("false");
-		$filter->setLevelToMatch(LoggerLevel::getLevelWarn());
-		
-		$eventError = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$eventDebug = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelDebug(),
"testmessage");
-		$eventWarn = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelWarn(),
"testmessage");
-		
-		$result = $filter->decide($eventError);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-		
-		$result = $filter->decide($eventDebug);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-		
-		$result = $filter->decide($eventWarn);
-		self::assertEquals($result, LoggerFilter::DENY);
+		foreach($levels as $level) {
+			$filter->setLevelToMatch($level);
+				
+			foreach($events as $event) {
+				// Expecting given level to be denied, neutral for others
+				$expected = ($event->getLevel() == $level) ? LoggerFilter::DENY : LoggerFilter::NEUTRAL;
+				$actual = $filter->decide($event);
+					
+				// Get string represenations for logging
+				$sExpected = LoggerTestHelper::decisionToString($expected);
+				$sActual = LoggerTestHelper::decisionToString($actual);
+	
+				$this->assertSame($expected, $actual, "Failed asserting filter decision for event
level <$level>. Expected <$sExpected>, found <$sActual>.");
+			}
+		}
 	}
 	
-	public function testDecideDenyDebugLevel() {
+	/** Test that filter always decides NEUTRAL when levelToMatch is not set. */
+	public function testDecideNull() {
 		$filter = new LoggerFilterLevelMatch();
-		$filter->setAcceptOnMatch("false");
-		$filter->setLevelToMatch(LoggerLevel::getLevelDebug());
-		
-		$eventError = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$eventDebug = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelDebug(),
"testmessage");
-		$eventWarn = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelWarn(),
"testmessage");
+		$events = LoggerTestHelper::getAllEvents();
 		
-		$result = $filter->decide($eventError);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
+		foreach($events as $event) {
+			$expected = LoggerFilter::NEUTRAL;
+			$actual = $filter->decide($event);
+				
+			// Get string represenations for logging
+			$sExpected = LoggerTestHelper::decisionToString($expected);
+			$sActual = LoggerTestHelper::decisionToString($actual);
+			$level = $event->getLevel();
+			
+			$this->assertSame($expected, $actual, "Failed asserting filter decision for event level
<$level>. Expected <$sExpected>, found <$sActual>.");
+		}
+	}
+	
+	/** Test logger configuration. */
+	public function testAcceptConfig() {
+		$config = LoggerTestHelper::getEchoConfig();
 		
-		$result = $filter->decide($eventDebug);
-		self::assertEquals($result, LoggerFilter::DENY);
-		
-		$result = $filter->decide($eventWarn);
-		self::assertEquals($result, LoggerFilter::NEUTRAL);
-	}
-	
-	public function testConfiguration() {
-		$config = LoggerConfigurator::getDefaultConfiguration();
-		
-		// Add filters to accept debug and deny all others
+		// Add filters to default appender
 		$config['appenders']['default']['filters'] = array(
+			
+			// Accepts only INFO events
 			array(
 				'class' => 'LoggerFilterLevelMatch',
 				'params' => array(
-					'levelToMatch' => 'error',
+					'levelToMatch' => 'info',
 					'acceptOnMatch' => true
 				)
 			),
+			
+			// Denies all events not accepted by first filter
 			array(
 				'class' => 'LoggerFilterDenyAll',
 			)
@@ -150,13 +134,48 @@ class LoggerFilterLevelMatchTest extends
 		$logger->warn('Test');
 		$logger->error('Test');
 		$logger->fatal('Test');
-		$actual = ob_get_clean();
 		
-		var_dump($actual);
+		$actual = ob_get_clean();
+
 		
-		$this->assertEmpty($actual);
+		$expected = "INFO - Test" . PHP_EOL;
 	}
 	
+	public function testDenyConfig() {
+		$config = LoggerTestHelper::getEchoConfig();
+	
+		// Add filter which denies INFO events
+		$config['appenders']['default']['filters'] = array(
+			array(
+				'class' => 'LoggerFilterLevelMatch',
+				'params' => array(
+					'levelToMatch' => 'info',
+					'acceptOnMatch' => false
+				)
+			)
+		);
+			
+		Logger::configure($config);
+		$logger = Logger::getRootLogger();
+			
+		ob_start();
+		$logger->trace('Test');
+		$logger->debug('Test');
+		$logger->info('Test');
+		$logger->warn('Test');
+		$logger->error('Test');
+		$logger->fatal('Test');
 	
+		$actual = ob_get_clean();
+		
+		// Should log all except info
+		$expected = 
+			"TRACE - Test" . PHP_EOL . 
+			"DEBUG - Test" . PHP_EOL . 
+			"WARN - Test"  . PHP_EOL . 
+			"ERROR - Test" . PHP_EOL . 
+			"FATAL - Test" . PHP_EOL;	
 	
+		$this->assertSame($expected, $actual);
+	}
 }

Modified: logging/log4php/branches/experimental/config-adapters/xsd/log4php.xsd
URL: http://svn.apache.org/viewvc/logging/log4php/branches/experimental/config-adapters/xsd/log4php.xsd?rev=1183662&r1=1173517&r2=1183662&view=diff
==============================================================================
--- logging/log4php/branches/experimental/config-adapters/xsd/log4php.xsd (original)
+++ logging/log4php/branches/experimental/config-adapters/xsd/log4php.xsd Sat Oct 15 15:38:45
2011
@@ -1,9 +1,29 @@
 <?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 	elementFormDefault="unqualified" 
 	targetNamespace="http://logging.apache.org/log4php"
 	xmlns="http://logging.apache.org/log4php">
 	
+	<!-- ============================================== -->
+	<!--               Configuration                    -->
+	<!-- ============================================== -->
+
 	<xs:element name="configuration">
 		<xs:annotation>
 			<xs:documentation>
@@ -44,6 +64,11 @@
 		</xs:complexType>
 	</xs:element>
 	<xs:element name="appender">
+		<xs:annotation>
+			<xs:documentation>
+				Defines an appender.
+			</xs:documentation>
+		</xs:annotation>
 		<xs:complexType>
 			<xs:sequence>
 				<xs:element minOccurs="0" ref="layout" />
@@ -56,6 +81,11 @@
 		</xs:complexType>
 	</xs:element>
 	<xs:element name="layout">
+		<xs:annotation>
+			<xs:documentation>
+				Defines a layout.
+			</xs:documentation>
+		</xs:annotation>
 		<xs:complexType>
 			<xs:sequence>
 				<xs:element minOccurs="0" maxOccurs="unbounded" ref="param" />
@@ -64,6 +94,11 @@
 		</xs:complexType>
 	</xs:element>
 	<xs:element name="filter">
+		<xs:annotation>
+			<xs:documentation>
+				Defines a filter.
+			</xs:documentation>
+		</xs:annotation>
 		<xs:complexType>
 			<xs:sequence>
 				<xs:element minOccurs="0" maxOccurs="unbounded" ref="param" />
@@ -114,6 +149,11 @@
 			</xs:sequence>
 		</xs:complexType>
 	</xs:element>
+	
+	<!-- ============================================== -->
+	<!--                   Events                       -->
+	<!-- ============================================== -->
+	
 	<xs:element name="eventSet">
 		<xs:complexType>
 			<xs:sequence>
@@ -137,6 +177,7 @@
 			</xs:attribute>
 		</xs:complexType>
 	</xs:element>
+	
 	<xs:element name="event">
 		<xs:complexType>
 			<xs:sequence>



Mime
View raw message