use of org.datanucleus.samples.validation.ValidatedPerson2 in project tests by datanucleus.
the class BeanValidationTest method testNonPersistentNotNull.
/**
* Test for not null annotation on a non-persistent field.
*/
public void testNonPersistentNotNull() {
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();
ValidatedPerson2 p1 = new ValidatedPerson2("John", "Smith");
pm.makePersistent(p1);
tx.commit();
fail("Exception should have been thrown, but persisted with null non-persistent 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();
ValidatedPerson2 p1 = new ValidatedPerson2("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, ValidatedPerson2.class);
validationPMF.close();
}
}
use of org.datanucleus.samples.validation.ValidatedPerson2 in project tests by datanucleus.
the class BeanValidationTest method testInsertTransientNotNull.
public void testInsertTransientNotNull() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
ValidatedPerson2 p1 = new ValidatedPerson2(1, "Fred", "Bloggs");
em.persist(p1);
tx.commit();
fail("Should have thrown exception with null transient field, but persisted!");
} catch (ConstraintViolationException cve) {
// Expected
LOG.info("Threw exception when attempting persist of object with null NotNull transient field");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
ValidatedPerson2 p1 = new ValidatedPerson2(1, "Fred", "Bloggs");
p1.setPassword("topsecret");
em.persist(p1);
tx.commit();
} catch (ConstraintViolationException cve) {
LOG.error("Exception in persist of object with transient not null field", cve);
fail("Should have persisted ok but exception thrown : " + cve.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(ValidatedPerson2.class);
}
}
Aggregations