Search in sources :

Example 1 with JodaSample4

use of org.datanucleus.samples.types.jodatime.JodaSample4 in project tests by datanucleus.

the class JodaTimeTest method testDuration.

/**
 * Test for Duration persistence and retrieval.
 */
public void testDuration() {
    try {
        // Create some data we can use for access
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        Duration duration1 = new Duration(20011986);
        Duration duration2 = new Duration(1234567890);
        try {
            tx.begin();
            JodaSample4 s = new JodaSample4(1, duration1, duration2);
            JodaSample4 s2 = new JodaSample4(2, null, null);
            em.persist(s);
            em.persist(s2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error persisting Duration sample", e);
            fail("Error persisting Duration sample");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Retrieve the data
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            JodaSample4 s = em.find(JodaSample4.class, 1);
            Duration ld1 = s.getDuration1();
            assertNotNull("Retrieved Duration was null!", ld1);
            assertEquals("Duration was wrong", 20011986, ld1.getMillis());
            Duration ld2 = s.getDuration2();
            assertNotNull("Retrieved Duration was null!", ld2);
            assertEquals("Duration was wrong", 1234567890, ld2.getMillis());
            JodaSample4 s2 = em.find(JodaSample4.class, 2);
            assertNull(s2.getDuration1());
            assertNull(s2.getDuration2());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error retrieving Duration data", e);
            fail("Error retrieving Duration data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(JodaSample4.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JodaSample4(org.datanucleus.samples.types.jodatime.JodaSample4) Duration(org.joda.time.Duration)

Aggregations

EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 JodaSample4 (org.datanucleus.samples.types.jodatime.JodaSample4)1 Duration (org.joda.time.Duration)1