Search in sources :

Example 1 with ValidatedPerson3

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();
    }
}
Also used : ValidatedPerson3(org.datanucleus.samples.validation.ValidatedPerson3) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Aggregations

Properties (java.util.Properties)1 PersistenceManager (javax.jdo.PersistenceManager)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Transaction (javax.jdo.Transaction)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 ValidatedPerson3 (org.datanucleus.samples.validation.ValidatedPerson3)1