Search in sources :

Example 86 with ConstraintViolation

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);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ConstraintValidator(javax.validation.ConstraintValidator) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Example 87 with ConstraintViolation

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"));
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 88 with ConstraintViolation

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());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 89 with ConstraintViolation

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);
}
Also used : ErrorList(org.superbiz.webapp2.messages.ErrorList) ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ErrorResponse(org.superbiz.webapp2.messages.ErrorResponse)

Example 90 with ConstraintViolation

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());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) CmsDocumentBlobSegment(gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment) Test(org.junit.Test)

Aggregations

ConstraintViolation (javax.validation.ConstraintViolation)95 Test (org.junit.Test)78 Validator (javax.validation.Validator)19 ConstraintViolationException (javax.validation.ConstraintViolationException)12 SMSMessage (org.apache.camel.component.cm.client.SMSMessage)12 ChangePasswordForm (org.orcid.frontend.web.forms.ChangePasswordForm)11 CmsDocumentBlobSegment (gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment)10 ValidatorFactory (javax.validation.ValidatorFactory)9 ManagePasswordOptionsForm (org.orcid.frontend.web.forms.ManagePasswordOptionsForm)8 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 CMConfiguration (org.apache.camel.component.cm.CMConfiguration)6 Set (java.util.Set)5 InitialContext (javax.naming.InitialContext)5 Session (org.hibernate.Session)5 Transaction (org.hibernate.Transaction)5 Method (java.lang.reflect.Method)4 ExecutableValidator (javax.validation.executable.ExecutableValidator)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 Exchange (org.apache.camel.Exchange)4