public void testBeanValidation() throws Exception { Artist a = new Artist(); a.setName("TOO OLD ARTIST"); a.setAge(120); XPersistence.getManager().persist(a); try { XPersistence.commit(); } catch (RollbackException ex) { if (ex.getCause() instanceof javax.validation.ConstraintViolationException) { javax.validation.ConstraintViolationException vex = (javax.validation.ConstraintViolationException) ex.getCause(); assertEquals("1 invalid value is expected", 1, vex.getConstraintViolations().size()); ConstraintViolation violation = vex.getConstraintViolations().iterator().next(); assertEquals("Bean", "Artist", violation.getRootBeanClass().getSimpleName()); String expectedMessage = "es".equals(Locale.getDefault().getLanguage()) ? "tiene que ser menor o igual que 90" : "must be less than or equal to 90"; assertEquals("Message text", expectedMessage, violation.getMessage()); return; } } fail("A constraint violation exception should be thrown"); }
@Override public void createBluetoothDwell(BluetoothDwell entry) { try { em.persist(entry); } catch (javax.validation.ConstraintViolationException err) { logger.error(err.getConstraintViolations().toString()); } catch (javax.ejb.EJBException err) { Throwable cause = err.getCause(); if (cause instanceof ConstraintViolationException) { logger.error(((ConstraintViolationException) cause).getConstraintViolations().toString()); } } catch (Exception err) { logger.error(null, err); } }
@Override public void updateBluetoothFileSendSummary(BluetoothFileSendSummary entry) { try { em.merge(entry); } catch (javax.validation.ConstraintViolationException err) { logger.error(err.getConstraintViolations().toString()); } catch (javax.ejb.EJBException err) { Throwable cause = err.getCause(); if (cause instanceof ConstraintViolationException) { logger.error(((ConstraintViolationException) cause).getConstraintViolations().toString()); } else { logger.error(null, err); } } catch (Exception err) { logger.error(null, err); } }