Java Code Examples for com.cronutils.model.time.ExecutionTime#lastExecution()

The following examples show how to use com.cronutils.model.time.ExecutionTime#lastExecution() . 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: 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 2
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 3
Source File: CronParserQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #78: ExecutionTime.forCron fails on intervals
 */
@Test
public void testIntervalSeconds() {
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("0/20 * * * * ?"));
    final ZonedDateTime now = ZonedDateTime.parse("2005-08-09T18:32:42Z");
    final Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(now);
    if (lastExecution.isPresent()) {
        final ZonedDateTime assertDate = ZonedDateTime.parse("2005-08-09T18:32:40Z");
        assertEquals(assertDate, lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 4
Source File: CronParserQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #78: ExecutionTime.forCron fails on intervals
 */
@Test
public void testIntervalMinutes() {
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("0 0/7 * * * ?"));
    final ZonedDateTime now = ZonedDateTime.parse("2005-08-09T18:32:42Z");
    final Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(now);
    if (lastExecution.isPresent()) {
        final ZonedDateTime assertDate = ZonedDateTime.parse("2005-08-09T18:28:00Z");
        assertEquals(assertDate, lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 5
Source File: Issue143Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testCase1() {
    ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 31 12 ? *"));
    Optional<ZonedDateTime> olast = et.lastExecution(currentDateTime);
    ZonedDateTime last = olast.orElse(null);

    ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2015, 12, 31, 12, 0),
            ZoneId.systemDefault());
    Assert.assertEquals(expected, last);
}
 
Example 6
Source File: Issue143Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testCase2() {
    final ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 ? 12 SAT#5 *"));
    final Optional<ZonedDateTime> lastExecution = et.lastExecution(currentDateTime);
    if (lastExecution.isPresent()) {
        final ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2012, 12, 29, 12, 0), ZoneId.systemDefault());
        Assert.assertEquals(expected, lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 7
Source File: Issue143Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testCase3() {
    final ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 31 1/1 ? *"));
    final Optional<ZonedDateTime> lastExecution = et.lastExecution(currentDateTime);
    if (lastExecution.isPresent()) {
        final ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2016, 10, 31, 12, 0), ZoneId.systemDefault());
        Assert.assertEquals(expected, lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 8
Source File: Issue143Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testCase4() {
    final ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 12 ? 1/1 SAT#5 *"));
    final Optional<ZonedDateTime> lastExecution = et.lastExecution(currentDateTime);
    if (lastExecution.isPresent()) {
        final ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2016, 10, 29, 12, 0), ZoneId.systemDefault());
        Assert.assertEquals(expected, lastExecution.get());
    } else {
        fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 9
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
private Optional<ZonedDateTime> getLastExecutionFor(final Cron cron, final ZonedDateTime dateTime) {
    final ExecutionTime executionTime = ExecutionTime.forCron(cron);
    return  executionTime.lastExecution(dateTime);
}