18
18
import java .util .Locale ;
19
19
import java .util .TimeZone ;
20
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 ;
21
+ import org .junit .jupiter .api .*;
25
22
26
23
import ch .qos .logback .core .CoreConstants ;
27
24
import ch .qos .logback .core .util .EnvUtil ;
28
25
26
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
27
+
29
28
public class RollingCalendarTest {
30
29
31
30
String dailyPattern = "yyyy-MM-dd" ;
32
31
32
+
33
33
@ BeforeEach
34
34
public void setUp () {
35
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.
36
+ // Due to fist day of week differences, tests may fail
37
+ // certain locales, namely GB.
39
38
//
40
39
// These tests are:
41
40
//
@@ -47,55 +46,58 @@ public void setUp() {
47
46
// assertEquals(PeriodicityType.TOP_OF_WEEK, rc.getPeriodicityType());
48
47
// }
49
48
//
49
+ }
50
50
51
- Locale oldLocale = Locale .getDefault ();
52
- Locale .setDefault (oldLocale );
51
+ void set_EN_US_Locale () {
52
+ Locale usEn_Locale = Locale .forLanguageTag ("en-US" );
53
+ Locale .setDefault (usEn_Locale );
53
54
}
54
55
55
56
@ AfterEach
56
57
public void tearDown () {
58
+
57
59
}
58
60
59
61
@ Test
60
62
public void testPeriodicity () {
61
63
{
62
64
RollingCalendar rc = new RollingCalendar ("yyyy-MM-dd_HH_mm_ss" );
63
- Assertions . assertEquals (PeriodicityType .TOP_OF_SECOND , rc .getPeriodicityType ());
65
+ assertEquals (PeriodicityType .TOP_OF_SECOND , rc .getPeriodicityType ());
64
66
}
65
67
66
68
{
67
69
RollingCalendar rc = new RollingCalendar ("yyyy-MM-dd_HH_mm" );
68
- Assertions . assertEquals (PeriodicityType .TOP_OF_MINUTE , rc .getPeriodicityType ());
70
+ assertEquals (PeriodicityType .TOP_OF_MINUTE , rc .getPeriodicityType ());
69
71
}
70
72
71
73
{
72
74
RollingCalendar rc = new RollingCalendar ("yyyy-MM-dd_HH" );
73
- Assertions . assertEquals (PeriodicityType .TOP_OF_HOUR , rc .getPeriodicityType ());
75
+ assertEquals (PeriodicityType .TOP_OF_HOUR , rc .getPeriodicityType ());
74
76
}
75
77
76
78
{
77
79
RollingCalendar rc = new RollingCalendar ("yyyy-MM-dd_hh" );
78
- Assertions . assertEquals (PeriodicityType .TOP_OF_HOUR , rc .getPeriodicityType ());
80
+ assertEquals (PeriodicityType .TOP_OF_HOUR , rc .getPeriodicityType ());
79
81
}
80
82
81
83
{
82
84
RollingCalendar rc = new RollingCalendar ("yyyy-MM-dd" );
83
- Assertions . assertEquals (PeriodicityType .TOP_OF_DAY , rc .getPeriodicityType ());
85
+ assertEquals (PeriodicityType .TOP_OF_DAY , rc .getPeriodicityType ());
84
86
}
85
87
86
88
{
87
89
RollingCalendar rc = new RollingCalendar ("yyyy-MM" );
88
- Assertions . assertEquals (PeriodicityType .TOP_OF_MONTH , rc .getPeriodicityType ());
90
+ assertEquals (PeriodicityType .TOP_OF_MONTH , rc .getPeriodicityType ());
89
91
}
90
92
91
93
{
92
94
RollingCalendar rc = new RollingCalendar ("yyyy-ww" );
93
- Assertions . assertEquals (PeriodicityType .TOP_OF_WEEK , rc .getPeriodicityType ());
95
+ assertEquals (PeriodicityType .TOP_OF_WEEK , rc .getPeriodicityType ());
94
96
}
95
97
96
98
{
97
99
RollingCalendar rc = new RollingCalendar ("yyyy-W" );
98
- Assertions . assertEquals (PeriodicityType .TOP_OF_WEEK , rc .getPeriodicityType ());
100
+ assertEquals (PeriodicityType .TOP_OF_WEEK , rc .getPeriodicityType ());
99
101
}
100
102
}
101
103
@@ -109,7 +111,7 @@ public void testVaryingNumberOfHourlyPeriods() {
109
111
long now = 1223325293589L ; // Mon Oct 06 22:34:53 CEST 2008
110
112
Instant result = rc .getEndOfNextNthPeriod (Instant .ofEpochMilli (now ), p );
111
113
long expected = now - (now % (MILLIS_IN_HOUR )) + p * MILLIS_IN_HOUR ;
112
- Assertions . assertEquals (expected , result .toEpochMilli ());
114
+ assertEquals (expected , result .toEpochMilli ());
113
115
}
114
116
}
115
117
@@ -126,7 +128,7 @@ public void testVaryingNumberOfDailyPeriods() {
126
128
127
129
long origin = now - ((now + offset ) % (MILLIS_IN_DAY ));
128
130
long expected = origin + p * MILLIS_IN_DAY ;
129
- Assertions . assertEquals (expected , result .toEpochMilli (), "p=" + p );
131
+ assertEquals (expected , result .toEpochMilli (), "p=" + p );
130
132
}
131
133
}
132
134
@@ -147,7 +149,7 @@ public void testBarrierCrossingComputation() {
147
149
148
150
private void checkPeriodBarriersCrossed (String pattern , long start , long end , int count ) {
149
151
RollingCalendar rc = new RollingCalendar (pattern );
150
- Assertions . assertEquals (count , rc .periodBarriersCrossed (start , end ));
152
+ assertEquals (count , rc .periodBarriersCrossed (start , end ));
151
153
}
152
154
153
155
@ Test
@@ -177,12 +179,20 @@ public void testCollisionFreenes() {
177
179
// checkCollisionFreeness("yyyy-MM-uu", false);
178
180
// }
179
181
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 );
182
+
183
+ Locale oldLocale = Locale .getDefault ();
184
+ try {
185
+ set_EN_US_Locale ();
186
+ // weekly
187
+ checkCollisionFreeness ("yyyy-MM-W" , true );
188
+ dumpCurrentLocale (Locale .getDefault ());
189
+ checkCollisionFreeness ("yyyy-W" , false );
190
+ checkCollisionFreeness ("yyyy-ww" , true );
191
+ checkCollisionFreeness ("ww" , false );
192
+ } finally {
193
+ if (oldLocale != null )
194
+ Locale .setDefault (oldLocale );
195
+ }
186
196
}
187
197
188
198
private void dumpCurrentLocale (Locale locale ) {
@@ -192,11 +202,7 @@ private void dumpCurrentLocale(Locale locale) {
192
202
193
203
private void checkCollisionFreeness (String pattern , boolean expected ) {
194
204
RollingCalendar rc = new RollingCalendar (pattern );
195
- if (expected ) {
196
- Assertions .assertTrue (rc .isCollisionFree ());
197
- } else {
198
- Assertions .assertFalse (rc .isCollisionFree ());
199
- }
205
+ assertEquals (expected , rc .isCollisionFree ());
200
206
}
201
207
202
208
@ Test
@@ -206,7 +212,7 @@ public void basicPeriodBarriersCrossed() {
206
212
long start = 1485456418969L ;
207
213
// Fri Jan 27 19:46:58 CET 2017, GMT offset = -1h
208
214
long end = start + CoreConstants .MILLIS_IN_ONE_DAY ;
209
- Assertions . assertEquals (1 , rc .periodBarriersCrossed (start , end ));
215
+ assertEquals (1 , rc .periodBarriersCrossed (start , end ));
210
216
}
211
217
212
218
@ Test
@@ -217,7 +223,7 @@ public void testPeriodBarriersCrossedWhenGoingIntoDaylightSaving() {
217
223
// Mon Mar 27 00:02:03 CEST 2017, GMT offset = -2h
218
224
long end = 1490565723333L ;
219
225
220
- Assertions . assertEquals (1 , rc .periodBarriersCrossed (start , end ));
226
+ assertEquals (1 , rc .periodBarriersCrossed (start , end ));
221
227
}
222
228
223
229
@ Test
@@ -227,7 +233,7 @@ public void testPeriodBarriersCrossedWhenLeavingDaylightSaving() {
227
233
long start = 1509228123333L ;// 1490482923333L+217*CoreConstants.MILLIS_IN_ONE_DAY-CoreConstants.MILLIS_IN_ONE_HOUR;
228
234
// Mon Oct 30 00:02:03 CET 2017, GMT offset = -1h
229
235
long end = 1509228123333L + 25 * CoreConstants .MILLIS_IN_ONE_HOUR ;
230
- Assertions . assertEquals (1 , rc .periodBarriersCrossed (start , end ));
236
+ assertEquals (1 , rc .periodBarriersCrossed (start , end ));
231
237
}
232
238
233
239
@ Test
@@ -240,7 +246,7 @@ public void testPeriodBarriersCrossedJustBeforeEnteringDaylightSaving() {
240
246
// Mon Mar 27 00:05:18 CEST 2017, GMT offset = +2h
241
247
long end = 1490565918333L ;
242
248
System .out .println (new Date (end ));
243
- Assertions . assertEquals (1 , rc .periodBarriersCrossed (start , end ));
249
+ assertEquals (1 , rc .periodBarriersCrossed (start , end ));
244
250
245
251
}
246
252
}
0 commit comments