OSDN Git Service

89355bcb7bb381d39cfddc5d6e9c938d3517ea81
[shogi-server/shogi-server.git] / test / TC_logger.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 $topdir = File.expand_path File.dirname(__FILE__)
3 require 'test/unit'
4 require 'shogi_server'
5
6 class TestableLogger < ShogiServer::Logger
7   def initialize(logdev, shift_age = 0, shift_size = 1048576)
8     super
9     class << @logdev
10       attr_accessor :return_age_file_exists
11       def age_file_exists?(age_file)
12         return @return_age_file_exists || false
13       end
14
15       attr_reader :result_rename_file
16       def rename_file(old_file, new_file)
17         @result_rename_file ||= []
18         @result_rename_file << [old_file, new_file]
19       end
20     end
21   end
22   attr_reader :logdev
23
24 end
25
26 class TestLogger < Test::Unit::TestCase
27   def setup
28     filename = File.join($topdir, "TC_logger_test.log")
29     @logger = TestableLogger.new(filename, "daily")
30     @logger.formatter = ShogiServer::Formatter.new
31     @logger.level = TestableLogger::DEBUG
32     @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
33   end
34
35   def test_dummy
36     assert true
37   end
38
39   def test_age_file_name
40     time =  Time.mktime(2010, 7, 25, 23, 59, 59)
41     assert_equal File.expand_path(File.join($topdir, "2010", "07", "24", "TC_logger_test.log")), 
42                  @logger.logdev.age_file_name(time)
43   end
44
45   def test_age_file_exists
46     assert !@logger.logdev.age_file_exists?(nil)
47     @logger.logdev.return_age_file_exists = true
48     assert @logger.logdev.age_file_exists?(nil)
49   end
50
51   def test_rename_file
52     @logger.logdev.rename_file("old", "new")
53     assert_equal [["old", "new"]], @logger.logdev.result_rename_file
54   end
55
56   def test_move_age_file_in_the_way
57     assert !@logger.logdev.age_file_exists?(nil)
58     @logger.logdev.move_age_file_in_the_way("hoge.log")
59     assert_nil @logger.logdev.result_rename_file
60
61     @logger.logdev.return_age_file_exists = true
62     @logger.logdev.move_age_file_in_the_way("hoge.log")
63     assert_equal 1, @logger.logdev.result_rename_file.size
64     assert_equal "hoge.log", @logger.logdev.result_rename_file.first.first
65   end
66
67   def test_log_info
68     @logger.info("test_log_info")
69     assert true
70   end
71
72   def test_shift_log_period
73     @logger.info("test_shift_log_period")
74     now =  Time.mktime(2010, 7, 25, 23, 59, 59)
75     @logger.logdev.shift_log_period(now)
76     assert_equal [["/home/daigo/rubyprojects/shogi-server/test/TC_logger_test.log",
77         "/home/daigo/rubyprojects/shogi-server/test/2010/07/24/TC_logger_test.log"]], 
78         @logger.logdev.result_rename_file
79   end
80 end