use of org.apache.cayenne.QueryResponse 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 = new ObjectId("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, new SelectQuery("test_entity"));
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.QueryResponse in project cayenne by apache.
the class ClientChannelTest method testOnQuerySelect.
@Test
public void testOnQuerySelect() {
final MockPersistentObject o1 = new MockPersistentObject();
ObjectId oid1 = new ObjectId("test_entity");
o1.setObjectId(oid1);
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(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, new SelectQuery("test_entity"));
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.QueryResponse in project cayenne by apache.
the class DataDomainFiltersIT method testOnQuery_Blocking.
@Test
public void testOnQuery_Blocking() {
DataDomain domain = runtime.getDataDomain();
final QueryResponse r1 = new ListResponse();
final QueryResponse r2 = new ListResponse();
DataChannelFilter f1 = new MockDataChannelFilter() {
@Override
public QueryResponse onQuery(ObjectContext originatingContext, Query query, DataChannelFilterChain filterChain) {
return r1;
}
};
DataChannelFilter f2 = new MockDataChannelFilter() {
@Override
public QueryResponse onQuery(ObjectContext originatingContext, Query query, DataChannelFilterChain filterChain) {
return r2;
}
};
domain.filters.add(f1);
domain.filters.add(f2);
SelectQuery query = new SelectQuery(Artist.class);
QueryResponse response = domain.onQuery(context, query);
assertSame(r2, response);
}
use of org.apache.cayenne.QueryResponse in project cayenne by apache.
the class DataContextEJBQLUpdateIT method testUpdateNoQualifierDecimal.
@Test
public void testUpdateNoQualifierDecimal() throws Exception {
createThreeArtistsTwoPaintings();
EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.estimatedPrice is NULL or p.estimatedPrice <> 1.1");
Object notUpdated = Cayenne.objectForQuery(context, check);
assertEquals(new Long(2l), notUpdated);
String ejbql = "UPDATE Painting AS p SET p.estimatedPrice = 1.1";
EJBQLQuery query = new EJBQLQuery(ejbql);
QueryResponse result = context.performGenericQuery(query);
int[] count = result.firstUpdateCount();
assertNotNull(count);
assertEquals(1, count.length);
assertEquals(2, count[0]);
notUpdated = Cayenne.objectForQuery(context, check);
assertEquals(new Long(0l), notUpdated);
}
use of org.apache.cayenne.QueryResponse in project cayenne by apache.
the class DataContextEJBQLUpdateIT method testUpdateQualifier.
@Test
public void testUpdateQualifier() throws Exception {
createThreeArtistsTwoPaintings();
EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.paintingTitle is NULL or p.paintingTitle <> 'XX'");
Object notUpdated = Cayenne.objectForQuery(context, check);
assertEquals(new Long(2l), notUpdated);
String ejbql = "UPDATE Painting AS p SET p.paintingTitle = 'XX' WHERE p.paintingTitle = 'P1'";
EJBQLQuery query = new EJBQLQuery(ejbql);
QueryResponse result = context.performGenericQuery(query);
int[] count = result.firstUpdateCount();
assertNotNull(count);
assertEquals(1, count.length);
assertEquals(1, count[0]);
notUpdated = Cayenne.objectForQuery(context, check);
assertEquals(new Long(1l), notUpdated);
}
Aggregations