Search in sources :

Example 76 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class DispatchHelperTest method testBootstrapMessage.

@Test
public void testBootstrapMessage() {
    EntityResolver resolver = new EntityResolver();
    MockDataChannel channel = new MockDataChannel(resolver);
    assertSame(resolver.getClientEntityResolver(), DispatchHelper.dispatch(channel, new BootstrapMessage()));
}
Also used : BootstrapMessage(org.apache.cayenne.remote.BootstrapMessage) EntityResolver(org.apache.cayenne.map.EntityResolver) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Example 77 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class AshwoodEntitySorter method buildDigraph.

protected <E> Digraph<E, Boolean> buildDigraph(ObjEntity objEntity, List<E> objects, Function<E, Persistent> converter) {
    EntityResolver resolver = converter.apply(objects.get(0)).getObjectContext().getEntityResolver();
    ClassDescriptor descriptor = resolver.getClassDescriptor(objEntity.getName());
    String[] reflexiveRelNames = getReflexiveRelationshipsNames(objEntity);
    int size = objects.size();
    Digraph<E, Boolean> objectDependencyGraph = new MapDigraph<>();
    Persistent[] masters = new Persistent[reflexiveRelNames.length];
    for (int i = 0; i < size; i++) {
        E current = objects.get(i);
        objectDependencyGraph.addVertex(current);
        int actualMasterCount = 0;
        for (int k = 0; k < reflexiveRelNames.length; k++) {
            String reflexiveRelName = reflexiveRelNames[k];
            if (reflexiveRelName == null) {
                continue;
            }
            Persistent persistent = converter.apply(current);
            masters[k] = (Persistent) descriptor.getProperty(reflexiveRelName).readProperty(persistent);
            if (masters[k] == null) {
                masters[k] = findReflexiveMaster(persistent, objEntity.getRelationship(reflexiveRelName), persistent.getObjectId().getEntityName());
            }
            if (masters[k] != null) {
                actualMasterCount++;
            }
        }
        int mastersFound = 0;
        for (int j = 0; j < size && mastersFound < actualMasterCount; j++) {
            if (i == j) {
                continue;
            }
            E masterCandidate = objects.get(j);
            for (Persistent master : masters) {
                if (converter.apply(masterCandidate) == master) {
                    objectDependencyGraph.putArc(masterCandidate, current, Boolean.TRUE);
                    mastersFound++;
                }
            }
        }
    }
    return objectDependencyGraph;
}
Also used : MapDigraph(org.apache.cayenne.ashwood.graph.MapDigraph) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Persistent(org.apache.cayenne.Persistent) EntityResolver(org.apache.cayenne.map.EntityResolver)

Example 78 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class EJBQLQueryIT method testCacheParameters.

@Test
public void testCacheParameters() {
    String ejbql1 = "select a FROM Artist a WHERE a.artistName = ?1 OR a.artistName = :name";
    EJBQLQuery q1 = new EJBQLQuery(ejbql1);
    q1.setParameter(1, "X");
    q1.setParameter("name", "Y");
    q1.setFetchOffset(1);
    q1.setFetchLimit(5);
    q1.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    String ejbql2 = "select a FROM Artist a WHERE a.artistName = ?1 OR a.artistName = :name";
    EJBQLQuery q2 = new EJBQLQuery(ejbql2);
    q2.setParameter(1, "X");
    q2.setParameter("name", "Y");
    q2.setFetchOffset(1);
    q2.setFetchLimit(5);
    q2.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    assertEquals(q1.getMetaData(resolver).getCacheKey(), q2.getMetaData(resolver).getCacheKey());
}
Also used : EntityResolver(org.apache.cayenne.map.EntityResolver) Test(org.junit.Test)

Example 79 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class CayenneContextIT method testBeforePropertyReadShouldInflateHollow.

@Test
public void testBeforePropertyReadShouldInflateHollow() {
    ObjectId gid = ObjectId.of("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.getName(), 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 80 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class CayenneContextIT method testBeforeHollowDeleteShouldChangeStateToCommited.

@Test
public void testBeforeHollowDeleteShouldChangeStateToCommited() {
    ObjectId gid = ObjectId.of("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);
    CayenneContext context = new CayenneContext(channel);
    context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
    ClientMtTable1 hollow = context.localObject(inflated);
    assertEquals(PersistenceState.HOLLOW, hollow.getPersistenceState());
    // testing this...
    context.deleteObjects(hollow);
    assertSame(hollow, context.getGraphManager().getNode(gid));
    assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
    assertEquals(PersistenceState.DELETED, hollow.getPersistenceState());
}
Also used : 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)

Aggregations

EntityResolver (org.apache.cayenne.map.EntityResolver)87 Test (org.junit.Test)57 ObjEntity (org.apache.cayenne.map.ObjEntity)17 ObjectId (org.apache.cayenne.ObjectId)14 DataMap (org.apache.cayenne.map.DataMap)14 DbEntity (org.apache.cayenne.map.DbEntity)10 ArrayList (java.util.ArrayList)7 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 HashMap (java.util.HashMap)6 QueryResponse (org.apache.cayenne.QueryResponse)6 DataNode (org.apache.cayenne.access.DataNode)6 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)6 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)6 MockEventManager (org.apache.cayenne.event.MockEventManager)5 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)5 GenericResponse (org.apache.cayenne.util.GenericResponse)5 List (java.util.List)4 MappingNamespace (org.apache.cayenne.map.MappingNamespace)4 Artist (org.apache.cayenne.testdo.testmap.Artist)4 Map (java.util.Map)3