Search in sources :

Example 1 with ValidatedPerson

use of org.datanucleus.samples.validation.ValidatedPerson in project tests by datanucleus.

the class BeanValidationTest method testNotNull.

/**
 * Test for not null annotation.
 */
public void testNotNull() {
    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 {
            tx.begin();
            ValidatedPerson p1 = new ValidatedPerson("John", null);
            pm.makePersistent(p1);
            tx.commit();
            fail("Should have thrown validation exception on persist but didnt");
        } catch (ConstraintViolationException cve) {
            // expected
            LOG.info("Exception correctly thrown : " + cve.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) ValidatedPerson(org.datanucleus.samples.validation.ValidatedPerson)

Example 2 with ValidatedPerson

use of org.datanucleus.samples.validation.ValidatedPerson in project tests by datanucleus.

the class BeanValidationTest method testInsertSize.

public void testInsertSize() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ValidatedPerson p1 = new ValidatedPerson(1, "Fred", "Smith");
            p1.setLogin("012345678901");
            em.persist(p1);
            tx.commit();
            fail("Should have thrown ConstraintViolationException");
        } catch (ConstraintViolationException cve) {
        // Expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ValidatedPerson.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidatedPerson(org.datanucleus.samples.validation.ValidatedPerson)

Example 3 with ValidatedPerson

use of org.datanucleus.samples.validation.ValidatedPerson in project tests by datanucleus.

the class BeanValidationTest method testInsertNotNull.

public void testInsertNotNull() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ValidatedPerson p1 = new ValidatedPerson(1, "Fred", null);
            em.persist(p1);
            tx.commit();
            fail("Should have thrown ConstraintViolationException");
        } catch (ConstraintViolationException cve) {
        // Expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ValidatedPerson.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidatedPerson(org.datanucleus.samples.validation.ValidatedPerson)

Example 4 with ValidatedPerson

use of org.datanucleus.samples.validation.ValidatedPerson in project tests by datanucleus.

the class BeanValidationTest method testUpdateNotNull.

public void testUpdateNotNull() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ValidatedPerson p1 = new ValidatedPerson(1, "Fred", "Smith");
            em.persist(p1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            ValidatedPerson p1 = (ValidatedPerson) em.find(ValidatedPerson.class, new Long(1));
            p1.setSurname(null);
            tx.commit();
            fail("Should have thrown a ConstraintViolationException on update");
        } catch (ConstraintViolationException cve) {
        // Expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ValidatedPerson.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidatedPerson(org.datanucleus.samples.validation.ValidatedPerson)

Example 5 with ValidatedPerson

use of org.datanucleus.samples.validation.ValidatedPerson in project tests by datanucleus.

the class BeanValidationTest method testSize.

/**
 * Test for @Size annotation.
 */
public void testSize() {
    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 {
            tx.begin();
            ValidatedPerson p1 = new ValidatedPerson("John", "Smith");
            p1.setLogin("12345678901");
            LOG.info(">> Calling persist");
            pm.makePersistent(p1);
            tx.commit();
            fail("Should have thrown validation exception on persist but didnt");
        } catch (ConstraintViolationException cve) {
            // expected
            LOG.info("Exception correctly thrown : " + cve.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) ValidatedPerson(org.datanucleus.samples.validation.ValidatedPerson)

Aggregations

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