1
2
3
4
5
6
7
8
9
10
11
12
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
37
38
39
40
41
42
43
44
45
46
47
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;
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;
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
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
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
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
175
176
177
178
179
180
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
206 long start = 1485456418969L;
207
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
216 long start = 1490482923333L;
217
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
227 long start = 1509228123333L;
228
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
237 long start = 1490559518333L;
238 System.out.println(new Date(start));
239
240
241 long end = 1490565918333L;
242 System.out.println(new Date(end));
243 Assertions.assertEquals(1, rc.periodBarriersCrossed(start, end));
244
245 }
246 }