diff --git a/.sdkmanrc b/.sdkmanrc new file mode 100644 index 00000000..b6ab7022 --- /dev/null +++ b/.sdkmanrc @@ -0,0 +1,3 @@ +# Enable auto-env through the sdkman_auto_env config +# Add key=value pairs of SDKs to use below +java=8.0.412-zulu diff --git a/src/main/java/com/cronutils/mapper/CronMapper.java b/src/main/java/com/cronutils/mapper/CronMapper.java index 8446fac4..99590b3b 100755 --- a/src/main/java/com/cronutils/mapper/CronMapper.java +++ b/src/main/java/com/cronutils/mapper/CronMapper.java @@ -29,7 +29,6 @@ import com.cronutils.model.field.definition.FieldDefinition; import com.cronutils.model.field.expression.*; import com.cronutils.model.field.expression.visitor.FieldExpressionVisitorAdaptor; -import com.cronutils.model.field.expression.visitor.ValueMappingFieldExpressionVisitor; import com.cronutils.model.field.value.FieldValue; import com.cronutils.model.field.value.IntegerFieldValue; import com.cronutils.model.field.value.SpecialChar; @@ -286,12 +285,14 @@ static Function dayOfWeekMapping(final DayOfWeekFieldDefin final FieldExpression expression = field.getExpression(); FieldExpression dest = null; dest = expression.accept(new FieldExpressionVisitorAdaptor() { + @Override public FieldExpression visit(Every every) { return new Every(every.getExpression().accept(this), every.getPeriod()); } + @Override public FieldExpression visit(On on) { - return new On(mapDayOfWeek(sourceDef, targetDef, on.getTime()), on.getSpecialChar()); + return new On(mapDayOfWeek(sourceDef, targetDef, on.getTime()), on.getSpecialChar(), on.getNth()); } @Override diff --git a/src/test/java/com/cronutils/Issue605Test.java b/src/test/java/com/cronutils/Issue605Test.java new file mode 100644 index 00000000..184606b1 --- /dev/null +++ b/src/test/java/com/cronutils/Issue605Test.java @@ -0,0 +1,44 @@ +package com.cronutils; + +import com.cronutils.mapper.CronMapper; +import com.cronutils.model.Cron; +import com.cronutils.model.CronType; +import com.cronutils.model.definition.CronDefinition; +import com.cronutils.model.definition.CronDefinitionBuilder; +import com.cronutils.parser.CronParser; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static com.cronutils.model.CronType.QUARTZ; +import static com.cronutils.model.CronType.SPRING; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class Issue605Test { + + static Stream cronExpressions() { + return Stream.of( + Arguments.of(QUARTZ, CronMapper.fromQuartzToCron4j(), "0 0 * * 4#1"), + Arguments.of(QUARTZ, CronMapper.fromQuartzToSpring(), "0 0 0 ? * 4#1"), + Arguments.of(QUARTZ, CronMapper.fromQuartzToUnix(), "0 0 * * 4#1"), + Arguments.of(SPRING, CronMapper.fromSpringToQuartz(), "0 0 0 ? * 6#1 *") + ); + } + + @ParameterizedTest + @MethodSource("cronExpressions") + void testDayOfWeekMappingSpring(CronType cronType, CronMapper mapper, String expectedExpression) { + Cron sourceCron = getCron(cronType, "0 0 0 ? * 5#1"); + Cron destinationCron = mapper.map(sourceCron); + assertEquals(expectedExpression, destinationCron.asString()); + } + + private Cron getCron(CronType cronType, @SuppressWarnings("SameParameterValue") final String quartzExpression) { + final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(cronType); + final CronParser parser = new CronParser(cronDefinition); + return parser.parse(quartzExpression); + } + +}