Search in sources :

Example 1 with ValidatedPerson2

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

Example 2 with ValidatedPerson2

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);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidatedPerson2(org.datanucleus.samples.validation.ValidatedPerson2)

Aggregations

ConstraintViolationException (javax.validation.ConstraintViolationException)2 ValidatedPerson2 (org.datanucleus.samples.validation.ValidatedPerson2)2 Properties (java.util.Properties)1 PersistenceManager (javax.jdo.PersistenceManager)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Transaction (javax.jdo.Transaction)1 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1