|
13 | 13 |
|
14 | 14 | package com.cronutils.converter;
|
15 | 15 |
|
| 16 | +import org.junit.jupiter.api.Test; |
16 | 17 | import org.junit.jupiter.params.ParameterizedTest;
|
17 | 18 | import org.junit.jupiter.params.provider.Arguments;
|
18 | 19 | import org.junit.jupiter.params.provider.MethodSource;
|
19 | 20 |
|
20 | 21 | import java.time.ZoneId;
|
| 22 | +import java.util.Calendar; |
| 23 | +import java.util.GregorianCalendar; |
| 24 | +import java.util.TimeZone; |
| 25 | +import java.util.function.Function; |
21 | 26 | import java.util.stream.Stream;
|
22 | 27 |
|
23 | 28 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
24 |
| -import static org.mockito.Mockito.spy; |
25 | 29 |
|
26 | 30 | public class CronConverterTest {
|
27 |
| - private CronConverter cronConverter = spy(new CronConverter( |
28 |
| - new CronToCalendarTransformer(), |
29 |
| - new CalendarToCronTransformer() |
30 |
| - )); |
31 |
| - |
32 |
| - public static Stream<Arguments> cronExpressions() { |
33 |
| - return Stream.of(Arguments.of("Pacific/Pago_Pago", "15 * * * *", "15 * * * *"), |
34 |
| - Arguments.of("Antarctica/Casey", "? * * * *", "? * * * *"), |
35 |
| - Arguments.of("Antarctica/Troll", "45 * * * *", "45 * * * *"), |
36 |
| - Arguments.of("Pacific/Chatham", "15 * * * *", "30 * * * *"), |
37 |
| - Arguments.of("Asia/Colombo", "45 * * ? *", "15 * * ? *"), |
38 |
| - Arguments.of("Asia/Colombo", "0/45 * * ? *", "0/45 * * ? *"), |
39 |
| - Arguments.of("Australia/Eucla", "13 * * ? *", "28 * * ? *"), |
40 |
| - Arguments.of("America/St_Johns", "0 0/15 * * * ?", "30 0/15 * * * ?"), |
41 |
| - Arguments.of("America/St_Johns", "0 8 * * ?", "30 10 * * ?"), |
42 |
| - Arguments.of("America/St_Johns", "0 0/1 * * ?", "30 0/1 * * ?"), |
43 |
| - Arguments.of("America/St_Johns", "20 0 * * ?", "50 2 * * ?"), |
44 |
| - Arguments.of("Asia/Kolkata", "20 0 * * ?", "50 18 * * ?") |
45 |
| - ); |
46 |
| - } |
47 |
| - |
48 |
| - @ParameterizedTest |
49 |
| - @MethodSource("cronExpressions") |
50 |
| - public void testCronConverterBuilder(String timezone, String inputCronExpression, String expectedCronExpression) { |
51 |
| - assertEquals(expectedCronExpression, |
52 |
| - cronConverter.using(inputCronExpression) |
53 |
| - .from(ZoneId.of(timezone)).to(ZoneId.of("UTC")) |
54 |
| - .convert()); |
55 |
| - } |
| 31 | + |
| 32 | + // Fix the date to prevent test failure during the transition between Standard Time and Daylight Time. |
| 33 | + Function<ZoneId, Calendar> calendarFactory = (zoneId) -> { |
| 34 | + Calendar fixedDay = new GregorianCalendar(2020, 06, 01); |
| 35 | + fixedDay.setTimeZone(TimeZone.getTimeZone(zoneId)); |
| 36 | + return fixedDay; |
| 37 | + }; |
| 38 | + |
| 39 | + private CronConverter cronConverter = new CronConverter( |
| 40 | + new CronToCalendarTransformer(), |
| 41 | + new CalendarToCronTransformer(), |
| 42 | + calendarFactory |
| 43 | + ); |
| 44 | + |
| 45 | + public static Stream<Arguments> cronExpressions() { |
| 46 | + return Stream.of(Arguments.of("Pacific/Pago_Pago", "15 * * * *", "15 * * * *"), |
| 47 | + Arguments.of("Antarctica/Casey", "? * * * *", "? * * * *"), |
| 48 | + Arguments.of("Antarctica/Troll", "45 * * * *", "45 * * * *"), |
| 49 | + Arguments.of("Pacific/Chatham", "15 * * * *", "30 * * * *"), |
| 50 | + Arguments.of("Asia/Colombo", "45 * * ? *", "15 * * ? *"), |
| 51 | + Arguments.of("Asia/Colombo", "0/45 * * ? *", "0/45 * * ? *"), |
| 52 | + Arguments.of("Australia/Eucla", "13 * * ? *", "28 * * ? *"), |
| 53 | + Arguments.of("America/St_Johns", "0 0/15 * * * ?", "30 0/15 * * * ?"), |
| 54 | + Arguments.of("America/St_Johns", "0 8 * * ?", "30 10 * * ?"), |
| 55 | + Arguments.of("America/St_Johns", "0 0/1 * * ?", "30 0/1 * * ?"), |
| 56 | + Arguments.of("America/St_Johns", "20 0 * * ?", "50 2 * * ?"), |
| 57 | + Arguments.of("Asia/Kolkata", "20 0 * * ?", "50 18 * * ?") |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @MethodSource("cronExpressions") |
| 63 | + public void testCronConverterBuilder(String timezone, String inputCronExpression, String expectedCronExpression) { |
| 64 | + assertEquals(expectedCronExpression, |
| 65 | + cronConverter.using(inputCronExpression) |
| 66 | + .from(ZoneId.of(timezone)).to(ZoneId.of("UTC")) |
| 67 | + .convert()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testCronExpressionsDefaultTimeFactory() { |
| 72 | + CronConverter defaultCronConverter = new CronConverter(new CronToCalendarTransformer(), new CalendarToCronTransformer()); |
| 73 | + // No Daylight Time @ Kolkata |
| 74 | + assertEquals("50 18 * * ?", |
| 75 | + defaultCronConverter.using("20 0 * * ?") |
| 76 | + .from(ZoneId.of("Asia/Kolkata")).to(ZoneId.of("UTC")) |
| 77 | + .convert()); |
| 78 | + } |
56 | 79 | }
|
0 commit comments