-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from NielsDoucet/add-jsr303-validator
Add optional jsr 303 validator.
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.cronutils.validation; | ||
|
||
import com.cronutils.model.CronType; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.*; | ||
|
||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = CronValidator.class) | ||
@Inherited | ||
@Documented | ||
public @interface Cron { | ||
|
||
String message() default "UNUSED"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
CronType type(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.cronutils.validation; | ||
|
||
import com.cronutils.model.CronType; | ||
import com.cronutils.model.definition.CronDefinition; | ||
import com.cronutils.model.definition.CronDefinitionBuilder; | ||
import com.cronutils.parser.CronParser; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class CronValidator implements ConstraintValidator<Cron, String> { | ||
|
||
private CronType type; | ||
|
||
@Override | ||
public void initialize(Cron constraintAnnotation) { | ||
this.type = constraintAnnotation.type(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(type); | ||
CronParser cronParser = new CronParser(cronDefinition); | ||
try { | ||
cronParser.parse(value).validate(); | ||
return true; | ||
} catch (IllegalArgumentException e) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(e.getMessage()).addConstraintViolation(); | ||
return false; | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/com/cronutils/validation/CronValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.cronutils.validation; | ||
|
||
import com.cronutils.model.CronType; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
|
||
import java.util.Set; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(Parameterized.class) | ||
public class CronValidatorTest { | ||
|
||
private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); | ||
|
||
private final String expression; | ||
private final boolean valid; | ||
|
||
public CronValidatorTest(String expression, boolean valid) { | ||
this.expression = expression; | ||
this.valid = valid; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{0} ") | ||
public static Object[] expressions() { | ||
return new Object[][]{ | ||
{"0 0 * * * *", true}, | ||
{"*/10 * * * * *", true}, | ||
{"0 0 8-10 * * *", true}, | ||
{"0 0 6,19 * * *", true}, | ||
{"0 0/30 8-10 * * *", true}, | ||
{"0 0 9-17 * * MON-FRI", true}, | ||
{"0 0 0 25 12 ?", true}, | ||
{"0 0 0 L 12 ?", false}, | ||
{"1,2, * * * * *", false}, | ||
{"1- * * * * *", false} | ||
}; | ||
} | ||
|
||
@Test | ||
public void validateExamples() { | ||
TestPojo testPojo = new TestPojo(expression); | ||
Set<ConstraintViolation<TestPojo>> violations = validator.validate(testPojo); | ||
|
||
if (valid) { | ||
assertTrue(violations.isEmpty()); | ||
} else { | ||
assertFalse(violations.isEmpty()); | ||
} | ||
} | ||
|
||
public static class TestPojo { | ||
@Cron(type = CronType.SPRING) | ||
private final String cron; | ||
|
||
public TestPojo(String cron) { | ||
this.cron = cron; | ||
} | ||
|
||
public String getCron() { | ||
return cron; | ||
} | ||
|
||
} | ||
} |