|
| 1 | +package com.cronutils; |
| 2 | + |
| 3 | +import com.cronutils.model.CronType; |
| 4 | +import com.cronutils.model.definition.CronDefinition; |
| 5 | +import com.cronutils.model.definition.CronDefinitionBuilder; |
| 6 | +import com.cronutils.model.time.ExecutionTime; |
| 7 | +import com.cronutils.parser.CronParser; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.time.LocalDate; |
| 11 | +import java.time.LocalTime; |
| 12 | +import java.time.ZoneId; |
| 13 | +import java.time.ZonedDateTime; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.fail; |
| 18 | + |
| 19 | +public class Issue418Test { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testQuartzEvery7DaysStartingSunday() { |
| 23 | + final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); |
| 24 | + final CronParser parser = new CronParser(cronDefinition); |
| 25 | + final ExecutionTime execTime = ExecutionTime.forCron(parser.parse("0 0 2 ? * 1/7 *")); |
| 26 | + |
| 27 | + final ZonedDateTime startDate = ZonedDateTime.of( |
| 28 | + LocalDate.of(2020, 4, 1), |
| 29 | + LocalTime.of(3, 0), |
| 30 | + ZoneId.systemDefault() |
| 31 | + ); |
| 32 | + final ZonedDateTime[] expectedDates = { |
| 33 | + ZonedDateTime.of( |
| 34 | + LocalDate.of(2020, 4, 5), |
| 35 | + LocalTime.of(2, 0), |
| 36 | + ZoneId.systemDefault() |
| 37 | + ), |
| 38 | + ZonedDateTime.of( |
| 39 | + LocalDate.of(2020, 4, 12), |
| 40 | + LocalTime.of(2, 0), |
| 41 | + ZoneId.systemDefault() |
| 42 | + ) |
| 43 | + }; |
| 44 | + |
| 45 | + Optional<ZonedDateTime> nextExecution = execTime.nextExecution(startDate); |
| 46 | + assert(nextExecution.isPresent()); |
| 47 | + assertEquals( expectedDates[0], nextExecution.get()); |
| 48 | + |
| 49 | + nextExecution = execTime.nextExecution(nextExecution.get()); |
| 50 | + assert(nextExecution.isPresent()); |
| 51 | + assertEquals( expectedDates[1], nextExecution.get()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void TestInvalidWeekDayStart() { |
| 56 | + try { |
| 57 | + final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); |
| 58 | + final CronParser parser = new CronParser(cronDefinition); |
| 59 | + parser.parse("0 0 2 ? * 0/7 *"); |
| 60 | + fail("Expected exception for invalid expression"); |
| 61 | + } catch (IllegalArgumentException expected) { |
| 62 | + assertEquals("Failed to parse '0 0 2 ? * 0/7 *'. Value 0 not in range [1, 7]", expected.getMessage()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void TestInvalidWeekDayEnd() { |
| 68 | + try { |
| 69 | + final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); |
| 70 | + final CronParser parser = new CronParser(cronDefinition); |
| 71 | + parser.parse("0 0 2 ? * 1/8 *"); |
| 72 | + fail("Expected exception for invalid expression"); |
| 73 | + } catch (IllegalArgumentException expected) { |
| 74 | + assertEquals("Failed to parse '0 0 2 ? * 1/8 *'. Period 8 not in range [1, 7]", expected.getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments