use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method configuringConstraintsOnGenericTypeArgumentOfListThrowsException.
@Test(expectedExceptions = UnexpectedTypeException.class, expectedExceptionsMessageRegExp = "HV000030:.*")
@TestForIssue(jiraKey = "HV-1279")
public void configuringConstraintsOnGenericTypeArgumentOfListThrowsException() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test8", List.class).parameter(0).containerElementType(0).constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
fishTank.test8(Arrays.asList("Too long"));
}
use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForParameterProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test1", Optional.class, Map.class).parameter(0).containerElementType().constraint(new SizeDef().max(5)).parameter(1).containerElementType(0).constraint(new SizeDef().min(3).max(10)).containerElementType(1).constraint(new MinDef().value(1));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
HashMap<String, Integer> fishCountByType = new HashMap<>();
fishCountByType.put("A", -1);
fishCountByType.put("BB", -2);
fishTank.test1(Optional.of("Too long"), fishCountByType);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Size.class).withMessage("size must be between 3 and 10"), violationOf(Min.class).withMessage("must be greater than or equal to 1"), violationOf(Min.class).withMessage("must be greater than or equal to 1"));
}
}
use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareDeeplyNestedContainerElementConstraintsForParameterProgrammatically.
@Test
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareDeeplyNestedContainerElementConstraintsForParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test3", List.class).parameter(0).containerElementType(0, 1, 0).constraint(new NotNullDef());
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
Set<String> bobsTags = CollectionHelper.asSet((String) null);
Map<String, Set<String>> januaryTags = new HashMap<>();
januaryTags.put("bob", bobsTags);
List<Map<String, Set<String>>> tagsOfFishOfTheMonth = new ArrayList<>();
tagsOfFishOfTheMonth.add(januaryTags);
fishTank.test3(tagsOfFishOfTheMonth);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
}
}
use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.
the class UniqueElementsDefTest method testUniqueElementsDef.
@Test
@TestForIssue(jiraKey = "HV-1466")
public void testUniqueElementsDef() {
final HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration(HibernateValidator.class);
final ConstraintMapping programmaticMapping = configuration.createConstraintMapping();
programmaticMapping.type(Library.class).field("books").constraint(new UniqueElementsDef());
configuration.addMapping(programmaticMapping);
Validator validator = configuration.buildValidatorFactory().getValidator();
Set<ConstraintViolation<Library>> violations = validator.validate(new Library(Arrays.asList("A Prayer for Owen Meany", "The Cider House Rules", "The Cider House Rules")));
assertThat(violations).containsOnlyViolations(violationOf(UniqueElements.class).withProperty("books"));
}
use of org.hibernate.validator.testutil.TestForIssue in project hibernate-validator by hibernate.
the class ConstraintValidatorContextTest method testAddParameterNodeUsingCustomParameterNameProvider.
@Test
@TestForIssue(jiraKey = "HV-709")
public void testAddParameterNodeUsingCustomParameterNameProvider() throws Exception {
ExecutableValidator executableValidator = ValidatorUtil.getConfiguration().parameterNameProvider(new PrefixableParameterNameProvider("param")).buildValidatorFactory().getValidator().forExecutables();
Set<ConstraintViolation<User>> constraintViolations = executableValidator.validateParameters(new User(), User.class.getMethod("setAddresses", Map.class), new java.lang.Object[] { new HashMap<>() });
assertThat(constraintViolations).containsOnlyPaths(pathWith().method("setAddresses").parameter("param0", 0), pathWith().method("setAddresses").parameter("param0", 0).bean(), pathWith().method("setAddresses").parameter("param0", 0).property("myNode1", true, null, 23).bean());
}
Aggregations