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();
}
}
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));
}
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));
}
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());
}
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);
}
Aggregations