From e0c678e9a015e811283a9cafe875f0e928ea179e Mon Sep 17 00:00:00 2001 From: "Timothy A. Bish" Date: Wed, 16 Feb 2011 21:38:41 +0000 Subject: [PATCH] Apply patch for: https://issues.apache.org/jira/browse/AMQ-3036 git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1071411 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/broker/scheduler/CronParser.java | 4 +- .../broker/scheduler/CronParserTest.java | 84 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java b/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java index 5c07ff76e8..3565acd493 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java @@ -43,7 +43,7 @@ public class CronParser { CronEntry hours = entries.get(HOURS); CronEntry dayOfMonth = entries.get(DAY_OF_MONTH); CronEntry month = entries.get(MONTH); - CronEntry dayOfWeek = entries.get(DAY_OF_MONTH); + CronEntry dayOfWeek = entries.get(DAY_OF_WEEK); int currentMinutes = working.get(Calendar.MINUTE); if (!isCurrent(minutes, currentMinutes)) { @@ -71,7 +71,7 @@ public class CronParser { } int currentDayOfMonth = working.get(Calendar.DAY_OF_MONTH); if (!isCurrent(dayOfMonth, currentDayOfMonth)) { - int nextDay = getNext(dayOfMonth, currentMonth); + int nextDay = getNext(dayOfMonth, currentDayOfMonth); working.add(Calendar.DAY_OF_MONTH, nextDay); result = working.getTimeInMillis(); } diff --git a/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java b/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java index cd8d9a9447..f6460a4910 100644 --- a/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java @@ -17,6 +17,7 @@ package org.apache.activemq.broker.scheduler; import static org.junit.Assert.assertEquals; + import static org.junit.Assert.fail; import java.util.Calendar; @@ -26,6 +27,79 @@ import org.junit.Test; public class CronParserTest { + @Test + public void testgetNextTimeDayOfWeek() throws MessageFormatException { + + + // using an absolute date so that result will be absolute - Monday 15 Nov 2010 + Calendar current = Calendar.getInstance(); + current.set(2010, Calendar.NOVEMBER, 15, 9, 15, 30); + System.out.println("start:" + current.getTime()); + + String test = "* * * * 5"; + long next = CronParser.getNextScheduledTime(test, current.getTimeInMillis()); + + Calendar result = Calendar.getInstance(); + result.setTimeInMillis(next); + System.out.println("next:" + result.getTime()); + + assertEquals(30,result.get(Calendar.SECOND)); + assertEquals(15,result.get(Calendar.MINUTE)); + assertEquals(9,result.get(Calendar.HOUR)); + // expecting Friday 19th + assertEquals(19,result.get(Calendar.DAY_OF_MONTH)); + assertEquals(Calendar.NOVEMBER,result.get(Calendar.MONTH)); + assertEquals(2010,result.get(Calendar.YEAR)); + } + + @Test + public void testgetNextTimeMonth() throws MessageFormatException { + + + // using an absolute date so that result will be absolute - Monday 15 Nov 2010 + Calendar current = Calendar.getInstance(); + current.set(2010, Calendar.NOVEMBER, 15, 9, 15, 30); + System.out.println("start:" + current.getTime()); + + String test = "* * * 12 *"; + long next = CronParser.getNextScheduledTime(test, current.getTimeInMillis()); + + Calendar result = Calendar.getInstance(); + result.setTimeInMillis(next); + System.out.println("next:" + result.getTime()); + + assertEquals(30,result.get(Calendar.SECOND)); + assertEquals(15,result.get(Calendar.MINUTE)); + assertEquals(9,result.get(Calendar.HOUR)); + assertEquals(15,result.get(Calendar.DAY_OF_MONTH)); + assertEquals(Calendar.DECEMBER,result.get(Calendar.MONTH)); + assertEquals(2010,result.get(Calendar.YEAR)); + } + + @Test + public void testgetNextTimeDays() throws MessageFormatException { + + + // using an absolute date so that result will be absolute - Monday 15 Nov 2010 + Calendar current = Calendar.getInstance(); + current.set(2010, Calendar.NOVEMBER, 15, 9, 15, 30); + System.out.println("start:" + current.getTime()); + + String test = "* * 16 * *"; + long next = CronParser.getNextScheduledTime(test, current.getTimeInMillis()); + + Calendar result = Calendar.getInstance(); + result.setTimeInMillis(next); + System.out.println("next:" + result.getTime()); + + assertEquals(30,result.get(Calendar.SECOND)); + assertEquals(15,result.get(Calendar.MINUTE)); + assertEquals(9,result.get(Calendar.HOUR)); + assertEquals(16,result.get(Calendar.DAY_OF_MONTH)); + assertEquals(Calendar.NOVEMBER,result.get(Calendar.MONTH)); + assertEquals(2010,result.get(Calendar.YEAR)); + } + @Test public void testgetNextTimeMinutes() throws MessageFormatException { String test = "30 * * * *"; @@ -153,6 +227,16 @@ public class CronParserTest { } catch (Throwable e) { } + test = "0 1 2 3 4"; + list = CronParser.tokenize(test); + assertEquals(list.size(), 5); + + assertEquals(list.get(0), "0"); + assertEquals(list.get(1), "1"); + assertEquals(list.get(2), "2"); + assertEquals(list.get(3), "3"); + assertEquals(list.get(4), "4"); + } public void testGetNextScheduledTime() {