Java Code Examples for com.cronutils.parser.CronParser#parse()
The following examples show how to use
com.cronutils.parser.CronParser#parse() .
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: ExecutionTimeCustomDefinitionIntegrationTest.java From cron-utils with Apache License 2.0 | 6 votes |
/** * A CronDefinition with only 3 required fields is legal to instantiate, but the parser considers an expression * with 4 fields as an error: * java.lang.IllegalArgumentException: Cron expression contains 4 parts but we expect one of [6, 7] */ @Test public void testThreeRequiredFieldsSupported() { final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron() .withSeconds().and() .withMinutes().and() .withHours().and() .withDayOfMonth().supportsL().supportsW().supportsLW().supportsQuestionMark().optional().and() .withMonth().optional().and() .withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2).supportsHash().supportsL() .supportsQuestionMark().optional().and() .withYear().withValidRange(2000, 2099).optional().and() .instance(); final CronParser cronParser = new CronParser(cronDefinition); cronParser.parse("* * 4 3"); }
Example 2
Source File: Issue223Test.java From cron-utils with Apache License 2.0 | 6 votes |
/** * 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: ExecutionTimeCustomDefinitionIntegrationTest.java From cron-utils with Apache License 2.0 | 6 votes |
/** * A CronDefinition with only 5 required fields is legal to instantiate, but the parser considers an expression * with 5 fields as an error: * java.lang.IllegalArgumentException: Cron expression contains 4 parts but we expect one of [6, 7] */ @Test public void testFiveRequiredFieldsSupported() { final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron() .withSeconds().and() .withMinutes().and() .withHours().and() .withDayOfMonth().supportsL().supportsW().supportsLW().supportsQuestionMark().and() .withMonth().and() .withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2).supportsHash().supportsL() .supportsQuestionMark().optional().and() .withYear().withValidRange(2000, 2099).optional().and() .instance(); final CronParser cronParser = new CronParser(cronDefinition); cronParser.parse("* * 4 3 *"); }
Example 4
Source File: ExecutionTimeCustomDefinitionIntegrationTest.java From cron-utils with Apache License 2.0 | 6 votes |
/** * 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 5
Source File: Issue218Test.java From cron-utils with Apache License 2.0 | 5 votes |
@Test public void testCronDefinitionExecutionTimeGenerator() { final CronDefinition cronDefinition = defineCron().withMinutes().and() .withHours().and() .withDayOfWeek() .optional() .and() .instance(); final CronParser parser = new CronParser(cronDefinition); final Cron cron = parser.parse(CRON_EXPRESSION); final ExecutionTime executionTime = ExecutionTime.forCron(cron); executionTime.isMatch(ZonedDateTime.now()); }
Example 6
Source File: Issue388Test.java From cron-utils with Apache License 2.0 | 5 votes |
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 7
Source File: Issue228Test.java From cron-utils with Apache License 2.0 | 5 votes |
@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 8
Source File: Issue228Test.java From cron-utils with Apache License 2.0 | 5 votes |
@Test public void testEveryWeekdaySecondWeekOfMonthNextExecution() { final CronParser parser = new CronParser(cronDefinition); // This is 9am on Mon-Fri day between the 8th and 14th (in this case it should be Oct 9 Mon) final Cron myCron = parser.parse("0 9 8-14 * 1-5"); final ZonedDateTime time = ZonedDateTime.parse(TEST_DATE); assertEquals(ZonedDateTime.parse("2017-10-09T09:00-07:00"), getNextExecutionTime(myCron, time)); }
Example 9
Source File: OpenIssuesTest.java From cron-utils with Apache License 2.0 | 5 votes |
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 10
Source File: CompositeCronTest.java From cron-utils with Apache License 2.0 | 5 votes |
@Test(expected = IllegalArgumentException.class) public void weDoNotSupportCronsWithDifferentDefinitions() throws Exception { CronDefinition definition2 = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX); CronParser parser = new CronParser(definition1); CronParser parser2 = new CronParser(definition2); Cron cron1 = parser.parse("0 0 0 15 8 ? 2015/2"); Cron cron2 = parser2.parse("0 0 0 * *"); List<Cron> crons = new ArrayList<>(); crons.add(cron1); crons.add(cron2); new CompositeCron(crons); }
Example 11
Source File: CronDefinitionBuilderTest.java From cron-utils with Apache License 2.0 | 5 votes |
@Test(expected = IllegalArgumentException.class) public void testCronDefinitionShouldNotAcceptQuestionmark() { final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX); final CronParser parser = new CronParser(cronDefinition); final Cron quartzCron = parser.parse("* * * * ?"); quartzCron.validate(); }
Example 12
Source File: Issue338Test.java From cron-utils with Apache License 2.0 | 5 votes |
@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 13
Source File: CronTest.java From cron-utils with Apache License 2.0 | 5 votes |
@Test public void testEquivalent() { final CronDefinition unixcd = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX); final CronDefinition quartzcd = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); final CronParser unix = new CronParser(unixcd); final CronParser quartz = new CronParser(quartzcd); final Cron cron1 = unix.parse("* * * * MON"); final Cron cron2 = unix.parse("*/1 * * * 1"); final Cron cron3 = unix.parse("0 * * * *"); final Cron cron4 = quartz.parse("0 * * ? * MON *"); assertTrue(cron1.equivalent(CronMapper.sameCron(unixcd), cron2)); assertFalse(cron1.equivalent(CronMapper.sameCron(unixcd), cron3)); assertTrue(cron1.equivalent(CronMapper.fromQuartzToCron4j(), cron4)); }
Example 14
Source File: Issue55UnexpectedExecutionTimes.java From cron-utils with Apache License 2.0 | 5 votes |
/** * Test. */ @Test public void testOnceAMonthTwelveInstantsInYear() { LOGGER.debug("TEST2 - expecting 12 instants"); final ZonedDateTime startTime = ZonedDateTime.of(0, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); final ZonedDateTime endTime = startTime.plusYears(1); final CronParser parser = new CronParser(cronDefinition); final Cron cron = parser.parse("0 12 L * ?"); final ExecutionTime executionTime = ExecutionTime.forCron(cron); final List<Instant> instants = getInstants(executionTime, startTime, endTime); LOGGER.debug("instants.size() == {}", instants.size()); LOGGER.debug("instants: {}", instants); assertEquals(12, instants.size()); }
Example 15
Source File: Issue418Test.java From cron-utils with Apache License 2.0 | 5 votes |
@Test public void testInvalidWeekDayEnd() { try { final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); final CronParser parser = new CronParser(cronDefinition); parser.parse("0 0 2 ? * 1/8 *"); fail("Expected exception for invalid expression"); } catch (IllegalArgumentException expected) { assertEquals("Failed to parse '0 0 2 ? * 1/8 *'. Period 8 not in range [1, 7]", expected.getMessage()); } }
Example 16
Source File: CompositeCronTest.java From cron-utils with Apache License 2.0 | 5 votes |
@Test public void testExampleIssue318(){ CronDefinition definition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); CronParser parser = new CronParser(definition); Cron cron1 = parser.parse("0 0 9 * * ? *"); Cron cron2 = parser.parse("0 0 10 * * ? *"); Cron cron3 = parser.parse("0 30 11 * * ? *"); Cron cron4 = parser.parse("0 0 12 * * ? *"); List<Cron> crons = new ArrayList<>(); crons.add(cron1); crons.add(cron2); crons.add(cron3); crons.add(cron4); Cron composite = new CompositeCron(crons); ZonedDateTime defaultt = ZonedDateTime.of(2000, 4, 15, 0, 0, 0, 0, UTC); assertEquals("0 0|0|30|0 9|10|11|12 * * ? *", composite.asString()); ExecutionTime executionTime = ExecutionTime.forCron(composite); ZonedDateTime date1 = ZonedDateTime.of(2015, 4, 15, 0, 0, 0, 0, UTC); assertEquals(ZonedDateTime.of(2015, 4, 15, 9, 0, 0, 0, UTC), executionTime.nextExecution(date1).orElse(defaultt)); ZonedDateTime date2 = ZonedDateTime.of(2015, 4, 15, 9, 30, 0, 0, UTC); assertEquals(ZonedDateTime.of(2015, 4, 15, 10, 0, 0, 0, UTC), executionTime.nextExecution(date2).orElse(defaultt)); ZonedDateTime date3 = ZonedDateTime.of(2015, 4, 15, 11, 0, 0, 0, UTC); assertEquals(ZonedDateTime.of(2015, 4, 15, 11, 30, 0, 0, UTC), executionTime.nextExecution(date3).orElse(defaultt)); ZonedDateTime date4 = ZonedDateTime.of(2015, 4, 15, 11, 30, 0, 0, UTC); assertEquals(ZonedDateTime.of(2015, 4, 15, 12, 0, 0, 0, UTC), executionTime.nextExecution(date4).orElse(defaultt)); ZonedDateTime date5 = ZonedDateTime.of(2015, 4, 15, 12, 30, 0, 0, UTC); assertEquals(ZonedDateTime.of(2015, 4, 16, 9, 0, 0, 0, UTC), executionTime.nextExecution(date5).orElse(defaultt)); }
Example 17
Source File: Issue413Test.java From cron-utils with Apache License 2.0 | 5 votes |
@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 18
Source File: ExecutionTimeCustomDefinitionIntegrationTest.java From cron-utils with Apache License 2.0 | 5 votes |
@Test public void testCronExpressionBeforeHalf() { 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/30 * * * * *"); final ZonedDateTime startDateTime = ZonedDateTime.of(2015, 8, 28, 12, 5, 14, 0, UTC); final ZonedDateTime expectedDateTime = ZonedDateTime.of(2015, 8, 28, 12, 5, 30, 0, UTC); final ExecutionTime executionTime = ExecutionTime.forCron(cron); final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(startDateTime); if (nextExecution.isPresent()) { assertEquals(expectedDateTime, nextExecution.get()); } else { fail(NEXT_EXECUTION_NOT_PRESENT_ERROR); } }
Example 19
Source File: Issue382Test.java From cron-utils with Apache License 2.0 | 5 votes |
@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 20
Source File: Issue281Test.java From cron-utils with Apache License 2.0 | 4 votes |
private Cron buildCron(String expression) { final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ); final CronParser parser = new CronParser(cronDefinition); return parser.parse(expression); }