openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r563702 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Date Tue, 07 Aug 2007 23:47:26 GMT
Author: pcl
Date: Tue Aug  7 16:47:19 2007
New Revision: 563702

URL: http://svn.apache.org/viewvc?view=rev&rev=563702
Log:
OPENJPA-165

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java
      - copied, changed from r563356, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestNewlinesInQueries.java
Modified:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Copied: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java
(from r563356, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestNewlinesInQueries.java)
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java?view=diff&rev=563702&p1=openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestNewlinesInQueries.java&r1=563356&p2=openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java&r2=563702
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestNewlinesInQueries.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryPagination.java
Tue Aug  7 16:47:19 2007
@@ -24,9 +24,9 @@
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
 /**
- * Test that our parser handles newlines in queries
+ * Test that query pagination works properly.
  */
-public class TestNewlinesInQueries
+public class TestQueryPagination
     extends SingleEMFTestCase {
 
     public void setUp() {
@@ -34,16 +34,48 @@
 
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
-        em.persist(new SimpleEntity("foo", "bar"));
+        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 testQuery() {
+    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 \n from simple e");
-        SimpleEntity e = (SimpleEntity) q.getSingleResult();
-        assertEquals("foo", e.getName());
+        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();
     }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?view=diff&rev=563702&r1=563701&r2=563702
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Tue Aug  7 16:47:19 2007
@@ -191,7 +191,13 @@
 
     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;
     }
 



Mime
View raw message