OPENJPA-165

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@563702 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Patrick Linskey 2007-08-07 23:47:19 +00:00
parent 86e47000db
commit 63c3748900
2 changed files with 88 additions and 1 deletions

View File

@ -0,0 +1,81 @@
/*
* 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.openjpa.persistence.query;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that query pagination works properly.
*/
public class TestQueryPagination
extends SingleEMFTestCase {
public void setUp() {
setUp(SimpleEntity.class, CLEAR_TABLES);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new SimpleEntity("foo", "bar" + 0));
em.persist(new SimpleEntity("foo", "bar" + 1));
em.persist(new SimpleEntity("foo", "bar" + 2));
em.persist(new SimpleEntity("foo", "bar" + 3));
em.persist(new SimpleEntity("foo", "bar" + 4));
em.persist(new SimpleEntity("foo", "bar" + 5));
em.getTransaction().commit();
em.close();
}
public void testFirstThenMax() {
helper(true, 2, 3, 3);
}
public void testMaxThenFirst() {
helper(false, 2, 3, 3);
}
public void testNoResultsFirstFirst() {
helper(true, 10, 3, 0);
}
public void testNoResultsFirstLast() {
helper(false, 10, 3, 0);
}
public void testAllResultsFirstFirst() {
helper(true, 0, 10, 6);
}
public void testAllResultsFirstLast() {
helper(false, 0, 10, 6);
}
private void helper(boolean firstFirst, int first, int max, int expected) {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select e from simple e");
if (firstFirst)
q.setFirstResult(first).setMaxResults(max);
else
q.setMaxResults(max).setFirstResult(first);
assertEquals(expected, q.getResultList().size());
em.close();
}
}

View File

@ -191,7 +191,13 @@ public class QueryImpl
public OpenJPAQuery setFirstResult(int startPosition) {
_em.assertNotCloseInvoked();
_query.setRange(startPosition, _query.getEndRange());
long end;
if (_query.getEndRange() == Long.MAX_VALUE)
end = Long.MAX_VALUE;
else
end = startPosition +
(_query.getEndRange() - _query.getStartRange());
_query.setRange(startPosition, end);
return this;
}