use of org.datanucleus.samples.validation.ValidatedPerson3 in project tests by datanucleus.
the class BeanValidationTest method testTransactionalNotNull.
/**
* Test for not null annotation on a transactional field.
*/
public void testTransactionalNotNull() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_VALIDATION_MODE, "auto");
PersistenceManagerFactory validationPMF = getPMF(1, userProps);
try {
PersistenceManager pm = validationPMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Persist with null password
tx.begin();
ValidatedPerson3 p1 = new ValidatedPerson3("John", "Smith");
pm.makePersistent(p1);
tx.commit();
fail("Exception should have been thrown, but persisted with null transactional field");
} catch (ConstraintViolationException cve) {
LOG.info("Exception thrown as expected : " + cve.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = validationPMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ValidatedPerson3 p1 = new ValidatedPerson3("John", "Smith");
p1.setPassword("secret");
pm.makePersistent(p1);
tx.commit();
} catch (ConstraintViolationException cve) {
LOG.error("Exception thrown", cve);
fail("Exception thrown but shouldn't have been");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(validationPMF, ValidatedPerson3.class);
validationPMF.close();
}
}
Aggregations