SPR-7239: fix CronTrigger

This commit is contained in:
David Syer 2010-05-26 17:41:54 +00:00
parent f0777d130f
commit b4af04ba9d
3 changed files with 58 additions and 9 deletions

View File

@ -68,7 +68,6 @@ public class CronSequenceGenerator {
private final TimeZone timeZone; private final TimeZone timeZone;
/** /**
* Construct a {@link CronSequenceGenerator} from the pattern provided. * Construct a {@link CronSequenceGenerator} from the pattern provided.
* @param expression a space-separated list of time fields * @param expression a space-separated list of time fields
@ -81,7 +80,6 @@ public class CronSequenceGenerator {
parse(expression); parse(expression);
} }
/** /**
* Get the next {@link Date} in the sequence matching the Cron pattern and * 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 * after the value provided. The return value will have a whole number of
@ -156,13 +154,13 @@ public class CronSequenceGenerator {
if (dayOfMonth == updateDayOfMonth) { if (dayOfMonth == updateDayOfMonth) {
resets.add(Calendar.DAY_OF_MONTH); resets.add(Calendar.DAY_OF_MONTH);
} else { } else {
doNext(calendar); doNext(calendar);
} }
int month = calendar.get(Calendar.MONTH); int month = calendar.get(Calendar.MONTH);
int updateMonth = findNext(this.months, month, calendar, Calendar.MONTH, Calendar.YEAR, resets); int updateMonth = findNext(this.months, month, calendar, Calendar.MONTH, Calendar.YEAR, resets);
if (month != updateMonth) { if (month != updateMonth) {
doNext(calendar); doNext(calendar);
} }
} }
@ -314,6 +312,9 @@ public class CronSequenceGenerator {
result[0] = Integer.valueOf(split[0]); result[0] = Integer.valueOf(split[0]);
result[1] = Integer.valueOf(split[1]); result[1] = Integer.valueOf(split[1]);
} }
if (result[0] >= max || result[1] >= max) {
throw new IllegalArgumentException("Range exceeds maximum (" + max + "): " + field);
}
return result; return result;
} }

View File

@ -74,5 +74,10 @@ public class CronTrigger implements Trigger {
public int hashCode() { public int hashCode() {
return this.sequenceGenerator.hashCode(); return this.sequenceGenerator.hashCode();
} }
@Override
public String toString() {
return sequenceGenerator.toString();
}
} }

View File

@ -49,12 +49,12 @@ public class CronTriggerTests {
this.timeZone = timeZone; this.timeZone = timeZone;
this.date = date; this.date = date;
} }
@Parameters @Parameters
public static List<Object[]> getParameters() { public static List<Object[]> getParameters() {
List<Object[]> list = new ArrayList<Object[]>(); List<Object[]> list = new ArrayList<Object[]>();
list.add(new Object[] {new Date(), TimeZone.getDefault()}); list.add(new Object[] { new Date(), TimeZone.getDefault() });
list.add(new Object[] {new Date(), TimeZone.getTimeZone("CET")}); list.add(new Object[] { new Date(), TimeZone.getTimeZone("CET") });
return list; return list;
} }
@ -66,7 +66,6 @@ public class CronTriggerTests {
calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.MILLISECOND, 0);
} }
@Before @Before
public void setUp() { public void setUp() {
calendar.setTimeZone(timeZone); calendar.setTimeZone(timeZone);
@ -561,6 +560,51 @@ public class CronTriggerTests {
assertEquals(trigger1, trigger2); 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 @Test
public void testWhitespace() throws Exception { public void testWhitespace() throws Exception {
CronTrigger trigger1 = new CronTrigger("* * * * 1 *", timeZone); CronTrigger trigger1 = new CronTrigger("* * * * 1 *", timeZone);
@ -579,7 +623,6 @@ public class CronTriggerTests {
assertEquals(calendar.getTime(), trigger.nextExecutionTime(context)); assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
} }
private static TriggerContext getTriggerContext(Date lastCompletionTime) { private static TriggerContext getTriggerContext(Date lastCompletionTime) {
SimpleTriggerContext context = new SimpleTriggerContext(); SimpleTriggerContext context = new SimpleTriggerContext();
context.update(null, null, lastCompletionTime); context.update(null, null, lastCompletionTime);