com.cronutils.parser.CronParser Java Examples

The following examples show how to use com.cronutils.parser.CronParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Issue200Test.java    From cron-utils with Apache License 2.0 7 votes vote down vote up
@Test
public void testMustMatchCronEvenIfNanoSecondsVaries() {
    final CronDefinition cronDefinition =
            CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);

    final CronParser parser = new CronParser(cronDefinition);
    final Cron quartzCron = parser.parse("00 00 10 * * ?");

    quartzCron.validate();

    // NOTE: Off by 3 nano seconds
    final ZonedDateTime zdt = ZonedDateTime.of(1999, 07, 18, 10, 00, 00, 03, ZoneId.systemDefault());

    // Must be true
    assertTrue("Nano seconds must not affect matching of Cron Expressions", ExecutionTime.forCron(quartzCron).isMatch(zdt));
}
 
Example #2
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #124:
 * https://github.com/jmrozanec/cron-utils/issues/124
 * Reported case: next execution time is set improperly
 * Potential duplicate: https://github.com/jmrozanec/cron-utils/issues/123
 */
@Test
public void testNextExecutionTimeProperlySet2() {
    final CronParser quartzCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ));
    final String quartzCronExpression2 = "0 3/27 10-14 * * ? *";
    final Cron parsedQuartzCronExpression = quartzCronParser.parse(quartzCronExpression2);

    final ExecutionTime executionTime = ExecutionTime.forCron(parsedQuartzCronExpression);

    final ZonedDateTime zonedDateTime = LocalDateTime.of(2016, 1, 1, 10, 0, 0, 0).atZone(ZoneOffset.UTC);

    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(zonedDateTime);
    if (nextExecution.isPresent()) {
        final ZonedDateTime nextExecutionTime = nextExecution.get();
        assertEquals("2016-01-01T10:03Z", nextExecutionTime.toString());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #3
Source File: CronServiceTest.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Test
public void should_get_exec_time_from_crontab() throws InterruptedException {
    CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    CronParser parser = new CronParser(definition);

    Cron cron = parser.parse("* * * * *");
    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    Assert.assertNotNull(executionTime);

    ZonedDateTime now = ZonedDateTime.now();
    long seconds = executionTime.timeToNextExecution(now).get().getSeconds();
    log.info("--- {} ----", seconds);

    ObjectWrapper<Boolean> result = new ObjectWrapper<>(false);
    CountDownLatch counter = new CountDownLatch(1);

    service.schedule(() -> {
        result.setValue(true);
        counter.countDown();
    }, seconds, TimeUnit.SECONDS);

    counter.await();
    Assert.assertTrue(result.getValue());
}
 
Example #4
Source File: Issue293Test.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@Test
   public void test() {
       CronDefinition def = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
       CronParser parser = new CronParser(def);

       Cron cron = parser.parse(cronText);
       ExecutionTime et = ExecutionTime.forCron(cron);

       ZonedDateTime vs = ZonedDateTime.of(2017, 12, 1, 9, 30, 0, 0, ZONE);
       assertEquals(DayOfWeek.FRIDAY, vs.getDayOfWeek());

       // Last match prior to our reference time
       ZonedDateTime expected = ZonedDateTime.of(2017, 11, 30, 18, 15, 0, 0, ZONE);
       assertEquals(DayOfWeek.THURSDAY, expected.getDayOfWeek());

Optional<ZonedDateTime> lastExecution = et.lastExecution(vs);
if (lastExecution.isPresent()) {
    ZonedDateTime actual = lastExecution.get();
    assertEquals(expected, actual);
} else {
    fail("last execution was not present");
}
   }
 
Example #5
Source File: CronServiceTest.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Test
public void should_get_exec_time_from_crontab() throws InterruptedException {
    CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    CronParser parser = new CronParser(definition);

    Cron cron = parser.parse("* * * * *");
    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    Assert.assertNotNull(executionTime);

    ZonedDateTime now = ZonedDateTime.now();
    long seconds = executionTime.timeToNextExecution(now).get().getSeconds();
    log.info("--- {} ----", seconds);

    ObjectWrapper<Boolean> result = new ObjectWrapper<>(false);
    CountDownLatch counter = new CountDownLatch(1);

    service.schedule(() -> {
        result.setValue(true);
        counter.countDown();
    }, seconds, TimeUnit.SECONDS);

    counter.await();
    Assert.assertTrue(result.getValue());
}
 
Example #6
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #142: https://github.com/jmrozanec/cron-utils/pull/142
 * Special Character L for day of week behaves differently in Quartz
 * @throws ParseException in case the CronExpression can not be created
 */
@Test
public void lastDayOfTheWeek() throws ParseException {
    // L (“last”) - If used in the day-of-week field by itself, it simply means “7” or “SAT”.
    final Cron cron = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ)).parse("0 0 0 ? * L *");

    final ZoneId utc = ZoneId.of("UTC");
    final ZonedDateTime date = LocalDate.parse("2016-12-22").atStartOfDay(utc);   // Thursday
    final ZonedDateTime expected = date.plusDays(2);   // Saturday

    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(cron).nextExecution(date);
    if (nextExecution.isPresent()) {
        final ZonedDateTime cronUtilsNextTime = nextExecution.get();// 2016-12-30T00:00:00Z

        final org.quartz.CronExpression cronExpression = new org.quartz.CronExpression(cron.asString());
        cronExpression.setTimeZone(TimeZone.getTimeZone(utc));
        final Date quartzNextTime = cronExpression.getNextValidTimeAfter(Date.from(date.toInstant()));// 2016-12-24T00:00:00Z

        assertEquals(expected.toInstant(), quartzNextTime.toInstant());    // test the reference implementation
        assertEquals(expected.toInstant(), cronUtilsNextTime.toInstant()); // and compare with cronUtils
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #7
Source File: QuartzServiceImpl.java    From spring-batch-quartz-admin with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param jobName
 * @return String
 */
public String getScheduledJobDescription(String jobName) {
    String message = Constants.JOB_IS_NOT_SCHEDULED;
    JobKey jobKey = new JobKey(jobName, Constants.QUARTZ_GROUP);
    try {
        JobDetail jobDetail = schedulerFactory.getScheduler().getJobDetail(jobKey);
        if (null != jobDetail) {
            List<? extends Trigger> triggersOfJob = schedulerFactory.getScheduler().getTriggersOfJob(jobKey);
            if (null != triggersOfJob && !triggersOfJob.isEmpty()) {
                CronTrigger trigger = (CronTrigger) triggersOfJob.get(0);
                String cronExpression = trigger.getCronExpression();
                CronDescriptor descriptor = CronDescriptor.instance(Locale.US);
                CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ));
                message = descriptor.describe(parser.parse(cronExpression));
            }

        }
    } catch (SchedulerException e) {
        BatchAdminLogger.getLogger().error(e.getMessage(), e);
    }
    return message;
}
 
Example #8
Source File: Issue223Test.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #223: for dayOfWeek value == 3 && division of day, nextExecution do not return correct results.
 */
@Test
public void testEveryWednesdayOfEveryDayNextExecution() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    final Cron myCron = parser.parse("* * * * 3");
    ZonedDateTime time = ZonedDateTime.parse("2017-09-05T11:31:55.407-05:00");
    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(myCron).nextExecution(time);
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2017-09-06T00:00-05:00"), nextExecution.get());
    } else {
        fail("next execution was not present");
    }

    final Cron myCron2 = parser.parse("* * */1 * 3");
    time = ZonedDateTime.parse("2017-09-05T11:31:55.407-05:00");
    final Optional<ZonedDateTime> nextExecution2 = ExecutionTime.forCron(myCron2).nextExecution(time);
    if (nextExecution2.isPresent()) {
        assertEquals(ZonedDateTime.parse("2017-09-06T00:00-05:00"), nextExecution2.get());
    } else {
        fail("next execution was not present");
    }
}
 
Example #9
Source File: CompositeCronTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp(){
    definition1 = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    CronParser parser = new CronParser(definition1);

    Cron cron1 = parser.parse("0 0 0 15 8 ? 2015/2");
    Cron cron2 = parser.parse("0 0 0 16 9 ? 2015/2");
    Cron cron3 = parser.parse("0 0 0 17 10 ? 2015/2");
    List<Cron> crons = new ArrayList<>();
    crons.add(cron1);
    crons.add(cron2);
    this.cron1 = new CompositeCron(crons);
    List<Cron> crons2 = new ArrayList<>();
    crons2.add(cron2);
    crons2.add(cron3);
    this.cron2 = new CompositeCron(crons2);
}
 
Example #10
Source File: Issue319Test.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@Test
// Daylightsaving change in EU is - 2018-03-25T02:00
// - Bug319: endless loop/fails/hangs at 2018-03-25T02:00 and 2018-03-26T02:00
public void testPreviousClosestMatchDailightSavingsChangeBug319_loop() {
    CronParser cronparser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    for (int month = 1; month < 13; month++) {
        for (int day = 1; day < 29; day++) {
            ZonedDateTime date = ZonedDateTime.of(2018, month, day, 2, 00, 00, 0, ZoneId.of("Europe/Berlin"));
            System.out.print(date);
            Cron cron = cronparser.parse("00 02 * * * ");
            ExecutionTime exectime = ExecutionTime.forCron(cron);
            ZonedDateTime lastrun = exectime.lastExecution(date).get();
            System.out.println("-ok");
        }
    }
}
 
Example #11
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #45: next execution does not match expected date. Result is not in same timezone as reference date.
 */
@Test
public void testMondayWeekdayNextExecution() {
    final String crontab = "* * * * 1";
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    final Cron cron = parser.parse(crontab);
    final ZonedDateTime date = ZonedDateTime.parse("2015-10-13T17:26:54.468-07:00");
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2015-10-19T00:00:00.000-07:00"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #12
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #61: lastExecution over daylight savings is wrong.
 */
@Test
public void testLastExecutionDaylightSaving() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("0 17 * * *"));// daily at 17:00
    // Daylight savings for New York 2016 is Mar 13 at 2am
    final ZonedDateTime now = ZonedDateTime.of(2016, 3, 12, 17, 0, 0, 0, ZoneId.of("America/Phoenix"));
    final Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(now);
    if (lastExecution.isPresent()) {
        final long millis = Duration.between(lastExecution.get(), now).toMillis();
        assertEquals(24, (millis / 3600000));
        assertEquals(now.getZone(), lastExecution.get().getZone());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #13
Source File: TimerTaskExecutorProvider.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
public static Flux<ZonedDateTime> getLastExecuteTimes(String cronExpression, Date from, long times) {
    return Flux.create(sink -> {
        CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
        Cron cron = parser.parse(cronExpression);
        ExecutionTime executionTime = ExecutionTime.forCron(cron);
        ZonedDateTime dateTime = ZonedDateTime.ofInstant(from.toInstant(), ZoneId.systemDefault());

        for (long i = 0; i < times; i++) {
            dateTime = executionTime.nextExecution(dateTime)
                .orElse(null);
            if (dateTime != null) {
                sink.next(dateTime);
            } else {
                break;
            }
        }
        sink.complete();


    });
}
 
Example #14
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #123:
 * https://github.com/jmrozanec/cron-utils/issues/123
 * Reported case: next execution time is set improperly
 * Potential duplicate: https://github.com/jmrozanec/cron-utils/issues/124
 */
@Test
public void testNextExecutionTimeProperlySet() {
    final CronParser quartzCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ));
    final String quartzCronExpression2 = "0 5/15 * * * ? *";
    final Cron parsedQuartzCronExpression = quartzCronParser.parse(quartzCronExpression2);

    final ExecutionTime executionTime = ExecutionTime.forCron(parsedQuartzCronExpression);

    final ZonedDateTime zonedDateTime = LocalDateTime.of(2016, 7, 30, 15, 0, 0, 527).atZone(ZoneOffset.UTC);
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(zonedDateTime);
    final Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(zonedDateTime);
    if (nextExecution.isPresent() && lastExecution.isPresent()) {
        final ZonedDateTime nextExecutionTime = nextExecution.get();
        final ZonedDateTime lastExecutionTime = lastExecution.get();

        assertEquals("2016-07-30T14:50Z", lastExecutionTime.toString());
        assertEquals("2016-07-30T15:05Z", nextExecutionTime.toString());
    } else {
        fail(ASSERTED_EXECUTION_NOT_PRESENT);
    }
}
 
Example #15
Source File: ExecutionTimeCustomDefinitionIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * A CronDefinition with only 3 required fields is legal to instantiate, but the parser considers an expression
 * with 4 fields as an error:
 * java.lang.IllegalArgumentException: Cron expression contains 4 parts but we expect one of [6, 7]
 */
@Test
public void testThreeRequiredFieldsSupported() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
            .withSeconds().and()
            .withMinutes().and()
            .withHours().and()
            .withDayOfMonth().supportsL().supportsW().supportsLW().supportsQuestionMark().optional().and()
            .withMonth().optional().and()
            .withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2).supportsHash().supportsL()
            .supportsQuestionMark().optional().and()
            .withYear().withValidRange(2000, 2099).optional().and()
            .instance();
    final CronParser cronParser = new CronParser(cronDefinition);
    cronParser.parse("* * 4 3");
}
 
Example #16
Source File: ExecutionTimeCustomDefinitionIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #136: Bug exposed at PR #136
 * https://github.com/jmrozanec/cron-utils/pull/136
 * Reported case: when executing isMatch for a given range of dates,
 * if date is invalid, we get an exception, not a boolean as response.
 */
@Test
public void testMatchWorksAsExpectedForCustomCronsWhenPreviousOrNextOccurrenceIsMissing() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
            .withDayOfMonth()
            .supportsL().supportsW()
            .and()
            .withMonth().and()
            .withYear()
            .and().instance();

    final CronParser parser = new CronParser(cronDefinition);
    final Cron cron = parser.parse("05 05 2004");
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    ZonedDateTime start = ZonedDateTime.of(2004, 5, 5, 23, 55, 0, 0, ZoneId.of("UTC"));
    final ZonedDateTime end = ZonedDateTime.of(2004, 5, 6, 1, 0, 0, 0, ZoneId.of("UTC"));
    while (start.compareTo(end) < 0) {
        assertTrue(executionTime.isMatch(start) == (start.getDayOfMonth() == 5));
        start = start.plusMinutes(1);
    }
}
 
Example #17
Source File: ExecutionTimeCustomDefinitionIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Test for issue #38
 * https://github.com/jmrozanec/cron-utils/issues/38
 * Reported case: lastExecution and nextExecution do not work properly
 * Expected: should return expected date
 */
@Test
public void testCronExpressionEveryTwoHoursAsteriskSlash() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
            .withSeconds().and()
            .withMinutes().and()
            .withHours().and()
            .withDayOfMonth().and()
            .withMonth().and()
            .withDayOfWeek().withValidRange(0, 7).withMondayDoWValue(1).withIntMapping(7, 0).and()
            .instance();

    final CronParser parser = new CronParser(cronDefinition);
    final Cron cron = parser.parse("0 0 */2 * * *");
    final ZonedDateTime startDateTime = ZonedDateTime.parse("2015-08-28T12:05:14.000-03:00");

    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(cron).nextExecution(startDateTime);
    if (nextExecution.isPresent()) {
        assertTrue(ZonedDateTime.parse("2015-08-28T14:00:00.000-03:00").compareTo(nextExecution.get()) == 0);
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #18
Source File: Issue340Test.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testDayOfWeekRollover() {
    // Every Friday to Tuesday (Fri, Sat, Sun, Mon, Tue) at 5 AM
    String schedule = "0 0 5 ? * FRI-TUE *";
    // Java DayOfWeek is MON (1) to SUN (7)
    Set<Integer> validDaysOfWeek = Sets.newSet(1, 2, 5, 6, 7);
    CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
    CronParser parser = new CronParser(cronDefinition);
    Cron quartzCron = parser.parse(schedule);

    ZonedDateTime time = ZonedDateTime.now();
    ExecutionTime executionTime = ExecutionTime.forCron(quartzCron);

    // Check the next 100 execution times
    for (int i = 0; i < 100; i++) {
        Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(time);
        Assert.assertTrue(nextExecution.isPresent());
        time = nextExecution.get();
        int dayOfWeek = time.getDayOfWeek().getValue();
        Assert.assertTrue(validDaysOfWeek.contains(dayOfWeek));
        Assert.assertEquals(5, time.getHour());
        Assert.assertEquals(0, time.getMinute());
        Assert.assertEquals(0, time.getSecond());
    }
}
 
Example #19
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #112: Calling nextExecution exactly on the first instant of the fallback hour (after the DST ends) makes it go back to DST.
 * https://github.com/jmrozanec/cron-utils/issues/112
 */
@Test
public void testWrongNextExecutionOnDSTEnd() {
    final ZoneId zone = ZoneId.of("America/Sao_Paulo");

    //2016-02-20T23:00-03:00[America/Sao_Paulo], first minute of fallback hour
    final ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1456020000000L), zone);
    final ZonedDateTime expected = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1456020000000L + 60000), zone);

    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * *"));
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
    if (nextExecution.isPresent()) {
        assertEquals(expected, nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #20
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #79: Next execution skipping valid date.
 */
@Test
public void testNextExecution2014() {
    final String crontab = "0 8 * * 1";//m,h,dom,m,dow ; every monday at 8AM
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    final Cron cron = parser.parse(crontab);
    final ZonedDateTime date = ZonedDateTime.parse("2014-11-30T00:00:00Z");
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2014-12-01T08:00:00Z"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }

}
 
Example #21
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #61: nextExecution over daylight savings is wrong.
 */
@Test
public void testNextExecutionDaylightSaving() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("0 17 * * *"));// daily at 17:00
    // Daylight savings for New York 2016 is Mar 13 at 2am
    final ZonedDateTime last = ZonedDateTime.of(2016, 3, 12, 17, 0, 0, 0, ZONE_ID_NEW_YORK);
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(last);
    if (nextExecution.isPresent()) {
        final long millis = Duration.between(last, nextExecution.get()).toMillis();
        assertEquals(23, (millis / 3600000));
        assertEquals(last.getZone(), nextExecution.get().getZone());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #22
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #41: for everything other than a dayOfWeek value == 1, nextExecution and lastExecution do not return correct results.
 */
@Test
public void testEveryTuesdayAtThirdHourOfDayNextExecution() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    final Cron myCron = parser.parse("0 3 * * 3");
    final ZonedDateTime time = ZonedDateTime.parse("2015-09-17T00:00:00.000-07:00");
    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(myCron).nextExecution(time);
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2015-09-23T03:00:00.000-07:00"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #23
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #92: Next execution skipping valid date.
 */
@Test
public void testNextExecution2016() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("1 0 * * tue"));
    final ZonedDateTime date = ZonedDateTime.parse("2016-05-24T01:02:50Z");
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2016-05-31T00:01:00Z"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #24
Source File: ScheduleHelper.java    From ResearchStack with Apache License 2.0 5 votes vote down vote up
public static Date nextSchedule(String cronString, Date lastExecution) {
    DateTime now = new DateTime(lastExecution);
    CronParser cronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    Cron cron = cronParser.parse(cronString);

    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    DateTime nextExecution = executionTime.nextExecution(now);

    return nextExecution.toDate();
}
 
Example #25
Source File: Issue143Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    // Make sure that current date is before Dec-31
    currentDateTime = ZonedDateTime.of(LocalDateTime.of(2016, 12, 20, 12, 0),
            ZoneId.systemDefault());

    parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
}
 
Example #26
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #37: for pattern "every 10 minutes", nextExecution returns a date from past.
 */
@Test
public void testEveryTenMinutesNextExecution() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("*/10 * * * *"));
    final ZonedDateTime time = ZonedDateTime.parse("2015-09-05T13:43:00.000-07:00");
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(time);
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2015-09-05T13:50:00.000-07:00"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #27
Source File: CompositeCronTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testExampleIssue318(){
    CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    CronParser parser = new CronParser(definition);
    Cron cron1 = parser.parse("0 0 9 * * ? *");
    Cron cron2 = parser.parse("0 0 10 * * ? *");
    Cron cron3 = parser.parse("0 30 11 * * ? *");
    Cron cron4 = parser.parse("0 0 12 * * ? *");

    List<Cron> crons = new ArrayList<>();
    crons.add(cron1);
    crons.add(cron2);
    crons.add(cron3);
    crons.add(cron4);
    Cron composite = new CompositeCron(crons);

    ZonedDateTime defaultt = ZonedDateTime.of(2000, 4, 15, 0, 0, 0, 0, UTC);

    assertEquals("0 0|0|30|0 9|10|11|12 * * ? *", composite.asString());
    ExecutionTime executionTime = ExecutionTime.forCron(composite);
    ZonedDateTime date1 = ZonedDateTime.of(2015, 4, 15, 0, 0, 0, 0, UTC);
    assertEquals(ZonedDateTime.of(2015, 4, 15, 9, 0, 0, 0, UTC), executionTime.nextExecution(date1).orElse(defaultt));
    ZonedDateTime date2 = ZonedDateTime.of(2015, 4, 15, 9, 30, 0, 0, UTC);
    assertEquals(ZonedDateTime.of(2015, 4, 15, 10, 0, 0, 0, UTC), executionTime.nextExecution(date2).orElse(defaultt));
    ZonedDateTime date3 = ZonedDateTime.of(2015, 4, 15, 11, 0, 0, 0, UTC);
    assertEquals(ZonedDateTime.of(2015, 4, 15, 11, 30, 0, 0, UTC), executionTime.nextExecution(date3).orElse(defaultt));
    ZonedDateTime date4 = ZonedDateTime.of(2015, 4, 15, 11, 30, 0, 0, UTC);
    assertEquals(ZonedDateTime.of(2015, 4, 15, 12, 0, 0, 0, UTC), executionTime.nextExecution(date4).orElse(defaultt));
    ZonedDateTime date5 = ZonedDateTime.of(2015, 4, 15, 12, 30, 0, 0, UTC);
    assertEquals(ZonedDateTime.of(2015, 4, 16, 9, 0, 0, 0, UTC), executionTime.nextExecution(date5).orElse(defaultt));
}
 
Example #28
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsMatchForUnix01() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final String crontab = "* * * * *";//m,h,dom,M,dow
    final Cron cron = parser.parse(crontab);
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final ZonedDateTime scanTime = ZonedDateTime.parse("2016-02-29T11:00:00.000-06:00");
    assertTrue(executionTime.isMatch(scanTime));
}
 
Example #29
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #41: for everything other than a dayOfWeek value == 1, nextExecution and lastExecution do not return correct results.
 */
@Test
public void testEveryTuesdayAtThirdHourOfDayLastExecution() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    final Cron myCron = parser.parse("0 3 * * 3");
    final ZonedDateTime time = ZonedDateTime.parse("2015-09-17T00:00:00.000-07:00");
    final Optional<ZonedDateTime> lastExecution = ExecutionTime.forCron(myCron).lastExecution(time);
    if (lastExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2015-09-16T03:00:00.000-07:00"), lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #30
Source File: CronTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
    final CronDefinition cron4jcd = CronDefinitionBuilder.instanceDefinitionFor(CronType.CRON4J);
    final CronDefinition unixcd = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronDefinition quartzcd = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    final CronParser unix = new CronParser(unixcd);
    final CronParser quartz = new CronParser(quartzcd);
    final CronParser cron4j = new CronParser(cron4jcd);

    final Cron[] toTest = new Cron[] {
            unix.parse("* * * * MON"),
            unix.parse("*/1 * * * 1"),
            unix.parse("0 * * * *"),
            unix.parse("*/2 * * * *"),
            quartz.parse("0 * * ? * MON *"),
            cron4j.parse("* 1 1,2 * 4"),
            cron4j.parse("* 1 1-2 * 4"),
            cron4j.parse("0 18 * * 1"),
            cron4j.parse("0/15 * * * *"),
            cron4j.parse("0 0/2 * * *"),
            cron4j.parse("0 6 * * MON-FRI")
    };

    for (final Cron expected : toTest) {
        final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try (ObjectOutputStream objOut = new ObjectOutputStream(byteOut)) {
            objOut.writeObject(expected);
        }

        try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()))) {
            final Cron actual = (Cron) objIn.readObject();
            assertEquals(expected.asString(), actual.asString());
        }
    }
}