[hibernate-commits] Hibernate SVN: r15896 - core/branches/Branch_3_2/test/org/hibernate/test/pagination.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Feb 4 18:11:46 EST 2009


Author: steve.ebersole at jboss.com
Date: 2009-02-04 18:11:46 -0500 (Wed, 04 Feb 2009)
New Revision: 15896

Modified:
   core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.hbm.xml
   core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.java
   core/branches/Branch_3_2/test/org/hibernate/test/pagination/PaginationTest.java
Log:
HHH-3750 : dialect first-result handling (tests)

Modified: core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.hbm.xml
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.hbm.xml	2009-02-04 23:10:24 UTC (rev 15895)
+++ core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.hbm.xml	2009-02-04 23:11:46 UTC (rev 15896)
@@ -12,7 +12,8 @@
 		<id name="id">
 			<generator class="increment"/>
 		</id>
-		<property name="x">
+        <property name="sequence" not-null="true" column="seqval" type="int" />
+        <property name="x">
 			<column name="xval" not-null="true" precision="20" scale="19" unique-key="xy"/>
 		</property>
 		<property name="y">

Modified: core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.java	2009-02-04 23:10:24 UTC (rev 15895)
+++ core/branches/Branch_3_2/test/org/hibernate/test/pagination/DataPoint.java	2009-02-04 23:11:46 UTC (rev 15896)
@@ -8,51 +8,78 @@
  */
 public class DataPoint {
 	private long id;
+	private int sequence;
 	private BigDecimal x;
 	private BigDecimal y;
 	private String description;
+
 	/**
+	 * @return Returns the id.
+	 */
+	public long getId() {
+		return id;
+	}
+
+	/**
+	 * @param id The id to set.
+	 */
+	public void setId(long id) {
+		this.id = id;
+	}
+
+	/**
+	 * Getter for property 'sequence'.
+	 *
+	 * @return Value for property 'sequence'.
+	 */
+	public int getSequence() {
+		return sequence;
+	}
+
+	/**
+	 * Setter for property 'sequence'.
+	 *
+	 * @param sequence Value to set for property 'sequence'.
+	 */
+	public void setSequence(int sequence) {
+		this.sequence = sequence;
+	}
+
+	/**
 	 * @return Returns the description.
 	 */
 	public String getDescription() {
 		return description;
 	}
+
 	/**
 	 * @param description The description to set.
 	 */
 	public void setDescription(String description) {
 		this.description = description;
 	}
+
 	/**
-	 * @return Returns the id.
-	 */
-	public long getId() {
-		return id;
-	}
-	/**
-	 * @param id The id to set.
-	 */
-	public void setId(long id) {
-		this.id = id;
-	}
-	/**
 	 * @return Returns the x.
 	 */
 	public BigDecimal getX() {
 		return x;
 	}
+
 	/**
 	 * @param x The x to set.
 	 */
 	public void setX(BigDecimal x) {
 		this.x = x;
 	}
+
 	/**
 	 * @return Returns the y.
 	 */
 	public BigDecimal getY() {
 		return y;
 	}
+
 	/**
 	 * @param y The y to set.
 	 */

Modified: core/branches/Branch_3_2/test/org/hibernate/test/pagination/PaginationTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/pagination/PaginationTest.java	2009-02-04 23:10:24 UTC (rev 15895)
+++ core/branches/Branch_3_2/test/org/hibernate/test/pagination/PaginationTest.java	2009-02-04 23:11:46 UTC (rev 15896)
@@ -2,13 +2,14 @@
 package org.hibernate.test.pagination;
 
 import java.math.BigDecimal;
+import java.util.List;
 
 import junit.framework.Test;
 
 import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.cfg.Environment;
+import org.hibernate.SQLQuery;
+import org.hibernate.Query;
+import org.hibernate.Criteria;
 import org.hibernate.criterion.Order;
 import org.hibernate.junit.functional.FunctionalTestCase;
 import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
@@ -17,7 +18,8 @@
  * @author Gavin King
  */
 public class PaginationTest extends FunctionalTestCase {
-	
+	public static final int ROWS = 100;
+
 	public PaginationTest(String str) {
 		super(str);
 	}
@@ -26,10 +28,6 @@
 		return new String[] { "pagination/DataPoint.hbm.xml" };
 	}
 
-	public void configure(Configuration cfg) {
-		cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "20");
-	}
-
 	public String getCacheConcurrencyStrategy() {
 		return null;
 	}
@@ -37,40 +35,126 @@
 	public static Test suite() {
 		return new FunctionalTestClassTestSuite( PaginationTest.class );
 	}
-	
-	public void testPagination() {
-		Session s = openSession();
-		Transaction t = s.beginTransaction();		
-		for ( int i=0; i<10; i++ ) {
-			DataPoint dp = new DataPoint();
-			dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) );
-			dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) );
-			s.persist(dp);
+
+	public void testLimit() {
+		if ( ! getDialect().supportsLimit() ) {
+			reportSkip( "Dialect does not support limit" );
+			return;
 		}
-		t.commit();
-		s.close();
-		
-		s = openSession();
-		t = s.beginTransaction();
-		int size = s.createSQLQuery("select id, xval, yval, description from DataPoint order by xval, yval")
-			.addEntity(DataPoint.class)
-			.setMaxResults(5)
-			.list().size();
-		assertEquals(size, 5);
-		size = s.createQuery("from DataPoint order by x, y")
-			.setFirstResult(5)
-			.setMaxResults(2)
-			.list().size();
-		assertEquals(size, 2);
-		size = s.createCriteria(DataPoint.class)
-			.addOrder( Order.asc("x") )
-			.addOrder( Order.asc("y") )
-			.setFirstResult(8)
-			.list().size();
-		assertEquals(size, 2);
-		t.commit();
-		s.close();
-		
+
+		prepareTestData();
+
+		Session session = openSession();
+		session.beginTransaction();
+
+		int count;
+
+		count = generateBaseHQLQuery( session )
+				.setMaxResults( 5 )
+				.list()
+				.size();
+		assertEquals( 5, count );
+
+		count = generateBaseCriteria( session )
+				.setMaxResults( 18 )
+				.list()
+				.size();
+		assertEquals( 18, count );
+
+		count = generateBaseSQLQuery( session )
+				.setMaxResults( 13 )
+				.list()
+				.size();
+		assertEquals( 13, count );
+
+		session.getTransaction().commit();
+		session.close();
+
+		cleanupTestData();
 	}
+
+	public void testLimitOffset() {
+		if ( ! getDialect().supportsLimitOffset() ) {
+			reportSkip( "Dialect does not support limit+offset" );
+			return;
+		}
+
+		prepareTestData();
+
+		Session session = openSession();
+		session.beginTransaction();
+
+		List result;
+
+		result = generateBaseHQLQuery( session )
+				.setFirstResult( 0 )
+				.setMaxResults( 20 )
+				.list();
+		assertEquals( 20, result.size() );
+		assertEquals( 0, ( ( DataPoint ) result.get( 0 ) ).getSequence() );
+		assertEquals( 1, ( ( DataPoint ) result.get( 1 ) ).getSequence() );
+
+		result = generateBaseCriteria( session )
+				.setFirstResult( 1 )
+				.setMaxResults( 20 )
+				.list();
+		assertEquals( 20, result.size() );
+		assertEquals( 1, ( ( DataPoint ) result.get( 0 ) ).getSequence() );
+		assertEquals( 2, ( ( DataPoint ) result.get( 1 ) ).getSequence() );
+
+		result = generateBaseCriteria( session )
+				.setFirstResult( 99 )
+				.setMaxResults( Integer.MAX_VALUE )
+				.list();
+		assertEquals( 1, result.size() );
+		assertEquals( 99, ( ( DataPoint ) result.get( 0 ) ).getSequence() );
+
+		session.getTransaction().commit();
+		session.close();
+
+		cleanupTestData();
+	}
+
+	private Query generateBaseHQLQuery(Session session) {
+		return session.createQuery( "select dp from DataPoint dp order by dp.sequence" );
+	}
+
+	private Criteria generateBaseCriteria(Session session) {
+		return session.createCriteria( DataPoint.class )
+				.addOrder( Order.asc( "sequence" ) );
+	}
+
+	private SQLQuery generateBaseSQLQuery(Session session) {
+		return session.createSQLQuery( "select id, seqval, xval, yval, description from DataPoint order by seqval" )
+				.addEntity( DataPoint.class );
+	}
+
+	private void prepareTestData() {
+		Session session = openSession();
+		session.beginTransaction();
+		for ( int i = 0; i < ROWS; i++ ) {
+			DataPoint dataPoint = new DataPoint();
+			dataPoint.setSequence( i );
+			dataPoint.setDescription( "data point #" + i );
+			BigDecimal x = new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN );
+			dataPoint.setX( x );
+			dataPoint.setY( new BigDecimal( Math.cos( x.doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) );
+			session.save( dataPoint );
+		}
+		session.getTransaction().commit();
+		session.close();
+	}
+
+	private void cleanupTestData() {
+		Session session = openSession();
+		session.beginTransaction();
+		session.createQuery( "delete DataPoint" ).executeUpdate();
+		session.getTransaction().commit();
+		session.close();
+	}
+
+	private void reportSkip(String message) {
+		reportSkip( message, "pagination support" );
+	}
 }
 




More information about the hibernate-commits mailing list