From 63c3748900811032ec5dc4e5e42d9745cfc53c4f Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Tue, 7 Aug 2007 23:47:19 +0000 Subject: [PATCH] OPENJPA-165 git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@563702 13f79535-47bb-0310-9956-ffa450edef68 --- .../query/TestQueryPagination.java | 81 +++++++++++++++++++ .../apache/openjpa/persistence/QueryImpl.java | 8 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java new file mode 100644 index 000000000..397680d41 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java @@ -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(); + } +} \ No newline at end of file diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java index f59120be4..166c5eae0 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java @@ -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; }