Skip to content

Commit 64673b2

Browse files
authored
Merge pull request #425 from jamescosford/Issue423
Issue 423: Strange results for a strange expression
2 parents 21f14d5 + 23693e2 commit 64673b2

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.cronutils;
2+
3+
import com.cronutils.descriptor.CronDescriptor;
4+
import com.cronutils.model.Cron;
5+
import com.cronutils.model.CronType;
6+
import com.cronutils.model.definition.CronDefinitionBuilder;
7+
import com.cronutils.model.time.ExecutionTime;
8+
import com.cronutils.parser.CronParser;
9+
import org.junit.Test;
10+
11+
import java.time.LocalDate;
12+
import java.time.LocalTime;
13+
import java.time.ZoneId;
14+
import java.time.ZonedDateTime;
15+
import java.util.Arrays;
16+
import java.util.Locale;
17+
18+
import static org.junit.Assert.*;
19+
20+
public class Issue423Test {
21+
private static final LocalDate saturday = LocalDate.of(2020, 4, 25);
22+
private static ZonedDateTime shortZDT(int h, int m) {
23+
return ZonedDateTime.of(
24+
saturday,
25+
LocalTime.of(h, m),
26+
ZoneId.of("Australia/Perth")
27+
);
28+
}
29+
30+
private static class TestPair {
31+
public final ZonedDateTime test;
32+
public final ZonedDateTime expected;
33+
public TestPair(ZonedDateTime t, ZonedDateTime exp) {
34+
test = t;
35+
expected = exp;
36+
}
37+
}
38+
39+
@Test
40+
public void issue423() {
41+
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
42+
final Cron cron = parser.parse("0 0 0-07,17-0 ? * SAT");
43+
final CronDescriptor cd = CronDescriptor.instance(Locale.UK);
44+
assertTrue(cd.describe(cron).length() > 0);
45+
// at time of test creation, the descriptor is
46+
// "every hour between 0 and 7 and every hour between 17 and 0 at Saturday day"
47+
48+
final ExecutionTime et = ExecutionTime.forCron(cron);
49+
// At this point, an an exception WAS logged. But, not anymore!
50+
51+
Arrays.asList(
52+
new TestPair(shortZDT( 0, 0), shortZDT( 1, 0)),
53+
new TestPair(shortZDT( 0, 30), shortZDT( 1, 0)),
54+
new TestPair(shortZDT( 6, 0), shortZDT( 7, 0)),
55+
new TestPair(shortZDT( 7, 0), shortZDT(17, 0)),
56+
new TestPair(shortZDT(16, 0), shortZDT(17, 0)), // Should be 17:00, but skips to the next Saturday
57+
new TestPair(shortZDT(17, 0), shortZDT(18, 0)), // Should be 18:00, but skips to the next Saturday
58+
new TestPair(shortZDT(18, 0), shortZDT(19, 0)) // Should be 19:00, but skips to the next Saturday
59+
).forEach(tp -> {
60+
// System.err.println("Expected: " + tp.expected + "; Actual: " + et.nextExecution(tp.test).get().toString());
61+
assertEquals(
62+
"All these should be on the same Saturday",
63+
tp.expected,
64+
et.nextExecution(tp.test).get()
65+
);
66+
});
67+
}
68+
}

0 commit comments

Comments
 (0)