Search in sources :

Example 21 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPreRemoveCallback.

@Test
public void testPreRemoveCallback() throws Exception {
    // an exception was triggered within POST_LOAD callback
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    final boolean[] flag = new boolean[1];
    try {
        callbackRegistry.addListener(MtTable1.class, new LifecycleListener() {

            public void postLoad(Object entity) {
            }

            public void postPersist(Object entity) {
            }

            public void postRemove(Object entity) {
            }

            public void postUpdate(Object entity) {
            }

            public void postAdd(Object entity) {
            }

            public void preRemove(Object entity) {
                flag[0] = true;
            }

            public void preUpdate(Object entity) {
            }

            public void prePersist(Object entity) {
            }
        });
        ClientMtTable1 object = clientContext.newObject(ClientMtTable1.class);
        assertFalse(flag[0]);
        clientContext.commitChanges();
        assertFalse(flag[0]);
        clientContext.deleteObjects(object);
        clientContext.commitChanges();
        assertTrue(flag[0]);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 22 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    final ObjectId prefetchedId = new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, 1);
    SelectQuery q = new SelectQuery(ClientMtTable2.class);
    q.addOrdering(ClientMtTable2.GLOBAL_ATTRIBUTE_PROPERTY, SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            for (ClientMtTable2 o : results) {
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(clientContext, o.getObjectContext());
                ClientMtTable1 o1 = o.getTable1();
                assertNotNull(o1);
                assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
                assertSame(clientContext, o1.getObjectContext());
                assertEquals(prefetchedId, o1.getObjectId());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 23 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class CayenneContextWithDataContextIT method testCreateBadFault.

@Test
public void testCreateBadFault() throws Exception {
    tMtTable1.insert(1, "g1", "s1");
    ObjectId id = new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, 2);
    Object fault = clientContext.createFault(id);
    assertTrue(fault instanceof ClientMtTable1);
    ClientMtTable1 o = (ClientMtTable1) fault;
    // try tripping fault
    try {
        o.getGlobalAttribute1();
        fail("resolving bad fault should've thrown");
    } catch (FaultFailureException e) {
    // expected
    }
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Example 24 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class NestedCayenneContextIT method testDeleteNew.

@Test
public void testDeleteNew() throws Exception {
    ObjectContext child = runtime.newContext(clientContext);
    ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
    clientContext.commitChanges();
    ClientMtTable2 p = child.newObject(ClientMtTable2.class);
    ClientMtTable1 aChild = (ClientMtTable1) Cayenne.objectForPK(child, a.getObjectId());
    p.setGlobalAttribute("X");
    aChild.addToTable2Array(p);
    child.commitChangesToParent();
    child.deleteObjects(p);
    aChild.removeFromTable2Array(p);
    child.commitChangesToParent();
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Test(org.junit.Test)

Example 25 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class NestedCayenneContextIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    final ClientMtTable1 mt11 = clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 mt21 = clientContext.newObject(ClientMtTable2.class);
    ClientMtTable2 mt22 = clientContext.newObject(ClientMtTable2.class);
    mt21.setTable1(mt11);
    mt22.setTable1(mt11);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    SelectQuery<ClientMtTable2> q = new SelectQuery<ClientMtTable2>(ClientMtTable2.class);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = child.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            Iterator<?> it = results.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = (ClientMtTable2) it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(child, o.getObjectContext());
                ClientMtTable1 o1 = o.getTable1();
                assertNotNull(o1);
                assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
                assertSame(child, o1.getObjectContext());
                assertEquals(mt11.getObjectId(), o1.getObjectId());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) Test(org.junit.Test)

Aggregations

ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)112 Test (org.junit.Test)112 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)46 SelectQuery (org.apache.cayenne.query.SelectQuery)26 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)19 ObjectContext (org.apache.cayenne.ObjectContext)12 Expression (org.apache.cayenne.exp.Expression)12 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)11 List (java.util.List)8 ObjectId (org.apache.cayenne.ObjectId)7 EntityResolver (org.apache.cayenne.map.EntityResolver)7 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)7 RemoteIncrementalFaultList (org.apache.cayenne.remote.RemoteIncrementalFaultList)6 GraphDiff (org.apache.cayenne.graph.GraphDiff)5 MockEventManager (org.apache.cayenne.event.MockEventManager)4 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)3 MtTable1 (org.apache.cayenne.testdo.mt.MtTable1)3