Search in sources :

Example 71 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextQueryCachingIT method testSharedCacheDataRowsRefresh.

@Test
public void testSharedCacheDataRowsRefresh() throws Exception {
    ObjectSelect<DataRow> select = ObjectSelect.dataRowQuery(Artist.class).sharedCache();
    MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
    try {
        // first run, no cache yet
        List<?> rows1 = mockupDataRows(2);
        engine.reset();
        engine.addExpectedResult(select, rows1);
        List<?> resultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows1, resultRows);
        QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
        assertEquals(rows1, context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertNull(context.getQueryCache().get(cacheKey));
        // second run, must refresh the cache
        List<?> rows2 = mockupDataRows(5);
        engine.reset();
        engine.addExpectedResult(select, rows2);
        select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE_REFRESH);
        List<?> freshResultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows2, freshResultRows);
        assertEquals(rows2, context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertNull(context.getQueryCache().get(cacheKey));
    } finally {
        engine.stopInterceptNode();
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) QueryMetadata(org.apache.cayenne.query.QueryMetadata) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 72 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextQueryCachingIT method testLocalCacheDataRowsRefresh.

@Test
public void testLocalCacheDataRowsRefresh() throws Exception {
    ObjectSelect<DataRow> select = ObjectSelect.dataRowQuery(Artist.class).localCache();
    MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
    try {
        // first run, no cache yet
        List<?> rows1 = mockupDataRows(2);
        engine.reset();
        engine.addExpectedResult(select, rows1);
        List<?> resultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows1, resultRows);
        QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
        assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertEquals(rows1, context.getQueryCache().get(cacheKey));
        // second run, must refresh the cache
        List<?> rows2 = mockupDataRows(4);
        engine.reset();
        engine.addExpectedResult(select, rows2);
        select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
        List<?> freshResultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows2, freshResultRows);
        assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertEquals(rows2, context.getQueryCache().get(cacheKey));
    } finally {
        engine.stopInterceptNode();
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) QueryMetadata(org.apache.cayenne.query.QueryMetadata) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 73 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextQueryCachingIT method mockupDataRows.

private List<?> mockupDataRows(int len) {
    List<Object> rows = new ArrayList<>(len);
    for (int i = 0; i < len; i++) {
        DataRow a = new DataRow(3);
        a.put("ARTIST_ID", i + 1);
        a.put("ARTIST_NAME", "A-" + (i + 1));
        rows.add(a);
    }
    return rows;
}
Also used : ArrayList(java.util.ArrayList) DataObject(org.apache.cayenne.DataObject) DataRow(org.apache.cayenne.DataRow)

Example 74 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class DataContextNoPkIT method testNoPkFetchDataRows.

@Test
public void testNoPkFetchDataRows() {
    List<DataRow> rows = ObjectSelect.dataRowQuery(NoPkTestEntity.class).select(context);
    assertNotNull(rows);
    assertEquals(2, rows.size());
    DataRow row1 = rows.get(0);
    DataRow row2 = rows.get(1);
    // assert that rows have different values
    // (there was a bug earlier that fetched distinct rows for
    // entities with no primary key.
    assertTrue(!row1.get("ATTRIBUTE1").equals(row2.get("ATTRIBUTE1")));
}
Also used : NoPkTestEntity(org.apache.cayenne.testdo.no_pk.NoPkTestEntity) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 75 with DataRow

use of org.apache.cayenne.DataRow in project cayenne by apache.

the class ProcedureCallIT method testSelectWithRowDescriptor.

@Test
public void testSelectWithRowDescriptor() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    // TESTING THIS ***
    // A.ARTIST_ID, A.DATE_OF_BIRTH, A.ARTIST_NAME
    ColumnDescriptor[] columns = new ColumnDescriptor[3];
    // read ID as Long, and everything else as default types
    columns[0] = new ColumnDescriptor("ARTIST_ID", Types.BIGINT);
    columns[1] = new ColumnDescriptor("ARTIST_NAME", Types.CHAR);
    columns[2] = new ColumnDescriptor("DATE_OF_BIRTH", Types.DATE);
    List<?> rows = runProcedureSelect(ProcedureCall.dataRowQuery(SELECT_STORED_PROCEDURE).param("aName", "An Artist").param("paintingPrice", 3000).resultDescriptor(columns)).firstList();
    // check the results
    assertNotNull("Null result from StoredProcedure.", rows);
    assertEquals(1, rows.size());
    DataRow artistRow = (DataRow) rows.get(0);
    assertEquals(3, artistRow.size());
    artistRow = uppercaseConverter(artistRow);
    Object id = artistRow.get("ARTIST_ID");
    assertNotNull(id);
    assertTrue("Expected Long, got: " + id.getClass().getName(), id instanceof Long);
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Aggregations

DataRow (org.apache.cayenne.DataRow)152 Test (org.junit.Test)113 Artist (org.apache.cayenne.testdo.testmap.Artist)31 ObjectId (org.apache.cayenne.ObjectId)25 DataObject (org.apache.cayenne.DataObject)20 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)20 SQLTemplate (org.apache.cayenne.query.SQLTemplate)18 Date (java.util.Date)14 HashMap (java.util.HashMap)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Persistent (org.apache.cayenne.Persistent)8 Painting (org.apache.cayenne.testdo.testmap.Painting)8 Connection (java.sql.Connection)6 Calendar (java.util.Calendar)5 Map (java.util.Map)5 QueryResponse (org.apache.cayenne.QueryResponse)5 DataMap (org.apache.cayenne.map.DataMap)5