Package org.apache.cayenne.jpa.itest.ch3

Examples of org.apache.cayenne.jpa.itest.ch3._3_1_1_EntityManagerTransactionTest


public class _3_5_1_LifecycleCallbackMethodsTest extends EntityManagerCase {

    public void testPrePersist() {

        // regular entity
        CallbackEntity e = new CallbackEntity();
        assertFalse(e.isPrePersistCalled());

        EntityManager em = getEntityManager();

        // spec reqires the callback to be invoked as a part of persist, without waiting
        // for flush or commit.
        em.persist(e);
        assertTrue(e.isPrePersistCalled());

        // entity with same callback method handling multiple callbacks
        CallbackEntity2 e2 = new CallbackEntity2();
        assertFalse(e2.isMixedCallbackCalled());
        em.persist(e2);
View Full Code Here


    }

    public void testPostPersist() {

        // regular entity
        CallbackEntity e = new CallbackEntity();
        assertFalse(e.isPostPersistCalled());

        // don't use super getEntityManager - it starts a tran
        EntityManager em = ItestSetup.getInstance().createEntityManager();

        // spec reqires the callback to be invoked as a part of persist, without waiting
        // for flush or commit.
        em.getTransaction().begin();
        em.persist(e);
        assertFalse(e.isPostPersistCalled());
        assertEquals(0, e.getPostPersistedId());
        em.getTransaction().commit();

        assertTrue(e.isPostPersistCalled());
       
        // Per spec, id must be availble during PostPersist
        assertEquals(e.getId(), e.getPostPersistedId());
        assertTrue(e.getId() > 0);

        // external listeners
        EntityListenerState.reset();
        assertEquals("", EntityListenerState.getPostPersistCalled());
        ListenerEntity1 e3 = new ListenerEntity1();
View Full Code Here

        // for flush or commit.
        em.persist(e);
        assertTrue(e.isPrePersistCalled());

        // entity with same callback method handling multiple callbacks
        CallbackEntity2 e2 = new CallbackEntity2();
        assertFalse(e2.isMixedCallbackCalled());
        em.persist(e2);
        assertTrue(e2.isMixedCallbackCalled());

        // external listeners
        EntityListenerState.reset();
        assertEquals("", EntityListenerState.getPrePersistCalled());
        ListenerEntity1 e3 = new ListenerEntity1();
View Full Code Here

        assertTrue(e2.isMixedCallbackCalled());

        // external listeners
        EntityListenerState.reset();
        assertEquals("", EntityListenerState.getPrePersistCalled());
        ListenerEntity1 e3 = new ListenerEntity1();
        em.persist(e3);
        assertEquals(":"
                + EntityListener1.class.getName()
                + ":"
                + EntityListener2.class.getName(), EntityListenerState
View Full Code Here

        assertTrue(e.getId() > 0);

        // external listeners
        EntityListenerState.reset();
        assertEquals("", EntityListenerState.getPostPersistCalled());
        ListenerEntity1 e3 = new ListenerEntity1();

        // reset EM
        em = ItestSetup.getInstance().createEntityManager();
        assertEquals("", EntityListenerState.getPostPersistCalled());
        em.getTransaction().begin();
View Full Code Here

                + EntityListener2.class.getName(), EntityListenerState
                .getPrePersistCalled());

        EntityListenerState.reset();
        assertEquals("", EntityListenerState.getPrePersistCalled());
        ListenerEntity2 e4 = new ListenerEntity2();
        em.persist(e4);
        // here annotations must be called in a different order from e3.
        assertEquals(":"
                + EntityListener2.class.getName()
                + ":"
View Full Code Here

        assertEquals(1, getDbHelper().getRowCount("SimpleEntity"));
    }

    public void testPersistNonEntity() throws Exception {
        try {
            getEntityManager().persist(new NonEntity());
            fail("Must have thrown IllegalARgumentException on non entity");
        }
        catch (IllegalArgumentException e) {
            // expected
        }
View Full Code Here

    }

    public void testMergeNonEntity() throws Exception {

        try {
            getEntityManager().merge(new NonEntity());
            fail("must have thrown IllegalArgumentException on merging a non entity.");
        }
        catch (IllegalArgumentException e) {
            // expected
        }
View Full Code Here

        assertEquals(0, getDbHelper().getRowCount("SimpleEntity"));
    }

    public void testRemoveNonEntity() throws Exception {
        try {
            getEntityManager().remove(new NonEntity());
            fail("Must have thrown IllegalArgumentException on non entity");
        }
        catch (IllegalArgumentException e) {
            // expected
        }
View Full Code Here

        Query query = em.createQuery("SELECT a FROM SimpleEntity a ORDER BY a.property1");
        query.setMaxResults(2);
       
        List<?> results = query.getResultList();
        assertEquals(2, results.size());
        SimpleEntity e1 = (SimpleEntity) results.get(0);
        SimpleEntity e2 = (SimpleEntity) results.get(1);
        assertEquals("A", e1.getProperty1());
        assertEquals("B", e2.getProperty1());
    }
View Full Code Here

TOP

Related Classes of org.apache.cayenne.jpa.itest.ch3._3_1_1_EntityManagerTransactionTest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.