use of com.cronutils.model.field.CronField in project cron-utils by jmrozanec.
the class CronDescriptorTest method testNthDayOfWeekInMonth.
@Test
public void testNthDayOfWeekInMonth() {
final int dayOfWeek = 2;
final int hour = 10;
final int minute = 15;
final List<CronField> results = new ArrayList<>();
results.add(new CronField(CronFieldName.HOUR, new On(new IntegerFieldValue(hour)), nullFieldConstraints));
results.add(new CronField(CronFieldName.MINUTE, new On(new IntegerFieldValue(minute)), nullFieldConstraints));
results.add(new CronField(CronFieldName.DAY_OF_WEEK, new On(new IntegerFieldValue(dayOfWeek), new SpecialCharFieldValue(SpecialChar.HASH), new IntegerFieldValue(dayOfWeek)), nullFieldConstraints));
assertEquals(String.format("at %s:%s Tuesday %s of every month", hour, minute, dayOfWeek), descriptor.describe(new SingleCron(mockDefinition, results)));
}
use of com.cronutils.model.field.CronField in project cron-utils by jmrozanec.
the class CronDescriptorTest method testDescribeAtXTimeBetweenDaysOfWeek.
@Test
public void testDescribeAtXTimeBetweenDaysOfWeek() {
final int hour = 11;
final int minute = 30;
final int start = 2;
final int end = 6;
final Between expression = new Between(new IntegerFieldValue(start), new IntegerFieldValue(end));
final List<CronField> results = new ArrayList<>();
results.add(new CronField(CronFieldName.HOUR, new On(new IntegerFieldValue(hour)), nullFieldConstraints));
results.add(new CronField(CronFieldName.MINUTE, new On(new IntegerFieldValue(minute)), nullFieldConstraints));
results.add(new CronField(CronFieldName.DAY_OF_WEEK, expression, nullFieldConstraints));
assertEquals(String.format("at %s:%s every day between Tuesday and Saturday", hour, minute), descriptor.describe(new SingleCron(mockDefinition, results)));
}
use of com.cronutils.model.field.CronField in project cron-utils by jmrozanec.
the class CronDescriptorTest method testLastDayOfWeekInMonth.
@Test
public void testLastDayOfWeekInMonth() {
final int dayOfWeek = 2;
final int hour = 10;
final int minute = 15;
final List<CronField> results = new ArrayList<>();
results.add(new CronField(CronFieldName.HOUR, new On(new IntegerFieldValue(hour)), nullFieldConstraints));
results.add(new CronField(CronFieldName.MINUTE, new On(new IntegerFieldValue(minute)), nullFieldConstraints));
results.add(new CronField(CronFieldName.DAY_OF_WEEK, new On(new IntegerFieldValue(dayOfWeek), new SpecialCharFieldValue(SpecialChar.L)), nullFieldConstraints));
assertEquals(String.format("at %s:%s last Tuesday of every month", hour, minute), descriptor.describe(new SingleCron(mockDefinition, results)));
}
use of com.cronutils.model.field.CronField in project cron-utils by jmrozanec.
the class SingleCron method asString.
public String asString() {
if (asString == null) {
final ArrayList<CronField> temporaryFields = new ArrayList<>(fields.values());
temporaryFields.sort(CronField.createFieldComparator());
final StringBuilder builder = new StringBuilder();
for (final CronField field : temporaryFields) {
builder.append(String.format("%s ", field.getExpression().asString()));
}
asString = builder.toString().trim();
}
return asString;
}
use of com.cronutils.model.field.CronField in project cron-utils by jmrozanec.
the class SingleCron method validate.
/**
* Validates this Cron instance by validating its cron expression.
*
* @return this Cron instance
* @throws IllegalArgumentException if the cron expression is invalid
*/
public Cron validate() {
for (final Map.Entry<CronFieldName, CronField> field : retrieveFieldsAsMap().entrySet()) {
final CronFieldName fieldName = field.getKey();
field.getValue().getExpression().accept(new ValidationFieldExpressionVisitor(getCronDefinition().getFieldDefinition(fieldName).getConstraints()));
}
for (final CronConstraint constraint : getCronDefinition().getCronConstraints()) {
if (!constraint.validate(this)) {
throw new IllegalArgumentException(String.format("Invalid cron expression: %s. %s", asString(), constraint.getDescription()));
}
}
return this;
}
Aggregations