Skip to content

Commit 8030462

Browse files
author
Elena Sacchi
committed
Fix every description and fix #421
1 parent 90877ab commit 8030462

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/Issue421Test.java

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

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

43-
Cron cron = getEveryMonth(now, 6).instance();
42+
Cron cron = getEveryMonthFromNow(now, 5).instance();
4443
ZonedDateTime nextRun;
4544

4645
nextRun = nextRun(cron, now); // first run
4746
Assert.assertEquals(2020, nextRun.getYear());
48-
Assert.assertEquals(10, nextRun.getMonthValue());
47+
Assert.assertEquals(7, nextRun.getMonthValue());
4948

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

55-
@Ignore
5681
@Test
57-
public void testWrongEveryXMonthsDescription() {
82+
public void testDescription_EveryXMonths() {
5883
ZonedDateTime now = ZonedDateTime.now();
5984

6085
String description = CronDescriptor.instance(Locale.US).describe(getEveryMonth(now, 3).instance());
@@ -75,6 +100,15 @@ public static CronBuilder getEveryMonth(ZonedDateTime now, int every) {
75100
.withDoY(questionMark())
76101
.withMonth(every(every));
77102
}
103+
public static CronBuilder getEveryMonthFromNow(ZonedDateTime now, int every) {
104+
return CronBuilder.cron(definition)
105+
.withMinute(on(now.getMinute()))
106+
.withHour(on(now.getHour()))
107+
.withDoW(questionMark())
108+
.withDoM(on(now.getDayOfMonth()))
109+
.withDoY(questionMark())
110+
.withMonth(every(on(now.getMonthValue()),every));
111+
}
78112

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

0 commit comments

Comments
 (0)