CronPredictorImpl implements CronPredictor { @Override public Optional<Date> predictNextRun(CrontabEntry schedule) { CronExpression cronExpression = Quartz.cronExpression(schedule, timeZone); return Optional.fromNullable(cronExpression.getNextValidTimeAfter(new Date(clock.nowMillis()))); } @Inject  CronPredictorImpl(Clock clock, TimeZone timeZone); @Override Optional<Date> predictNextRun(CrontabEntry schedule);  }
@Test public void testValidSchedule() { clock.advance(Amount.of(1L, Time.DAYS)); Date expectedPrediction = new Date(Amount.of(1L, Time.DAYS).as(Time.MILLISECONDS) + Amount.of(1L, Time.MINUTES).as(Time.MILLISECONDS)); assertEquals( Optional.of(expectedPrediction), cronPredictor.predictNextRun(CrontabEntry.parse("* * * * *"))); }
@Test public void testInvalidPrediction() { clock.advance(Amount.of(Long.MAX_VALUE, Time.DAYS)); assertEquals(Optional.absent(), cronPredictor.predictNextRun(CrontabEntry.parse("* * * * *"))); }
@Test public void testCronPredictorConforms() throws Exception { for (ExpectedPrediction expectedPrediction : ExpectedPrediction.getAll()) { List<Date> results = Lists.newArrayList(); clock.setNowMillis(0); for (int i = 0; i < expectedPrediction.getTriggerTimes().size(); i++) { Optional<Date> nextTriggerTime = cronPredictor.predictNextRun(expectedPrediction.parseCrontabEntry()); assertTrue(nextTriggerTime.isPresent()); Date triggerTime = nextTriggerTime.get(); results.add(triggerTime); clock.setNowMillis(triggerTime.getTime()); } assertEquals( "Cron schedule " + expectedPrediction.getSchedule() + " made unexpected predictions.", Lists.transform( expectedPrediction.getTriggerTimes(), Date::new ), results); } }