#-- # ============================================================================= # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # * The names of its contributors may not be used to endorse or promote # products derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ============================================================================= #++ $:.unshift "../lib" require 'copland/log-factory' require 'test/unit' require 'mock' class TC_Logger < Test::Unit::TestCase def test_log_filename factory = Copland::LogFactory.new :config_file => 'bogus' assert_equal Copland::LogFactory::DEFAULT_LOG_FILENAME, factory.device.filename factory.close assert factory.closed? factory = Copland::LogFactory.new :config_file => 'bogus', :filename => './sample.log' assert_equal "./sample.log", factory.device.filename factory.close factory = Copland::LogFactory.new :config_file => 'bogus', :device => STDOUT assert_equal STDOUT, factory.device factory.close assert !STDOUT.closed? end def test_log_config factory = Copland::LogFactory.new assert_equal "./sample.log", factory.device.filename assert_equal "%Y-%m-%d %H:%M:%S", factory.default_format assert_equal Logger::INFO, factory.default_level factory.close factory = Copland::LogFactory.new :config_file => 'custom-logger.yml' assert_equal "./custom.log", factory.device.filename assert_equal "date %Y-%m-%d %H:%M:%S", factory.default_format assert_equal Logger::FATAL, factory.default_level factory.close end def test_log_options factory = Copland::LogFactory.new :default_format => "%Y", :default_level => Logger::FATAL assert_equal "%Y", factory.default_format assert_equal Logger::FATAL, factory.default_level factory.close end def test_log_get factory = Copland::LogFactory.new log1a = factory.get( "test.log1" ) log2 = factory.get( "test.log2" ) log1b = factory.get( "test.log1" ) assert_equal log1a.id, log1b.id assert_not_equal log1a.id, log2.id factory.close end def test_log_write io = MockLogIO.new factory = Copland::LogFactory.new :device => io log = factory.get( "test.log1" ) # the config file has DEBUG turned off... log.debug "test" assert_equal "", io.message log.info "test" assert_match( /INFO -- test.log1: test/, io.message ) factory.close assert_equal "", io.message end def test_levels io = MockLogIO.new factory = Copland::LogFactory.new :device => io log = factory.get( "level.log1" ) log.info "test" assert_equal "", io.message log = factory.get( "level.test.log1" ) log.debug "test" assert_match( /DEBUG -- level.test.log1: test/, io.message ) factory.close end end