|
44 | 44 | import static com.cronutils.model.CronType.QUARTZ; |
45 | 45 | import static java.time.ZoneOffset.UTC; |
46 | 46 | import static org.junit.Assert.assertEquals; |
| 47 | +import static org.junit.Assert.assertFalse; |
47 | 48 | import static org.junit.Assert.assertNotEquals; |
48 | 49 | import static org.junit.Assert.assertNotNull; |
49 | 50 | import static org.junit.Assert.assertTrue; |
@@ -842,6 +843,49 @@ public void testLastExecutionIssue312() { |
842 | 843 | } |
843 | 844 | } |
844 | 845 |
|
| 846 | + /** |
| 847 | + * Issue #402 |
| 848 | + * https://github.com/jmrozanec/cron-utils/issues/402 |
| 849 | + * Fix last and next execution time when using every X years |
| 850 | + */ |
| 851 | + @Test |
| 852 | + public void testLastExecutionIssue402() { |
| 853 | + // Every 2 years at March 1st midnight |
| 854 | + ExecutionTime execution = ExecutionTime.forCron(parser.parse("0 0 0 1 3 ? 2001-2020/2")); |
| 855 | + |
| 856 | + ZonedDateTime currentDateTime = ZonedDateTime.of(LocalDate.of(2015, 1, 15), LocalTime.MIDNIGHT, ZoneOffset.UTC); |
| 857 | + // Check next execution time is correct |
| 858 | + Optional<ZonedDateTime> nextExecution = execution.nextExecution(currentDateTime); |
| 859 | + assertTrue(nextExecution.isPresent()); |
| 860 | + assertEquals(ZonedDateTime.of(LocalDate.of(2015, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), nextExecution.get()); |
| 861 | + // Check previous execution time is correct |
| 862 | + Optional<ZonedDateTime> lastExecution = execution.lastExecution(currentDateTime); |
| 863 | + assertTrue(lastExecution.isPresent()); |
| 864 | + assertEquals(ZonedDateTime.of(LocalDate.of(2013, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), lastExecution.get()); |
| 865 | + |
| 866 | + lastExecution = execution.lastExecution(nextExecution.get()); |
| 867 | + assertTrue(lastExecution.isPresent()); |
| 868 | + assertEquals(ZonedDateTime.of(LocalDate.of(2013, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), lastExecution.get()); |
| 869 | + |
| 870 | + // Assume the current time is before the start time of every expression, check last and next execution.ß |
| 871 | + ZonedDateTime timeBeforeStart = ZonedDateTime.of(LocalDate.of(1996, 1, 15), LocalTime.MIDNIGHT, ZoneOffset.UTC); |
| 872 | + Optional<ZonedDateTime> nextBeforeStart = execution.nextExecution(timeBeforeStart); |
| 873 | + assertTrue(nextBeforeStart.isPresent()); |
| 874 | + assertEquals(ZonedDateTime.of(LocalDate.of(2001, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), nextBeforeStart.get()); |
| 875 | + |
| 876 | + Optional<ZonedDateTime> lastBeforeStart = execution.lastExecution(nextBeforeStart.get()); |
| 877 | + assertFalse(lastBeforeStart.isPresent()); |
| 878 | + |
| 879 | + // Assume the current time is before the start time of every expression, check last and next execution.ß |
| 880 | + ZonedDateTime timeAfterEnd = ZonedDateTime.of(LocalDate.of(2025, 1, 15), LocalTime.MIDNIGHT, ZoneOffset.UTC); |
| 881 | + Optional<ZonedDateTime> nextAfterEnd = execution.nextExecution(timeAfterEnd); |
| 882 | + assertFalse(nextAfterEnd.isPresent()); |
| 883 | + |
| 884 | + Optional<ZonedDateTime> lastAfterEnd = execution.lastExecution(timeAfterEnd); |
| 885 | + assertTrue(lastAfterEnd.isPresent()); |
| 886 | + assertEquals(ZonedDateTime.of(LocalDate.of(2019, 3, 1), LocalTime.MIDNIGHT, ZoneOffset.UTC), lastAfterEnd.get()); |
| 887 | + } |
| 888 | + |
845 | 889 | /** |
846 | 890 | * Issue #424 |
847 | 891 | * https://github.com/jmrozanec/cron-utils/issues/424 |
|
0 commit comments