Java Code Examples for com.cronutils.model.definition.CronDefinitionBuilder#instanceDefinitionFor()

The following examples show how to use com.cronutils.model.definition.CronDefinitionBuilder#instanceDefinitionFor() . 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: 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 2
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 3
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 4
Source File: Issue338Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testEverySecondInFrench() {
	CronParser cronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
	String cronString = "* * * * * ? *";
	Cron cron = cronParser.parse(cronString);
	String description = CronDescriptor.instance(Locale.FRANCE).describe(cron);
	Assert.assertEquals("chaque seconde", description);
}
 
Example 5
Source File: Issue424Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
    CronParser parser = new CronParser(cronDefinition);
    ExecutionTime execution = ExecutionTime.forCron(parser.parse("0 0 12 ? * SUN#4 2020"));
    LocalDate date = LocalDate.of(2021, 1, 1);
    LocalTime time = LocalTime.of(0, 0, 0);
    ZonedDateTime dateTime = ZonedDateTime.of(date, time, ZoneOffset.UTC);
    for (int index = 0, size = 12; index < size; index++) {
        dateTime = execution.lastExecution(dateTime).orElse(null);
        assertEquals(LocalDateTime.of(2020, 12 - index, 1, 12, 0, 0).with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.SUNDAY)), dateTime.toLocalDateTime());
    }
}
 
Example 6
Source File: SimpleScheduler.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public SimpleScheduler(SchedulerContext context, Config config, SchedulerRuntimeConfig schedulerRuntimeConfig) {
    this.running = true;
    this.enabled = schedulerRuntimeConfig.enabled;
    this.scheduledTasks = new ArrayList<>();
    this.executor = context.getExecutor();

    if (!schedulerRuntimeConfig.enabled) {
        this.scheduledExecutor = null;
        LOGGER.info("Simple scheduler is disabled by config property and will not be started");
    } else if (context.getScheduledMethods().isEmpty()) {
        this.scheduledExecutor = null;
        LOGGER.info("No scheduled business methods found - Simple scheduler will not be started");
    } else {
        this.scheduledExecutor = new JBossScheduledThreadPoolExecutor(1, new Runnable() {
            @Override
            public void run() {
                // noop
            }
        });

        CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(context.getCronType());
        CronParser parser = new CronParser(definition);

        for (ScheduledMethodMetadata method : context.getScheduledMethods()) {
            ScheduledInvoker invoker = context.createInvoker(method.getInvokerClassName());
            int nameSequence = 0;
            for (Scheduled scheduled : method.getSchedules()) {
                nameSequence++;
                SimpleTrigger trigger = createTrigger(method.getInvokerClassName(), parser, scheduled, nameSequence,
                        config);
                scheduledTasks.add(new ScheduledTask(trigger, invoker));
            }
        }
    }
}
 
Example 7
Source File: Issue363Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void everyMinute01Test() {
    // every minute 1 of every hour/day/year
    String cronExpression = "0 1 * * * ?";
    ZonedDateTime now = ZonedDateTime.of(2019, 1, 1, 0, 1, 0, 0, ZoneOffset.UTC);
    CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));

    Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(parser.parse(cronExpression)).nextExecution(now);

    assertTrue(nextExecution.isPresent());

    ZonedDateTime expectedTime = ZonedDateTime.of(2019, 1, 1, 1, 1, 0, 0, ZoneOffset.UTC);
    assertEquals(expectedTime, nextExecution.get());
}
 
Example 8
Source File: Issue406Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testDayOfWeekIsCorrectlyApplied() {
    // GIVEN a spring cron operating at 1AM every weekday
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING);
    final CronParser parser = new CronParser(cronDefinition);
    final ExecutionTime execTime = ExecutionTime.forCron(parser.parse("0 0 1 * * MON-FRI"));

    // WHEN I get the next execution at 3AM on Saturday
    final ZonedDateTime threeAmFifthJanuary2019 = ZonedDateTime.of(
        LocalDate.of(2019, 1, 5),
        LocalTime.of(3, 0),
        ZoneId.systemDefault()
    );
    final Optional<ZonedDateTime> nextExecution = execTime.nextExecution(threeAmFifthJanuary2019);
    
    // THEN the result is 1AM on Monday
    assertEquals(
        Optional.of(
                ZonedDateTime.of(
                LocalDate.of(2019, 1, 7),
                LocalTime.of(1, 0),
                ZoneId.systemDefault()
            )
        ),
        nextExecution
    );
}
 
Example 9
Source File: Issue382Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testLastExecutionWithMillis() {
    CronParser cronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    String cronString = "0 0 * * WED";
    Cron cron = cronParser.parse(cronString);
    ExecutionTime executionTime = ExecutionTime.forCron(cron);

    ZonedDateTime date = ZonedDateTime.of(2019, 6, 12, 0, 0, 0, 0, UTC);
    ZonedDateTime lastExecution = executionTime.lastExecution(date.plus(ofMillis(300))).get();
    assertEquals(date, lastExecution);
}
 
Example 10
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 11
Source File: Issue200Test.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatchExact() {
    final CronDefinition cronDefinition =
            CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);

    final CronParser parser = new CronParser(cronDefinition);
    final Cron quartzCron = parser.parse("00 00 10 * * ?");

    quartzCron.validate();

    final ZonedDateTime zdt = ZonedDateTime.of(1999, 07, 18, 10, 00, 00, 00, ZoneId.systemDefault());

    assertTrue("Nano seconds must not affect matching of Cron Expressions", ExecutionTime.forCron(quartzCron).isMatch(zdt));
}
 
Example 12
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 13
Source File: CronMapperIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
private CronParser cron4jParser() {
    return new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.CRON4J));
}
 
Example 14
Source File: Issue227Test.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
}
 
Example 15
Source File: CronDescriptorCron4jIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    descriptor = CronDescriptor.instance(Locale.UK);
    parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.CRON4J));
}
 
Example 16
Source File: CronDescriptorQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    descriptor = CronDescriptor.instance(Locale.UK);
    parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
}
 
Example 17
Source File: Issue215Test.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
private void testWorkdays8Quartz(final LocalDateTime startDate, final LocalDateTime expectedNextExecution) {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
    final Cron quartzCron = parser.parse("0 0 8 ? * MON-FRI");
    checkNextExecution(startDate, expectedNextExecution, quartzCron);
}
 
Example 18
Source File: Issue58UnixCronAsStringIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    cronParser = new CronParser(cronDefinition);
}
 
Example 19
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
private Cron getUnixCron(final String cronExpression) {
    final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
    final CronParser parser = new CronParser(cronDefinition);
    return parser.parse(cronExpression);
}
 
Example 20
Source File: Issue215Test.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
private void testFridayToSaturdayCron4j(final LocalDateTime startDate, final LocalDateTime expectedNextExecution) {
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.CRON4J));
    final Cron quartzCron = parser.parse("0 8 * * FRI-SAT");
    checkNextExecution(startDate, expectedNextExecution, quartzCron);
}