use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class BootStrapValidationTestCase method testSafeHTML.
/**
* Ensure that including a cross-site script attack triggers the @SafeHtml validation rule
*/
@Test
public void testSafeHTML() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
assertNotNull(configuration);
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();
Employee emp = new Employee();
// create employee
emp.setFirstName("Joe");
emp.setLastName("Cocker");
emp.setEmail("none@jboss.org");
emp.setWebsite("<script> Cross-site scripting http://en.wikipedia.org/wiki/Joe_Cocker <script/>.");
Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
}
use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class ConstraintValidationTestCase method testObjectGraphValidation.
@Test
public void testObjectGraphValidation() throws NamingException {
Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
// create first passenger
UserBean user1 = new UserBean("MADHUMITA", "SADHUKHAN");
user1.setEmail("madhu@gmail.com");
user1.setAddress("REDHAT Brno");
// create second passenger
UserBean user2 = new UserBean("Mickey", "Mouse");
user2.setAddress("");
List<UserBean> passengers = new ArrayList<UserBean>();
passengers.add(user1);
passengers.add(user2);
// Create a Car
Car car = new Car("CET5678", passengers);
car.setModel("SKODA Octavia");
final Set<ConstraintViolation<Car>> errorresult = validator.validate(car);
Iterator<ConstraintViolation<Car>> it1 = errorresult.iterator();
String message = "";
while (it1.hasNext()) {
ConstraintViolation<Car> cts = it1.next();
String mess = cts.getMessage();
if (mess.contains("Please get a valid address")) {
message = mess;
}
}
assertEquals(2, errorresult.size());
assertTrue(message.contains("Please get a valid address"));
}
use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class ExpressionLanguageTestCase method testValidationUsingExpressionLanguage.
@Test
public void testValidationUsingExpressionLanguage() {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean());
assertEquals(1, violations.size());
assertEquals("'Bob' is too short, it should at least be 5 characters long.", violations.iterator().next().getMessage());
}
use of javax.validation.ConstraintViolation in project tomee by apache.
the class ConstraintViolationExceptionMapper method getConstraintViolationErrors.
private static Object getConstraintViolationErrors(final ConstraintViolationException ex) {
final List<ErrorResponse> errors = new ArrayList<ErrorResponse>();
for (final ConstraintViolation violation : ex.getConstraintViolations()) {
final ErrorResponse error = new ErrorResponse();
error.setMessage(violation.getMessage());
errors.add(error);
}
return new ErrorList<ErrorResponse>(errors);
}
use of javax.validation.ConstraintViolation in project API by ca-cwds.
the class CmsDocumentBlobSegmentTest method testConstraintDocHandleNull.
@Test
public void testConstraintDocHandleNull() throws Exception {
CmsDocumentBlobSegment blob = new CmsDocumentBlobSegment(null, segmentSequence, docBlob);
Set<ConstraintViolation<CmsDocumentBlobSegment>> violations = validator.validate(blob);
assertEquals(1, violations.size());
assertEquals("may not be null", violations.iterator().next().getMessage());
}
Aggregations