Skip to content

Commit 231d894

Browse files
committed
Merge branch 'fix_every_expression_behavior_#421' of https://github.com/Lifeindeath/cron-utils into Lifeindeath-fix_every_expression_behavior_#421
2 parents 0221ca5 + 8030462 commit 231d894

3 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/main/java/com/cronutils/descriptor/DescriptionStrategyFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package com.cronutils.descriptor;
1515

16+
import com.cronutils.model.field.expression.Every;
1617
import java.time.DayOfWeek;
1718
import java.time.Month;
1819
import java.time.format.TextStyle;
@@ -102,7 +103,13 @@ public static DescriptionStrategy daysOfMonthInstance(final ResourceBundle bundl
102103
* @return - DescriptionStrategy instance, never null
103104
*/
104105
public static DescriptionStrategy monthsInstance(final ResourceBundle bundle, final FieldExpression expression) {
105-
return new NominalDescriptionStrategy(bundle, integer -> Month.of(integer).getDisplayName(TextStyle.FULL, bundle.getLocale()), expression);
106+
Function<Integer, String> mappingFunction;
107+
if (expression instanceof Every) {
108+
mappingFunction = Object::toString;
109+
} else {
110+
mappingFunction = integer -> Month.of(integer).getDisplayName(TextStyle.FULL, bundle.getLocale());
111+
}
112+
return new NominalDescriptionStrategy(bundle, mappingFunction, expression);
106113
}
107114

108115
/**

src/main/java/com/cronutils/model/field/expression/Every.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
/**
2020
* Represents every x time on a cron field.
21-
*/
21+
* Usage examples:
22+
* - To represent a scheduling every 3 months on a specific time (the standard 0 0 0 *&#47;3 *), use the Every(3) constructor
23+
* - To represent a scheduling every 3 months FROM NOW, use the Every(on(now.getMonth, 3)) constructor */
2224
public class Every extends FieldExpression {
2325

2426
private static final long serialVersionUID = -1103196842332906994L;

src/test/java/com/cronutils/Issue421Test.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,54 @@ public class Issue421Test {
3434
.withCronValidation(CronConstraintsFactory.ensureEitherDayOfYearOrMonth())
3535
.instance();
3636

37-
@Ignore
3837
@Test
39-
public void testWrongIntervalsForEvery6Months() {
40-
LocalDateTime firstOfJanuary = LocalDateTime.of(2020, 4, 25, 0, 0);
38+
public void testIntervals_Every5thMonths_SinceASpecificMonth() {
39+
LocalDateTime firstOfJanuary = LocalDateTime.of(2020, 2, 10, 0, 0);
4140
Clock clock = Clock.fixed(firstOfJanuary.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
4241
ZonedDateTime now = ZonedDateTime.now(clock);
4342
System.out.println("now: " + now);
4443

45-
Cron cron = getEveryMonth(now, 6).instance();
44+
Cron cron = getEveryMonthFromNow(now, 5).instance();
4645
ZonedDateTime nextRun;
4746

4847
nextRun = nextRun(cron, now); // first run
4948
Assert.assertEquals(2020, nextRun.getYear());
50-
Assert.assertEquals(10, nextRun.getMonthValue());
49+
Assert.assertEquals(7, nextRun.getMonthValue());
5150

52-
nextRun = nextRun(cron, nextRun); // second
51+
nextRun = nextRun(cron, nextRun); // first run
52+
Assert.assertEquals(2020, nextRun.getYear());
53+
Assert.assertEquals(12, nextRun.getMonthValue());
54+
55+
nextRun = nextRun(cron, nextRun); // first run
56+
Assert.assertEquals(2021, nextRun.getYear());
57+
Assert.assertEquals(2, nextRun.getMonthValue());
58+
}
59+
60+
@Test
61+
public void testIntervals_Every5thMonth() {
62+
LocalDateTime firstOfJanuary = LocalDateTime.of(2020, 2, 10, 0, 0);
63+
Clock clock = Clock.fixed(firstOfJanuary.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
64+
ZonedDateTime now = ZonedDateTime.now(clock);
65+
System.out.println("now: " + now);
66+
67+
Cron cron = getEveryMonth(now, 5).instance();
68+
ZonedDateTime nextRun;
69+
70+
nextRun = nextRun(cron, now); // first run
71+
Assert.assertEquals(2020, nextRun.getYear());
72+
Assert.assertEquals(6, nextRun.getMonthValue());
73+
74+
nextRun = nextRun(cron, nextRun); // first run
75+
Assert.assertEquals(2020, nextRun.getYear());
76+
Assert.assertEquals(11, nextRun.getMonthValue());
77+
78+
nextRun = nextRun(cron, nextRun); // first run
5379
Assert.assertEquals(2021, nextRun.getYear());
54-
Assert.assertEquals(4, nextRun.getMonthValue());
80+
Assert.assertEquals(1, nextRun.getMonthValue());
5581
}
5682

57-
@Ignore
5883
@Test
59-
public void testWrongEveryXMonthsDescription() {
84+
public void testDescription_EveryXMonths() {
6085
ZonedDateTime now = ZonedDateTime.now();
6186

6287
String description = CronDescriptor.instance(Locale.US).describe(getEveryMonth(now, 3).instance());
@@ -77,6 +102,15 @@ public static CronBuilder getEveryMonth(ZonedDateTime now, int every) {
77102
.withDoY(questionMark())
78103
.withMonth(every(every));
79104
}
105+
public static CronBuilder getEveryMonthFromNow(ZonedDateTime now, int every) {
106+
return CronBuilder.cron(definition)
107+
.withMinute(on(now.getMinute()))
108+
.withHour(on(now.getHour()))
109+
.withDoW(questionMark())
110+
.withDoM(on(now.getDayOfMonth()))
111+
.withDoY(questionMark())
112+
.withMonth(every(on(now.getMonthValue()),every));
113+
}
80114

81115
private static ZonedDateTime nextRun(Cron cron, ZonedDateTime when) {
82116
final Optional<ZonedDateTime> next = ExecutionTime.forCron(cron).nextExecution(when);

0 commit comments

Comments
 (0)