Search in sources :

Example 11 with MockEventManager

use of org.apache.cayenne.event.MockEventManager in project cayenne by apache.

the class ClientChannelTest method testOnQuerySelectOverrideCached.

@Test
public void testOnQuerySelectOverrideCached() {
    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);
    EntityResolver resolver = new EntityResolver(entities);
    CayenneContext context = new CayenneContext();
    context.setEntityResolver(resolver);
    ObjectId oid = new ObjectId("test_entity", "x", "y");
    MockPersistentObject o1 = new MockPersistentObject(oid);
    context.getGraphManager().registerNode(oid, o1);
    assertSame(o1, context.getGraphManager().getNode(oid));
    // another object with the same GID ... we must merge it with cached and return
    // cached object instead of the one fetched
    MockPersistentObject o2 = new MockPersistentObject(oid);
    MockClientConnection connection = new MockClientConnection(new GenericResponse(Arrays.asList(o2)));
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    context.setChannel(channel);
    QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
    assertNotNull(response);
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertTrue("Expected cached object, got: " + list, list.contains(o1));
    assertSame(o1, context.getGraphManager().getNode(oid));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 12 with MockEventManager

use of org.apache.cayenne.event.MockEventManager in project cayenne by apache.

the class ClientChannelTest method testEventBridgeFailure.

@Test
public void testEventBridgeFailure() throws Exception {
    MockClientConnection connection = new MockClientConnection() {

        @Override
        public EventBridge getServerEventBridge() throws CayenneRuntimeException {
            return new EventBridge(Collections.EMPTY_LIST, "ext") {

                @Override
                protected void sendExternalEvent(CayenneEvent localEvent) throws Exception {
                }

                @Override
                protected void shutdownExternal() throws Exception {
                }

                @Override
                protected void startupExternal() throws Exception {
                    // intentionally throw an exception
                    throw new CayenneRuntimeException("Test failure");
                }
            };
        }
    };
    // default constructor must fail
    try {
        new ClientChannel(connection, false, new MockEventManager(), false);
        fail("Channel didn't throw on broken EventBridge");
    } catch (CayenneRuntimeException e) {
    // expected
    }
    try {
        DefaultEventManager manager = new DefaultEventManager(2);
        managers.add(manager);
        new ClientChannel(connection, false, manager, false);
        fail("Channel didn't throw on broken EventBridge");
    } catch (CayenneRuntimeException e) {
    // expected
    }
    try {
        DefaultEventManager manager = new DefaultEventManager(2);
        managers.add(manager);
        new ClientChannel(connection, false, manager, true);
    } catch (CayenneRuntimeException e) {
        fail("Channel threw on broken EventBridge");
    }
}
Also used : DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) CayenneEvent(org.apache.cayenne.event.CayenneEvent) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Aggregations

MockEventManager (org.apache.cayenne.event.MockEventManager)12 Test (org.junit.Test)12 CayenneContext (org.apache.cayenne.CayenneContext)6 ObjectId (org.apache.cayenne.ObjectId)6 EntityResolver (org.apache.cayenne.map.EntityResolver)5 GenericResponse (org.apache.cayenne.util.GenericResponse)5 EventBridge (org.apache.cayenne.event.EventBridge)4 DataMap (org.apache.cayenne.map.DataMap)4 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)4 Collections (java.util.Collections)3 MockPersistentObject (org.apache.cayenne.MockPersistentObject)3 ObjectContext (org.apache.cayenne.ObjectContext)3 QueryResponse (org.apache.cayenne.QueryResponse)3 DataDomain (org.apache.cayenne.access.DataDomain)3 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)3 DefaultDataRowStoreFactory (org.apache.cayenne.access.DefaultDataRowStoreFactory)3 QueryCache (org.apache.cayenne.cache.QueryCache)3 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)3 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)3 DIBootstrap (org.apache.cayenne.di.DIBootstrap)3