Search in sources :

Example 1 with CallbackSub1Sub1

use of org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1 in project tests by datanucleus.

the class CallbackTest method testCallbackNotOverridden.

public void testCallbackNotOverridden() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            CallbackBase.invoked.clear();
            tx.begin();
            CallbackSub1Sub1 d = new CallbackSub1Sub1();
            d.setName("dpt1");
            d.setId("1");
            em.persist(d);
            em.flush();
            assertEquals(1, CallbackBase.invoked.size());
            assertEquals(CallbackSub1Sub1.class.getName(), (CallbackBase.invoked.get(0)).getName());
            tx.rollback();
        } finally {
            CallbackBase.invoked.clear();
            CallbackSub1Listener.invoked.clear();
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(CallbackSub1Sub1.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CallbackSub1Sub1(org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1)

Example 2 with CallbackSub1Sub1

use of org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1 in project tests by datanucleus.

the class CallbackTest method testExpectionInCallbackListenerNotEaten.

public void testExpectionInCallbackListenerNotEaten() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            CallbackSub1Listener.raiseException = true;
            CallbackSub1Sub1 d = new CallbackSub1Sub1();
            d.setName("dpt1");
            d.setId("1");
            em.persist(d);
            em.flush();
            fail("Expected ArithmeticException");
        } catch (ArithmeticException ex) {
        // expected
        } finally {
            CallbackBase.invoked.clear();
            CallbackSub1Listener.raiseException = false;
            CallbackSub1Listener.invoked.clear();
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(CallbackSub1Sub1.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CallbackSub1Sub1(org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1)

Example 3 with CallbackSub1Sub1

use of org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1 in project tests by datanucleus.

the class CallbackTest method testListenerClassCalled.

public void testListenerClassCalled() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            CallbackSub1Sub1 d = new CallbackSub1Sub1();
            d.setName("dpt1");
            d.setId("1");
            em.persist(d);
            em.flush();
            assertEquals(2, CallbackSub1Listener.invoked.size());
            assertEquals(PrePersist.class.getName(), CallbackSub1Listener.invoked.get(0));
            assertEquals(PostPersist.class.getName(), CallbackSub1Listener.invoked.get(1));
            tx.rollback();
        } finally {
            CallbackBase.invoked.clear();
            CallbackSub1Listener.invoked.clear();
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(CallbackSub1Sub1.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CallbackSub1Sub1(org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1) PrePersist(javax.persistence.PrePersist) PostPersist(javax.persistence.PostPersist)

Aggregations

EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 CallbackSub1Sub1 (org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1)3 PostPersist (javax.persistence.PostPersist)1 PrePersist (javax.persistence.PrePersist)1