|
| 1 | +import com.cronutils.builder.CronBuilder; |
| 2 | +import com.cronutils.descriptor.CronDescriptor; |
| 3 | +import com.cronutils.model.Cron; |
| 4 | +import com.cronutils.model.definition.CronConstraintsFactory; |
| 5 | +import com.cronutils.model.definition.CronDefinition; |
| 6 | +import com.cronutils.model.definition.CronDefinitionBuilder; |
| 7 | +import com.cronutils.model.time.ExecutionTime; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.time.*; |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +import static com.cronutils.model.field.expression.FieldExpression.questionMark; |
| 16 | +import static com.cronutils.model.field.expression.FieldExpressionFactory.every; |
| 17 | +import static com.cronutils.model.field.expression.FieldExpressionFactory.on; |
| 18 | +import static junit.framework.TestCase.fail; |
| 19 | + |
| 20 | +public class Issue421Test { |
| 21 | + |
| 22 | + private static final CronDefinition definition = CronDefinitionBuilder.defineCron() |
| 23 | + .withMinutes().and() |
| 24 | + .withHours().and() |
| 25 | + .withDayOfWeek().supportsQuestionMark().and() |
| 26 | + .withDayOfMonth().supportsL().supportsQuestionMark().and() |
| 27 | + .withDayOfYear().supportsQuestionMark().and() |
| 28 | + .withMonth().and() |
| 29 | + .matchDayOfWeekAndDayOfMonth() |
| 30 | + .withCronValidation(CronConstraintsFactory.ensureEitherDayOfWeekOrDayOfMonth()) |
| 31 | + .withCronValidation(CronConstraintsFactory.ensureEitherDayOfYearOrMonth()) |
| 32 | + .instance(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testWrongIntervalsForEvery6Months() { |
| 36 | + LocalDateTime firstOfJanuary = LocalDateTime.of(2020, 4, 25, 0, 0); |
| 37 | + Clock clock = Clock.fixed(firstOfJanuary.toInstant(ZoneOffset.UTC), ZoneId.systemDefault()); |
| 38 | + ZonedDateTime now = ZonedDateTime.now(clock); |
| 39 | + System.out.println("now: " + now); |
| 40 | + |
| 41 | + Cron cron = getEveryMonth(now, 6).instance(); |
| 42 | + ZonedDateTime nextRun; |
| 43 | + |
| 44 | + nextRun = nextRun(cron, now); // first run |
| 45 | + Assert.assertEquals(2020, nextRun.getYear()); |
| 46 | + Assert.assertEquals(10, nextRun.getMonthValue()); |
| 47 | + |
| 48 | + nextRun = nextRun(cron, nextRun); // second |
| 49 | + Assert.assertEquals(2021, nextRun.getYear()); |
| 50 | + Assert.assertEquals(4, nextRun.getMonthValue()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testWrongEveryXMonthsDescription() { |
| 55 | + ZonedDateTime now = ZonedDateTime.now(); |
| 56 | + |
| 57 | + String description = CronDescriptor.instance(Locale.US).describe(getEveryMonth(now, 3).instance()); |
| 58 | + System.out.println(description); |
| 59 | + Assert.assertTrue(description.contains("every 3 months")); |
| 60 | + |
| 61 | + description = CronDescriptor.instance(Locale.US).describe(getEveryMonth(now, 6).instance()); |
| 62 | + System.out.println(description); |
| 63 | + Assert.assertTrue(description.contains("every 6 months")); |
| 64 | + } |
| 65 | + |
| 66 | + public static CronBuilder getEveryMonth(ZonedDateTime now, int every) { |
| 67 | + return CronBuilder.cron(definition) |
| 68 | + .withMinute(on(now.getMinute())) |
| 69 | + .withHour(on(now.getHour())) |
| 70 | + .withDoW(questionMark()) |
| 71 | + .withDoM(on(now.getDayOfMonth())) |
| 72 | + .withDoY(questionMark()) |
| 73 | + .withMonth(every(every)); |
| 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 | + System.out.println("Calculated next run at " + next.get()); |
| 82 | + return next.get(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments