Skip to content

Commit

Permalink
Add test for issue #446
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrozanec committed Oct 6, 2020
1 parent e46c94d commit b3ae6da
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
19 changes: 1 addition & 18 deletions src/test/java/com/cronutils/Issue421Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,6 @@ public class Issue421Test {
.withCronValidation(CronConstraintsFactory.ensureEitherDayOfYearOrMonth())
.instance();

@Test
public void testWrongIntervalsForEvery6Months() {
LocalDateTime dayOfApril = LocalDateTime.of(2020, 4, 25, 0, 0);
Clock clock = Clock.fixed(dayOfApril.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
ZonedDateTime dayOfAprilInLocalTimezone = ZonedDateTime.now(clock);
System.out.println("now: " + dayOfAprilInLocalTimezone);
Cron cron = getEveryMonthFromNow(dayOfAprilInLocalTimezone, 6).instance();

ZonedDateTime nextRun = nextRun(cron, dayOfAprilInLocalTimezone); // first run
Assert.assertEquals(2020, nextRun.getYear());
Assert.assertEquals(10, nextRun.getMonthValue());

nextRun = nextRun(cron, nextRun); // second
System.out.println(nextRun);
Assert.assertEquals(2021, nextRun.getYear());
Assert.assertEquals(4, nextRun.getMonthValue());
}

@Test
public void testIntervalsEvery5thMonthsSinceASpecificMonth() {
LocalDateTime firstOfJanuary = LocalDateTime.of(2020, 2, 10, 0, 0);
Expand Down Expand Up @@ -120,6 +102,7 @@ public static CronBuilder getEveryMonth(ZonedDateTime now, int every) {
.withDoY(questionMark())
.withMonth(every(every));
}

public static CronBuilder getEveryMonthFromNow(ZonedDateTime now, int every) {
return CronBuilder.cron(definition)
.withMinute(on(now.getMinute()))
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/com/cronutils/Issue446Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.cronutils;

import com.cronutils.builder.CronBuilder;
import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronConstraintsFactory;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.time.*;
import java.util.Optional;

import static com.cronutils.model.field.expression.FieldExpression.questionMark;
import static com.cronutils.model.field.expression.FieldExpressionFactory.every;
import static com.cronutils.model.field.expression.FieldExpressionFactory.on;
import static junit.framework.TestCase.fail;

@Ignore
public class Issue446Test {
private static final CronDefinition definition = CronDefinitionBuilder.defineCron()
.withMinutes().and()
.withHours().and()
.withDayOfWeek().supportsQuestionMark().and()
.withDayOfMonth().supportsL().supportsQuestionMark().and()
.withDayOfYear().supportsQuestionMark().and()
.withMonth().and()
.matchDayOfWeekAndDayOfMonth()
.withCronValidation(CronConstraintsFactory.ensureEitherDayOfWeekOrDayOfMonth())
.withCronValidation(CronConstraintsFactory.ensureEitherDayOfYearOrMonth())
.instance();

@Test
public void testWrongIntervalsForEvery6Months() {
LocalDateTime dayOfApril = LocalDateTime.of(2020, 4, 25, 0, 0);
Clock clock = Clock.fixed(dayOfApril.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
ZonedDateTime dayOfAprilInLocalTimezone = ZonedDateTime.now(clock);
System.out.println("now: " + dayOfAprilInLocalTimezone);
Cron cron = getEveryMonthFromNow(dayOfAprilInLocalTimezone, 6).instance();

ZonedDateTime nextRun = nextRun(cron, dayOfAprilInLocalTimezone); // first run
Assert.assertEquals(2020, nextRun.getYear());
Assert.assertEquals(10, nextRun.getMonthValue());

nextRun = nextRun(cron, nextRun); // second
System.out.println(nextRun);
Assert.assertEquals(2021, nextRun.getYear());
Assert.assertEquals(4, nextRun.getMonthValue());
}

public static CronBuilder getEveryMonthFromNow(ZonedDateTime now, int every) {
return CronBuilder.cron(definition)
.withMinute(on(now.getMinute()))
.withHour(on(now.getHour()))
.withDoW(questionMark())
.withDoM(on(now.getDayOfMonth()))
.withDoY(questionMark())
.withMonth(every(on(now.getMonthValue()), every));
}

private static ZonedDateTime nextRun(Cron cron, ZonedDateTime when) {
final Optional<ZonedDateTime> next = ExecutionTime.forCron(cron).nextExecution(when);
if (!next.isPresent()) {
fail();
}
System.out.println("Calculated next run at " + next.get());
return next.get();
}
}

0 comments on commit b3ae6da

Please sign in to comment.