com.cronutils.model.time.ExecutionTime Java Examples

The following examples show how to use com.cronutils.model.time.ExecutionTime. 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: 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 #3
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 #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: 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: Issue394Test.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testEveryMondayAt0900hours() {
    String cron = "0 0 9 * * MON";

    CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(SPRING);
    CronParser parser = new CronParser(cronDefinition);
    ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(cron));
    ZonedDateTime now = ZonedDateTime.now();
    Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(now);
    assertTrue(nextExecution.isPresent());
    // Should be the next Monday at 0900 hours
    assertEquals(DayOfWeek.MONDAY, nextExecution.get().getDayOfWeek());
    assertEquals(9, nextExecution.get().getHour());
    // The next execution after that should also be a Monday at 0900 hours, the following week
    ZonedDateTime nextExpectedExecution = nextExecution.get().plusWeeks(1);
    nextExecution = executionTime.nextExecution(nextExecution.get());
    assertTrue(nextExecution.isPresent());
    assertEquals(DayOfWeek.MONDAY, nextExecution.get().getDayOfWeek());
    assertEquals(9, nextExecution.get().getHour());
    assertEquals(nextExpectedExecution, nextExecution.get());
}
 
Example #7
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 #8
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 #9
Source File: TimeUtil.java    From styx with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an ordered list of instants between firstInstant (inclusive) and lastInstant (exclusive)
 * according to the {@link Schedule}.
 *
 * @param firstInstant The first instant
 * @param lastInstant  The last instant
 * @param schedule     The schedule of the workflow
 * @return instants within the range
 */
public static List<Instant> instantsInRange(Instant firstInstant, Instant lastInstant,
                                            Schedule schedule) {
  Preconditions.checkArgument(
      isAligned(firstInstant, schedule) && isAligned(lastInstant, schedule),
      "unaligned instant");
  Preconditions.checkArgument(!lastInstant.isBefore(firstInstant),
      "last instant should not be before first instant");

  final ExecutionTime executionTime = ExecutionTime.forCron(cron(schedule));
  final List<Instant> instants = new ArrayList<>();

  Instant currentInstant = firstInstant;
  while (currentInstant.isBefore(lastInstant)) {
    instants.add(currentInstant);
    final ZonedDateTime utcDateTime = currentInstant.atZone(UTC);
    currentInstant = executionTime.nextExecution(utcDateTime)
        .orElseThrow(IllegalArgumentException::new) // with unix cron, this should not happen
        .toInstant();
  }

  return instants;
}
 
Example #10
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 #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: TimeUtil.java    From styx with Apache License 2.0 6 votes vote down vote up
public static Instant offsetInstant(Instant origin, Schedule schedule, int offset) {
  Preconditions.checkArgument(isAligned(origin, schedule), "unaligned origin");
  return schedule.wellKnown().unit()
      .map(unit -> {
        try {
          return origin.plus(offset, unit);
        } catch (UnsupportedTemporalTypeException ignored) {
          return null;
        }
      })
      .orElseGet(() -> {
        final ExecutionTime executionTime = ExecutionTime.forCron(cron(schedule));
        ZonedDateTime time = origin.atZone(UTC);
        for (int i = 0; i < Math.abs(offset); i++) {
          final Optional<ZonedDateTime> execution = offset <= 0
                                                    ? executionTime.lastExecution(time)
                                                    : executionTime.nextExecution(time);
          time = execution
              .orElseThrow(AssertionError::new); // with unix cron, this should not happen
        }
        return time.toInstant();
      });
}
 
Example #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
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 #29
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 #30
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);
    }
}