Skip to content

Commit b4af04b

Browse files
author
David Syer
committed
SPR-7239: fix CronTrigger
1 parent f0777d1 commit b4af04b

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

org.springframework.context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public class CronSequenceGenerator {
6868

6969
private final TimeZone timeZone;
7070

71-
7271
/**
7372
* Construct a {@link CronSequenceGenerator} from the pattern provided.
7473
* @param expression a space-separated list of time fields
@@ -81,7 +80,6 @@ public CronSequenceGenerator(String expression, TimeZone timeZone) {
8180
parse(expression);
8281
}
8382

84-
8583
/**
8684
* Get the next {@link Date} in the sequence matching the Cron pattern and
8785
* after the value provided. The return value will have a whole number of
@@ -156,13 +154,13 @@ private void doNext(Calendar calendar) {
156154
if (dayOfMonth == updateDayOfMonth) {
157155
resets.add(Calendar.DAY_OF_MONTH);
158156
} else {
159-
doNext(calendar);
157+
doNext(calendar);
160158
}
161159

162160
int month = calendar.get(Calendar.MONTH);
163161
int updateMonth = findNext(this.months, month, calendar, Calendar.MONTH, Calendar.YEAR, resets);
164162
if (month != updateMonth) {
165-
doNext(calendar);
163+
doNext(calendar);
166164
}
167165

168166
}
@@ -314,6 +312,9 @@ private int[] getRange(String field, int max) {
314312
result[0] = Integer.valueOf(split[0]);
315313
result[1] = Integer.valueOf(split[1]);
316314
}
315+
if (result[0] >= max || result[1] >= max) {
316+
throw new IllegalArgumentException("Range exceeds maximum (" + max + "): " + field);
317+
}
317318
return result;
318319
}
319320

org.springframework.context/src/main/java/org/springframework/scheduling/support/CronTrigger.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,10 @@ public boolean equals(Object obj) {
7474
public int hashCode() {
7575
return this.sequenceGenerator.hashCode();
7676
}
77+
78+
@Override
79+
public String toString() {
80+
return sequenceGenerator.toString();
81+
}
7782

7883
}

org.springframework.context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public CronTriggerTests(Date date, TimeZone timeZone) {
4949
this.timeZone = timeZone;
5050
this.date = date;
5151
}
52-
52+
5353
@Parameters
5454
public static List<Object[]> getParameters() {
5555
List<Object[]> list = new ArrayList<Object[]>();
56-
list.add(new Object[] {new Date(), TimeZone.getDefault()});
57-
list.add(new Object[] {new Date(), TimeZone.getTimeZone("CET")});
56+
list.add(new Object[] { new Date(), TimeZone.getDefault() });
57+
list.add(new Object[] { new Date(), TimeZone.getTimeZone("CET") });
5858
return list;
5959
}
6060

@@ -66,7 +66,6 @@ private void roundup(Calendar calendar) {
6666
calendar.set(Calendar.MILLISECOND, 0);
6767
}
6868

69-
7069
@Before
7170
public void setUp() {
7271
calendar.setTimeZone(timeZone);
@@ -561,6 +560,51 @@ public void testMonthNamesMixedCase() throws Exception {
561560
assertEquals(trigger1, trigger2);
562561
}
563562

563+
@Test(expected = IllegalArgumentException.class)
564+
public void testSecondInvalid() throws Exception {
565+
new CronTrigger("77 * * * * *", timeZone);
566+
}
567+
568+
@Test(expected = IllegalArgumentException.class)
569+
public void testSecondRangeInvalid() throws Exception {
570+
new CronTrigger("44-77 * * * * *", timeZone);
571+
}
572+
573+
@Test(expected = IllegalArgumentException.class)
574+
public void testMinuteInvalid() throws Exception {
575+
new CronTrigger("* 77 * * * *", timeZone);
576+
}
577+
578+
@Test(expected = IllegalArgumentException.class)
579+
public void testMinuteRangeInvalid() throws Exception {
580+
new CronTrigger("* 44-77 * * * *", timeZone);
581+
}
582+
583+
@Test(expected = IllegalArgumentException.class)
584+
public void testHourInvalid() throws Exception {
585+
new CronTrigger("* * 27 * * *", timeZone);
586+
}
587+
588+
@Test(expected = IllegalArgumentException.class)
589+
public void testHourRangeInvalid() throws Exception {
590+
new CronTrigger("* * 23-28 * * *", timeZone);
591+
}
592+
593+
@Test(expected = IllegalArgumentException.class)
594+
public void testDayInvalid() throws Exception {
595+
new CronTrigger("* * * 45 * *", timeZone);
596+
}
597+
598+
@Test(expected = IllegalArgumentException.class)
599+
public void testDayRangeInvalid() throws Exception {
600+
new CronTrigger("* * * 28-45 * *", timeZone);
601+
}
602+
603+
@Test(expected = IllegalArgumentException.class)
604+
public void testMonthInvalid() throws Exception {
605+
new CronTrigger("* * * * 11-13 *", timeZone);
606+
}
607+
564608
@Test
565609
public void testWhitespace() throws Exception {
566610
CronTrigger trigger1 = new CronTrigger("* * * * 1 *", timeZone);
@@ -579,7 +623,6 @@ private void assertMatchesNextSecond(CronTrigger trigger, Calendar calendar) {
579623
assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
580624
}
581625

582-
583626
private static TriggerContext getTriggerContext(Date lastCompletionTime) {
584627
SimpleTriggerContext context = new SimpleTriggerContext();
585628
context.update(null, null, lastCompletionTime);

0 commit comments

Comments
 (0)