|
| 1 | +package com.cronutils; |
| 2 | + |
| 3 | +import com.cronutils.builder.CronBuilder; |
| 4 | +import com.cronutils.model.Cron; |
| 5 | +import com.cronutils.model.definition.CronConstraintsFactory; |
| 6 | +import com.cronutils.model.definition.CronDefinition; |
| 7 | +import com.cronutils.model.definition.CronDefinitionBuilder; |
| 8 | +import com.cronutils.model.time.ExecutionTime; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Ignore; |
| 11 | +import org.junit.Test; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import java.time.*; |
| 16 | +import java.time.temporal.ChronoUnit; |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +import static com.cronutils.model.field.expression.FieldExpression.questionMark; |
| 20 | +import static com.cronutils.model.field.expression.FieldExpressionFactory.always; |
| 21 | +import static com.cronutils.model.field.expression.FieldExpressionFactory.on; |
| 22 | +import static junit.framework.Assert.assertTrue; |
| 23 | +import static junit.framework.TestCase.fail; |
| 24 | + |
| 25 | +public class Issue480Test { |
| 26 | + |
| 27 | + private static final Logger LOGGER = LoggerFactory.getLogger(Issue480Test.class); |
| 28 | + |
| 29 | + private static final CronDefinition definition = CronDefinitionBuilder.defineCron() |
| 30 | + .withMinutes().and() |
| 31 | + .withHours().and() |
| 32 | + .withDayOfWeek().supportsQuestionMark().and() |
| 33 | + .withDayOfMonth().supportsL().supportsQuestionMark().and() |
| 34 | + .withDayOfYear().supportsQuestionMark().and() |
| 35 | + .withMonth().and() |
| 36 | + .withYear().optional().withValidRange(1970, 2099).and() |
| 37 | + .matchDayOfWeekAndDayOfMonth() |
| 38 | + .withCronValidation(CronConstraintsFactory.ensureEitherDayOfWeekOrDayOfMonth()) |
| 39 | + .withCronValidation(CronConstraintsFactory.ensureEitherDayOfYearOrMonth()) |
| 40 | + .instance(); |
| 41 | + |
| 42 | + @Test |
| 43 | + @Ignore // TODO broken?? |
| 44 | + public void testIntervalsEvery5thMonthsSinceASpecificMonth() { |
| 45 | + LocalDateTime sunday = LocalDateTime.of(2021, 6, 27, 0, 0); |
| 46 | + Assert.assertEquals(sunday.getDayOfWeek(), DayOfWeek.SUNDAY); |
| 47 | + Clock clock = Clock.fixed(sunday.toInstant(ZoneOffset.UTC), ZoneId.systemDefault()); |
| 48 | + ZonedDateTime now = ZonedDateTime.now(clock); |
| 49 | + LOGGER.info("Now: {}", now); |
| 50 | + |
| 51 | + Cron cron = getWeekly(now).instance(); |
| 52 | + ZonedDateTime nextRun; |
| 53 | + |
| 54 | + final ZonedDateTime nowPlusWeek = now.plusWeeks(1); |
| 55 | + LOGGER.info("now + 1 week: {}", nowPlusWeek); |
| 56 | + |
| 57 | + nextRun = nextRun(cron, now); // first run |
| 58 | + LOGGER.info("nextRun: {}", nextRun); |
| 59 | + |
| 60 | + assertTrue(nextRun.truncatedTo(ChronoUnit.MINUTES) |
| 61 | + .isEqual(nowPlusWeek.truncatedTo(ChronoUnit.MINUTES))); |
| 62 | + } |
| 63 | + |
| 64 | + private CronBuilder getWeekly(ZonedDateTime now) { |
| 65 | + return CronBuilder.cron(definition) |
| 66 | + .withMinute(on(now.getMinute())) |
| 67 | + .withHour(on(now.getHour())) |
| 68 | + .withDoW(on(now.getDayOfWeek().ordinal())) // ordinal -- 0 to 6 |
| 69 | +// .withDoW(on(now.getDayOfWeek().getValue())) // value -- 1 to 7 |
| 70 | + .withDoM(questionMark()) |
| 71 | + .withDoY(questionMark()) |
| 72 | + .withMonth(always()) |
| 73 | + .withYear(always()); |
| 74 | + } |
| 75 | + |
| 76 | + private static ZonedDateTime nextRun(Cron cron, ZonedDateTime when) { |
| 77 | + final Optional<ZonedDateTime> next = ExecutionTime.forCron(cron).nextExecution(when); |
| 78 | + if (!next.isPresent()) { |
| 79 | + fail(); |
| 80 | + } |
| 81 | + LOGGER.info("Calculated next run at {}", next.get()); |
| 82 | + return next.get(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments