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();
}
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();
}
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();
}
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();
}
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);
}
}
Aggregations