com.cronutils.model.Cron Java Examples

The following examples show how to use com.cronutils.model.Cron. 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: 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 #2
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
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 #3
Source File: AbstractMirror.java    From centraldogma with 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 #4
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 #5
Source File: MirrorTest.java    From centraldogma with 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 #6
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 #7
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 #8
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 #9
Source File: CronMapper.java    From cron-utils with 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 #10
Source File: Issue421Test.java    From cron-utils with 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 #11
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 #12
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 #13
Source File: CronParserQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #14
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@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 #15
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #16
Source File: CronServiceImpl.java    From flow-platform-x with 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 #17
Source File: TestDescriptor.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testFull() {
    final Cron cron = getCron("3/4 5/6 7/8 9/2 10/2 ? 2017");
    assertEquals("every 4 seconds starting at second 03, every 6 minutes starting at minute 05, every 8 hours starting at 07am, "
                 + "every 2 days startint on the 9th, every 2 months starting in October, in 2017", descriptor.describe(cron));
}
 
Example #18
Source File: TestDescriptor.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testEverySecondOnTheDayEveryMonthWithoutYear() {
    final Cron first = getCron("* * * 1 * ?");
    final Cron second = getCron("* * * 1 * ?");
    final Cron third = getCron("* * * 1 * ?");
    final Cron otherCron = getCron("* * * 15 * ?");
    assertEquals("every second, on the 1st day, every month", descriptor.describe(first));
    assertEquals("every second, on the 2nd day, every month", descriptor.describe(second));
    assertEquals("every second, on the 3rd day, every month", descriptor.describe(third));
    assertEquals("every second, on the 15th day, every month", descriptor.describe(otherCron));
}
 
Example #19
Source File: TestDescriptor.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testAtSecondEveryMinuteWithoutYear() {
    final Cron cron = getCron("1 * * * * ?");
    final Cron otherCron = getCron("15 * * * * ?");
    assertEquals("at second 01 of every minute", descriptor.describe(cron));
    assertEquals("at second 15 of every minute", descriptor.describe(otherCron));
}
 
Example #20
Source File: CronParserQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #39: reported issue about exception being raised on parse.
 */
@Test
public void testDescribeExpressionWithQuestionMarkAndWeekdays() {
    final Cron quartzCron = parser.parse("0 0 0 ? * MON,TUE *");
    final CronDescriptor descriptor = CronDescriptor.instance(Locale.ENGLISH);
    descriptor.describe(quartzCron);
}
 
Example #21
Source File: Issue215Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
private void checkNextExecution(final LocalDateTime startDate, final LocalDateTime expectedNextExecution, final Cron cron) {
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final ZonedDateTime zonedDateTime = ZonedDateTime.of(startDate, ZoneId.systemDefault());
    final Optional<ZonedDateTime> next = executionTime.nextExecution(zonedDateTime);
    assert (next.isPresent());
    assertEquals(ZonedDateTime.of(expectedNextExecution, ZoneId.systemDefault()), next.get());
}
 
Example #22
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #23
Source File: Issue228Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
private ZonedDateTime getNextExecutionTime(final Cron cron, final ZonedDateTime time) {
    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(cron).nextExecution(time);
    if (nextExecution.isPresent()) {
        return nextExecution.get();
    } else {
        throw new NullPointerException("next execution was not present");
    }
}
 
Example #24
Source File: Issue228Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testEveryWeekendForthWeekOfMonthNextExecution() {
    final CronParser parser = new CronParser(cronDefinition);

    // This is 9am on Sat and Sun day between the 22nd and 28th (in this case it should be Oct 22)
    final Cron myCron = parser.parse("0 9 22-28 * 6-7");
    final ZonedDateTime time = ZonedDateTime.parse(TEST_DATE);
    assertEquals(ZonedDateTime.parse("2017-10-22T09:00-07:00"), getNextExecutionTime(myCron, time));
}
 
Example #25
Source File: ExecutionTimeCustomDefinitionIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #26
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 #27
Source File: Issue228Test.java    From cron-utils with 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 #28
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@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 #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 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 #30
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #114: Describe day of week is incorrect.
 */
@Test
public void descriptionForExpressionTellsWrongDoW() {
    final CronDescriptor descriptor = CronDescriptor.instance();
    final Cron quartzCron = parser.parse("0 0 8 ? * SUN *");
    assertEquals("at 08:00 at Sunday day", descriptor.describe(quartzCron));
}