From: <bla...@us...> - 2008-11-13 02:16:28
|
Revision: 54 http://drawbridge.svn.sourceforge.net/drawbridge/?rev=54&view=rev Author: blamonica Date: 2008-11-13 02:16:25 +0000 (Thu, 13 Nov 2008) Log Message: ----------- Added a way to check the validity of cron expressions. Still needs to be added to the jsps. Modified Paths: -------------- trunk/drawbridge/WebContent/WEB-INF/dwr.xml trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrDrawbridgeFacade.java trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrDrawbridgeFacade.java trunk/drawbridge/src/java/net/sf/drawbridge/sched/DrawbridgeScheduleLoader.java trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzDrawbridgeScheduleLoader.java Added Paths: ----------- trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrCronValidatorFacade.java trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacade.java trunk/drawbridge/src/java/net/sf/drawbridge/sched/CronValidator.java trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzCronValidator.java trunk/drawbridge/src/test/net/sf/drawbridge/dwr/ trunk/drawbridge/src/test/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacadeTest.java trunk/drawbridge/src/test/net/sf/drawbridge/sched/QuartzCronValidatorTest.java Modified: trunk/drawbridge/WebContent/WEB-INF/dwr.xml =================================================================== --- trunk/drawbridge/WebContent/WEB-INF/dwr.xml 2008-11-11 22:59:42 UTC (rev 53) +++ trunk/drawbridge/WebContent/WEB-INF/dwr.xml 2008-11-13 02:16:25 UTC (rev 54) @@ -5,6 +5,9 @@ <create creator="spring" javascript="DrawbridgeService"> <param name="beanName" value="DwrDrawbridgeFacade"/> </create> + <create creator="spring" javascript="CronValidator"> + <param name="beanName" value="CronValidator"/> + </create> <convert converter="bean" match="net.sf.drawbridge.vo.RunAsAccount"/> </allow> </dwr> \ No newline at end of file Added: trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrCronValidatorFacade.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrCronValidatorFacade.java (rev 0) +++ trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrCronValidatorFacade.java 2008-11-13 02:16:25 UTC (rev 54) @@ -0,0 +1,24 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Ben La Monica + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package net.sf.drawbridge.dwr; + +public interface DwrCronValidatorFacade { + String getNextRun(String startDate, String stopDate, String cronExpression); + String[] getNextRuns(String startDate, String stopDate, String cronExpression, int numRuns); +} Modified: trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrDrawbridgeFacade.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrDrawbridgeFacade.java 2008-11-11 22:59:42 UTC (rev 53) +++ trunk/drawbridge/src/java/net/sf/drawbridge/dwr/DwrDrawbridgeFacade.java 2008-11-13 02:16:25 UTC (rev 54) @@ -1,3 +1,21 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Adam Cresse + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + package net.sf.drawbridge.dwr; import java.util.List; @@ -5,7 +23,5 @@ import net.sf.drawbridge.vo.RunAsAccount; public interface DwrDrawbridgeFacade { - public List<RunAsAccount> listRunAsAccounts(Integer databaseId); - } Added: trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacade.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacade.java (rev 0) +++ trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacade.java 2008-11-13 02:16:25 UTC (rev 54) @@ -0,0 +1,102 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Ben La Monica + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package net.sf.drawbridge.dwr; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import net.sf.drawbridge.sched.CronValidator; + +/** + * Provides a wrapper around the CronValidator service so that the formatting on the page will be easier. + */ +public class WrapperDwrCronValidatorFacade implements DwrCronValidatorFacade { + + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm"; + + private CronValidator validator; + + public WrapperDwrCronValidatorFacade(CronValidator validator) { + this.validator = validator; + } + + private Date getDate(final SimpleDateFormat df, final String inDate) { + if (inDate != null && !"NOW".equalsIgnoreCase(inDate) && !"NEVER".equalsIgnoreCase(inDate)) { + try { + return df.parse(inDate); + } catch (Exception e) { + // unable to parse date + } + } + return null; + } + + public String getNextRun(final String inStartDate, final String inStopDate, final String inCronExpression) { + String nextRun = ""; + String errorMessage = validator.getErrorMessage(inCronExpression); + + if (errorMessage == null) { + SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); + Date startDate = getDate(df, inStartDate); + Date stopDate = getDate(df, inStopDate); + Date nextRunDate = validator.getNextRun(startDate, stopDate, inCronExpression); + + if (nextRunDate == null) { + errorMessage = "Unable to determine next run date, are the start/stop dates correct?"; + } else { + nextRun = df.format(nextRunDate); + } + } + + if (errorMessage != null) { + return "Error: " + errorMessage; + } else { + return nextRun; + } + } + + public String[] getNextRuns(final String inStartDate, final String inStopDate, final String inCronExpression, final int numRuns) { + String[] nextRuns = {""}; + String errorMessage = validator.getErrorMessage(inCronExpression); + + if (errorMessage == null) { + SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); + Date startDate = getDate(df, inStartDate); + Date stopDate = getDate(df, inStopDate); + List<Date> nextRunDates = validator.getNextRuns(startDate, stopDate, inCronExpression, numRuns); + + if (nextRunDates.isEmpty()) { + errorMessage = "Unable to determine next run date, are the start/stop dates correct?"; + } else { + nextRuns = new String[nextRunDates.size()]; + int i = 0; + for (Date nextRunDate : nextRunDates) { + nextRuns[i++] = df.format(nextRunDate); + } + } + } + + if (errorMessage != null) { + return new String[] {"Error: " + errorMessage}; + } else { + return nextRuns; + } + } +} Modified: trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrDrawbridgeFacade.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrDrawbridgeFacade.java 2008-11-11 22:59:42 UTC (rev 53) +++ trunk/drawbridge/src/java/net/sf/drawbridge/dwr/WrapperDwrDrawbridgeFacade.java 2008-11-13 02:16:25 UTC (rev 54) @@ -1,3 +1,21 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Adam Cresse + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + package net.sf.drawbridge.dwr; import java.util.List; @@ -20,6 +38,4 @@ } return accounts; } - - } Added: trunk/drawbridge/src/java/net/sf/drawbridge/sched/CronValidator.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/sched/CronValidator.java (rev 0) +++ trunk/drawbridge/src/java/net/sf/drawbridge/sched/CronValidator.java 2008-11-13 02:16:25 UTC (rev 54) @@ -0,0 +1,28 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Ben La Monica + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package net.sf.drawbridge.sched; + +import java.util.Date; +import java.util.List; + +public interface CronValidator { + Date getNextRun(Date startDate, Date stopDate, String cronExpression); + String getErrorMessage(String cronExpression); + List<Date> getNextRuns(Date startDate, Date stopDate, String cronExpression, int numRuns); +} Modified: trunk/drawbridge/src/java/net/sf/drawbridge/sched/DrawbridgeScheduleLoader.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/sched/DrawbridgeScheduleLoader.java 2008-11-11 22:59:42 UTC (rev 53) +++ trunk/drawbridge/src/java/net/sf/drawbridge/sched/DrawbridgeScheduleLoader.java 2008-11-13 02:16:25 UTC (rev 54) @@ -18,7 +18,5 @@ package net.sf.drawbridge.sched; public interface DrawbridgeScheduleLoader { - void loadSchedule() throws Exception; - } Added: trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzCronValidator.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzCronValidator.java (rev 0) +++ trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzCronValidator.java 2008-11-13 02:16:25 UTC (rev 54) @@ -0,0 +1,92 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Ben La Monica + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package net.sf.drawbridge.sched; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.quartz.CronExpression; + +public class QuartzCronValidator implements CronValidator { + + private static final Log log = LogFactory.getLog(QuartzCronValidator.class); + + /** + * Will return the next date that will trigger the cron expression + */ + public Date getNextRun(Date startDate, final Date stopDate, final String cronExpression) { + Date nextRunTime = null; + try { + if (startDate == null) { + // use today's date, since it obviously isn't going to execute before today. + startDate = new Date(); + } + CronExpression exp = new CronExpression(cronExpression); + if (stopDate == null || (stopDate != null && startDate.before(stopDate))) { + nextRunTime = exp.getNextValidTimeAfter(startDate); + } + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("Caught " + e.getClass().getSimpleName() + " [" + e.getLocalizedMessage() + "] Invalid Cron Expression: '" + cronExpression + "'."); + } + } + return nextRunTime; + } + + /** + * Will return the next numRuns dates that will trigger the cron expression + */ + public List<Date> getNextRuns(final Date startDate, final Date stopDate, final String cronExpression, final int numRuns) { + List<Date> nextRunTimes = new ArrayList<Date>(); + try { + CronExpression exp = new CronExpression(cronExpression); + Date lastRun = startDate != null ? startDate : new Date(); + for (int i = 0; i < numRuns && (stopDate == null || lastRun.before(stopDate)); i++) { + lastRun = exp.getNextValidTimeAfter(lastRun); + nextRunTimes.add(lastRun); + } + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("Caught " + e.getClass().getSimpleName() + " [" + e.getLocalizedMessage() + "] Invalid Cron Expression: '" + cronExpression + "'."); + } + } + return nextRunTimes; + } + + /** + * Returns error message if cronExpression is not formed correctly + * @param cronExpression + * @return + */ + public String getErrorMessage(String cronExpression) { + String message = null; + + try { + new CronExpression(cronExpression); + } catch (ParseException e) { + message = e.getLocalizedMessage(); + } + + return message; + } +} Modified: trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzDrawbridgeScheduleLoader.java =================================================================== --- trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzDrawbridgeScheduleLoader.java 2008-11-11 22:59:42 UTC (rev 53) +++ trunk/drawbridge/src/java/net/sf/drawbridge/sched/QuartzDrawbridgeScheduleLoader.java 2008-11-13 02:16:25 UTC (rev 54) @@ -1,6 +1,7 @@ /* * This file is part of DrawBridge. * Copyright 2008 Adam Cresse + * Copyright 2008 Ben La Monica * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License Added: trunk/drawbridge/src/test/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacadeTest.java =================================================================== --- trunk/drawbridge/src/test/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacadeTest.java (rev 0) +++ trunk/drawbridge/src/test/net/sf/drawbridge/dwr/WrapperDwrCronValidatorFacadeTest.java 2008-11-13 02:16:25 UTC (rev 54) @@ -0,0 +1,125 @@ +/* + * This file is part of DrawBridge. + * Copyright 2008 Adam Cresse + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation,Inc.,51 Franklin Street,Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package net.sf.drawbridge.dwr; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import net.sf.drawbridge.sched.CronValidator; + +import org.jmock.Mock; +import org.jmock.MockObjectTestCase; + +public class WrapperDwrCronValidatorFacadeTest extends MockObjectTestCase { + + private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + private Mock mockCronValidator = mock(CronValidator.class); + + private WrapperDwrCronValidatorFacade target = new WrapperDwrCronValidatorFacade((CronValidator) mockCronValidator.proxy()); + + public void testShouldReturnNextFireDateAsStringGivenCorrectCronParameters() throws Exception { + String cronExp = "* 0 0 2 ? * *"; + String startDate = "2007-01-12 14:30"; + String stopDate = "2008-09-28 18:38"; + String nextRun = "2007-10-12 15:07"; + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRun").with(eq(df.parse(startDate)), eq(df.parse(stopDate)), eq(cronExp)).will(returnValue(df.parse(nextRun))); + String result = target.getNextRun(startDate, stopDate, cronExp); + assertEquals(nextRun, result); + } + + public void testShouldReturnNextFireDateAsStringGivenWhenStartDateIsNowAndStopDateIsNever() throws Exception { + String cronExp = "* 0 0 2 ? * *"; + String nextRun = "2007-10-12 15:07"; + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRun").with(eq(null), eq(null), eq(cronExp)).will(returnValue(df.parse(nextRun))); + String result = target.getNextRun("Now", "Never", cronExp); + assertEquals(nextRun, result); + } + + public void testShouldTreatAnInvalidDateAsNull() throws Exception { + String cronExp = "* 0 0 2 ? * *"; + String nextRun = "2007-10-12 15:07"; + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRun").with(eq(null), eq(null), eq(cronExp)).will(returnValue(df.parse(nextRun))); + String result = target.getNextRun("blah invalid start blah", "blah invalid stop blah", cronExp); + assertEquals(nextRun, result); + } + + public void testShouldReturnErrorMessageIfUnableToGetNextRunTime() throws Exception { + String cronExp = "blah invlalid cron exp blah"; + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRun").with(eq(null), eq(null), eq(cronExp)).will(returnValue(null)); + String result = target.getNextRun("Now", "Never", cronExp); + assertTrue(result.contains("Error: Unable")); + } + + public void testShouldReturnNextNRunsFireDateAsStringGivenCorrectCronParameters() throws Exception { + String cronExp = "* 0 0 2 ? * *"; + String startDate = "2007-01-12 14:30"; + String stopDate = "2008-09-28 18:38"; + String nextRun = "2007-10-12 15:07"; + List<Date> dates = new ArrayList<Date>(); + dates.add(df.parse(nextRun)); + dates.add(df.parse(startDate)); + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRuns").with(eq(df.parse(startDate)), eq(df.parse(stopDate)), eq(cronExp), eq(20)).will(returnValue(dates)); + String[] result = target.getNextRuns(startDate, stopDate, cronExp, 20); + assertEquals(dates.size(), result.length); + } + + public void testShouldReturnNextNRunsFireDateAsStringGivenWhenStartDateIsNowAndStopDateIsNever() throws Exception { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + String cronExp = "* 0 0 2 ? * *"; + String startDate = "2007-01-12 14:30"; + String nextRun = "2007-10-12 15:07"; + List<Date> dates = new ArrayList<Date>(); + dates.add(df.parse(nextRun)); + dates.add(df.parse(startDate)); + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRuns").with(eq(null), eq(null), eq(cronExp), eq(20)).will(returnValue(dates)); + String[] result = target.getNextRuns("Now", "Never", cronExp, 20); + assertEquals(dates.size(), result.length); + } + + public void testShouldTreatAnInvalidDateAsNullWhenRetrievingMultipleRuns() throws Exception { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + String cronExp = "* 0 0 2 ? * *"; + String nextRun = "2007-10-12 15:07"; + List<Date> dates = new ArrayList<Date>(); + dates.add(df.parse(nextRun)); + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRuns").with(eq(null), eq(null), eq(cronExp), eq(20)).will(returnValue(dates)); + String[] result = target.getNextRuns("blah invalid start blah", "blah invalid stop blah", cronExp, 20); + assertEquals(nextRun, result[0]); + } + + public void testShouldReturnErrorMessageIfUnableToGetNextNRunTimes() throws Exception { + String cronExp = "blah invlalid cron exp blah"; + mockCronValidator.expects(once()).method("getErrorMessage").with(eq(cronExp)).will(returnValue(null)); + mockCronValidator.expects(once()).method("getNextRuns").with(eq(null), eq(null), eq(cronExp), eq(20)).will(returnValue(Collections.emptyList())); + String[] result = target.getNextRuns("Now", "Never", cronExp, 20); + assertTrue(result[0].contains("Error: Unable")); + } + +} Added: trunk/drawbridge/src/test/net/sf/drawbridge/sched/QuartzCronValidatorTest.java =================================================================== --- trunk/drawbridge/src/test/net/sf/drawbridge/sched/QuartzCronValidatorTest.java (rev 0) +++ trunk/drawbridge/src/test/net/sf/drawbridge/sched/QuartzCronValidatorTest.java 2008-11-13 02:16:25 UTC (rev 54) @@ -0,0 +1,73 @@ +package net.sf.drawbridge.sched; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import junit.framework.TestCase; + +public class QuartzCronValidatorTest extends TestCase { + + private QuartzCronValidator target = new QuartzCronValidator(); + + private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); + + public void testShouldReturnNullIfInvalidCronExpression() { + assertNull("returned something even though the cron expression should be invalid", target.getNextRun(null, null, "5 2 1 blah blahblahblah")); + } + + public void testShouldReturnNullIfStartDateIsAfterStopDate() throws Exception { + Date startDate = df.parse("2008-09-28"); + Date stopDate = df.parse("2007-01-12"); + assertNull("returned something even though the cron expression should be invalid", target.getNextRun(startDate, stopDate, "* 0 0 14 5 * *")); + } + + public void testShouldReturnDateOfNextFireDate() throws Exception { + String fireDate = "2007-02-01"; + Date startDate = df.parse("2007-01-12"); + Date stopDate = df.parse("2008-09-28"); + Date nextFireDate = target.getNextRun(startDate, stopDate, "* 0 0 ? 2 * *"); + assertEquals(fireDate, df.format(nextFireDate)); + } + + public void testShouldReturnNullIfStartDateIsNullAndStopDateIsBeforeToday() throws Exception { + Date stopDate = df.parse("2008-09-28"); + Date nextFireDate = target.getNextRun(null, stopDate, "* 0 0 ? 2 * *"); + assertNull(nextFireDate); + } + + public void testShouldReturnDateOfNextFireDateIfStartDateIsNullAndStopDateIsAfterToday() throws Exception { + Date stopDate = df.parse("2050-09-28"); + Date nextFireDate = target.getNextRun(null, stopDate, "* 0 0 ? 2 * *"); + assertNotNull(nextFireDate); + } + + public void testShouldReturnEmptyListIfInvalidCronExpression() { + assertEquals(0, target.getNextRuns(null, null, "blah blah blahblah", 20).size()); + } + + public void testShouldReturnEmptyListIfStartDateIsAfterStopDate() throws Exception { + Date stopDate = df.parse("2007-01-12"); + Date startDate = df.parse("2008-09-28"); + assertEquals(0, target.getNextRuns(startDate, stopDate, "* 0 0 ? 2 * *", 20).size()); + } + + public void testShouldReturnSpecifiedNumberOfNextFireTiems() throws Exception { + Date stopDate = df.parse("2007-01-12"); + Date startDate = df.parse("2008-09-28"); + assertEquals(0, target.getNextRuns(startDate, stopDate, "* 0 0 ? 2 * *", 20).size()); + } + + public void testShouldOnlyIncludeFireTimesWithinStartStopRangeRegardlessOfNumberRequested() throws Exception { + Date startDate = df.parse("2007-01-12"); + Date stopDate = df.parse("2008-09-28"); + assertEquals(20, target.getNextRuns(startDate, stopDate, "* 0 0 ? 2 * *", 20).size()); + } + + public void testShouldReturnNullIfCronExpressionIsValid() { + assertNull("cron expression is not valid", target.getErrorMessage("* 0 0 14 5 * *")); + } + + public void testShouldReturnFalseIfCronExpressionIsNotValid() { + assertEquals("cron expression is valid, but should not be", "Illegal characters for this position: 'BLA'", target.getErrorMessage("5 2 1 blah blahblahblah")); + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |