com.cronutils.model.CronType Java Examples

The following examples show how to use com.cronutils.model.CronType. 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: Issue305Test.java    From cron-utils with Apache License 2.0 8 votes vote down vote up
@Test
public void testIssue305(){
    CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    CronParser parser = new CronParser(cronDefinition);
    Cron cron = parser.parse("0 0 0 15 8 ? 2015-2099/2");

    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")));
    Set<ZonedDateTime> dates = new LinkedHashSet<>();
    while (!nextExecution.get().isAfter(ZonedDateTime.of(2020, 12, 31, 0, 0, 0, 0, ZoneId.of("UTC")))) {
        dates.add(nextExecution.get());
        nextExecution = executionTime.nextExecution(nextExecution.get());
    }
    Set<Integer> years = dates.stream().map(d->d.getYear()).collect(Collectors.toSet());
    Set<Integer> expectedYears = new HashSet<>();
    expectedYears.add(2015);
    expectedYears.add(2017);
    expectedYears.add(2019);
    assertEquals(expectedYears, years);
}
 
Example #2
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 #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: 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 #5
Source File: CronPolicy.java    From kafka-connect-fs with Apache License 2.0 6 votes vote down vote up
@Override
protected void configPolicy(Map<String, Object> customConfigs) {
    try {
        if (customConfigs.get(CRON_POLICY_END_DATE) != null &&
                !customConfigs.get(CRON_POLICY_END_DATE).toString().equals("")) {
            endDate = Date.from(LocalDateTime.parse(customConfigs.get(CRON_POLICY_END_DATE).toString().trim())
                    .atZone(ZoneId.systemDefault()).toInstant());
        }
        executionTime = ExecutionTime.forCron(
                new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
                        .parse(customConfigs.get(CRON_POLICY_EXPRESSION).toString())
        );
    } catch (DateTimeException dte) {
        throw new ConfigException(CRON_POLICY_END_DATE + " property must have a proper value. Got: '" +
                customConfigs.get(CRON_POLICY_END_DATE) + "'.");
    } catch (IllegalArgumentException iae) {
        throw new ConfigException(CRON_POLICY_EXPRESSION + " property must have a proper value. Got: '" +
                customConfigs.get(CRON_POLICY_EXPRESSION) + "'.");
    }
}
 
Example #6
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 #7
Source File: MappingOptionalFieldsTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testMappingOptionalFields() {
    CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
            .withMinutes().withStrictRange().withValidRange(0, 60).and()
            .withHours().withStrictRange().and()
            .withDayOfMonth().supportsL().withStrictRange().and()
            .withMonth().withStrictRange().and()
            .withDayOfWeek().withValidRange(0, 7).withMondayDoWValue(1).withIntMapping(7, 0).supportsHash().supportsL().withStrictRange().and()
            .withYear().optional().and()
            .instance();

    final CronDefinition quartzDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);

    CronParser parser = new CronParser(quartzDefinition);
    CronMapper mapper = new CronMapper(quartzDefinition, cronDefinition, cron -> cron);


    final String expected = "0 9-18 * * 0-2";
    final String expression = "5 0 9-18 ? * 1-3";
    final String mapping = mapper.map(parser.parse(expression)).asString();
    assertEquals(String.format("Expected [%s] but got [%s]", expected, mapping), expected, mapping);
}
 
Example #8
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 #9
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #38: every 2 min schedule doesn't roll over to next hour.
 */
@Test
public void testEveryTwoMinRollsOverHour() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final Cron cron = new CronParser(cronDefinition).parse("*/2 * * * *");
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final ZonedDateTime time = ZonedDateTime.parse("2015-09-05T13:56:00.000-07:00");
    final Optional<ZonedDateTime> nextExecutionTime = executionTime.nextExecution(time);
    if (nextExecutionTime.isPresent()) {
        final ZonedDateTime next = nextExecutionTime.get();
        final Optional<ZonedDateTime> shouldBeInNextHourExecution = executionTime.nextExecution(next);
        if (shouldBeInNextHourExecution.isPresent()) {
            assertEquals(next.plusMinutes(2), shouldBeInNextHourExecution.get());
            return;
        }
    }
    fail("one of the asserted values was not present.");
}
 
Example #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: CronParserTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test // issue #180
public void testThatEveryMinuteIsPreserved() {
    final CronDefinition quartzDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    parser = new CronParser(quartzDefinition);

    final Cron expression = parser.parse("0 0/1 * 1/1 * ? *");
    assertEquals("0 0/1 * 1/1 * ? *", expression.asString());
}
 
Example #16
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #125: Prints stack trace for NoSuchValueException for expressions with comma-separated values
 * https://github.com/jmrozanec/cron-utils/issues/125
 */
@Test
public void testNextExecutionProducesStackTraces() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("45 1,13 * * *"));
    executionTime.nextExecution(ZonedDateTime.parse("2016-05-24T01:02:50Z"));
}
 
Example #17
Source File: CronParserTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMulticron(){
    String multicron = "0 0|0|30|0 9|10|11|12 * * ? *";
    parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
    Cron cron = parser.parse(multicron);
    assertEquals(multicron, cron.asString());
}
 
Example #18
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsMatchForUnix02() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final String crontab = "0 * * * 1-5";//m,h,dom,M,dow
    final Cron cron = parser.parse(crontab);
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    final ZonedDateTime scanTime = ZonedDateTime.parse("2016-03-04T11:00:00.000-06:00");
    assertTrue(executionTime.isMatch(scanTime));
}
 
Example #19
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 #20
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 #21
Source File: Issue388Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
public void testLastAndNextExecutionWithDowAndDom() {

        ZonedDateTime dateTime = ZonedDateTime.of(2019, 07, 01, 8, 0, 0, 0, UTC);

        CronParser springCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING));
        Cron springCron = springCronParser.parse("0 0 8 1-7 * SAT");
        ExecutionTime springExecutionTime = ExecutionTime.forCron(springCron);
        ZonedDateTime nextSpringExecutionTime = springExecutionTime.nextExecution(dateTime).get();
        ZonedDateTime lastSpringExecutionTime = springExecutionTime.lastExecution(dateTime).get();

        //quartz
        CronParser quartzCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
        Cron quartzCron = quartzCronParser.parse("0 0 8 ? * SAT#1");
        ExecutionTime quartzExecutionTime = ExecutionTime.forCron(quartzCron);
        ZonedDateTime nextQuartzExecutionTime = quartzExecutionTime.nextExecution(dateTime).get();
        ZonedDateTime lastQuartzExecutionTime = quartzExecutionTime.lastExecution(dateTime).get();

        ZonedDateTime lastMonthFirstSaturday = dateTime.withMonth(6)
                                                       .with(firstInMonth(DayOfWeek.SATURDAY))
                                                       .withHour(8)
                                                       .withMinute(0)
                                                       .withSecond(0)
                                                       .withNano(0);
        ZonedDateTime nextMonthFirstSaturday = dateTime.withMonth(7)
                                                       .with(firstInMonth(DayOfWeek.SATURDAY))
                                                       .withHour(8)
                                                       .withMinute(0)
                                                       .withSecond(0)
                                                       .withNano(0);
        //quartz
        assertEquals(lastMonthFirstSaturday, lastQuartzExecutionTime);
        assertEquals(nextMonthFirstSaturday, nextQuartzExecutionTime);
        //spring
        assertEquals(lastMonthFirstSaturday, lastSpringExecutionTime);
        assertEquals(nextMonthFirstSaturday, nextSpringExecutionTime);

    }
 
Example #22
Source File: Issue343Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
public void test() {
	CronParser pareser = new CronParser( CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING) );
	
	String actualDescription = CronDescriptor.instance(Locale.ENGLISH)
			.describe(pareser.parse(expressionToTest.getExpression()));
	
	Assert.assertThat(actualDescription, IsEqual.equalTo(expressionToTest.getExpectedDescription()));
}
 
Example #23
Source File: CronParserTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Corresponds to issue#148
 * https://github.com/jmrozanec/cron-utils/issues/148
 */
@Test
public void testParseEveryXyears() {
    final CronDefinition quartzDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    parser = new CronParser(quartzDefinition);

    parser.parse("0/59 0/59 0/23 1/30 1/11 ? 2017/3");
}
 
Example #24
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #130: Wrong last execution time if schedule hit is less than one second ago
 * https://github.com/jmrozanec/cron-utils/issues/130
 */
@Test
public void exactHitReturnsFullIntervalDuration() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final Cron cron = parser.parse("0 12 * * *");
    final ZonedDateTime time = ZonedDateTime.of(2016, 12, 2, 12, 0, 0, 0, ZoneId.of("Europe/Vienna"));
    final Optional<Duration> timeFromLastExecution = ExecutionTime.forCron(cron).timeFromLastExecution(time);
    if (timeFromLastExecution.isPresent()) {
        assertEquals(timeFromLastExecution.get(), Duration.ofHours(24));
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example #25
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #130: Wrong last execution time if schedule hit is less than one second ago
 * https://github.com/jmrozanec/cron-utils/issues/130
 */
@Test
public void fuzzyHitReturnsVerySmallIntervalDuration() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final Cron cron = parser.parse("0 12 * * *");
    ZonedDateTime time = ZonedDateTime.of(2016, 12, 2, 12, 0, 0, 0, ZoneId.of("Europe/Vienna"));
    final Duration diff = Duration.ofMillis(300);
    time = time.plus(diff);
    final Optional<Duration> timeFromLastExecution = ExecutionTime.forCron(cron).timeFromLastExecution(time);
    if (timeFromLastExecution.isPresent()) {
        assertEquals(timeFromLastExecution.get(), diff);
    } else {
        fail(LAST_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 invalidDayInMonthCron() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    final Cron myCron = parser.parse("0 0 31 2 *");
    final ZonedDateTime time = ZonedDateTime.parse("2015-09-17T00:00:00.000-07:00");
    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(myCron).nextExecution(time);
    assertFalse(nextExecution.isPresent());
}
 
Example #27
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #28
Source File: OpenIssuesTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
private static ZonedDateTime nextSchedule(final String cronString, final ZonedDateTime lastExecution) {
    final CronParser cronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    final Cron cron = cronParser.parse(cronString);

    final ExecutionTime executionTime = ExecutionTime.forCron(cron);

    final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(lastExecution);
    if (nextExecution.isPresent()) {
        return nextExecution.get();
    } else {
        throw new NullPointerException("next execution is not present");
    }
}
 
Example #29
Source File: Issue413Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testFridayToSaturdayUnix() {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    try {
        parser.parse("* * * */12 *");
    } catch (IllegalArgumentException expected) {
        assertEquals("Failed to parse '* * * */12 *'. Period 12 not in range [1, 12]", expected.getMessage());
    }
}
 
Example #30
Source File: CronParserTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testRejectionOfZeroPeriod() {
    final CronDefinition quartzDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    parser = new CronParser(quartzDefinition);

    parser.parse("0/0 0 0 1 1 ? 2017/3");
}