diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java b/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java index e801d413894..86700699673 100644 --- a/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java @@ -68,7 +68,6 @@ public class CronSequenceGenerator { private final TimeZone timeZone; - /** * Construct a {@link CronSequenceGenerator} from the pattern provided. * @param expression a space-separated list of time fields @@ -81,7 +80,6 @@ public class CronSequenceGenerator { parse(expression); } - /** * Get the next {@link Date} in the sequence matching the Cron pattern and * after the value provided. The return value will have a whole number of @@ -156,13 +154,13 @@ public class CronSequenceGenerator { if (dayOfMonth == updateDayOfMonth) { resets.add(Calendar.DAY_OF_MONTH); } else { - doNext(calendar); + doNext(calendar); } int month = calendar.get(Calendar.MONTH); int updateMonth = findNext(this.months, month, calendar, Calendar.MONTH, Calendar.YEAR, resets); if (month != updateMonth) { - doNext(calendar); + doNext(calendar); } } @@ -314,6 +312,9 @@ public class CronSequenceGenerator { result[0] = Integer.valueOf(split[0]); result[1] = Integer.valueOf(split[1]); } + if (result[0] >= max || result[1] >= max) { + throw new IllegalArgumentException("Range exceeds maximum (" + max + "): " + field); + } return result; } diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronTrigger.java b/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronTrigger.java index 00db7933605..ddb8f265771 100644 --- a/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronTrigger.java +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/support/CronTrigger.java @@ -74,5 +74,10 @@ public class CronTrigger implements Trigger { public int hashCode() { return this.sequenceGenerator.hashCode(); } + + @Override + public String toString() { + return sequenceGenerator.toString(); + } } diff --git a/org.springframework.context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java b/org.springframework.context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java index 387b7ac54fa..b23fe9fb6d0 100644 --- a/org.springframework.context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java +++ b/org.springframework.context/src/test/java/org/springframework/scheduling/support/CronTriggerTests.java @@ -49,12 +49,12 @@ public class CronTriggerTests { this.timeZone = timeZone; this.date = date; } - + @Parameters public static List getParameters() { List list = new ArrayList(); - list.add(new Object[] {new Date(), TimeZone.getDefault()}); - list.add(new Object[] {new Date(), TimeZone.getTimeZone("CET")}); + list.add(new Object[] { new Date(), TimeZone.getDefault() }); + list.add(new Object[] { new Date(), TimeZone.getTimeZone("CET") }); return list; } @@ -66,7 +66,6 @@ public class CronTriggerTests { calendar.set(Calendar.MILLISECOND, 0); } - @Before public void setUp() { calendar.setTimeZone(timeZone); @@ -561,6 +560,51 @@ public class CronTriggerTests { assertEquals(trigger1, trigger2); } + @Test(expected = IllegalArgumentException.class) + public void testSecondInvalid() throws Exception { + new CronTrigger("77 * * * * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testSecondRangeInvalid() throws Exception { + new CronTrigger("44-77 * * * * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testMinuteInvalid() throws Exception { + new CronTrigger("* 77 * * * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testMinuteRangeInvalid() throws Exception { + new CronTrigger("* 44-77 * * * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testHourInvalid() throws Exception { + new CronTrigger("* * 27 * * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testHourRangeInvalid() throws Exception { + new CronTrigger("* * 23-28 * * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testDayInvalid() throws Exception { + new CronTrigger("* * * 45 * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testDayRangeInvalid() throws Exception { + new CronTrigger("* * * 28-45 * *", timeZone); + } + + @Test(expected = IllegalArgumentException.class) + public void testMonthInvalid() throws Exception { + new CronTrigger("* * * * 11-13 *", timeZone); + } + @Test public void testWhitespace() throws Exception { CronTrigger trigger1 = new CronTrigger("* * * * 1 *", timeZone); @@ -579,7 +623,6 @@ public class CronTriggerTests { assertEquals(calendar.getTime(), trigger.nextExecutionTime(context)); } - private static TriggerContext getTriggerContext(Date lastCompletionTime) { SimpleTriggerContext context = new SimpleTriggerContext(); context.update(null, null, lastCompletionTime);