@@ -34,29 +34,72 @@ public class Issue421Test {
3434 .withCronValidation (CronConstraintsFactory .ensureEitherDayOfYearOrMonth ())
3535 .instance ();
3636
37- @ Ignore
3837 @ Test
3938 public void testWrongIntervalsForEvery6Months () {
40- LocalDateTime firstOfJanuary = LocalDateTime .of (2020 , 4 , 25 , 0 , 0 );
39+ LocalDateTime dayOfApril = LocalDateTime .of (2020 , 4 , 25 , 0 , 0 );
40+ Clock clock = Clock .fixed (dayOfApril .toInstant (ZoneOffset .UTC ), ZoneId .systemDefault ());
41+ ZonedDateTime dayOfAprilInLocalTimezone = ZonedDateTime .now (clock );
42+ System .out .println ("now: " + dayOfAprilInLocalTimezone );
43+ Cron cron = getEveryMonthFromNow (dayOfAprilInLocalTimezone , 6 ).instance ();
44+
45+ ZonedDateTime nextRun = nextRun (cron , dayOfAprilInLocalTimezone ); // first run
46+ Assert .assertEquals (2020 , nextRun .getYear ());
47+ Assert .assertEquals (10 , nextRun .getMonthValue ());
48+
49+ nextRun = nextRun (cron , nextRun ); // second
50+ System .out .println (nextRun );
51+ Assert .assertEquals (2021 , nextRun .getYear ());
52+ Assert .assertEquals (4 , nextRun .getMonthValue ());
53+ }
54+
55+ @ Test
56+ public void testIntervalsEvery5thMonthsSinceASpecificMonth () {
57+ LocalDateTime firstOfJanuary = LocalDateTime .of (2020 , 2 , 10 , 0 , 0 );
4158 Clock clock = Clock .fixed (firstOfJanuary .toInstant (ZoneOffset .UTC ), ZoneId .systemDefault ());
4259 ZonedDateTime now = ZonedDateTime .now (clock );
4360 System .out .println ("now: " + now );
4461
45- Cron cron = getEveryMonth (now , 6 ).instance ();
62+ Cron cron = getEveryMonthFromNow (now , 5 ).instance ();
4663 ZonedDateTime nextRun ;
4764
4865 nextRun = nextRun (cron , now ); // first run
4966 Assert .assertEquals (2020 , nextRun .getYear ());
50- Assert .assertEquals (10 , nextRun .getMonthValue ());
67+ Assert .assertEquals (7 , nextRun .getMonthValue ());
5168
52- nextRun = nextRun (cron , nextRun ); // second
69+ nextRun = nextRun (cron , nextRun ); // first run
70+ Assert .assertEquals (2020 , nextRun .getYear ());
71+ Assert .assertEquals (12 , nextRun .getMonthValue ());
72+
73+ nextRun = nextRun (cron , nextRun ); // first run
5374 Assert .assertEquals (2021 , nextRun .getYear ());
54- Assert .assertEquals (4 , nextRun .getMonthValue ());
75+ Assert .assertEquals (2 , nextRun .getMonthValue ());
5576 }
5677
57- @ Ignore
5878 @ Test
59- public void testWrongEveryXMonthsDescription () {
79+ public void testIntervalsEvery5thMonth () {
80+ LocalDateTime firstOfJanuary = LocalDateTime .of (2020 , 2 , 10 , 0 , 0 );
81+ Clock clock = Clock .fixed (firstOfJanuary .toInstant (ZoneOffset .UTC ), ZoneId .systemDefault ());
82+ ZonedDateTime now = ZonedDateTime .now (clock );
83+ System .out .println ("now: " + now );
84+
85+ Cron cron = getEveryMonth (now , 5 ).instance ();
86+ ZonedDateTime nextRun ;
87+
88+ nextRun = nextRun (cron , now ); // first run
89+ Assert .assertEquals (2020 , nextRun .getYear ());
90+ Assert .assertEquals (6 , nextRun .getMonthValue ());
91+
92+ nextRun = nextRun (cron , nextRun ); // first run
93+ Assert .assertEquals (2020 , nextRun .getYear ());
94+ Assert .assertEquals (11 , nextRun .getMonthValue ());
95+
96+ nextRun = nextRun (cron , nextRun ); // first run
97+ Assert .assertEquals (2021 , nextRun .getYear ());
98+ Assert .assertEquals (1 , nextRun .getMonthValue ());
99+ }
100+
101+ @ Test
102+ public void testDescriptionEveryXMonths () {
60103 ZonedDateTime now = ZonedDateTime .now ();
61104
62105 String description = CronDescriptor .instance (Locale .US ).describe (getEveryMonth (now , 3 ).instance ());
@@ -77,6 +120,15 @@ public static CronBuilder getEveryMonth(ZonedDateTime now, int every) {
77120 .withDoY (questionMark ())
78121 .withMonth (every (every ));
79122 }
123+ public static CronBuilder getEveryMonthFromNow (ZonedDateTime now , int every ) {
124+ return CronBuilder .cron (definition )
125+ .withMinute (on (now .getMinute ()))
126+ .withHour (on (now .getHour ()))
127+ .withDoW (questionMark ())
128+ .withDoM (on (now .getDayOfMonth ()))
129+ .withDoY (questionMark ())
130+ .withMonth (every (on (now .getMonthValue ()), every ));
131+ }
80132
81133 private static ZonedDateTime nextRun (Cron cron , ZonedDateTime when ) {
82134 final Optional <ZonedDateTime > next = ExecutionTime .forCron (cron ).nextExecution (when );
0 commit comments