Search in sources :

Example 61 with ClientMtTable1

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

the class CayenneContextIT method testBeforePropertyReadShouldInflateHollow.

@Test
public void testBeforePropertyReadShouldInflateHollow() {
    ObjectId gid = new ObjectId("MtTable1", "a", "b");
    final ClientMtTable1 inflated = new ClientMtTable1();
    inflated.setPersistenceState(PersistenceState.COMMITTED);
    inflated.setObjectId(gid);
    inflated.setGlobalAttribute1("abc");
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(inflated));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    // check that a HOLLOW object is infalted on "beforePropertyRead"
    ClientMtTable1 hollow = new ClientMtTable1();
    hollow.setPersistenceState(PersistenceState.HOLLOW);
    hollow.setObjectId(gid);
    final boolean[] selectExecuted = new boolean[1];
    CayenneContext context = new CayenneContext(channel) {

        @Override
        public List<?> performQuery(Query query) {
            selectExecuted[0] = true;
            return super.performQuery(query);
        }
    };
    context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
    context.graphManager.registerNode(hollow.getObjectId(), hollow);
    // testing this...
    context.prepareForAccess(hollow, ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, false);
    assertTrue(selectExecuted[0]);
    assertSame(hollow, context.getGraphManager().getNode(gid));
    assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
    assertEquals(PersistenceState.COMMITTED, hollow.getPersistenceState());
}
Also used : Query(org.apache.cayenne.query.Query) GenericResponse(org.apache.cayenne.util.GenericResponse) BootstrapMessage(org.apache.cayenne.remote.BootstrapMessage) ClientMessage(org.apache.cayenne.remote.ClientMessage) EntityResolver(org.apache.cayenne.map.EntityResolver) ClientChannel(org.apache.cayenne.remote.ClientChannel) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConnection(org.apache.cayenne.remote.ClientConnection) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 62 with ClientMtTable1

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

the class CayenneContextInheritanceIT method testPerformQueryInheritanceSuper.

@Test
public void testPerformQueryInheritanceSuper() throws Exception {
    tMtTable1.insert(1, "a", "yyy", null);
    tMtTable1.insert(2, "sub1", "zzz", "sa1");
    tMtTable1.insert(3, "z", "aaa", null);
    SelectQuery query = new SelectQuery(ClientMtTable1.class);
    List<ClientMtTable1> objects = context.performQuery(query);
    assertEquals(3, objects.size());
    int checkedFields = 0;
    for (int i = 0; i < objects.size(); i++) {
        Integer id = (Integer) objects.get(i).getObjectId().getIdSnapshot().get("TABLE1_ID");
        if (id == 1) {
            assertEquals("a", objects.get(i).getGlobalAttribute1());
            checkedFields++;
        } else if (id == 2) {
            assertEquals("sa1", ((ClientMtTable1Subclass1) objects.get(i)).getSubclass1Attribute1());
            checkedFields++;
        }
    }
    assertEquals(2, checkedFields);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1Subclass1(org.apache.cayenne.testdo.mt.ClientMtTable1Subclass1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Example 63 with ClientMtTable1

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

the class CayenneContextInheritanceIT method testPerformQueryWithQualifierInheritanceSuper.

@Test
public void testPerformQueryWithQualifierInheritanceSuper() throws Exception {
    tMtTable1.insert(1, "a", "XX", null);
    tMtTable1.insert(2, "sub1", "XXA", "sa1");
    tMtTable1.insert(3, "z", "MM", null);
    SelectQuery query = new SelectQuery(ClientMtTable1.class);
    query.andQualifier(ExpressionFactory.likeExp(ClientMtTable1.SERVER_ATTRIBUTE1_PROPERTY, "X%"));
    List<ClientMtTable1> objects = context.performQuery(query);
    assertEquals(2, objects.size());
    int checkedFields = 0;
    for (int i = 0; i < objects.size(); i++) {
        Integer id = (Integer) objects.get(i).getObjectId().getIdSnapshot().get("TABLE1_ID");
        if (id == 1) {
            assertEquals("a", objects.get(i).getGlobalAttribute1());
            checkedFields++;
        } else if (id == 2) {
            assertEquals("sa1", ((ClientMtTable1Subclass1) objects.get(i)).getSubclass1Attribute1());
            checkedFields++;
        }
    }
    assertEquals(2, checkedFields);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1Subclass1(org.apache.cayenne.testdo.mt.ClientMtTable1Subclass1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Example 64 with ClientMtTable1

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

the class CayenneContextRefreshQueryIT method testRefreshToMany.

@Test
public void testRefreshToMany() throws Exception {
    createM1AndTwoM2sDataSet();
    ClientMtTable1 a = Cayenne.objectForPK(context, ClientMtTable1.class, 1);
    assertEquals(2, a.getTable2Array().size());
    delete1M2DataSet();
    RefreshQuery refresh = new RefreshQuery(a);
    context.performGenericQuery(refresh);
    assertEquals(PersistenceState.HOLLOW, a.getPersistenceState());
    assertEquals(1, a.getTable2Array().size());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) RefreshQuery(org.apache.cayenne.query.RefreshQuery) Test(org.junit.Test)

Example 65 with ClientMtTable1

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

the class CayenneContextSQLTemplateIT method testObjectRoot.

@Test
public void testObjectRoot() throws Exception {
    assertNull(Cayenne.objectForPK(context, ClientMtTable1.class, 1));
    context.performGenericQuery(new SQLTemplate(ClientMtTable1.class, "insert into MT_TABLE1 " + "(TABLE1_ID, GLOBAL_ATTRIBUTE1, SERVER_ATTRIBUTE1) " + "values (1, 'g1', 's1')"));
    assertNotNull(Cayenne.objectForPK(context, ClientMtTable1.class, 1));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) 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