Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add day of month to day of week, X#Y syntax #625

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .sdkmanrc
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions src/main/java/com/cronutils/mapper/CronMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -286,12 +285,14 @@ static Function<CronField, CronField> 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
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/cronutils/Issue605Test.java
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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);
}

}