/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/
package org.apache.cayenne.access;
import org.apache.cayenne.di.Inject;
import org.apache.cayenne.query.QueryCacheStrategy;
import org.apache.cayenne.query.SelectQuery;
import org.apache.cayenne.test.jdbc.DBHelper;
import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.testmap.Artist;
import org.apache.cayenne.unit.di.server.CayenneProjects;
import org.apache.cayenne.unit.di.server.ServerCase;
import org.apache.cayenne.unit.di.server.UseServerRuntime;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
@UseServerRuntime(CayenneProjects.TESTMAP_PROJECT)
public class DataContextPaginatedQueryIT extends ServerCase {
@Inject
protected DataContext context;
@Inject
protected DBHelper dbHelper;
protected TableHelper tArtist;
@Before
public void setUp() throws Exception {
tArtist = new TableHelper(dbHelper, "ARTIST");
tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
}
protected void createArtistsDataSet() throws Exception {
tArtist.insert(33001, "artist1");
tArtist.insert(33002, "artist2");
tArtist.insert(33003, "artist3");
tArtist.insert(33004, "artist4");
tArtist.insert(33005, "artist5");
tArtist.insert(33006, "artist6");
tArtist.insert(33007, "artist7");
tArtist.insert(33008, "artist8");
tArtist.insert(33009, "artist9");
tArtist.insert(33010, "artist10");
}
@Test
public void testLocalCache() throws Exception {
createArtistsDataSet();
SelectQuery<Artist> query = new SelectQuery<Artist>(Artist.class);
query.addOrdering(Artist.ARTIST_NAME.asc());
query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
query.setPageSize(5);
List<?> results1 = context.performQuery(query);
assertNotNull(results1);
List<?> results2 = context.performQuery(query);
assertNotNull(results2);
assertSame(results1, results2);
results1.get(1);
List<?> results3 = context.performQuery(query);
assertNotNull(results3);
assertSame(results1, results3);
results1.get(7);
List<?> results4 = context.performQuery(query);
assertNotNull(results4);
assertSame(results1, results4);
}
}