Java Code Examples for com.cronutils.model.Cron

The following examples show how to use com.cronutils.model.Cron. These examples are extracted from open source projects. 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
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 2
Source Project: centraldogma   Source File: AbstractMirror.java    License: Apache License 2.0 6 votes vote down vote up
protected AbstractMirror(Cron schedule, MirrorDirection direction, MirrorCredential credential,
                         Repository localRepo, String localPath,
                         URI remoteRepoUri, String remotePath, @Nullable String remoteBranch) {

    this.schedule = requireNonNull(schedule, "schedule");
    this.direction = requireNonNull(direction, "direction");
    this.credential = requireNonNull(credential, "credential");
    this.localRepo = requireNonNull(localRepo, "localRepo");
    this.localPath = normalizePath(requireNonNull(localPath, "localPath"));
    this.remoteRepoUri = requireNonNull(remoteRepoUri, "remoteRepoUri");
    this.remotePath = normalizePath(requireNonNull(remotePath, "remotePath"));
    this.remoteBranch = remoteBranch;

    executionTime = ExecutionTime.forCron(this.schedule);

    // Pre-calculate a constant jitter value up to 1 minute for a mirror.
    // Use the properties' hash code so that the same properties result in the same jitter.
    jitterMillis = Math.abs(Objects.hash(this.schedule.asString(), this.direction,
                                         this.localRepo.parent().name(), this.localRepo.name(),
                                         this.remoteRepoUri, this.remotePath, this.remoteBranch) /
                            (Integer.MAX_VALUE / 60000));
}
 
Example 3
Source Project: centraldogma   Source File: MirrorTest.java    License: Apache License 2.0 6 votes vote down vote up
private static <T extends Mirror> T newMirror(String remoteUri, Cron schedule,
                                              Repository repository, Class<T> mirrorType) {
    final MirrorCredential credential = mock(MirrorCredential.class);
    final Mirror mirror = Mirror.of(schedule, MirrorDirection.LOCAL_TO_REMOTE,
                                    credential, repository, "/", URI.create(remoteUri));

    assertThat(mirror).isInstanceOf(mirrorType);
    assertThat(mirror.direction()).isEqualTo(MirrorDirection.LOCAL_TO_REMOTE);
    assertThat(mirror.credential()).isSameAs(credential);
    assertThat(mirror.localRepo()).isSameAs(repository);
    assertThat(mirror.localPath()).isEqualTo("/");

    @SuppressWarnings("unchecked")
    final T castMirror = (T) mirror;
    return castMirror;
}
 
Example 4
Source Project: flow-platform-x   Source File: CronServiceTest.java    License: 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 5
/**
 * 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 6
/**
 * 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 7
Source Project: cron-utils   Source File: CronMapper.java    License: Apache License 2.0 6 votes vote down vote up
private static Function<Cron, Cron> setQuestionMark() {
    return cron -> {
        final CronField dow = cron.retrieve(CronFieldName.DAY_OF_WEEK);
        final CronField dom = cron.retrieve(CronFieldName.DAY_OF_MONTH);
        if (dow == null && dom == null) {
            return cron;
        }
        if (dow.getExpression() instanceof QuestionMark || dom.getExpression() instanceof QuestionMark) {
            return cron;
        }
        final Map<CronFieldName, CronField> fields = new EnumMap<>(CronFieldName.class);
        fields.putAll(cron.retrieveFieldsAsMap());
        if (dow.getExpression() instanceof Always) {
            fields.put(CronFieldName.DAY_OF_WEEK,
                    new CronField(CronFieldName.DAY_OF_WEEK, questionMark(), fields.get(CronFieldName.DAY_OF_WEEK).getConstraints()));
        } else {
            if (dom.getExpression() instanceof Always) {
                fields.put(CronFieldName.DAY_OF_MONTH,
                        new CronField(CronFieldName.DAY_OF_MONTH, questionMark(), fields.get(CronFieldName.DAY_OF_MONTH).getConstraints()));
            } else {
                cron.validate();
            }
        }
        return new SingleCron(cron.getCronDefinition(), new ArrayList<>(fields.values()));
    };
}
 
Example 8
Source Project: cron-utils   Source File: Issue421Test.java    License: Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testWrongIntervalsForEvery6Months() {
    LocalDateTime firstOfJanuary = LocalDateTime.of(2020, 4, 25, 0, 0);
    Clock clock = Clock.fixed(firstOfJanuary.toInstant(ZoneOffset.UTC), ZoneId.systemDefault());
    ZonedDateTime now = ZonedDateTime.now(clock);
    System.out.println("now: " + now);

    Cron cron = getEveryMonth(now, 6).instance();
    ZonedDateTime nextRun;

    nextRun = nextRun(cron, now); // first run
    Assert.assertEquals(2020, nextRun.getYear());
    Assert.assertEquals(10, nextRun.getMonthValue());

    nextRun = nextRun(cron, nextRun); // second
    Assert.assertEquals(2021, nextRun.getYear());
    Assert.assertEquals(4, nextRun.getMonthValue());
}
 
Example 9
Source Project: cron-utils   Source File: Issue340Test.java    License: 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 10
private void assertExpectedNextExecution(final String cronExpression, final ZonedDateTime lastRun,
        final ZonedDateTime expectedNextRun) {

    final String testCaseDescription = "cron expression '" + cronExpression + "' with zdt " + lastRun;
    LOGGER.debug("TESTING: " + testCaseDescription);
    final Cron cron = parser.parse(cronExpression);
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    try {
        final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(lastRun);
        if (nextExecution.isPresent()) {
            final ZonedDateTime nextRun = nextExecution.get();
            assertEquals(testCaseDescription, expectedNextRun, nextRun);
        } else {
            fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
        }
    } catch (final DateTimeException e) {
        fail("Issue #110: " + testCaseDescription + " led to " + e);
    }
}
 
Example 11
/**
 * 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 12
/**
 * Issue #154: Quartz Cron Year Pattern is not fully supported - i.e. increments on years are not supported
 * https://github.com/jmrozanec/cron-utils/issues/154
 * Duplicate of #148
 */
@Test
public void supportQuartzCronExpressionIncrementsOnYears() {
    final String[] sampleCronExpressions = {
            "0 0 0 1 * ? 2017/2",
            "0 0 0 1 * ? 2017/3",
            "0 0 0 1 * ? 2017/10",
            "0 0 0 1 * ? 2017-2047/2",
    };

    final CronParser quartzCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
    for (final String cronExpression : sampleCronExpressions) {
        final Cron quartzCron = quartzCronParser.parse(cronExpression);
        quartzCron.validate();
    }
}
 
Example 13
@Test
public void bigNumbersOnDayOfMonthField() {
    final Cron cron = parser.parse("0 0 0 31 * ?");
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final ZonedDateTime now = ZonedDateTime.of(2016, 11, 1, 0, 0, 0, 0, ZoneId.of("UTC"));

    //nextRun expected to be  2016-12-31 00:00:00 000
    //quartz-2.2.3 return the right date
    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(now);
    if (nextExecution.isPresent()) {
        final ZonedDateTime nextRun = nextExecution.get();
        assertEquals(ZonedDateTime.of(2016, 12, 31, 0, 0, 0, 0, ZoneId.of("UTC")), nextRun);
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 14
/**
 * Issue #59: Incorrect next execution time for "month" and "day of week".
 * Considers bad DoW
 */
@Test
public void testCorrectNextExecutionDoW() {
    //DoW: 0-6 -> 0, 4 (sunday, thursday)
    final Cron cron = getUnixCron("0 0 * * */4");
    Optional<ZonedDateTime> nextExecution = getNextExecutionFor(cron, ZonedDateTime.parse("2016-01-28T16:32:56.586-08:00"));
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2016-01-31T00:00:00.000-08:00"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
    nextExecution = getNextExecutionFor(cron, nextExecution.get());
    if (nextExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2016-02-04T00:00:00.000-08:00"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 15
Source Project: cron-utils   Source File: Issue223Test.java    License: 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 16
@Test
public void testDayLightSavingsSwitch() {
    //every 2 minutes
    final String expression = "* 0/2 * * * ?";
    final Cron cron = parser.parse(expression);

    // SIMULATE SCHEDULE JUST PRIOR TO DST
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd HH:mm:ss").withZone(ZoneId.of("America/Denver"));
    final ZonedDateTime startTime = ZonedDateTime.parse("2016 03 13 01:59:59", formatter);

    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final Optional<ZonedDateTime> nextExecutionBeforeDst = executionTime.nextExecution(startTime);
    if (nextExecutionBeforeDst.isPresent()) {
        final ZonedDateTime executionTimeBeforeDst = nextExecutionBeforeDst.get();
        // Assert we got 3:00am
        assertEquals("Incorrect Hour", 3, executionTimeBeforeDst.getHour());
        assertEquals("Incorrect Minute", 0, executionTimeBeforeDst.getMinute());

        final Optional<ZonedDateTime> nextExecutionAfterDst = executionTime.nextExecution(executionTimeBeforeDst.plusMinutes(1));
        if (nextExecutionAfterDst.isPresent()) {
            // SIMULATE SCHEDULE POST DST - simulate a schedule after DST 3:01 with the same cron, expect 3:02
            final ZonedDateTime executionTimeAfterDst = nextExecutionAfterDst.get();
            assertEquals("Incorrect Hour", 3, executionTimeAfterDst.getHour());
            assertEquals("Incorrect Minute", 2, executionTimeAfterDst.getMinute());

            // SIMULATE SCHEDULE NEXT DAY DST - verify after midnight on DST switch things still work as expected
            final ZonedDateTime oneDayAfterDst = ZonedDateTime.parse("2016-03-14T00:00:59Z");
            final Optional<ZonedDateTime> nextExecutionOneDayAfterDst = executionTime.nextExecution(oneDayAfterDst);
            if (nextExecutionOneDayAfterDst.isPresent()) {
                final ZonedDateTime executionTimeOneDayAfterDst = nextExecutionOneDayAfterDst.get();
                assertEquals("incorrect hour", executionTimeOneDayAfterDst.getHour(), 0);
                assertEquals("incorrect minute", executionTimeOneDayAfterDst.getMinute(), 2);
                return;
            }
        }
    }
    fail(ASSERTED_EXECUTION_NOT_PRESENT);
}
 
Example 17
Source Project: flow-platform-x   Source File: CronServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static long nextSeconds(String expression) {
    CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    CronParser parser = new CronParser(definition);

    Cron cron = parser.parse(expression);
    ExecutionTime executionTime = ExecutionTime.forCron(cron);

    ZonedDateTime now = ZonedDateTime.now();
    return executionTime.timeToNextExecution(now).get().getSeconds();
}
 
Example 18
Source Project: cron-utils   Source File: TestDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testEverySecondEveryDayInMonth() {
    final Cron cron = getCron("* * * * 1 ? *");
    final Cron otherCron = getCron("* * 12 * * ? *");
    assertEquals("every second, every day, in January", descriptor.describe(cron));
    assertEquals("every second, every day, in December", descriptor.describe(otherCron));
}
 
Example 19
/**
 * Issue #50: last execution does not match expected date when cron specifies day of week and last execution is in previous month.
 */
@Test
public void testLastExecutionDaysOfWeekOverMonthBoundary() {
    final Cron cron = getUnixCron("0 11 * * 1");
    final Optional<ZonedDateTime> lastExecution = getLastExecutionFor(cron, ZonedDateTime.parse("2015-11-02T00:10:00Z"));
    if (lastExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2015-10-26T11:00:00Z"), lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 20
/**
 * Issue #59: Incorrect next execution time for "month" and "day of week".
 * Considers Month in range 0-11 instead of 1-12
 */
@Test
public void testCorrectMonthScaleForNextExecution1() {
    final Cron cron = getUnixCron("* * */3 */4 */5");
    final Optional<ZonedDateTime> nextExecution = getNextExecutionFor(cron, ZonedDateTime.parse("2015-12-10T16:32:56.586-08:00"));
    if (nextExecution.isPresent()) {
        //DoW: 0-6 -> 0, 5 (sunday, friday)
        //DoM: 1-31 -> 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31
        //M: 1-12 -> 1, 5, 9
        assertEquals(ZonedDateTime.parse("2016-01-01T00:00:00.000-08:00"), nextExecution.get());
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 21
/**
 * Issue #52: "And" doesn't work for day of the week
 * 1,2 should be Monday and Tuesday, but instead it is treated as 1st/2nd of month.
 */
@Test
public void testWeekdayAndLastExecution() {
    final Cron cron = getUnixCron("* * * * 1,2");
    final Optional<ZonedDateTime> lastExecution = getLastExecutionFor(cron, ZonedDateTime.parse("2015-11-10T17:01:00Z"));
    if (lastExecution.isPresent()) {
        assertEquals(ZonedDateTime.parse("2015-11-10T17:00:00Z"), lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 22
Source Project: cron-utils   Source File: TestDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testEverySecondEveryDayInYear() {
    final Cron cron = getCron("* * * * * ? 2017");
    final Cron otherCron = getCron("* * * * * ? 2018");
    //maybe we could assert only "every second in 2017"
    assertEquals("every second, every day, in 2017", descriptor.describe(cron));
    assertEquals("every second, every day, in 2018", descriptor.describe(otherCron));
}
 
Example 23
Source Project: cron-utils   Source File: Issue343Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSpringCronExpressions() {
    CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING));

    try {
        Cron parsed = parser.parse(expression);
        Assert.assertNotNull(parsed);
    } catch (IllegalArgumentException e) {
        Assert.fail("This expression should pass: " + expression);
    }
}
 
Example 24
Source Project: cron-utils   Source File: Issue421Test.java    License: Apache License 2.0 5 votes vote down vote up
private static ZonedDateTime nextRun(Cron cron, ZonedDateTime when) {
    final Optional<ZonedDateTime> next = ExecutionTime.forCron(cron).nextExecution(when);
    if (!next.isPresent()) {
        fail();
    }
    System.out.println("Calculated next run at " + next.get());
    return next.get();
}
 
Example 25
Source Project: cron-utils   Source File: TestDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testAtTimeOfSpecificDayBetweenMultipleMonths() {
    final Cron cron = getCron("0 0 16 15 3-7,10-12 ? *");
    assertEquals("at 16:00:00pm, on the 15th day every month between March and July and every month between October and December",
                 descriptor.describe(cron));
}
 
Example 26
/**
 * https://github.com/jmrozanec/cron-utils/issues/336
 */
@Test
public void testEveryDayPerWeek() {
    CronParser cronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    // every 3 days - Sun (1), Wed (4), Sat (7)
    String cronString = "0 0 * * */3";
    Cron cron = cronParser.parse(cronString);
    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    Optional<ZonedDateTime> nextExecution = executionTime
            .nextExecution(ZonedDateTime.of(2018, 2, 8, 0, 0, 0, 0, ZoneId.of("UTC")));
    assertTrue(nextExecution.isPresent());
    assertEquals(ZonedDateTime.of(2018, 2, 10, 0, 0, 0, 0, ZoneId.of("UTC")), nextExecution.get());
}
 
Example 27
/**
 * 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 28
Source Project: cron-utils   Source File: Issue228Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEveryWeekdayFirstWeekOfMonthNextExecution() {
    final CronParser parser = new CronParser(cronDefinition);

    // This is 9am on Mon-Fri day between the 1st and 7th (in this case it should be Oct 2)
    final Cron myCron = parser.parse("0 9 1-7 * 1-5");
    final ZonedDateTime time = ZonedDateTime.parse(TEST_DATE);
    assertEquals(ZonedDateTime.parse("2017-10-02T09:00-07:00"), getNextExecutionTime(myCron, time));
}
 
Example 29
@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 30
@Test
public void testCronExpressionAfterHalf() {
    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("*/30 * * * * *");

    final ZonedDateTime startDateTime = ZonedDateTime.of(2015, 8, 28, 12, 5, 44, 0, UTC);
    final ZonedDateTime expectedDateTime = ZonedDateTime.of(2015, 8, 28, 12, 6, 0, 0, UTC);

    final ExecutionTime executionTime = ExecutionTime.forCron(cron);

    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(startDateTime);
    if (nextExecution.isPresent()) {
        final ZonedDateTime nextExecutionDateTime = nextExecution.get();
        assertEquals(expectedDateTime, nextExecutionDateTime);
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}