Search in sources :

Example 56 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testCollectionAssocNotValidated.

@Test
public void testCollectionAssocNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    screen.setStopButton(new Button());
    screen.getStopButton().setName("STOOOOOP");
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    Color c = new Color();
    c.setName("Blue");
    s.persist(c);
    c.setName(null);
    screen.getDisplayColors().add(c);
    try {
        s.persist(screen);
        s.flush();
        fail("Associated objects should not be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Color.class, constraintViolation.getRootBeanClass());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 57 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testAssocInEmbeddedNotValidated.

@Test
public void testAssocInEmbeddedNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    screen.setStopButton(new Button());
    screen.getStopButton().setName("STOOOOOP");
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    DisplayConnector conn = new DisplayConnector();
    conn.setNumber(1);
    screen.getConnectors().add(conn);
    final Display display = new Display();
    display.setBrand("dell");
    conn.setDisplay(display);
    s.persist(display);
    s.flush();
    try {
        display.setBrand(null);
        s.persist(screen);
        s.flush();
        fail("Collection of embedded objects should be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Display.class, constraintViolation.getRootBeanClass());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 58 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class BeanValidationProvidedFactoryTest method testListeners.

@Test
public void testListeners() {
    CupHolder ch = new CupHolder();
    ch.setRadius(new BigDecimal("12"));
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(ch);
        s.flush();
        fail("invalid object should not be persisted");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        assertEquals("Oops", e.getConstraintViolations().iterator().next().getMessage());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(jakarta.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 59 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class BeanValidationGroupsTest method testListeners.

@Test
public void testListeners() {
    CupHolder ch = new CupHolder();
    ch.setRadius(new BigDecimal("12"));
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(ch);
        s.flush();
    } catch (ConstraintViolationException e) {
        fail("invalid object should not be validated");
    }
    try {
        ch.setRadius(null);
        s.flush();
    } catch (ConstraintViolationException e) {
        fail("invalid object should not be validated");
    }
    try {
        s.delete(ch);
        s.flush();
        fail("invalid object should not be persisted");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        // TODO - seems this explicit case is necessary with JDK 5 (at least on Mac). With Java 6 there is no problem
        Annotation annotation = e.getConstraintViolations().iterator().next().getConstraintDescriptor().getAnnotation();
        assertEquals(NotNull.class, annotation.annotationType());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NotNull(jakarta.validation.constraints.NotNull) BigDecimal(java.math.BigDecimal) Annotation(java.lang.annotation.Annotation) Session(org.hibernate.Session) Test(org.junit.Test)

Example 60 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project glassfish-hk2 by eclipse-ee4j.

the class Utilities method invokeVetoableChangeListeners.

@SuppressWarnings("unchecked")
public static void invokeVetoableChangeListeners(DynamicChangeInfo<?> control, BaseHK2JAXBBean source, Object oldValue, Object newValue, String propertyName, ClassReflectionHelper helper) {
    if (control == null)
        return;
    Validator validator = control.findValidator();
    if (validator != null) {
        ModelImpl model = source._getModel();
        String javaName = model.getJavaNameFromKey(propertyName, helper);
        if (javaName != null) {
            Set<ConstraintViolation<BaseHK2JAXBBean>> violations = validator.<BaseHK2JAXBBean>validateValue((Class<BaseHK2JAXBBean>) source.getClass(), javaName, newValue);
            if (violations != null && !violations.isEmpty()) {
                throw new MultiException(new ConstraintViolationException(violations));
            }
        }
    }
    List<VetoableChangeListener> vetoers = control.getChangeListeners();
    PropertyChangeEvent event = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
    List<Throwable> errors = new LinkedList<Throwable>();
    for (VetoableChangeListener listener : vetoers) {
        try {
            listener.vetoableChange(event);
        } catch (PropertyVetoException pve) {
            // In this case we do NOT run the subsequent listeners
            errors.add(pve);
            throw new MultiException(errors);
        } catch (Throwable th) {
            // In this case we DO run the subsequent listeners but will
            // report all the errors in the end
            errors.add(th);
        }
    }
    if (!errors.isEmpty()) {
        throw new MultiException(errors);
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) VetoableChangeListener(java.beans.VetoableChangeListener) LinkedList(java.util.LinkedList) PropertyVetoException(java.beans.PropertyVetoException) BaseHK2JAXBBean(org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) MultiException(org.glassfish.hk2.api.MultiException) Validator(jakarta.validation.Validator)

Aggregations

ConstraintViolationException (jakarta.validation.ConstraintViolationException)123 Test (org.testng.annotations.Test)64 Validator (jakarta.validation.Validator)37 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)35 ConstraintViolation (jakarta.validation.ConstraintViolation)32 TestForIssue (org.hibernate.validator.testutil.TestForIssue)26 HibernateValidator (org.hibernate.validator.HibernateValidator)19 Size (jakarta.validation.constraints.Size)18 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)15 Test (org.junit.Test)14 Session (org.hibernate.Session)10 Transaction (org.hibernate.Transaction)10 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)10 CustomerRepositoryImpl (org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl)10 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)9 Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)9 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)9 ArrayList (java.util.ArrayList)8 Set (java.util.Set)8 FacesMessage (javax.faces.application.FacesMessage)8