Search in sources :

Example 21 with CronField

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)));
}
Also used : SpecialCharFieldValue(com.cronutils.model.field.value.SpecialCharFieldValue) ArrayList(java.util.ArrayList) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On) Test(org.junit.Test)

Example 22 with CronField

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)));
}
Also used : Between(com.cronutils.model.field.expression.Between) ArrayList(java.util.ArrayList) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On) Test(org.junit.Test)

Example 23 with CronField

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)));
}
Also used : SpecialCharFieldValue(com.cronutils.model.field.value.SpecialCharFieldValue) ArrayList(java.util.ArrayList) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On) Test(org.junit.Test)

Example 24 with CronField

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;
}
Also used : CronField(com.cronutils.model.field.CronField)

Example 25 with CronField

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;
}
Also used : CronFieldName(com.cronutils.model.field.CronFieldName) CronConstraint(com.cronutils.model.definition.CronConstraint) ValidationFieldExpressionVisitor(com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor) CronField(com.cronutils.model.field.CronField)

Aggregations

CronField (com.cronutils.model.field.CronField)34 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)18 Test (org.junit.Test)16 On (com.cronutils.model.field.expression.On)15 SingleCron (com.cronutils.model.SingleCron)13 ArrayList (java.util.ArrayList)13 Before (org.junit.Before)7 Between (com.cronutils.model.field.expression.Between)6 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)5 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)4 WeekDay (com.cronutils.mapper.WeekDay)3 Cron (com.cronutils.model.Cron)3 CronFieldName (com.cronutils.model.field.CronFieldName)3 Every (com.cronutils.model.field.expression.Every)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 CompositeCron (com.cronutils.model.CompositeCron)2 And (com.cronutils.model.field.expression.And)2 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)2 CronConstraint (com.cronutils.model.definition.CronConstraint)1 FieldConstraintsBuilder (com.cronutils.model.field.constraint.FieldConstraintsBuilder)1