View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.time.Instant;
17  import java.util.Date;
18  import java.util.Locale;
19  import java.util.TimeZone;
20  
21  import org.junit.jupiter.api.AfterEach;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import ch.qos.logback.core.CoreConstants;
27  import ch.qos.logback.core.util.EnvUtil;
28  
29  public class RollingCalendarTest {
30  
31      String dailyPattern = "yyyy-MM-dd";
32  
33      @BeforeEach
34      public void setUp() {
35  
36          // Most surprisingly, in certain environments (e.g. Windows 7), setting the
37          // default locale
38          // allows certain tests to pass which otherwise fail.
39          //
40          // These tests are:
41          //
42          // checkCollisionFreeness("yyyy-WW", false);
43          // checkCollisionFreeness("yyyy-ww", true);
44          // checkCollisionFreeness("ww", false);
45          // {
46          // RollingCalendar rc = new RollingCalendar("yyyy-ww");
47          // assertEquals(PeriodicityType.TOP_OF_WEEK, rc.getPeriodicityType());
48          // }
49          //
50  
51          Locale oldLocale = Locale.getDefault();
52          Locale.setDefault(oldLocale);
53      }
54  
55      @AfterEach
56      public void tearDown() {
57      }
58  
59      @Test
60      public void testPeriodicity() {
61          {
62              RollingCalendar rc = new RollingCalendar("yyyy-MM-dd_HH_mm_ss");
63              Assertions.assertEquals(PeriodicityType.TOP_OF_SECOND, rc.getPeriodicityType());
64          }
65  
66          {
67              RollingCalendar rc = new RollingCalendar("yyyy-MM-dd_HH_mm");
68              Assertions.assertEquals(PeriodicityType.TOP_OF_MINUTE, rc.getPeriodicityType());
69          }
70  
71          {
72              RollingCalendar rc = new RollingCalendar("yyyy-MM-dd_HH");
73              Assertions.assertEquals(PeriodicityType.TOP_OF_HOUR, rc.getPeriodicityType());
74          }
75  
76          {
77              RollingCalendar rc = new RollingCalendar("yyyy-MM-dd_hh");
78              Assertions.assertEquals(PeriodicityType.TOP_OF_HOUR, rc.getPeriodicityType());
79          }
80  
81          {
82              RollingCalendar rc = new RollingCalendar("yyyy-MM-dd");
83              Assertions.assertEquals(PeriodicityType.TOP_OF_DAY, rc.getPeriodicityType());
84          }
85  
86          {
87              RollingCalendar rc = new RollingCalendar("yyyy-MM");
88              Assertions.assertEquals(PeriodicityType.TOP_OF_MONTH, rc.getPeriodicityType());
89          }
90  
91          {
92              RollingCalendar rc = new RollingCalendar("yyyy-ww");
93              Assertions.assertEquals(PeriodicityType.TOP_OF_WEEK, rc.getPeriodicityType());
94          }
95  
96          {
97              RollingCalendar rc = new RollingCalendar("yyyy-W");
98              Assertions.assertEquals(PeriodicityType.TOP_OF_WEEK, rc.getPeriodicityType());
99          }
100     }
101 
102     @Test
103     public void testVaryingNumberOfHourlyPeriods() {
104         RollingCalendar rc = new RollingCalendar("yyyy-MM-dd_HH");
105 
106         long MILLIS_IN_HOUR = 3600 * 1000;
107 
108         for (int p = 100; p > -100; p--) {
109             long now = 1223325293589L; // Mon Oct 06 22:34:53 CEST 2008
110             Instant result = rc.getEndOfNextNthPeriod(Instant.ofEpochMilli(now), p);
111             long expected = now - (now % (MILLIS_IN_HOUR)) + p * MILLIS_IN_HOUR;
112             Assertions.assertEquals(expected, result.toEpochMilli());
113         }
114     }
115 
116     @Test
117     public void testVaryingNumberOfDailyPeriods() {
118         RollingCalendar rc = new RollingCalendar("yyyy-MM-dd");
119         final long MILLIS_IN_DAY = 24 * 3600 * 1000;
120 
121         for (int p = 20; p > -100; p--) {
122             long now = 1223325293589L; // Mon Oct 06 22:34:53 CEST 2008
123             Instant nowInstant = Instant.ofEpochMilli(now);
124             Instant result = rc.getEndOfNextNthPeriod(nowInstant, p);
125             long offset = rc.getTimeZone().getRawOffset() + rc.getTimeZone().getDSTSavings();
126 
127             long origin = now - ((now + offset) % (MILLIS_IN_DAY));
128             long expected = origin + p * MILLIS_IN_DAY;
129             Assertions.assertEquals(expected, result.toEpochMilli(), "p=" + p);
130         }
131     }
132 
133     // Wed Mar 23 23:07:05 CET 2016
134     final long WED_2016_03_23_T_230705_CET = 1458770825333L;
135 
136     @Test
137     public void testBarrierCrossingComputation() {
138         checkPeriodBarriersCrossed("yyyy-MM-dd'T'HHmmss", WED_2016_03_23_T_230705_CET,
139                 WED_2016_03_23_T_230705_CET + 3 * CoreConstants.MILLIS_IN_ONE_SECOND, 3);
140         checkPeriodBarriersCrossed("yyyy-MM-dd'T'HHmm", WED_2016_03_23_T_230705_CET,
141                 WED_2016_03_23_T_230705_CET + 3 * CoreConstants.MILLIS_IN_ONE_MINUTE, 3);
142         checkPeriodBarriersCrossed("yyyy-MM-dd'T'HH", WED_2016_03_23_T_230705_CET,
143                 WED_2016_03_23_T_230705_CET + 3 * CoreConstants.MILLIS_IN_ONE_HOUR, 3);
144         checkPeriodBarriersCrossed("yyyy-MM-dd", WED_2016_03_23_T_230705_CET,
145                 WED_2016_03_23_T_230705_CET + 3 * CoreConstants.MILLIS_IN_ONE_DAY, 3);
146     }
147 
148     private void checkPeriodBarriersCrossed(String pattern, long start, long end, int count) {
149         RollingCalendar rc = new RollingCalendar(pattern);
150         Assertions.assertEquals(count, rc.periodBarriersCrossed(start, end));
151     }
152 
153     @Test
154     public void testCollisionFreenes() {
155         // hourly
156         checkCollisionFreeness("yyyy-MM-dd hh", false);
157         checkCollisionFreeness("yyyy-MM-dd hh a", true);
158 
159         checkCollisionFreeness("yyyy-MM-dd HH", true);
160         checkCollisionFreeness("yyyy-MM-dd kk", true);
161 
162         checkCollisionFreeness("yyyy-MM-dd KK", false);
163         checkCollisionFreeness("yyyy-MM-dd KK a", true);
164 
165         // daily
166         checkCollisionFreeness("yyyy-MM-dd", true);
167         checkCollisionFreeness("yyyy-dd", false);
168         checkCollisionFreeness("dd", false);
169         checkCollisionFreeness("MM-dd", false);
170 
171         checkCollisionFreeness("yyyy-DDD", true);
172         checkCollisionFreeness("DDD", false);
173 
174         // 'u' is new to JDK 7
175 //        if (EnvUtil.isJDK7OrHigher()) {
176 //            checkCollisionFreeness("yyyy-MM-dd-uu", true);
177 //            checkCollisionFreeness("yyyy-MM-uu", false);
178 //        }
179 
180         // weekly
181         checkCollisionFreeness("yyyy-MM-W", true);
182         dumpCurrentLocale(Locale.getDefault());
183         checkCollisionFreeness("yyyy-W", false);
184         checkCollisionFreeness("yyyy-ww", true);
185         checkCollisionFreeness("ww", false);
186     }
187 
188     private void dumpCurrentLocale(Locale locale) {
189         System.out.println("***Current default locale is " + locale);
190 
191     }
192 
193     private void checkCollisionFreeness(String pattern, boolean expected) {
194         RollingCalendar rc = new RollingCalendar(pattern);
195         if (expected) {
196             Assertions.assertTrue(rc.isCollisionFree());
197         } else {
198             Assertions.assertFalse(rc.isCollisionFree());
199         }
200     }
201 
202     @Test
203     public void basicPeriodBarriersCrossed() {
204         RollingCalendar rc = new RollingCalendar(dailyPattern, TimeZone.getTimeZone("CET"), Locale.US);
205         // Thu Jan 26 19:46:58 CET 2017, GMT offset = -1h
206         long start = 1485456418969L;
207         // Fri Jan 27 19:46:58 CET 2017, GMT offset = -1h
208         long end = start + CoreConstants.MILLIS_IN_ONE_DAY;
209         Assertions.assertEquals(1, rc.periodBarriersCrossed(start, end));
210     }
211 
212     @Test
213     public void testPeriodBarriersCrossedWhenGoingIntoDaylightSaving() {
214         RollingCalendar rc = new RollingCalendar(dailyPattern, TimeZone.getTimeZone("CET"), Locale.US);
215         // Sun Mar 26 00:02:03 CET 2017, GMT offset = -1h
216         long start = 1490482923333L;
217         // Mon Mar 27 00:02:03 CEST 2017, GMT offset = -2h
218         long end = 1490565723333L;
219 
220         Assertions.assertEquals(1, rc.periodBarriersCrossed(start, end));
221     }
222 
223     @Test
224     public void testPeriodBarriersCrossedWhenLeavingDaylightSaving() {
225         RollingCalendar rc = new RollingCalendar(dailyPattern, TimeZone.getTimeZone("CET"), Locale.US);
226         // Sun Oct 29 00:02:03 CEST 2017, GMT offset = -2h
227         long start = 1509228123333L;// 1490482923333L+217*CoreConstants.MILLIS_IN_ONE_DAY-CoreConstants.MILLIS_IN_ONE_HOUR;
228         // Mon Oct 30 00:02:03 CET 2017, GMT offset = -1h
229         long end = 1509228123333L + 25 * CoreConstants.MILLIS_IN_ONE_HOUR;
230         Assertions.assertEquals(1, rc.periodBarriersCrossed(start, end));
231     }
232 
233     @Test
234     public void testPeriodBarriersCrossedJustBeforeEnteringDaylightSaving() {
235         RollingCalendar rc = new RollingCalendar(dailyPattern, TimeZone.getTimeZone("CET"), Locale.US);
236         // Sun Mar 26 22:18:38 CEST 2017, GMT offset = +2h
237         long start = 1490559518333L;
238         System.out.println(new Date(start));
239 
240         // Mon Mar 27 00:05:18 CEST 2017, GMT offset = +2h
241         long end = 1490565918333L;
242         System.out.println(new Date(end));
243         Assertions.assertEquals(1, rc.periodBarriersCrossed(start, end));
244 
245     }
246 }