Search in sources :

Example 1 with MockEventManager

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

the class ClientChannelServerDiffsIT method testReturnDiffInPrePersist.

@Test
public void testReturnDiffInPrePersist() {
    final List<GenericDiff> diffs = new ArrayList<>();
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
            super.nodePropertyChanged(nodeId, property, oldValue, newValue);
            diffs.add(new GenericDiff((ObjectId) nodeId, property, oldValue, newValue));
        }
    };
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
        ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {

            @Override
            public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
                GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
                assertNotNull(serverDiff);
                serverDiff.apply(diffReader);
                return serverDiff;
            }
        };
        CayenneContext context = new CayenneContext(channel);
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        ObjectId tempId = o.getObjectId();
        o.setServerAttribute1("YY");
        context.commitChanges();
        assertEquals(2, diffReader.size);
        assertEquals(1, diffs.size());
        assertEquals(tempId, diffs.get(0).sourceId);
        assertEquals(ClientMtTable1.GLOBAL_ATTRIBUTE1.getName(), diffs.get(0).property);
        assertNull(diffs.get(0).oldValue);
        assertEquals("XXX", diffs.get(0).newValue);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ArrayList(java.util.ArrayList) CayenneContext(org.apache.cayenne.CayenneContext) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectContext(org.apache.cayenne.ObjectContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 2 with MockEventManager

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

the class ClientChannelTest method testOnQuerySelect.

@Test
public void testOnQuerySelect() {
    final MockPersistentObject o1 = new MockPersistentObject();
    ObjectId oid1 = ObjectId.of("test_entity");
    o1.setObjectId(oid1);
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage(any())).thenAnswer(invocation -> {
        ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
        if (arg instanceof BootstrapMessage) {
            return new EntityResolver();
        } else {
            return new GenericResponse(Arrays.asList(o1));
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    CayenneContext context = new CayenneContext(channel);
    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));
    QueryResponse response = channel.onQuery(context, ObjectSelect.query(MockPersistentObject.class));
    assertNotNull(response);
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    Persistent o1_1 = (Persistent) list.get(0);
    assertEquals(o1.getObjectId(), o1_1.getObjectId());
    // ObjectContext must be injected
    assertEquals(context, o1_1.getObjectContext());
    assertSame(o1_1, context.getGraphManager().getNode(oid1));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) Persistent(org.apache.cayenne.Persistent) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) 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 3 with MockEventManager

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

the class ClientChannelTest method testOnQuerySelectOverrideModifiedCached.

@Test
public void testOnQuerySelectOverrideModifiedCached() {
    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 = ObjectId.of("test_entity", "x", "y");
    MockPersistentObject o1 = new MockPersistentObject(oid);
    o1.setPersistenceState(PersistenceState.MODIFIED);
    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, ObjectSelect.query(MockPersistentObject.class));
    assertNotNull(response);
    assertEquals(1, response.size());
    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) 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 4 with MockEventManager

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

the class DataContextFactoryTest method testCreateDataContextWithDedicatedCache.

@Test
public void testCreateDataContextWithDedicatedCache() throws Exception {
    final EventManager eventManager = new MockEventManager();
    final DataDomain domain = new DataDomain("d1");
    domain.setSharedCacheEnabled(false);
    Module testModule = binder -> {
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(DataDomain.class).toInstance(domain);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
        binder.bind(RuntimeProperties.class).toInstance(new DefaultRuntimeProperties(Collections.<String, String>emptyMap()));
        binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
        binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(DataDomainFlushActionFactory.class).to(DefaultDataDomainFlushActionFactory.class);
        binder.bind(DbRowOpSorter.class).to(DefaultDbRowOpSorter.class);
        binder.bind(EntitySorter.class).to(AshwoodEntitySorter.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataContextFactory factory = new DataContextFactory();
    injector.injectMembers(factory);
    DataContext c3 = (DataContext) factory.createContext();
    assertNotNull(c3.getObjectStore().getDataRowCache());
    assertNull(domain.getSharedSnapshotCache());
    assertNotSame(c3.getObjectStore().getDataRowCache(), domain.getSharedSnapshotCache());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DataContext(org.apache.cayenne.access.DataContext) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) TransactionManager(org.apache.cayenne.tx.TransactionManager) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) EntitySorter(org.apache.cayenne.map.EntitySorter) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) DbRowOpSorter(org.apache.cayenne.access.flush.operation.DbRowOpSorter) Collections(java.util.Collections) DataDomainFlushActionFactory(org.apache.cayenne.access.flush.DataDomainFlushActionFactory) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataDomain(org.apache.cayenne.access.DataDomain) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DataContext(org.apache.cayenne.access.DataContext) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 5 with MockEventManager

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

the class ClientChannelServerDiffsIT method testReturnDiffClientArcChanges.

@Test
public void testReturnDiffClientArcChanges() {
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler();
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {

        @Override
        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
            assertNotNull(serverDiff);
            serverDiff.apply(diffReader);
            return serverDiff;
        }
    };
    CayenneContext context = new CayenneContext(channel);
    ClientMtTable1 o = context.newObject(ClientMtTable1.class);
    ClientMtTable2 o2 = context.newObject(ClientMtTable2.class);
    o.addToTable2Array(o2);
    context.commitChanges();
    assertEquals(2, diffReader.size);
    diffReader.reset();
    ClientMtTable2 o3 = context.newObject(ClientMtTable2.class);
    o3.setTable1(o);
    context.commitChanges();
    assertEquals(1, diffReader.size);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) 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 Collections (java.util.Collections)3 ObjectContext (org.apache.cayenne.ObjectContext)3 DataDomain (org.apache.cayenne.access.DataDomain)3 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)3 DefaultDataRowStoreFactory (org.apache.cayenne.access.DefaultDataRowStoreFactory)3 AshwoodEntitySorter (org.apache.cayenne.ashwood.AshwoodEntitySorter)3 QueryCache (org.apache.cayenne.cache.QueryCache)3 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)3 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)3 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)3 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)3 DIBootstrap (org.apache.cayenne.di.DIBootstrap)3