|
| 1 | +package com.cronutils; |
| 2 | + |
| 3 | +import com.cronutils.model.definition.CronDefinition; |
| 4 | +import com.cronutils.model.definition.CronDefinitionBuilder; |
| 5 | +import com.cronutils.model.time.ExecutionTime; |
| 6 | +import com.cronutils.parser.CronParser; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.time.DayOfWeek; |
| 10 | +import java.time.ZonedDateTime; |
| 11 | +import java.util.Optional; |
| 12 | + |
| 13 | +import static com.cronutils.model.CronType.SPRING; |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +public class Issue394Test { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testEveryMondayAt0900hours() { |
| 21 | + String cron = "0 0 9 * * MON"; |
| 22 | + |
| 23 | + CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(SPRING); |
| 24 | + CronParser parser = new CronParser(cronDefinition); |
| 25 | + ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(cron)); |
| 26 | + ZonedDateTime now = ZonedDateTime.now(); |
| 27 | + Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(now); |
| 28 | + assertTrue(nextExecution.isPresent()); |
| 29 | + // Should be the next Monday at 0900 hours |
| 30 | + assertEquals(DayOfWeek.MONDAY, nextExecution.get().getDayOfWeek()); |
| 31 | + assertEquals(9, nextExecution.get().getHour()); |
| 32 | + // The next execution after that should also be a Monday at 0900 hours, the following week |
| 33 | + ZonedDateTime nextExpectedExecution = nextExecution.get().plusWeeks(1); |
| 34 | + nextExecution = executionTime.nextExecution(nextExecution.get()); |
| 35 | + assertTrue(nextExecution.isPresent()); |
| 36 | + assertEquals(DayOfWeek.MONDAY, nextExecution.get().getDayOfWeek()); |
| 37 | + assertEquals(9, nextExecution.get().getHour()); |
| 38 | + assertEquals(nextExpectedExecution, nextExecution.get()); |
| 39 | + } |
| 40 | +} |
0 commit comments