Search in sources :

Example 31 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 = 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);
    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)

Example 32 with EntityResolver

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

the class CayenneContextIT method testDeleteObject.

@Test
public void testDeleteObject() {
    CayenneContext context = new CayenneContext(new MockDataChannel());
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    // TRANSIENT ... should quietly ignore it
    Persistent transientObject = new MockPersistentObject();
    context.deleteObjects(transientObject);
    assertEquals(PersistenceState.TRANSIENT, transientObject.getPersistenceState());
    // NEW ... should make it TRANSIENT
    // create via context to make sure that object store would register it
    Persistent newObject = context.newObject(MockPersistentObject.class);
    assertNotNull(newObject.getObjectContext());
    context.deleteObjects(newObject);
    assertNull(newObject.getObjectContext());
    assertEquals(PersistenceState.TRANSIENT, newObject.getPersistenceState());
    assertFalse(context.internalGraphManager().dirtyNodes().contains(newObject.getObjectId()));
    // see CAY-547 for details...
    assertFalse(context.internalGraphManager().dirtyNodes().contains(null));
    // COMMITTED
    Persistent committed = new MockPersistentObject();
    committed.setPersistenceState(PersistenceState.COMMITTED);
    committed.setObjectId(new ObjectId("test_entity", "key", "value1"));
    committed.setObjectContext(context);
    context.deleteObjects(committed);
    assertEquals(PersistenceState.DELETED, committed.getPersistenceState());
    // MODIFIED
    Persistent modified = new MockPersistentObject();
    modified.setPersistenceState(PersistenceState.MODIFIED);
    modified.setObjectId(new ObjectId("test_entity", "key", "value2"));
    modified.setObjectContext(context);
    context.deleteObjects(modified);
    assertEquals(PersistenceState.DELETED, modified.getPersistenceState());
    // DELETED
    Persistent deleted = new MockPersistentObject();
    deleted.setPersistenceState(PersistenceState.DELETED);
    deleted.setObjectId(new ObjectId("test_entity", "key", "value3"));
    deleted.setObjectContext(context);
    context.deleteObjects(deleted);
    assertEquals(PersistenceState.DELETED, committed.getPersistenceState());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 33 with EntityResolver

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

the class CayenneContextIT method testNewObject.

@Test
public void testNewObject() {
    CayenneContext context = new CayenneContext(new MockDataChannel());
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    Persistent object = context.newObject(MockPersistentObject.class);
    assertNotNull(object);
    assertTrue(object instanceof MockPersistentObject);
    assertEquals(PersistenceState.NEW, object.getPersistenceState());
    assertSame(context, object.getObjectContext());
    assertTrue(context.internalGraphManager().dirtyNodes(PersistenceState.NEW).contains(object));
    assertNotNull(object.getObjectId());
    assertTrue(object.getObjectId().isTemporary());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 34 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 = 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 35 with EntityResolver

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

the class DataContextCallbacksIT method tearDown.

@After
public void tearDown() throws Exception {
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    resolver.getCallbackRegistry().clear();
}
Also used : EntityResolver(org.apache.cayenne.map.EntityResolver) After(org.junit.After)

Aggregations

EntityResolver (org.apache.cayenne.map.EntityResolver)78 Test (org.junit.Test)53 ObjectId (org.apache.cayenne.ObjectId)13 ObjEntity (org.apache.cayenne.map.ObjEntity)13 DataMap (org.apache.cayenne.map.DataMap)11 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 DataNode (org.apache.cayenne.access.DataNode)6 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)6 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)6 DbEntity (org.apache.cayenne.map.DbEntity)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 QueryResponse (org.apache.cayenne.QueryResponse)5 MockEventManager (org.apache.cayenne.event.MockEventManager)5 SelectQuery (org.apache.cayenne.query.SelectQuery)5 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)5 GenericResponse (org.apache.cayenne.util.GenericResponse)5 MappingNamespace (org.apache.cayenne.map.MappingNamespace)4 ObjRelationship (org.apache.cayenne.map.ObjRelationship)4 List (java.util.List)3