Search in sources :

Example 16 with ClientMtTable1

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

the class CayenneContextValidationIT method testValidate.

@Test
public void testValidate() throws Exception {
    ClientMtTable1 o1 = context.newObject(ClientMtTable1.class);
    o1.setGlobalAttribute1("G1");
    o1.resetValidation(false);
    // this one is not validating
    ClientMtTable2 o2 = context.newObject(ClientMtTable2.class);
    o2.setTable1(o1);
    context.commitChanges();
    assertTrue(o1.isValidatedForInsert());
    assertFalse(o1.isValidatedForDelete());
    assertFalse(o1.isValidatedForUpdate());
    o1.resetValidation(false);
    o1.setGlobalAttribute1("G2");
    context.commitChanges();
    assertFalse(o1.isValidatedForInsert());
    assertFalse(o1.isValidatedForDelete());
    assertTrue(o1.isValidatedForUpdate());
    o1.resetValidation(false);
    context.deleteObjects(o1);
    context.deleteObjects(o2);
    context.commitChanges();
    assertFalse(o1.isValidatedForInsert());
    assertTrue(o1.isValidatedForDelete());
    assertFalse(o1.isValidatedForUpdate());
    ClientMtTable1 o11 = context.newObject(ClientMtTable1.class);
    o11.setGlobalAttribute1("G1");
    o11.resetValidation(true);
    try {
        context.commitChanges();
        fail("Validation failure must have prevented commit");
    } catch (ValidationException e) {
    // expected
    }
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ValidationException(org.apache.cayenne.validation.ValidationException) Test(org.junit.Test)

Example 17 with ClientMtTable1

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

the class CayenneContextWithDataContextIT method testPrefetchingToMany.

@Test
public void testPrefetchingToMany() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    SelectQuery q = new SelectQuery(ClientMtTable1.class);
    q.addOrdering(ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable1.TABLE2ARRAY_PROPERTY);
    final List<ClientMtTable1> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            ClientMtTable1 o1 = results.get(0);
            assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
            assertSame(clientContext, o1.getObjectContext());
            List<?> children1 = o1.getTable2Array();
            assertEquals(2, children1.size());
            Iterator<?> it = children1.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = (ClientMtTable2) it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(clientContext, o.getObjectContext());
            // TODO: fixme... reverse relationship is not connected and will
            // cause a fetch
            // assertEquals(o1, o.getTable1());
            }
        }
    });
}
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) RemoteIncrementalFaultList(org.apache.cayenne.remote.RemoteIncrementalFaultList) List(java.util.List) Test(org.junit.Test)

Example 18 with ClientMtTable1

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

the class CayenneContextWithDataContextIT method testSetValueHolder.

@Test
public void testSetValueHolder() throws Exception {
    ClientMtTable1 t1 = clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 t2 = clientContext.newObject(ClientMtTable2.class);
    t2.setTable1(t1);
    assertEquals(1, t1.getTable2Array().size());
    assertSame(t1, t2.getTable1());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Test(org.junit.Test)

Example 19 with ClientMtTable1

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

the class CayenneContextWithDataContextIT method testAddToList.

@Test
public void testAddToList() throws Exception {
    ClientMtTable1 t1 = clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 t2 = clientContext.newObject(ClientMtTable2.class);
    t1.addToTable2Array(t2);
    assertEquals(1, t1.getTable2Array().size());
    assertSame(t1, t2.getTable1());
    // do it again to make sure action can handle series of changes
    ClientMtTable1 t3 = clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 t4 = clientContext.newObject(ClientMtTable2.class);
    t3.addToTable2Array(t4);
    assertEquals(1, t3.getTable2Array().size());
    assertSame(t3, t4.getTable1());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Test(org.junit.Test)

Example 20 with ClientMtTable1

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

the class CayenneContextWithDataContextIT method testPostAddOnObjectCallback.

@Test
public void testPostAddOnObjectCallback() throws Exception {
    final DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();
    LifecycleCallbackRegistry callbackRegistry = serverContext.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addCallback(LifecycleEvent.POST_ADD, MtTable1.class, "prePersistMethod");
        final Persistent clientObject = clientContext.newObject(ClientMtTable1.class);
        clientContext.commitChanges();
        new ParallelTestContainer() {

            @Override
            protected void assertResult() throws Exception {
                // find peer
                MtTable1 peer = (MtTable1) serverContext.getGraphManager().getNode(clientObject.getObjectId());
                assertNotNull(peer);
                assertTrue(peer.isPrePersisted());
            }
        }.runTest(1000);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : DataContext(org.apache.cayenne.access.DataContext) MtTable1(org.apache.cayenne.testdo.mt.MtTable1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) 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