1   /**
2    * LOGBack: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.rolling.helper;
11  
12  import java.util.Date;
13  
14  import junit.framework.TestCase;
15  
16  public class RollingCalendarTest extends TestCase {
17  
18    public RollingCalendarTest(String arg0) {
19      super(arg0);
20    }
21  
22    protected void setUp() throws Exception {
23      super.setUp();
24    }
25  
26    protected void tearDown() throws Exception {
27      super.tearDown();
28    }
29  
30    public void testPeriodicity() {
31      {
32        RollingCalendar rc = new RollingCalendar();
33        assertEquals(PeriodicityType.TOP_OF_SECOND, rc
34            .computePeriodicity("yyyy-MM-dd_HH_mm_ss"));
35      }
36  
37      {
38        RollingCalendar rc = new RollingCalendar();
39        assertEquals(PeriodicityType.TOP_OF_MINUTE, rc
40            .computePeriodicity("yyyy-MM-dd_HH_mm"));
41      }
42  
43      {
44        RollingCalendar rc = new RollingCalendar();
45        assertEquals(PeriodicityType.TOP_OF_HOUR, rc
46            .computePeriodicity("yyyy-MM-dd_HH"));
47      }
48  
49      {
50        RollingCalendar rc = new RollingCalendar();
51        assertEquals(PeriodicityType.TOP_OF_DAY, rc
52            .computePeriodicity("yyyy-MM-dd"));
53      }
54  
55      {
56        RollingCalendar rc = new RollingCalendar();
57        assertEquals(PeriodicityType.TOP_OF_MONTH, rc
58            .computePeriodicity("yyyy-MM"));
59      }
60    }
61  
62    public void testVaryingNumberOfHourlyPeriods() {
63      RollingCalendar rc = new RollingCalendar();
64      rc.init("yyyy-MM-dd_HH");
65      long MILLIS_IN_HOUR = 3600*1000;
66  
67      for (int p = 100; p > -100; p--) {
68        long now = 1223325293589L;  // Mon Oct 06 22:34:53 CEST 2008
69        Date result = rc.getRelativeDate(new Date(now), p);
70        long expected = now - (now % (MILLIS_IN_HOUR)) + p * MILLIS_IN_HOUR;
71        assertEquals(expected, result.getTime());
72      }
73    }
74  
75    public void testVaryingNumberOfDailyPeriods() {
76      RollingCalendar rc = new RollingCalendar();
77      rc.init("yyyy-MM-dd");
78      final long MILLIS_IN_DAY = 24*3600*1000;
79      
80      for (int p = 20; p > -100; p--) {
81        long now = 1223325293589L;  // Mon Oct 06 22:34:53 CEST 2008
82        Date nowDate = new Date(now);
83        Date result = rc.getRelativeDate(nowDate, p);
84        long offset = rc.getTimeZone().getRawOffset()+rc.getTimeZone().getDSTSavings();
85      
86        long origin = now - (now % (MILLIS_IN_DAY)) - offset;      
87        long expected = origin + p * MILLIS_IN_DAY;
88        assertEquals("p="+p, expected, result.getTime());
89      }
90    }
91  }