Search in sources :

Example 21 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class ClientServerChannelQueryIT method testSelectQuerySimpleQualifier.

@Test
public void testSelectQuerySimpleQualifier() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    SelectQuery q = new SelectQuery(ClientMtTable1.class, ExpressionFactory.exp("globalAttribute1 = 'g1'"));
    List<?> results = context.performQuery(q);
    assertEquals(1, results.size());
    assertTrue(results.get(0) instanceof ClientMtTable1);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Example 22 with SelectQuery

use of org.apache.cayenne.query.SelectQuery 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));
}
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 23 with SelectQuery

use of org.apache.cayenne.query.SelectQuery 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));
}
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) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockPersistentObject(org.apache.cayenne.MockPersistentObject) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 24 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class RemoteIncrementalFaultListIT method testLastIndexOf.

@Test
public void testLastIndexOf() throws Exception {
    prepareList(6);
    Expression qual = ExpressionFactory.matchExp(ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, "g20");
    SelectQuery<ClientMtTable1> query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class, qual);
    List<?> objects = list.context.performQuery(query);
    assertEquals(1, objects.size());
    ClientMtTable1 row = (ClientMtTable1) objects.get(0);
    assertEquals(19, list.lastIndexOf(row));
    assertEquals(-1, list.lastIndexOf(list.context.newObject(ClientMtTable1.class)));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Example 25 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class TransactionThreadIT method testThreadConnectionReuseOnSelect.

@Test
public void testThreadConnectionReuseOnSelect() throws Exception {
    Transaction t = new CayenneTransaction(logger);
    BaseTransaction.bindThreadTransaction(t);
    try {
        SelectQuery q1 = new SelectQuery(Artist.class);
        context.performQuery(q1);
        assertEquals(1, t.getConnections().size());
        // delegate will fail if the second query opens a new connection
        SelectQuery q2 = new SelectQuery(Artist.class);
        context.performQuery(q2);
        assertEquals(1, t.getConnections().size());
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)360 Test (org.junit.Test)348 Artist (org.apache.cayenne.testdo.testmap.Artist)128 Painting (org.apache.cayenne.testdo.testmap.Painting)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)64 Expression (org.apache.cayenne.exp.Expression)47 List (java.util.List)43 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)26 ValueHolder (org.apache.cayenne.ValueHolder)21 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)13 DataRow (org.apache.cayenne.DataRow)12 ObjectContext (org.apache.cayenne.ObjectContext)12 DbEntity (org.apache.cayenne.map.DbEntity)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)11 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)10 Query (org.apache.cayenne.query.Query)10